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 #include "core/fpdfapi/font/cfx_cttgsubtable.h"
8
9 #include <utility>
10
11 #include "core/fxge/cfx_fontmapper.h"
12 #include "third_party/base/ptr_util.h"
13 #include "third_party/base/stl_util.h"
14
15 namespace {
16
IsVerticalFeatureTag(uint32_t tag)17 bool IsVerticalFeatureTag(uint32_t tag) {
18 static constexpr uint32_t kTags[] = {
19 CFX_FontMapper::MakeTag('v', 'r', 't', '2'),
20 CFX_FontMapper::MakeTag('v', 'e', 'r', 't'),
21 };
22 return tag == kTags[0] || tag == kTags[1];
23 }
24
25 } // namespace
26
CFX_CTTGSUBTable(FT_Bytes gsub)27 CFX_CTTGSUBTable::CFX_CTTGSUBTable(FT_Bytes gsub) {
28 if (!LoadGSUBTable(gsub))
29 return;
30
31 for (const TScriptRecord& script : ScriptList) {
32 for (const auto& record : script.LangSysRecords) {
33 for (uint16_t index : record.FeatureIndices) {
34 if (IsVerticalFeatureTag(FeatureList[index].FeatureTag))
35 m_featureSet.insert(index);
36 }
37 }
38 }
39 if (!m_featureSet.empty())
40 return;
41
42 int i = 0;
43 for (const TFeatureRecord& feature : FeatureList) {
44 if (IsVerticalFeatureTag(feature.FeatureTag))
45 m_featureSet.insert(i);
46 ++i;
47 }
48 }
49
50 CFX_CTTGSUBTable::~CFX_CTTGSUBTable() = default;
51
LoadGSUBTable(FT_Bytes gsub)52 bool CFX_CTTGSUBTable::LoadGSUBTable(FT_Bytes gsub) {
53 if ((gsub[0] << 24u | gsub[1] << 16u | gsub[2] << 8u | gsub[3]) != 0x00010000)
54 return false;
55
56 return Parse(&gsub[gsub[4] << 8 | gsub[5]], &gsub[gsub[6] << 8 | gsub[7]],
57 &gsub[gsub[8] << 8 | gsub[9]]);
58 }
59
GetVerticalGlyph(uint32_t glyphnum) const60 uint32_t CFX_CTTGSUBTable::GetVerticalGlyph(uint32_t glyphnum) const {
61 uint32_t vglyphnum = 0;
62 for (uint32_t item : m_featureSet) {
63 if (GetVerticalGlyphSub(FeatureList[item], glyphnum, &vglyphnum))
64 break;
65 }
66 return vglyphnum;
67 }
68
GetVerticalGlyphSub(const TFeatureRecord & feature,uint32_t glyphnum,uint32_t * vglyphnum) const69 bool CFX_CTTGSUBTable::GetVerticalGlyphSub(const TFeatureRecord& feature,
70 uint32_t glyphnum,
71 uint32_t* vglyphnum) const {
72 for (int index : feature.LookupListIndices) {
73 if (!pdfium::IndexInBounds(LookupList, index))
74 continue;
75 if (LookupList[index].LookupType == 1 &&
76 GetVerticalGlyphSub2(LookupList[index], glyphnum, vglyphnum)) {
77 return true;
78 }
79 }
80 return false;
81 }
82
GetVerticalGlyphSub2(const TLookup & lookup,uint32_t glyphnum,uint32_t * vglyphnum) const83 bool CFX_CTTGSUBTable::GetVerticalGlyphSub2(const TLookup& lookup,
84 uint32_t glyphnum,
85 uint32_t* vglyphnum) const {
86 for (const auto& subTable : lookup.SubTables) {
87 switch (subTable->SubstFormat) {
88 case 1: {
89 auto* tbl1 = static_cast<TSubTable1*>(subTable.get());
90 if (GetCoverageIndex(tbl1->Coverage.get(), glyphnum) >= 0) {
91 *vglyphnum = glyphnum + tbl1->DeltaGlyphID;
92 return true;
93 }
94 break;
95 }
96 case 2: {
97 auto* tbl2 = static_cast<TSubTable2*>(subTable.get());
98 int index = GetCoverageIndex(tbl2->Coverage.get(), glyphnum);
99 if (pdfium::IndexInBounds(tbl2->Substitutes, index)) {
100 *vglyphnum = tbl2->Substitutes[index];
101 return true;
102 }
103 break;
104 }
105 }
106 }
107 return false;
108 }
109
GetCoverageIndex(TCoverageFormatBase * Coverage,uint32_t g) const110 int CFX_CTTGSUBTable::GetCoverageIndex(TCoverageFormatBase* Coverage,
111 uint32_t g) const {
112 if (!Coverage)
113 return -1;
114
115 switch (Coverage->CoverageFormat) {
116 case 1: {
117 int i = 0;
118 TCoverageFormat1* c1 = static_cast<TCoverageFormat1*>(Coverage);
119 for (const auto& glyph : c1->GlyphArray) {
120 if (static_cast<uint32_t>(glyph) == g)
121 return i;
122 ++i;
123 }
124 return -1;
125 }
126 case 2: {
127 TCoverageFormat2* c2 = static_cast<TCoverageFormat2*>(Coverage);
128 for (const auto& rangeRec : c2->RangeRecords) {
129 uint32_t s = rangeRec.Start;
130 uint32_t e = rangeRec.End;
131 uint32_t si = rangeRec.StartCoverageIndex;
132 if (s <= g && g <= e)
133 return si + g - s;
134 }
135 return -1;
136 }
137 }
138 return -1;
139 }
140
GetUInt8(FT_Bytes & p) const141 uint8_t CFX_CTTGSUBTable::GetUInt8(FT_Bytes& p) const {
142 uint8_t ret = p[0];
143 p += 1;
144 return ret;
145 }
146
GetInt16(FT_Bytes & p) const147 int16_t CFX_CTTGSUBTable::GetInt16(FT_Bytes& p) const {
148 uint16_t ret = p[0] << 8 | p[1];
149 p += 2;
150 return *(int16_t*)&ret;
151 }
152
GetUInt16(FT_Bytes & p) const153 uint16_t CFX_CTTGSUBTable::GetUInt16(FT_Bytes& p) const {
154 uint16_t ret = p[0] << 8 | p[1];
155 p += 2;
156 return ret;
157 }
158
GetInt32(FT_Bytes & p) const159 int32_t CFX_CTTGSUBTable::GetInt32(FT_Bytes& p) const {
160 uint32_t ret = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
161 p += 4;
162 return *(int32_t*)&ret;
163 }
164
GetUInt32(FT_Bytes & p) const165 uint32_t CFX_CTTGSUBTable::GetUInt32(FT_Bytes& p) const {
166 uint32_t ret = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
167 p += 4;
168 return ret;
169 }
170
Parse(FT_Bytes scriptlist,FT_Bytes featurelist,FT_Bytes lookuplist)171 bool CFX_CTTGSUBTable::Parse(FT_Bytes scriptlist,
172 FT_Bytes featurelist,
173 FT_Bytes lookuplist) {
174 ParseScriptList(scriptlist);
175 ParseFeatureList(featurelist);
176 ParseLookupList(lookuplist);
177 return true;
178 }
179
ParseScriptList(FT_Bytes raw)180 void CFX_CTTGSUBTable::ParseScriptList(FT_Bytes raw) {
181 FT_Bytes sp = raw;
182 ScriptList = std::vector<TScriptRecord>(GetUInt16(sp));
183 for (auto& scriptRec : ScriptList) {
184 scriptRec.ScriptTag = GetUInt32(sp);
185 ParseScript(&raw[GetUInt16(sp)], &scriptRec);
186 }
187 }
188
ParseScript(FT_Bytes raw,TScriptRecord * rec)189 void CFX_CTTGSUBTable::ParseScript(FT_Bytes raw, TScriptRecord* rec) {
190 FT_Bytes sp = raw;
191 rec->DefaultLangSys = GetUInt16(sp);
192 rec->LangSysRecords = std::vector<TLangSysRecord>(GetUInt16(sp));
193 for (auto& sysRecord : rec->LangSysRecords) {
194 sysRecord.LangSysTag = GetUInt32(sp);
195 ParseLangSys(&raw[GetUInt16(sp)], &sysRecord);
196 }
197 }
198
ParseLangSys(FT_Bytes raw,TLangSysRecord * rec)199 void CFX_CTTGSUBTable::ParseLangSys(FT_Bytes raw, TLangSysRecord* rec) {
200 FT_Bytes sp = raw;
201 rec->LookupOrder = GetUInt16(sp);
202 rec->ReqFeatureIndex = GetUInt16(sp);
203 rec->FeatureIndices = std::vector<uint16_t>(GetUInt16(sp));
204 for (auto& element : rec->FeatureIndices)
205 element = GetUInt16(sp);
206 }
207
ParseFeatureList(FT_Bytes raw)208 void CFX_CTTGSUBTable::ParseFeatureList(FT_Bytes raw) {
209 FT_Bytes sp = raw;
210 FeatureList = std::vector<TFeatureRecord>(GetUInt16(sp));
211 for (auto& featureRec : FeatureList) {
212 featureRec.FeatureTag = GetUInt32(sp);
213 ParseFeature(&raw[GetUInt16(sp)], &featureRec);
214 }
215 }
216
ParseFeature(FT_Bytes raw,TFeatureRecord * rec)217 void CFX_CTTGSUBTable::ParseFeature(FT_Bytes raw, TFeatureRecord* rec) {
218 FT_Bytes sp = raw;
219 rec->FeatureParams = GetUInt16(sp);
220 rec->LookupListIndices = std::vector<uint16_t>(GetUInt16(sp));
221 for (auto& listIndex : rec->LookupListIndices)
222 listIndex = GetUInt16(sp);
223 }
224
ParseLookupList(FT_Bytes raw)225 void CFX_CTTGSUBTable::ParseLookupList(FT_Bytes raw) {
226 FT_Bytes sp = raw;
227 LookupList = std::vector<TLookup>(GetUInt16(sp));
228 for (auto& lookup : LookupList)
229 ParseLookup(&raw[GetUInt16(sp)], &lookup);
230 }
231
ParseLookup(FT_Bytes raw,TLookup * rec)232 void CFX_CTTGSUBTable::ParseLookup(FT_Bytes raw, TLookup* rec) {
233 FT_Bytes sp = raw;
234 rec->LookupType = GetUInt16(sp);
235 rec->LookupFlag = GetUInt16(sp);
236 rec->SubTables = std::vector<std::unique_ptr<TSubTableBase>>(GetUInt16(sp));
237 if (rec->LookupType != 1)
238 return;
239
240 for (auto& subTable : rec->SubTables)
241 ParseSingleSubst(&raw[GetUInt16(sp)], &subTable);
242 }
243
244 std::unique_ptr<CFX_CTTGSUBTable::TCoverageFormatBase>
ParseCoverage(FT_Bytes raw)245 CFX_CTTGSUBTable::ParseCoverage(FT_Bytes raw) {
246 FT_Bytes sp = raw;
247 uint16_t format = GetUInt16(sp);
248 if (format == 1) {
249 auto rec = pdfium::MakeUnique<TCoverageFormat1>();
250 ParseCoverageFormat1(raw, rec.get());
251 return std::move(rec);
252 }
253 if (format == 2) {
254 auto rec = pdfium::MakeUnique<TCoverageFormat2>();
255 ParseCoverageFormat2(raw, rec.get());
256 return std::move(rec);
257 }
258 return nullptr;
259 }
260
ParseCoverageFormat1(FT_Bytes raw,TCoverageFormat1 * rec)261 void CFX_CTTGSUBTable::ParseCoverageFormat1(FT_Bytes raw,
262 TCoverageFormat1* rec) {
263 FT_Bytes sp = raw;
264 (void)GetUInt16(sp);
265 rec->GlyphArray = std::vector<uint16_t>(GetUInt16(sp));
266 for (auto& glyph : rec->GlyphArray)
267 glyph = GetUInt16(sp);
268 }
269
ParseCoverageFormat2(FT_Bytes raw,TCoverageFormat2 * rec)270 void CFX_CTTGSUBTable::ParseCoverageFormat2(FT_Bytes raw,
271 TCoverageFormat2* rec) {
272 FT_Bytes sp = raw;
273 (void)GetUInt16(sp);
274 rec->RangeRecords = std::vector<TRangeRecord>(GetUInt16(sp));
275 for (auto& rangeRec : rec->RangeRecords) {
276 rangeRec.Start = GetUInt16(sp);
277 rangeRec.End = GetUInt16(sp);
278 rangeRec.StartCoverageIndex = GetUInt16(sp);
279 }
280 }
281
ParseSingleSubst(FT_Bytes raw,std::unique_ptr<TSubTableBase> * rec)282 void CFX_CTTGSUBTable::ParseSingleSubst(FT_Bytes raw,
283 std::unique_ptr<TSubTableBase>* rec) {
284 FT_Bytes sp = raw;
285 uint16_t Format = GetUInt16(sp);
286 switch (Format) {
287 case 1:
288 *rec = pdfium::MakeUnique<TSubTable1>();
289 ParseSingleSubstFormat1(raw, static_cast<TSubTable1*>(rec->get()));
290 break;
291 case 2:
292 *rec = pdfium::MakeUnique<TSubTable2>();
293 ParseSingleSubstFormat2(raw, static_cast<TSubTable2*>(rec->get()));
294 break;
295 }
296 }
297
ParseSingleSubstFormat1(FT_Bytes raw,TSubTable1 * rec)298 void CFX_CTTGSUBTable::ParseSingleSubstFormat1(FT_Bytes raw, TSubTable1* rec) {
299 FT_Bytes sp = raw;
300 GetUInt16(sp);
301 uint16_t offset = GetUInt16(sp);
302 rec->Coverage = ParseCoverage(&raw[offset]);
303 rec->DeltaGlyphID = GetInt16(sp);
304 }
305
ParseSingleSubstFormat2(FT_Bytes raw,TSubTable2 * rec)306 void CFX_CTTGSUBTable::ParseSingleSubstFormat2(FT_Bytes raw, TSubTable2* rec) {
307 FT_Bytes sp = raw;
308 (void)GetUInt16(sp);
309 uint16_t offset = GetUInt16(sp);
310 rec->Coverage = ParseCoverage(&raw[offset]);
311 rec->Substitutes = std::vector<uint16_t>(GetUInt16(sp));
312 for (auto& substitute : rec->Substitutes)
313 substitute = GetUInt16(sp);
314 }
315
TLangSysRecord()316 CFX_CTTGSUBTable::TLangSysRecord::TLangSysRecord()
317 : LangSysTag(0), LookupOrder(0), ReqFeatureIndex(0) {}
318
~TLangSysRecord()319 CFX_CTTGSUBTable::TLangSysRecord::~TLangSysRecord() {}
320
TScriptRecord()321 CFX_CTTGSUBTable::TScriptRecord::TScriptRecord()
322 : ScriptTag(0), DefaultLangSys(0) {}
323
~TScriptRecord()324 CFX_CTTGSUBTable::TScriptRecord::~TScriptRecord() {}
325
TFeatureRecord()326 CFX_CTTGSUBTable::TFeatureRecord::TFeatureRecord()
327 : FeatureTag(0), FeatureParams(0) {}
328
~TFeatureRecord()329 CFX_CTTGSUBTable::TFeatureRecord::~TFeatureRecord() {}
330
TRangeRecord()331 CFX_CTTGSUBTable::TRangeRecord::TRangeRecord()
332 : Start(0), End(0), StartCoverageIndex(0) {}
333
TCoverageFormat1()334 CFX_CTTGSUBTable::TCoverageFormat1::TCoverageFormat1() {
335 CoverageFormat = 1;
336 }
337
~TCoverageFormat1()338 CFX_CTTGSUBTable::TCoverageFormat1::~TCoverageFormat1() {}
339
TCoverageFormat2()340 CFX_CTTGSUBTable::TCoverageFormat2::TCoverageFormat2() {
341 CoverageFormat = 2;
342 }
343
~TCoverageFormat2()344 CFX_CTTGSUBTable::TCoverageFormat2::~TCoverageFormat2() {}
345
TSubTableBase()346 CFX_CTTGSUBTable::TSubTableBase::TSubTableBase() {}
347
~TSubTableBase()348 CFX_CTTGSUBTable::TSubTableBase::~TSubTableBase() {}
349
TSubTable1()350 CFX_CTTGSUBTable::TSubTable1::TSubTable1() {
351 SubstFormat = 1;
352 }
353
~TSubTable1()354 CFX_CTTGSUBTable::TSubTable1::~TSubTable1() {}
355
TSubTable2()356 CFX_CTTGSUBTable::TSubTable2::TSubTable2() {
357 SubstFormat = 2;
358 }
359
~TSubTable2()360 CFX_CTTGSUBTable::TSubTable2::~TSubTable2() {}
361
TLookup()362 CFX_CTTGSUBTable::TLookup::TLookup() : LookupType(0), LookupFlag(0) {}
363
~TLookup()364 CFX_CTTGSUBTable::TLookup::~TLookup() {}
365