diff options
Diffstat (limited to 'core/fpdfapi/font/cfx_cttgsubtable.cpp')
-rw-r--r-- | core/fpdfapi/font/cfx_cttgsubtable.cpp | 389 |
1 files changed, 186 insertions, 203 deletions
diff --git a/core/fpdfapi/font/cfx_cttgsubtable.cpp b/core/fpdfapi/font/cfx_cttgsubtable.cpp index ae5a84126..c19649e56 100644 --- a/core/fpdfapi/font/cfx_cttgsubtable.cpp +++ b/core/fpdfapi/font/cfx_cttgsubtable.cpp @@ -27,44 +27,53 @@ bool IsVerticalFeatureTag(uint32_t tag) { } // namespace -CFX_CTTGSUBTable::CFX_CTTGSUBTable(FT_Bytes gsub) { +CFX_CTTGSUBTable::CFX_CTTGSUBTable(pdfium::span<const uint8_t> gsub) { if (!LoadGSUBTable(gsub)) return; - for (const TScriptRecord& script : ScriptList) { - for (const auto& record : script.LangSysRecords) { - for (uint16_t index : record.FeatureIndices) { - if (IsVerticalFeatureTag(FeatureList[index].FeatureTag)) - m_featureSet.insert(index); + for (const auto& script : script_list_) { + for (const auto& record : script) { + for (uint16_t index : record) { + if (IsVerticalFeatureTag(feature_list_[index].feature_tag)) { + feature_set_.insert(index); + } } } } - if (!m_featureSet.empty()) + if (!feature_set_.empty()) { return; + } int i = 0; - for (const TFeatureRecord& feature : FeatureList) { - if (IsVerticalFeatureTag(feature.FeatureTag)) - m_featureSet.insert(i); + for (const FeatureRecord& feature : feature_list_) { + if (IsVerticalFeatureTag(feature.feature_tag)) { + feature_set_.insert(i); + } ++i; } } CFX_CTTGSUBTable::~CFX_CTTGSUBTable() = default; -bool CFX_CTTGSUBTable::LoadGSUBTable(FT_Bytes gsub) { +bool CFX_CTTGSUBTable::LoadGSUBTable(pdfium::span<const uint8_t> gsub) { if (FXSYS_UINT32_GET_MSBFIRST(gsub) != 0x00010000) return false; - return Parse(&gsub[FXSYS_UINT16_GET_MSBFIRST(gsub + 4)], - &gsub[FXSYS_UINT16_GET_MSBFIRST(gsub + 6)], - &gsub[FXSYS_UINT16_GET_MSBFIRST(gsub + 8)]); + auto scriptlist_span = gsub.subspan(4, 2); + auto featurelist_span = gsub.subspan(6, 2); + auto lookuplist_span = gsub.subspan(8, 2); + size_t scriptlist_index = FXSYS_UINT16_GET_MSBFIRST(scriptlist_span); + size_t featurelist_index = FXSYS_UINT16_GET_MSBFIRST(featurelist_span); + size_t lookuplist_index = FXSYS_UINT16_GET_MSBFIRST(lookuplist_span); + Parse(gsub.subspan(scriptlist_index), gsub.subspan(featurelist_index), + gsub.subspan(lookuplist_index)); + return true; } uint32_t CFX_CTTGSUBTable::GetVerticalGlyph(uint32_t glyphnum) const { - for (uint32_t item : m_featureSet) { + for (uint32_t item : feature_set_) { absl::optional<uint32_t> result = - GetVerticalGlyphSub(FeatureList[item], glyphnum); + GetVerticalGlyphSub(feature_list_[item], glyphnum); if (result.has_value()) return result.value(); } @@ -72,15 +81,17 @@ uint32_t CFX_CTTGSUBTable::GetVerticalGlyph(uint32_t glyphnum) const { } absl::optional<uint32_t> CFX_CTTGSUBTable::GetVerticalGlyphSub( - const TFeatureRecord& feature, + const FeatureRecord& feature, uint32_t glyphnum) const { - for (int index : feature.LookupListIndices) { - if (!fxcrt::IndexInBounds(LookupList, index)) + for (int index : feature.lookup_list_indices) { + if (!fxcrt::IndexInBounds(lookup_list_, index)) { continue; - if (LookupList[index].LookupType != 1) + } + if (lookup_list_[index].lookup_type != 1) { continue; + } absl::optional<uint32_t> result = - GetVerticalGlyphSub2(LookupList[index], glyphnum); + GetVerticalGlyphSub2(lookup_list_[index], glyphnum); if (result.has_value()) return result.value(); } @@ -88,272 +99,244 @@ absl::optional<uint32_t> CFX_CTTGSUBTable::GetVerticalGlyphSub( } absl::optional<uint32_t> CFX_CTTGSUBTable::GetVerticalGlyphSub2( - const TLookup& lookup, + const Lookup& lookup, uint32_t glyphnum) const { - for (const auto& subTable : lookup.SubTables) { - switch (subTable->SubstFormat) { - case 1: { - auto* tbl1 = static_cast<TSubTable1*>(subTable.get()); - if (GetCoverageIndex(tbl1->Coverage.get(), glyphnum) >= 0) { - return glyphnum + tbl1->DeltaGlyphID; - } - break; + for (const auto& sub_table : lookup.sub_tables) { + if (absl::holds_alternative<absl::monostate>(sub_table.table_data)) { + continue; + } + int index = GetCoverageIndex(sub_table.coverage, glyphnum); + if (absl::holds_alternative<int16_t>(sub_table.table_data)) { + if (index >= 0) { + return glyphnum + absl::get<int16_t>(sub_table.table_data); } - case 2: { - auto* tbl2 = static_cast<TSubTable2*>(subTable.get()); - int index = GetCoverageIndex(tbl2->Coverage.get(), glyphnum); - if (fxcrt::IndexInBounds(tbl2->Substitutes, index)) { - return tbl2->Substitutes[index]; - } - break; + } else { + const auto& substitutes = + absl::get<DataVector<uint16_t>>(sub_table.table_data); + if (fxcrt::IndexInBounds(substitutes, index)) { + return substitutes[index]; } } } return absl::nullopt; } -int CFX_CTTGSUBTable::GetCoverageIndex(TCoverageFormatBase* Coverage, +int CFX_CTTGSUBTable::GetCoverageIndex(const CoverageFormat& coverage, uint32_t g) const { - if (!Coverage) + if (absl::holds_alternative<absl::monostate>(coverage)) { return -1; + } - switch (Coverage->CoverageFormat) { - case 1: { - int i = 0; - TCoverageFormat1* c1 = static_cast<TCoverageFormat1*>(Coverage); - for (const auto& glyph : c1->GlyphArray) { - if (static_cast<uint32_t>(glyph) == g) - return i; - ++i; + if (absl::holds_alternative<DataVector<uint16_t>>(coverage)) { + int i = 0; + const auto& glyph_array = absl::get<DataVector<uint16_t>>(coverage); + for (const auto& glyph : glyph_array) { + if (static_cast<uint32_t>(glyph) == g) { + return i; } - return -1; + ++i; } - case 2: { - TCoverageFormat2* c2 = static_cast<TCoverageFormat2*>(Coverage); - for (const auto& rangeRec : c2->RangeRecords) { - uint32_t s = rangeRec.Start; - uint32_t e = rangeRec.End; - uint32_t si = rangeRec.StartCoverageIndex; - if (s <= g && g <= e) - return si + g - s; - } - return -1; + return -1; + } + + const auto& range_records = absl::get<std::vector<RangeRecord>>(coverage); + for (const auto& range_rec : range_records) { + uint32_t s = range_rec.start; + uint32_t e = range_rec.end; + uint32_t si = range_rec.start_coverage_index; + if (s <= g && g <= e) { + return si + g - s; } } return -1; } -uint8_t CFX_CTTGSUBTable::GetUInt8(FT_Bytes& p) const { +uint8_t CFX_CTTGSUBTable::GetUInt8(const uint8_t*& p) const { uint8_t ret = p[0]; p += 1; return ret; } -int16_t CFX_CTTGSUBTable::GetInt16(FT_Bytes& p) const { +int16_t CFX_CTTGSUBTable::GetInt16(const uint8_t*& p) const { uint16_t ret = FXSYS_UINT16_GET_MSBFIRST(p); p += 2; - return *(int16_t*)&ret; + return *reinterpret_cast<int16_t*>(&ret); } -uint16_t CFX_CTTGSUBTable::GetUInt16(FT_Bytes& p) const { +uint16_t CFX_CTTGSUBTable::GetUInt16(const uint8_t*& p) const { uint16_t ret = FXSYS_UINT16_GET_MSBFIRST(p); p += 2; return ret; } -int32_t CFX_CTTGSUBTable::GetInt32(FT_Bytes& p) const { +int32_t CFX_CTTGSUBTable::GetInt32(const uint8_t*& p) const { uint32_t ret = FXSYS_UINT32_GET_MSBFIRST(p); p += 4; - return *(int32_t*)&ret; + return *reinterpret_cast<int32_t*>(&ret); } -uint32_t CFX_CTTGSUBTable::GetUInt32(FT_Bytes& p) const { +uint32_t CFX_CTTGSUBTable::GetUInt32(const uint8_t*& p) const { uint32_t ret = FXSYS_UINT32_GET_MSBFIRST(p); p += 4; return ret; } -bool CFX_CTTGSUBTable::Parse(FT_Bytes scriptlist, - FT_Bytes featurelist, - FT_Bytes lookuplist) { +void CFX_CTTGSUBTable::Parse(pdfium::span<const uint8_t> scriptlist, + pdfium::span<const uint8_t> featurelist, + pdfium::span<const uint8_t> lookuplist) { ParseScriptList(scriptlist); ParseFeatureList(featurelist); ParseLookupList(lookuplist); - return true; } -void CFX_CTTGSUBTable::ParseScriptList(FT_Bytes raw) { - FT_Bytes sp = raw; - ScriptList = std::vector<TScriptRecord>(GetUInt16(sp)); - for (auto& scriptRec : ScriptList) { - scriptRec.ScriptTag = GetUInt32(sp); - ParseScript(&raw[GetUInt16(sp)], &scriptRec); +void CFX_CTTGSUBTable::ParseScriptList(pdfium::span<const uint8_t> raw) { + const uint8_t* sp = raw.data(); + script_list_ = std::vector<ScriptRecord>(GetUInt16(sp)); + for (auto& script : script_list_) { + // Skip over "ScriptTag" field. + sp += 4; + script = ParseScript(&raw[GetUInt16(sp)]); } } -void CFX_CTTGSUBTable::ParseScript(FT_Bytes raw, TScriptRecord* rec) { - FT_Bytes sp = raw; - rec->DefaultLangSys = GetUInt16(sp); - rec->LangSysRecords = std::vector<TLangSysRecord>(GetUInt16(sp)); - for (auto& sysRecord : rec->LangSysRecords) { - sysRecord.LangSysTag = GetUInt32(sp); - ParseLangSys(&raw[GetUInt16(sp)], &sysRecord); +CFX_CTTGSUBTable::ScriptRecord CFX_CTTGSUBTable::ParseScript( + const uint8_t* raw) { + // Skip over "DefaultLangSys" field. + const uint8_t* sp = raw + 2; + ScriptRecord result(GetUInt16(sp)); + for (auto& record : result) { + // Skip over "LangSysTag" field. + sp += 4; + record = ParseLangSys(&raw[GetUInt16(sp)]); } + return result; } -void CFX_CTTGSUBTable::ParseLangSys(FT_Bytes raw, TLangSysRecord* rec) { - FT_Bytes sp = raw; - rec->LookupOrder = GetUInt16(sp); - rec->ReqFeatureIndex = GetUInt16(sp); - rec->FeatureIndices = DataVector<uint16_t>(GetUInt16(sp)); - for (auto& element : rec->FeatureIndices) +CFX_CTTGSUBTable::FeatureIndices CFX_CTTGSUBTable::ParseLangSys( + const uint8_t* raw) { + // Skip over "LookupOrder" and "ReqFeatureIndex" fields. + const uint8_t* sp = raw + 4; + FeatureIndices result(GetUInt16(sp)); + for (auto& element : result) { element = GetUInt16(sp); + } + return result; } -void CFX_CTTGSUBTable::ParseFeatureList(FT_Bytes raw) { - FT_Bytes sp = raw; - FeatureList = std::vector<TFeatureRecord>(GetUInt16(sp)); - for (auto& featureRec : FeatureList) { - featureRec.FeatureTag = GetUInt32(sp); - ParseFeature(&raw[GetUInt16(sp)], &featureRec); +void CFX_CTTGSUBTable::ParseFeatureList(pdfium::span<const uint8_t> raw) { + const uint8_t* sp = raw.data(); + feature_list_ = std::vector<FeatureRecord>(GetUInt16(sp)); + for (auto& record : feature_list_) { + record.feature_tag = GetUInt32(sp); + record.lookup_list_indices = + ParseFeatureLookupListIndices(&raw[GetUInt16(sp)]); } } -void CFX_CTTGSUBTable::ParseFeature(FT_Bytes raw, TFeatureRecord* rec) { - FT_Bytes sp = raw; - rec->FeatureParams = GetUInt16(sp); - rec->LookupListIndices = DataVector<uint16_t>(GetUInt16(sp)); - for (auto& listIndex : rec->LookupListIndices) - listIndex = GetUInt16(sp); +DataVector<uint16_t> CFX_CTTGSUBTable::ParseFeatureLookupListIndices( + const uint8_t* raw) { + // Skip over "FeatureParams" field. + const uint8_t* sp = raw + 2; + DataVector<uint16_t> result(GetUInt16(sp)); + for (auto& index : result) { + index = GetUInt16(sp); + } + return result; } -void CFX_CTTGSUBTable::ParseLookupList(FT_Bytes raw) { - FT_Bytes sp = raw; - LookupList = std::vector<TLookup>(GetUInt16(sp)); - for (auto& lookup : LookupList) - ParseLookup(&raw[GetUInt16(sp)], &lookup); +void CFX_CTTGSUBTable::ParseLookupList(pdfium::span<const uint8_t> raw) { + const uint8_t* sp = raw.data(); + lookup_list_ = std::vector<Lookup>(GetUInt16(sp)); + for (auto& lookup : lookup_list_) { + lookup = ParseLookup(&raw[GetUInt16(sp)]); + } } -void CFX_CTTGSUBTable::ParseLookup(FT_Bytes raw, TLookup* rec) { - FT_Bytes sp = raw; - rec->LookupType = GetUInt16(sp); - rec->LookupFlag = GetUInt16(sp); - rec->SubTables = std::vector<std::unique_ptr<TSubTableBase>>(GetUInt16(sp)); - if (rec->LookupType != 1) - return; +CFX_CTTGSUBTable::Lookup CFX_CTTGSUBTable::ParseLookup(const uint8_t* raw) { + const uint8_t* sp = raw; + CFX_CTTGSUBTable::Lookup result; + result.lookup_type = GetUInt16(sp); + // Skip over "LookupFlag" field. + sp += 2; + result.sub_tables = Lookup::SubTables(GetUInt16(sp)); + if (result.lookup_type != 1) { + return result; + } - for (auto& subTable : rec->SubTables) - subTable = ParseSingleSubst(&raw[GetUInt16(sp)]); + for (auto& sub_table : result.sub_tables) { + sub_table = ParseSingleSubst(&raw[GetUInt16(sp)]); + } + return result; } -std::unique_ptr<CFX_CTTGSUBTable::TCoverageFormatBase> -CFX_CTTGSUBTable::ParseCoverage(FT_Bytes raw) { - FT_Bytes sp = raw; +CFX_CTTGSUBTable::CoverageFormat CFX_CTTGSUBTable::ParseCoverage( + const uint8_t* raw) { + const uint8_t* sp = raw; uint16_t format = GetUInt16(sp); - if (format == 1) - return ParseCoverageFormat1(raw); - if (format == 2) - return ParseCoverageFormat2(raw); - return nullptr; -} + if (format != 1 && format != 2) { + return absl::monostate(); + } -std::unique_ptr<CFX_CTTGSUBTable::TCoverageFormat1> -CFX_CTTGSUBTable::ParseCoverageFormat1(FT_Bytes raw) { - FT_Bytes sp = raw; - (void)GetUInt16(sp); - auto rec = std::make_unique<TCoverageFormat1>(GetUInt16(sp)); - for (auto& glyph : rec->GlyphArray) - glyph = GetUInt16(sp); - return rec; -} + if (format == 1) { + DataVector<uint16_t> glyph_array(GetUInt16(sp)); + for (auto& glyph : glyph_array) { + glyph = GetUInt16(sp); + } + return glyph_array; + } -std::unique_ptr<CFX_CTTGSUBTable::TCoverageFormat2> -CFX_CTTGSUBTable::ParseCoverageFormat2(FT_Bytes raw) { - FT_Bytes sp = raw; - (void)GetUInt16(sp); - auto rec = std::make_unique<TCoverageFormat2>(GetUInt16(sp)); - for (auto& rangeRec : rec->RangeRecords) { - rangeRec.Start = GetUInt16(sp); - rangeRec.End = GetUInt16(sp); - rangeRec.StartCoverageIndex = GetUInt16(sp); + std::vector<RangeRecord> range_records(GetUInt16(sp)); + for (auto& range_rec : range_records) { + range_rec.start = GetUInt16(sp); + range_rec.end = GetUInt16(sp); + range_rec.start_coverage_index = GetUInt16(sp); } - return rec; + return range_records; } -std::unique_ptr<CFX_CTTGSUBTable::TSubTableBase> -CFX_CTTGSUBTable::ParseSingleSubst(FT_Bytes raw) { - FT_Bytes sp = raw; +CFX_CTTGSUBTable::SubTable CFX_CTTGSUBTable::ParseSingleSubst( + const uint8_t* raw) { + const uint8_t* sp = raw; uint16_t format = GetUInt16(sp); - if (format == 1) - return ParseSingleSubstFormat1(raw); - if (format == 2) - return ParseSingleSubstFormat2(raw); - return nullptr; -} - -std::unique_ptr<CFX_CTTGSUBTable::TSubTable1> -CFX_CTTGSUBTable::ParseSingleSubstFormat1(FT_Bytes raw) { - FT_Bytes sp = raw; - GetUInt16(sp); - uint16_t offset = GetUInt16(sp); - auto rec = std::make_unique<TSubTable1>(); - rec->Coverage = ParseCoverage(&raw[offset]); - rec->DeltaGlyphID = GetInt16(sp); - return rec; -} + SubTable rec; + if (format != 1 && format != 2) { + return rec; + } -std::unique_ptr<CFX_CTTGSUBTable::TSubTable2> -CFX_CTTGSUBTable::ParseSingleSubstFormat2(FT_Bytes raw) { - FT_Bytes sp = raw; - (void)GetUInt16(sp); uint16_t offset = GetUInt16(sp); - auto rec = std::make_unique<TSubTable2>(); - rec->Coverage = ParseCoverage(&raw[offset]); - rec->Substitutes = DataVector<uint16_t>(GetUInt16(sp)); - for (auto& substitute : rec->Substitutes) - substitute = GetUInt16(sp); + rec.coverage = ParseCoverage(&raw[offset]); + if (format == 1) { + rec.table_data = GetInt16(sp); + } else { + DataVector<uint16_t> table_data(GetUInt16(sp)); + for (auto& substitute : table_data) { + substitute = GetUInt16(sp); + } + rec.table_data = std::move(table_data); + } return rec; } -CFX_CTTGSUBTable::TLangSysRecord::TLangSysRecord() = default; - -CFX_CTTGSUBTable::TLangSysRecord::~TLangSysRecord() = default; - -CFX_CTTGSUBTable::TScriptRecord::TScriptRecord() = default; - -CFX_CTTGSUBTable::TScriptRecord::~TScriptRecord() = default; - -CFX_CTTGSUBTable::TFeatureRecord::TFeatureRecord() = default; - -CFX_CTTGSUBTable::TFeatureRecord::~TFeatureRecord() = default; - -CFX_CTTGSUBTable::TRangeRecord::TRangeRecord() = default; - -CFX_CTTGSUBTable::TCoverageFormat1::TCoverageFormat1(size_t initial_size) - : TCoverageFormatBase(1), GlyphArray(initial_size) {} - -CFX_CTTGSUBTable::TCoverageFormat1::~TCoverageFormat1() = default; - -CFX_CTTGSUBTable::TCoverageFormat2::TCoverageFormat2(size_t initial_size) - : TCoverageFormatBase(2), RangeRecords(initial_size) {} +CFX_CTTGSUBTable::FeatureRecord::FeatureRecord() = default; -CFX_CTTGSUBTable::TCoverageFormat2::~TCoverageFormat2() = default; +CFX_CTTGSUBTable::FeatureRecord::~FeatureRecord() = default; -CFX_CTTGSUBTable::TDevice::TDevice() = default; +CFX_CTTGSUBTable::RangeRecord::RangeRecord() = default; -CFX_CTTGSUBTable::TSubTableBase::TSubTableBase(uint16_t format) - : SubstFormat(format) {} +CFX_CTTGSUBTable::SubTable::SubTable() = default; -CFX_CTTGSUBTable::TSubTableBase::~TSubTableBase() = default; +CFX_CTTGSUBTable::SubTable::SubTable(SubTable&& that) noexcept = default; -CFX_CTTGSUBTable::TSubTable1::TSubTable1() : TSubTableBase(1) {} +CFX_CTTGSUBTable::SubTable& CFX_CTTGSUBTable::SubTable::operator=( + SubTable&& that) noexcept = default; -CFX_CTTGSUBTable::TSubTable1::~TSubTable1() = default; +CFX_CTTGSUBTable::SubTable::~SubTable() = default; -CFX_CTTGSUBTable::TSubTable2::TSubTable2() : TSubTableBase(2) {} +CFX_CTTGSUBTable::Lookup::Lookup() = default; -CFX_CTTGSUBTable::TSubTable2::~TSubTable2() = default; +CFX_CTTGSUBTable::Lookup::Lookup(Lookup&& that) noexcept = default; -CFX_CTTGSUBTable::TLookup::TLookup() = default; +CFX_CTTGSUBTable::Lookup& CFX_CTTGSUBTable::Lookup::operator=( + Lookup&& that) noexcept = default; -CFX_CTTGSUBTable::TLookup::~TLookup() = default; +CFX_CTTGSUBTable::Lookup::~Lookup() = default; |