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