Remove more cruft from fx_system.h
[pdfium.git] / core / src / fpdfapi / fpdf_font / ttgsubtable.h
1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4  
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #ifndef CORE_SRC_FPDFAPI_FPDF_FONT_TTGSUBTABLE_H_
8 #define CORE_SRC_FPDFAPI_FPDF_FONT_TTGSUBTABLE_H_
9
10 #include "../../../include/fxge/fx_freetype.h"
11 #include "../../../include/fxcrt/fx_basic.h"
12 #include "common.h"
13
14 class CFX_GlyphMap
15 {
16 public:
17     CFX_GlyphMap();
18     ~CFX_GlyphMap();
19     void        SetAt(int key, int value);
20     FX_BOOL     Lookup(int key, int &value);
21 protected:
22     CFX_BinaryBuf       m_Buffer;
23 };
24 class CFX_CTTGSUBTable 
25 {
26 public:
27     CFX_CTTGSUBTable(void): m_bFeautureMapLoad(FALSE), loaded(false) {};
28     CFX_CTTGSUBTable(FT_Bytes gsub): m_bFeautureMapLoad(FALSE), loaded(false)
29     {
30         LoadGSUBTable(gsub);
31     }
32     virtual ~CFX_CTTGSUBTable() {}
33     bool IsOk(void) const
34     {
35         return loaded;
36     }
37     bool LoadGSUBTable(FT_Bytes gsub);
38     bool GetVerticalGlyph(TT_uint32_t glyphnum, TT_uint32_t *vglyphnum);
39 private:
40     struct tt_gsub_header {
41         TT_uint32_t Version;
42         TT_uint16_t ScriptList;
43         TT_uint16_t FeatureList;
44         TT_uint16_t LookupList;
45     };
46     struct TLangSys {
47         TT_uint16_t LookupOrder;
48         TT_uint16_t ReqFeatureIndex;
49         TT_uint16_t FeatureCount;
50         TT_uint16_t *FeatureIndex;
51         TLangSys(): LookupOrder(0), ReqFeatureIndex(0), FeatureCount(0), FeatureIndex(NULL) {}
52         ~TLangSys()
53         {
54             if(FeatureIndex) {
55                 delete[] FeatureIndex;
56             }
57         }
58     private:
59         TLangSys(const TLangSys&);
60         TLangSys& operator=(const TLangSys&);
61     };
62     struct TLangSysRecord {
63         TT_uint32_t LangSysTag;
64         struct TLangSys LangSys;
65         TLangSysRecord(): LangSysTag(0) {}
66     private:
67         TLangSysRecord(const TLangSysRecord&);
68         TLangSysRecord& operator=(const TLangSysRecord&);
69     };
70     struct TScript {
71         TT_uint16_t DefaultLangSys;
72         TT_uint16_t LangSysCount;
73         struct TLangSysRecord *LangSysRecord;
74         TScript(): DefaultLangSys(0), LangSysCount(0), LangSysRecord(NULL) {}
75         ~TScript()
76         {
77             if(LangSysRecord) {
78                 delete[] LangSysRecord;
79             }
80         }
81     private:
82         TScript(const TScript&);
83         TScript& operator=(const TScript&);
84     };
85     struct TScriptRecord {
86         TT_uint32_t ScriptTag;
87         struct TScript Script;
88         TScriptRecord(): ScriptTag(0) {}
89     private:
90         TScriptRecord(const TScriptRecord&);
91         TScriptRecord& operator=(const TScriptRecord&);
92     };
93     struct TScriptList {
94         TT_uint16_t ScriptCount;
95         struct TScriptRecord *ScriptRecord;
96         TScriptList(): ScriptCount(0), ScriptRecord(NULL) {}
97         ~TScriptList()
98         {
99             if(ScriptRecord) {
100                 delete[] ScriptRecord;
101             }
102         }
103     private:
104         TScriptList(const TScriptList&);
105         TScriptList& operator=(const TScriptList&);
106     };
107     struct TFeature {
108         TT_uint16_t FeatureParams;
109         int LookupCount;
110         TT_uint16_t *LookupListIndex;
111         TFeature(): FeatureParams(0), LookupCount(0), LookupListIndex(NULL) {}
112         ~TFeature()
113         {
114             if(LookupListIndex) {
115                 delete[] LookupListIndex;
116             }
117         }
118     private:
119         TFeature(const TFeature&);
120         TFeature& operator=(const TFeature&);
121     };
122     struct TFeatureRecord {
123         TT_uint32_t FeatureTag;
124         struct TFeature Feature;
125         TFeatureRecord(): FeatureTag(0) {}
126     private:
127         TFeatureRecord(const TFeatureRecord&);
128         TFeatureRecord& operator=(const TFeatureRecord&);
129     };
130     struct TFeatureList {
131         int FeatureCount;
132         struct TFeatureRecord *FeatureRecord;
133         TFeatureList(): FeatureCount(0), FeatureRecord(NULL) {}
134         ~TFeatureList()
135         {
136             if(FeatureRecord) {
137                 delete[] FeatureRecord;
138             }
139         }
140     private:
141         TFeatureList(const TFeatureList&);
142         TFeatureList& operator=(const TFeatureList&);
143     };
144     enum TLookupFlag {
145         LOOKUPFLAG_RightToLeft = 0x0001,
146         LOOKUPFLAG_IgnoreBaseGlyphs = 0x0002,
147         LOOKUPFLAG_IgnoreLigatures = 0x0004,
148         LOOKUPFLAG_IgnoreMarks = 0x0008,
149         LOOKUPFLAG_Reserved = 0x00F0,
150         LOOKUPFLAG_MarkAttachmentType = 0xFF00,
151     };
152     struct TCoverageFormatBase {
153         TT_uint16_t CoverageFormat;
154         CFX_GlyphMap m_glyphMap;
155         TCoverageFormatBase(): CoverageFormat(0) {}
156         virtual ~TCoverageFormatBase() {}
157     private:
158         TCoverageFormatBase(const TCoverageFormatBase&);
159         TCoverageFormatBase& operator=(const TCoverageFormatBase&);
160     };
161     struct TCoverageFormat1: public TCoverageFormatBase {
162         TT_uint16_t GlyphCount;
163         TT_uint16_t *GlyphArray;
164         TCoverageFormat1(): GlyphCount(0), GlyphArray(NULL)
165         {
166             CoverageFormat = 1;
167         }
168         ~TCoverageFormat1()
169         {
170             if(GlyphArray) {
171                 delete[] GlyphArray;
172             }
173         }
174     private:
175         TCoverageFormat1(const TCoverageFormat1&);
176         TCoverageFormat1& operator=(const TCoverageFormat1&);
177     };
178     struct TRangeRecord {
179         TT_uint16_t Start;
180         TT_uint16_t End;
181         TT_uint16_t StartCoverageIndex;
182         TRangeRecord(): Start(0), End(0), StartCoverageIndex(0) {}
183         friend bool operator > (const TRangeRecord &r1, const TRangeRecord &r2)
184         {
185             return r1.Start > r2.Start;
186         }
187     private:
188         TRangeRecord(const TRangeRecord&);
189     };
190     struct TCoverageFormat2: public TCoverageFormatBase {
191         TT_uint16_t RangeCount;
192         struct TRangeRecord *RangeRecord;
193         TCoverageFormat2(): RangeCount(0), RangeRecord(NULL)
194         {
195             CoverageFormat = 2;
196         }
197         ~TCoverageFormat2()
198         {
199             if(RangeRecord) {
200                 delete[] RangeRecord;
201             }
202         }
203     private:
204         TCoverageFormat2(const TCoverageFormat2&);
205         TCoverageFormat2& operator=(const TCoverageFormat2&);
206     };
207     struct TClassDefFormatBase {
208         TT_uint16_t ClassFormat;
209         TClassDefFormatBase(): ClassFormat(0) {}
210         virtual ~TClassDefFormatBase() {}
211     private:
212         TClassDefFormatBase(const TClassDefFormatBase&);
213         TClassDefFormatBase& operator=(const TClassDefFormatBase&);
214     };
215     struct TClassDefFormat1: public TClassDefFormatBase {
216         TT_uint16_t StartGlyph;
217         TT_uint16_t GlyphCount;
218         TT_uint16_t *ClassValueArray;
219         TClassDefFormat1(): StartGlyph(0), GlyphCount(0), ClassValueArray(NULL)
220         {
221             ClassFormat = 1;
222         }
223         ~TClassDefFormat1()
224         {
225             if(ClassValueArray) {
226                 delete[] ClassValueArray;
227             }
228         }
229     private:
230         TClassDefFormat1(const TClassDefFormat1&);
231         TClassDefFormat1& operator=(const TClassDefFormat1&);
232     };
233     struct TClassRangeRecord {
234         TT_uint16_t Start;
235         TT_uint16_t End;
236         TT_uint16_t Class;
237         TClassRangeRecord(): Start(0), End(0), Class(0) {}
238     private:
239         TClassRangeRecord(const TClassRangeRecord&);
240         TClassRangeRecord& operator=(const TClassRangeRecord&);
241     };
242     struct TClassDefFormat2: public TClassDefFormatBase {
243         TT_uint16_t ClassRangeCount;
244         struct TClassRangeRecord *ClassRangeRecord;
245         TClassDefFormat2(): ClassRangeCount(0), ClassRangeRecord(NULL)
246         {
247             ClassFormat = 2;
248         }
249         ~TClassDefFormat2()
250         {
251             if(ClassRangeRecord) {
252                 delete[] ClassRangeRecord;
253             }
254         }
255     private:
256         TClassDefFormat2(const TClassDefFormat2&);
257         TClassDefFormat2& operator=(const TClassDefFormat2&);
258     };
259     struct TDevice {
260         TT_uint16_t StartSize;
261         TT_uint16_t EndSize;
262         TT_uint16_t DeltaFormat;
263         TDevice(): StartSize(0), EndSize(0), DeltaFormat(0) {}
264     private:
265         TDevice(const TDevice&);
266         TDevice& operator=(const TDevice&);
267     };
268     struct TSubTableBase {
269         TT_uint16_t SubstFormat;
270         TSubTableBase(): SubstFormat(0) {}
271         virtual ~TSubTableBase() {}
272     private:
273         TSubTableBase(const TSubTableBase&);
274         TSubTableBase& operator=(const TSubTableBase&);
275     };
276     struct TSingleSubstFormat1: public TSubTableBase {
277         TCoverageFormatBase *Coverage;
278         TT_int16_t DeltaGlyphID;
279         TSingleSubstFormat1(): Coverage(NULL), DeltaGlyphID(0)
280         {
281             SubstFormat = 1;
282         }
283         ~TSingleSubstFormat1()
284         {
285             if(Coverage) {
286                 delete Coverage;
287             }
288         }
289     private:
290         TSingleSubstFormat1(const TSingleSubstFormat1&);
291         TSingleSubstFormat1& operator=(const TSingleSubstFormat1&);
292     };
293     struct TSingleSubstFormat2: public TSubTableBase {
294         TCoverageFormatBase *Coverage;
295         TT_uint16_t GlyphCount;
296         TT_uint16_t *Substitute;
297         TSingleSubstFormat2(): Coverage(NULL), GlyphCount(0), Substitute(NULL)
298         {
299             SubstFormat = 2;
300         }
301         ~TSingleSubstFormat2()
302         {
303             if(Coverage) {
304                 delete Coverage;
305             }
306             if(Substitute) {
307                 delete[] Substitute;
308             }
309         }
310     private:
311         TSingleSubstFormat2(const TSingleSubstFormat2&);
312         TSingleSubstFormat2& operator=(const TSingleSubstFormat2&);
313     };
314     struct TLookup {
315         TT_uint16_t LookupType;
316         TT_uint16_t LookupFlag;
317         TT_uint16_t SubTableCount;
318         struct TSubTableBase **SubTable;
319         TLookup(): LookupType(0), LookupFlag(0), SubTableCount(0), SubTable(NULL) {}
320         ~TLookup()
321         {
322             if(SubTableCount > 0 && SubTable != NULL) {
323                 for(int i = 0; i < SubTableCount; i++) {
324                     delete SubTable[i];
325                 }
326                 delete[] SubTable;
327             }
328         }
329     private:
330         TLookup(const TLookup&);
331         TLookup& operator=(const TLookup&);
332     };
333     struct TLookupList {
334         int LookupCount;
335         struct TLookup *Lookup;
336         TLookupList(): LookupCount(0), Lookup(NULL) {}
337         ~TLookupList()
338         {
339             if(Lookup) {
340                 delete[] Lookup;
341             }
342         }
343     private:
344         TLookupList(const TLookupList&);
345         TLookupList& operator=(const TLookupList&);
346     };
347     bool Parse(
348         FT_Bytes scriptlist,
349         FT_Bytes featurelist,
350         FT_Bytes lookuplist);
351     void ParseScriptList(FT_Bytes raw, TScriptList *rec);
352     void ParseScript(FT_Bytes raw, TScript *rec);
353     void ParseLangSys(FT_Bytes raw, TLangSys *rec);
354     void ParseFeatureList(FT_Bytes raw, TFeatureList *rec);
355     void ParseFeature(FT_Bytes raw, TFeature *rec);
356     void ParseLookupList(FT_Bytes raw, TLookupList *rec);
357     void ParseLookup(FT_Bytes raw, TLookup *rec);
358     void ParseCoverage(FT_Bytes raw, TCoverageFormatBase **rec);
359     void ParseCoverageFormat1(FT_Bytes raw, TCoverageFormat1 *rec);
360     void ParseCoverageFormat2(FT_Bytes raw, TCoverageFormat2 *rec);
361     void ParseSingleSubst(FT_Bytes raw, TSubTableBase **rec);
362     void ParseSingleSubstFormat1(FT_Bytes raw, TSingleSubstFormat1 *rec);
363     void ParseSingleSubstFormat2(FT_Bytes raw, TSingleSubstFormat2 *rec);
364     bool GetVerticalGlyphSub(
365         TT_uint32_t glyphnum,
366         TT_uint32_t *vglyphnum,
367         struct TFeature *Feature);
368     bool GetVerticalGlyphSub2(
369         TT_uint32_t glyphnum,
370         TT_uint32_t *vglyphnum,
371         struct TLookup *Lookup);
372     int GetCoverageIndex(struct TCoverageFormatBase *Coverage, TT_uint32_t g);
373     TT_uint8_t GetUInt8(FT_Bytes& p) const
374     {
375         TT_uint8_t ret = p[0];
376         p += 1;
377         return ret;
378     }
379     TT_int16_t GetInt16(FT_Bytes& p) const
380     {
381         TT_uint16_t ret = p[0] << 8 | p[1];
382         p += 2;
383         return *(TT_int16_t*)&ret;
384     }
385     TT_uint16_t GetUInt16(FT_Bytes& p) const
386     {
387         TT_uint16_t ret = p[0] << 8 | p[1];
388         p += 2;
389         return ret;
390     }
391     TT_int32_t GetInt32(FT_Bytes& p) const
392     {
393         TT_uint32_t ret = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
394         p += 4;
395         return *(TT_int32_t*)&ret;
396     }
397     TT_uint32_t GetUInt32(FT_Bytes& p) const
398     {
399         TT_uint32_t ret = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
400         p += 4;
401         return ret;
402     }
403     CFX_CMapDWordToDWord m_featureMap;
404     FX_BOOL     m_bFeautureMapLoad;
405     bool loaded;
406     struct tt_gsub_header header;
407     struct TScriptList ScriptList;
408     struct TFeatureList FeatureList;
409     struct TLookupList LookupList;
410 };
411 class CFX_GSUBTable final : public IFX_GSUBTable
412 {
413 public:
414     virtual void        Release() override
415     {
416         delete this;
417     }
418     virtual FX_BOOL GetVerticalGlyph(FX_DWORD glyphnum, FX_DWORD* vglyphnum) override;
419
420     CFX_CTTGSUBTable m_GsubImp;
421
422 private:
423     ~CFX_GSUBTable() { }
424 };
425
426 #endif  // CORE_SRC_FPDFAPI_FPDF_FONT_TTGSUBTABLE_H_