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