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 <stdint.h>
11 
12 #include <map>
13 
14 #include "core/include/fxcrt/fx_basic.h"
15 #include "core/include/fxge/fx_font.h"
16 #include "core/include/fxge/fx_freetype.h"
17 
18 class CFX_GlyphMap {
19  public:
20   CFX_GlyphMap();
21   ~CFX_GlyphMap();
22   void SetAt(int key, int value);
23   FX_BOOL Lookup(int key, int& value);
24 
25  protected:
26   CFX_BinaryBuf m_Buffer;
27 };
28 class CFX_CTTGSUBTable {
29  public:
CFX_CTTGSUBTable(void)30   CFX_CTTGSUBTable(void) : m_bFeautureMapLoad(FALSE), loaded(false) {}
CFX_CTTGSUBTable(FT_Bytes gsub)31   CFX_CTTGSUBTable(FT_Bytes gsub) : m_bFeautureMapLoad(FALSE), loaded(false) {
32     LoadGSUBTable(gsub);
33   }
~CFX_CTTGSUBTable()34   virtual ~CFX_CTTGSUBTable() {}
IsOk(void)35   bool IsOk(void) const { return loaded; }
36   bool LoadGSUBTable(FT_Bytes gsub);
37   bool GetVerticalGlyph(uint32_t glyphnum, uint32_t* vglyphnum);
38 
39  private:
40   struct tt_gsub_header {
41     uint32_t Version;
42     uint16_t ScriptList;
43     uint16_t FeatureList;
44     uint16_t LookupList;
45   };
46   struct TLangSys {
47     uint16_t LookupOrder;
48     uint16_t ReqFeatureIndex;
49     uint16_t FeatureCount;
50     uint16_t* FeatureIndex;
TLangSysTLangSys51     TLangSys()
52         : LookupOrder(0),
53           ReqFeatureIndex(0),
54           FeatureCount(0),
55           FeatureIndex(NULL) {}
~TLangSysTLangSys56     ~TLangSys() { delete[] FeatureIndex; }
57 
58    private:
59     TLangSys(const TLangSys&);
60     TLangSys& operator=(const TLangSys&);
61   };
62   struct TLangSysRecord {
63     uint32_t LangSysTag;
64     struct TLangSys LangSys;
TLangSysRecordTLangSysRecord65     TLangSysRecord() : LangSysTag(0) {}
66 
67    private:
68     TLangSysRecord(const TLangSysRecord&);
69     TLangSysRecord& operator=(const TLangSysRecord&);
70   };
71   struct TScript {
72     uint16_t DefaultLangSys;
73     uint16_t LangSysCount;
74     struct TLangSysRecord* LangSysRecord;
TScriptTScript75     TScript() : DefaultLangSys(0), LangSysCount(0), LangSysRecord(NULL) {}
~TScriptTScript76     ~TScript() { delete[] LangSysRecord; }
77 
78    private:
79     TScript(const TScript&);
80     TScript& operator=(const TScript&);
81   };
82   struct TScriptRecord {
83     uint32_t ScriptTag;
84     struct TScript Script;
TScriptRecordTScriptRecord85     TScriptRecord() : ScriptTag(0) {}
86 
87    private:
88     TScriptRecord(const TScriptRecord&);
89     TScriptRecord& operator=(const TScriptRecord&);
90   };
91   struct TScriptList {
92     uint16_t ScriptCount;
93     struct TScriptRecord* ScriptRecord;
TScriptListTScriptList94     TScriptList() : ScriptCount(0), ScriptRecord(NULL) {}
~TScriptListTScriptList95     ~TScriptList() { delete[] ScriptRecord; }
96 
97    private:
98     TScriptList(const TScriptList&);
99     TScriptList& operator=(const TScriptList&);
100   };
101   struct TFeature {
102     uint16_t FeatureParams;
103     int LookupCount;
104     uint16_t* LookupListIndex;
TFeatureTFeature105     TFeature() : FeatureParams(0), LookupCount(0), LookupListIndex(NULL) {}
~TFeatureTFeature106     ~TFeature() { delete[] LookupListIndex; }
107 
108    private:
109     TFeature(const TFeature&);
110     TFeature& operator=(const TFeature&);
111   };
112   struct TFeatureRecord {
113     uint32_t FeatureTag;
114     struct TFeature Feature;
TFeatureRecordTFeatureRecord115     TFeatureRecord() : FeatureTag(0) {}
116 
117    private:
118     TFeatureRecord(const TFeatureRecord&);
119     TFeatureRecord& operator=(const TFeatureRecord&);
120   };
121   struct TFeatureList {
122     int FeatureCount;
123     struct TFeatureRecord* FeatureRecord;
TFeatureListTFeatureList124     TFeatureList() : FeatureCount(0), FeatureRecord(NULL) {}
~TFeatureListTFeatureList125     ~TFeatureList() { delete[] FeatureRecord; }
126 
127    private:
128     TFeatureList(const TFeatureList&);
129     TFeatureList& operator=(const TFeatureList&);
130   };
131   enum TLookupFlag {
132     LOOKUPFLAG_RightToLeft = 0x0001,
133     LOOKUPFLAG_IgnoreBaseGlyphs = 0x0002,
134     LOOKUPFLAG_IgnoreLigatures = 0x0004,
135     LOOKUPFLAG_IgnoreMarks = 0x0008,
136     LOOKUPFLAG_Reserved = 0x00F0,
137     LOOKUPFLAG_MarkAttachmentType = 0xFF00,
138   };
139   struct TCoverageFormatBase {
140     uint16_t CoverageFormat;
141     CFX_GlyphMap m_glyphMap;
TCoverageFormatBaseTCoverageFormatBase142     TCoverageFormatBase() : CoverageFormat(0) {}
~TCoverageFormatBaseTCoverageFormatBase143     virtual ~TCoverageFormatBase() {}
144 
145    private:
146     TCoverageFormatBase(const TCoverageFormatBase&);
147     TCoverageFormatBase& operator=(const TCoverageFormatBase&);
148   };
149   struct TCoverageFormat1 : public TCoverageFormatBase {
150     uint16_t GlyphCount;
151     uint16_t* GlyphArray;
TCoverageFormat1TCoverageFormat1152     TCoverageFormat1() : GlyphCount(0), GlyphArray(NULL) { CoverageFormat = 1; }
~TCoverageFormat1TCoverageFormat1153     ~TCoverageFormat1() override { delete[] GlyphArray; }
154 
155    private:
156     TCoverageFormat1(const TCoverageFormat1&);
157     TCoverageFormat1& operator=(const TCoverageFormat1&);
158   };
159   struct TRangeRecord {
160     uint16_t Start;
161     uint16_t End;
162     uint16_t StartCoverageIndex;
TRangeRecordTRangeRecord163     TRangeRecord() : Start(0), End(0), StartCoverageIndex(0) {}
164     friend bool operator>(const TRangeRecord& r1, const TRangeRecord& r2) {
165       return r1.Start > r2.Start;
166     }
167 
168    private:
169     TRangeRecord(const TRangeRecord&);
170   };
171   struct TCoverageFormat2 : public TCoverageFormatBase {
172     uint16_t RangeCount;
173     struct TRangeRecord* RangeRecord;
TCoverageFormat2TCoverageFormat2174     TCoverageFormat2() : RangeCount(0), RangeRecord(NULL) {
175       CoverageFormat = 2;
176     }
~TCoverageFormat2TCoverageFormat2177     ~TCoverageFormat2() override { delete[] RangeRecord; }
178 
179    private:
180     TCoverageFormat2(const TCoverageFormat2&);
181     TCoverageFormat2& operator=(const TCoverageFormat2&);
182   };
183   struct TClassDefFormatBase {
184     uint16_t ClassFormat;
TClassDefFormatBaseTClassDefFormatBase185     TClassDefFormatBase() : ClassFormat(0) {}
~TClassDefFormatBaseTClassDefFormatBase186     virtual ~TClassDefFormatBase() {}
187 
188    private:
189     TClassDefFormatBase(const TClassDefFormatBase&);
190     TClassDefFormatBase& operator=(const TClassDefFormatBase&);
191   };
192   struct TClassDefFormat1 : public TClassDefFormatBase {
193     uint16_t StartGlyph;
194     uint16_t GlyphCount;
195     uint16_t* ClassValueArray;
TClassDefFormat1TClassDefFormat1196     TClassDefFormat1() : StartGlyph(0), GlyphCount(0), ClassValueArray(NULL) {
197       ClassFormat = 1;
198     }
~TClassDefFormat1TClassDefFormat1199     ~TClassDefFormat1() override { delete[] ClassValueArray; }
200 
201    private:
202     TClassDefFormat1(const TClassDefFormat1&);
203     TClassDefFormat1& operator=(const TClassDefFormat1&);
204   };
205   struct TClassRangeRecord {
206     uint16_t Start;
207     uint16_t End;
208     uint16_t Class;
TClassRangeRecordTClassRangeRecord209     TClassRangeRecord() : Start(0), End(0), Class(0) {}
210 
211    private:
212     TClassRangeRecord(const TClassRangeRecord&);
213     TClassRangeRecord& operator=(const TClassRangeRecord&);
214   };
215   struct TClassDefFormat2 : public TClassDefFormatBase {
216     uint16_t ClassRangeCount;
217     struct TClassRangeRecord* ClassRangeRecord;
TClassDefFormat2TClassDefFormat2218     TClassDefFormat2() : ClassRangeCount(0), ClassRangeRecord(NULL) {
219       ClassFormat = 2;
220     }
~TClassDefFormat2TClassDefFormat2221     ~TClassDefFormat2() override { delete[] ClassRangeRecord; }
222 
223    private:
224     TClassDefFormat2(const TClassDefFormat2&);
225     TClassDefFormat2& operator=(const TClassDefFormat2&);
226   };
227   struct TDevice {
228     uint16_t StartSize;
229     uint16_t EndSize;
230     uint16_t DeltaFormat;
TDeviceTDevice231     TDevice() : StartSize(0), EndSize(0), DeltaFormat(0) {}
232 
233    private:
234     TDevice(const TDevice&);
235     TDevice& operator=(const TDevice&);
236   };
237   struct TSubTableBase {
238     uint16_t SubstFormat;
TSubTableBaseTSubTableBase239     TSubTableBase() : SubstFormat(0) {}
~TSubTableBaseTSubTableBase240     virtual ~TSubTableBase() {}
241 
242    private:
243     TSubTableBase(const TSubTableBase&);
244     TSubTableBase& operator=(const TSubTableBase&);
245   };
246   struct TSingleSubstFormat1 : public TSubTableBase {
247     TCoverageFormatBase* Coverage;
248     int16_t DeltaGlyphID;
TSingleSubstFormat1TSingleSubstFormat1249     TSingleSubstFormat1() : Coverage(NULL), DeltaGlyphID(0) { SubstFormat = 1; }
~TSingleSubstFormat1TSingleSubstFormat1250     ~TSingleSubstFormat1() override { delete Coverage; }
251 
252    private:
253     TSingleSubstFormat1(const TSingleSubstFormat1&);
254     TSingleSubstFormat1& operator=(const TSingleSubstFormat1&);
255   };
256   struct TSingleSubstFormat2 : public TSubTableBase {
257     TCoverageFormatBase* Coverage;
258     uint16_t GlyphCount;
259     uint16_t* Substitute;
TSingleSubstFormat2TSingleSubstFormat2260     TSingleSubstFormat2() : Coverage(NULL), GlyphCount(0), Substitute(NULL) {
261       SubstFormat = 2;
262     }
~TSingleSubstFormat2TSingleSubstFormat2263     ~TSingleSubstFormat2() override {
264       delete Coverage;
265       delete[] Substitute;
266     }
267 
268    private:
269     TSingleSubstFormat2(const TSingleSubstFormat2&);
270     TSingleSubstFormat2& operator=(const TSingleSubstFormat2&);
271   };
272   struct TLookup {
273     uint16_t LookupType;
274     uint16_t LookupFlag;
275     uint16_t SubTableCount;
276     struct TSubTableBase** SubTable;
TLookupTLookup277     TLookup()
278         : LookupType(0), LookupFlag(0), SubTableCount(0), SubTable(NULL) {}
~TLookupTLookup279     ~TLookup() {
280       if (SubTable) {
281         for (int i = 0; i < SubTableCount; ++i)
282           delete SubTable[i];
283         delete[] SubTable;
284       }
285     }
286 
287    private:
288     TLookup(const TLookup&);
289     TLookup& operator=(const TLookup&);
290   };
291   struct TLookupList {
292     int LookupCount;
293     struct TLookup* Lookup;
TLookupListTLookupList294     TLookupList() : LookupCount(0), Lookup(NULL) {}
~TLookupListTLookupList295     ~TLookupList() { delete[] Lookup; }
296 
297    private:
298     TLookupList(const TLookupList&);
299     TLookupList& operator=(const TLookupList&);
300   };
301   bool Parse(FT_Bytes scriptlist, FT_Bytes featurelist, FT_Bytes lookuplist);
302   void ParseScriptList(FT_Bytes raw, TScriptList* rec);
303   void ParseScript(FT_Bytes raw, TScript* rec);
304   void ParseLangSys(FT_Bytes raw, TLangSys* rec);
305   void ParseFeatureList(FT_Bytes raw, TFeatureList* rec);
306   void ParseFeature(FT_Bytes raw, TFeature* rec);
307   void ParseLookupList(FT_Bytes raw, TLookupList* rec);
308   void ParseLookup(FT_Bytes raw, TLookup* rec);
309   void ParseCoverage(FT_Bytes raw, TCoverageFormatBase** rec);
310   void ParseCoverageFormat1(FT_Bytes raw, TCoverageFormat1* rec);
311   void ParseCoverageFormat2(FT_Bytes raw, TCoverageFormat2* rec);
312   void ParseSingleSubst(FT_Bytes raw, TSubTableBase** rec);
313   void ParseSingleSubstFormat1(FT_Bytes raw, TSingleSubstFormat1* rec);
314   void ParseSingleSubstFormat2(FT_Bytes raw, TSingleSubstFormat2* rec);
315   bool GetVerticalGlyphSub(uint32_t glyphnum,
316                            uint32_t* vglyphnum,
317                            struct TFeature* Feature);
318   bool GetVerticalGlyphSub2(uint32_t glyphnum,
319                             uint32_t* vglyphnum,
320                             struct TLookup* Lookup);
321   int GetCoverageIndex(struct TCoverageFormatBase* Coverage, uint32_t g);
GetUInt8(FT_Bytes & p)322   uint8_t GetUInt8(FT_Bytes& p) const {
323     uint8_t ret = p[0];
324     p += 1;
325     return ret;
326   }
GetInt16(FT_Bytes & p)327   int16_t GetInt16(FT_Bytes& p) const {
328     uint16_t ret = p[0] << 8 | p[1];
329     p += 2;
330     return *(int16_t*)&ret;
331   }
GetUInt16(FT_Bytes & p)332   uint16_t GetUInt16(FT_Bytes& p) const {
333     uint16_t ret = p[0] << 8 | p[1];
334     p += 2;
335     return ret;
336   }
GetInt32(FT_Bytes & p)337   int32_t GetInt32(FT_Bytes& p) const {
338     uint32_t ret = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
339     p += 4;
340     return *(int32_t*)&ret;
341   }
GetUInt32(FT_Bytes & p)342   uint32_t GetUInt32(FT_Bytes& p) const {
343     uint32_t ret = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
344     p += 4;
345     return ret;
346   }
347   std::map<FX_DWORD, FX_DWORD> m_featureMap;
348   FX_BOOL m_bFeautureMapLoad;
349   bool loaded;
350   struct tt_gsub_header header;
351   struct TScriptList ScriptList;
352   struct TFeatureList FeatureList;
353   struct TLookupList LookupList;
354 };
355 class CFX_GSUBTable final : public IFX_GSUBTable {
356  public:
~CFX_GSUBTable()357   ~CFX_GSUBTable() override {}
358   FX_BOOL GetVerticalGlyph(FX_DWORD glyphnum, FX_DWORD* vglyphnum) override;
359 
360   CFX_CTTGSUBTable m_GsubImp;
361 };
362 
363 #endif  // CORE_SRC_FPDFAPI_FPDF_FONT_TTGSUBTABLE_H_
364