ttgsubtable.h revision 4d3acf4ec42bf6e838f9060103aff98fbf170794
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_FPDFAPI_FONT_TTGSUBTABLE_H_
8#define CORE_FPDFAPI_FONT_TTGSUBTABLE_H_
9
10#include <stdint.h>
11
12#include <map>
13#include <memory>
14
15#include "core/fxcrt/fx_basic.h"
16#include "core/fxge/fx_font.h"
17#include "core/fxge/fx_freetype.h"
18
19class CFX_GlyphMap {
20 public:
21  CFX_GlyphMap();
22  ~CFX_GlyphMap();
23
24  void SetAt(int key, int value);
25  bool Lookup(int key, int& value);
26
27 protected:
28  CFX_BinaryBuf m_Buffer;
29};
30
31class CFX_CTTGSUBTable {
32 public:
33  CFX_CTTGSUBTable();
34  explicit CFX_CTTGSUBTable(FT_Bytes gsub);
35  virtual ~CFX_CTTGSUBTable();
36
37  bool IsOk() const;
38  bool LoadGSUBTable(FT_Bytes gsub);
39  bool GetVerticalGlyph(uint32_t glyphnum, uint32_t* vglyphnum);
40
41 private:
42  struct tt_gsub_header {
43    uint32_t Version;
44    uint16_t ScriptList;
45    uint16_t FeatureList;
46    uint16_t LookupList;
47  };
48  struct TLangSys {
49    TLangSys();
50    ~TLangSys();
51
52    uint16_t LookupOrder;
53    uint16_t ReqFeatureIndex;
54    uint16_t FeatureCount;
55    std::unique_ptr<uint16_t[]> FeatureIndex;
56
57   private:
58    TLangSys(const TLangSys&);
59    TLangSys& operator=(const TLangSys&);
60  };
61  struct TLangSysRecord {
62    TLangSysRecord() : LangSysTag(0) {}
63
64    uint32_t LangSysTag;
65    TLangSys LangSys;
66
67   private:
68    TLangSysRecord(const TLangSysRecord&);
69    TLangSysRecord& operator=(const TLangSysRecord&);
70  };
71  struct TScript {
72    TScript();
73    ~TScript();
74
75    uint16_t DefaultLangSys;
76    uint16_t LangSysCount;
77    std::unique_ptr<TLangSysRecord[]> LangSysRecord;
78
79   private:
80    TScript(const TScript&);
81    TScript& operator=(const TScript&);
82  };
83  struct TScriptRecord {
84    TScriptRecord() : ScriptTag(0) {}
85
86    uint32_t ScriptTag;
87    TScript Script;
88
89   private:
90    TScriptRecord(const TScriptRecord&);
91    TScriptRecord& operator=(const TScriptRecord&);
92  };
93  struct TScriptList {
94    TScriptList();
95    ~TScriptList();
96
97    uint16_t ScriptCount;
98    std::unique_ptr<TScriptRecord[]> ScriptRecord;
99
100   private:
101    TScriptList(const TScriptList&);
102    TScriptList& operator=(const TScriptList&);
103  };
104  struct TFeature {
105    TFeature();
106    ~TFeature();
107
108    uint16_t FeatureParams;
109    int LookupCount;
110    std::unique_ptr<uint16_t[]> LookupListIndex;
111
112   private:
113    TFeature(const TFeature&);
114    TFeature& operator=(const TFeature&);
115  };
116  struct TFeatureRecord {
117    TFeatureRecord() : FeatureTag(0) {}
118
119    uint32_t FeatureTag;
120    TFeature Feature;
121
122   private:
123    TFeatureRecord(const TFeatureRecord&);
124    TFeatureRecord& operator=(const TFeatureRecord&);
125  };
126  struct TFeatureList {
127    TFeatureList();
128    ~TFeatureList();
129
130    int FeatureCount;
131    std::unique_ptr<TFeatureRecord[]> FeatureRecord;
132
133   private:
134    TFeatureList(const TFeatureList&);
135    TFeatureList& operator=(const TFeatureList&);
136  };
137  enum TLookupFlag {
138    LOOKUPFLAG_RightToLeft = 0x0001,
139    LOOKUPFLAG_IgnoreBaseGlyphs = 0x0002,
140    LOOKUPFLAG_IgnoreLigatures = 0x0004,
141    LOOKUPFLAG_IgnoreMarks = 0x0008,
142    LOOKUPFLAG_Reserved = 0x00F0,
143    LOOKUPFLAG_MarkAttachmentType = 0xFF00,
144  };
145  struct TCoverageFormatBase {
146    TCoverageFormatBase() : CoverageFormat(0) {}
147    explicit TCoverageFormatBase(uint16_t format) : CoverageFormat(format) {}
148    virtual ~TCoverageFormatBase() {}
149
150    uint16_t CoverageFormat;
151    CFX_GlyphMap m_glyphMap;
152
153   private:
154    TCoverageFormatBase(const TCoverageFormatBase&);
155    TCoverageFormatBase& operator=(const TCoverageFormatBase&);
156  };
157  struct TCoverageFormat1 : public TCoverageFormatBase {
158    TCoverageFormat1();
159    ~TCoverageFormat1() override;
160
161    uint16_t GlyphCount;
162    std::unique_ptr<uint16_t[]> GlyphArray;
163
164   private:
165    TCoverageFormat1(const TCoverageFormat1&);
166    TCoverageFormat1& operator=(const TCoverageFormat1&);
167  };
168  struct TRangeRecord {
169    TRangeRecord();
170
171    friend bool operator>(const TRangeRecord& r1, const TRangeRecord& r2) {
172      return r1.Start > r2.Start;
173    }
174
175    uint16_t Start;
176    uint16_t End;
177    uint16_t StartCoverageIndex;
178
179   private:
180    TRangeRecord(const TRangeRecord&);
181  };
182  struct TCoverageFormat2 : public TCoverageFormatBase {
183    TCoverageFormat2();
184    ~TCoverageFormat2() override;
185
186    uint16_t RangeCount;
187    std::unique_ptr<TRangeRecord[]> RangeRecord;
188
189   private:
190    TCoverageFormat2(const TCoverageFormat2&);
191    TCoverageFormat2& operator=(const TCoverageFormat2&);
192  };
193  struct TDevice {
194    TDevice() : StartSize(0), EndSize(0), DeltaFormat(0) {}
195
196    uint16_t StartSize;
197    uint16_t EndSize;
198    uint16_t DeltaFormat;
199
200   private:
201    TDevice(const TDevice&);
202    TDevice& operator=(const TDevice&);
203  };
204  struct TSubTableBase {
205    TSubTableBase() : SubstFormat(0) {}
206    explicit TSubTableBase(uint16_t format) : SubstFormat(format) {}
207    virtual ~TSubTableBase() {}
208
209    uint16_t SubstFormat;
210
211   private:
212    TSubTableBase(const TSubTableBase&);
213    TSubTableBase& operator=(const TSubTableBase&);
214  };
215  struct TSingleSubstFormat1 : public TSubTableBase {
216    TSingleSubstFormat1();
217    ~TSingleSubstFormat1() override;
218
219    std::unique_ptr<TCoverageFormatBase> Coverage;
220    int16_t DeltaGlyphID;
221
222   private:
223    TSingleSubstFormat1(const TSingleSubstFormat1&);
224    TSingleSubstFormat1& operator=(const TSingleSubstFormat1&);
225  };
226  struct TSingleSubstFormat2 : public TSubTableBase {
227    TSingleSubstFormat2();
228    ~TSingleSubstFormat2() override;
229
230    std::unique_ptr<TCoverageFormatBase> Coverage;
231    uint16_t GlyphCount;
232    std::unique_ptr<uint16_t[]> Substitute;
233
234   private:
235    TSingleSubstFormat2(const TSingleSubstFormat2&);
236    TSingleSubstFormat2& operator=(const TSingleSubstFormat2&);
237  };
238  struct TLookup {
239    TLookup();
240    ~TLookup();
241
242    uint16_t LookupType;
243    uint16_t LookupFlag;
244    uint16_t SubTableCount;
245    std::unique_ptr<TSubTableBase* []> SubTable;
246
247   private:
248    TLookup(const TLookup&);
249    TLookup& operator=(const TLookup&);
250  };
251  struct TLookupList {
252    TLookupList();
253    ~TLookupList();
254
255    int LookupCount;
256    std::unique_ptr<TLookup[]> Lookup;
257
258   private:
259    TLookupList(const TLookupList&);
260    TLookupList& operator=(const TLookupList&);
261  };
262
263  bool Parse(FT_Bytes scriptlist, FT_Bytes featurelist, FT_Bytes lookuplist);
264  void ParseScriptList(FT_Bytes raw, TScriptList* rec);
265  void ParseScript(FT_Bytes raw, TScript* rec);
266  void ParseLangSys(FT_Bytes raw, TLangSys* rec);
267  void ParseFeatureList(FT_Bytes raw, TFeatureList* rec);
268  void ParseFeature(FT_Bytes raw, TFeature* rec);
269  void ParseLookupList(FT_Bytes raw, TLookupList* rec);
270  void ParseLookup(FT_Bytes raw, TLookup* rec);
271  TCoverageFormatBase* ParseCoverage(FT_Bytes raw);
272  void ParseCoverageFormat1(FT_Bytes raw, TCoverageFormat1* rec);
273  void ParseCoverageFormat2(FT_Bytes raw, TCoverageFormat2* rec);
274  void ParseSingleSubst(FT_Bytes raw, TSubTableBase** rec);
275  void ParseSingleSubstFormat1(FT_Bytes raw, TSingleSubstFormat1* rec);
276  void ParseSingleSubstFormat2(FT_Bytes raw, TSingleSubstFormat2* rec);
277
278  bool GetVerticalGlyphSub(uint32_t glyphnum,
279                           uint32_t* vglyphnum,
280                           TFeature* Feature) const;
281  bool GetVerticalGlyphSub2(uint32_t glyphnum,
282                            uint32_t* vglyphnum,
283                            TLookup* Lookup) const;
284  int GetCoverageIndex(TCoverageFormatBase* Coverage, uint32_t g) const;
285
286  uint8_t GetUInt8(FT_Bytes& p) const;
287  int16_t GetInt16(FT_Bytes& p) const;
288  uint16_t GetUInt16(FT_Bytes& p) const;
289  int32_t GetInt32(FT_Bytes& p) const;
290  uint32_t GetUInt32(FT_Bytes& p) const;
291
292  std::map<uint32_t, uint32_t> m_featureMap;
293  bool m_bFeautureMapLoad;
294  bool loaded;
295  tt_gsub_header header;
296  TScriptList ScriptList;
297  TFeatureList FeatureList;
298  TLookupList LookupList;
299};
300
301#endif  // CORE_FPDFAPI_FONT_TTGSUBTABLE_H_
302