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_CFX_CTTGSUBTABLE_H_
8 #define CORE_FPDFAPI_FONT_CFX_CTTGSUBTABLE_H_
9 
10 #include <stdint.h>
11 
12 #include <memory>
13 #include <set>
14 #include <vector>
15 
16 #include "core/fxge/fx_freetype.h"
17 
18 class CFX_CTTGSUBTable {
19  public:
20   explicit CFX_CTTGSUBTable(FT_Bytes gsub);
21   ~CFX_CTTGSUBTable();
22 
23   uint32_t GetVerticalGlyph(uint32_t glyphnum) const;
24 
25  private:
26   struct TLangSysRecord {
27     TLangSysRecord();
28     ~TLangSysRecord();
29 
30     uint32_t LangSysTag;
31     uint16_t LookupOrder;
32     uint16_t ReqFeatureIndex;
33     std::vector<uint16_t> FeatureIndices;
34   };
35 
36   struct TScriptRecord {
37     TScriptRecord();
38     ~TScriptRecord();
39 
40     uint32_t ScriptTag;
41     uint16_t DefaultLangSys;
42     std::vector<TLangSysRecord> LangSysRecords;
43   };
44 
45   struct TFeatureRecord {
46     TFeatureRecord();
47     ~TFeatureRecord();
48 
49     uint32_t FeatureTag;
50     uint16_t FeatureParams;
51     std::vector<uint16_t> LookupListIndices;
52   };
53 
54   struct TRangeRecord {
55     TRangeRecord();
56 
57     uint16_t Start;
58     uint16_t End;
59     uint16_t StartCoverageIndex;
60   };
61 
62   struct TCoverageFormatBase {
63     virtual ~TCoverageFormatBase() = default;
64     uint16_t CoverageFormat;
65   };
66 
67   struct TCoverageFormat1 final : public TCoverageFormatBase {
68     TCoverageFormat1();
69     ~TCoverageFormat1() override;
70 
71     std::vector<uint16_t> GlyphArray;
72   };
73 
74   struct TCoverageFormat2 final : public TCoverageFormatBase {
75     TCoverageFormat2();
76     ~TCoverageFormat2() override;
77 
78     std::vector<TRangeRecord> RangeRecords;
79   };
80 
81   struct TDevice {
TDeviceTDevice82     TDevice() : StartSize(0), EndSize(0), DeltaFormat(0) {}
83 
84     uint16_t StartSize;
85     uint16_t EndSize;
86     uint16_t DeltaFormat;
87   };
88 
89   struct TSubTableBase {
90     TSubTableBase();
91     virtual ~TSubTableBase();
92 
93     std::unique_ptr<TCoverageFormatBase> Coverage;
94     uint16_t SubstFormat;
95   };
96 
97   struct TSubTable1 final : public TSubTableBase {
98     TSubTable1();
99     ~TSubTable1() override;
100 
101     int16_t DeltaGlyphID;
102   };
103 
104   struct TSubTable2 final : public TSubTableBase {
105     TSubTable2();
106     ~TSubTable2() override;
107 
108     std::vector<uint16_t> Substitutes;
109   };
110 
111   struct TLookup {
112     TLookup();
113     ~TLookup();
114 
115     uint16_t LookupType;
116     uint16_t LookupFlag;
117     std::vector<std::unique_ptr<TSubTableBase>> SubTables;
118   };
119 
120   bool LoadGSUBTable(FT_Bytes gsub);
121   bool Parse(FT_Bytes scriptlist, FT_Bytes featurelist, FT_Bytes lookuplist);
122   void ParseScriptList(FT_Bytes raw);
123   void ParseScript(FT_Bytes raw, TScriptRecord* rec);
124   void ParseLangSys(FT_Bytes raw, TLangSysRecord* rec);
125   void ParseFeatureList(FT_Bytes raw);
126   void ParseFeature(FT_Bytes raw, TFeatureRecord* rec);
127   void ParseLookupList(FT_Bytes raw);
128   void ParseLookup(FT_Bytes raw, TLookup* rec);
129   std::unique_ptr<TCoverageFormatBase> ParseCoverage(FT_Bytes raw);
130   void ParseCoverageFormat1(FT_Bytes raw, TCoverageFormat1* rec);
131   void ParseCoverageFormat2(FT_Bytes raw, TCoverageFormat2* rec);
132   void ParseSingleSubst(FT_Bytes raw, std::unique_ptr<TSubTableBase>* rec);
133   void ParseSingleSubstFormat1(FT_Bytes raw, TSubTable1* rec);
134   void ParseSingleSubstFormat2(FT_Bytes raw, TSubTable2* rec);
135 
136   bool GetVerticalGlyphSub(const TFeatureRecord& feature,
137                            uint32_t glyphnum,
138                            uint32_t* vglyphnum) const;
139   bool GetVerticalGlyphSub2(const TLookup& lookup,
140                             uint32_t glyphnum,
141                             uint32_t* vglyphnum) const;
142   int GetCoverageIndex(TCoverageFormatBase* Coverage, uint32_t g) const;
143 
144   uint8_t GetUInt8(FT_Bytes& p) const;
145   int16_t GetInt16(FT_Bytes& p) const;
146   uint16_t GetUInt16(FT_Bytes& p) const;
147   int32_t GetInt32(FT_Bytes& p) const;
148   uint32_t GetUInt32(FT_Bytes& p) const;
149 
150   std::set<uint32_t> m_featureSet;
151   std::vector<TScriptRecord> ScriptList;
152   std::vector<TFeatureRecord> FeatureList;
153   std::vector<TLookup> LookupList;
154 };
155 
156 #endif  // CORE_FPDFAPI_FONT_CFX_CTTGSUBTABLE_H_
157