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