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_FONT_INT_H_ 8 #define CORE_FPDFAPI_FONT_FONT_INT_H_ 9 10 #include <map> 11 #include <memory> 12 #include <vector> 13 14 #include "core/fpdfapi/font/cpdf_cidfont.h" 15 #include "core/fxcrt/cfx_maybe_owned.h" 16 #include "core/fxcrt/fx_basic.h" 17 18 class CPDF_CID2UnicodeMap; 19 class CPDF_CMap; 20 class CPDF_Font; 21 class CPDF_Stream; 22 23 using FXFT_Library = void*; 24 25 int TT2PDF(int m, FXFT_Face face); 26 bool FT_UseTTCharmap(FXFT_Face face, int platform_id, int encoding_id); 27 CIDSet CharsetFromOrdering(const CFX_ByteStringC& ordering); 28 29 class CPDF_CMapManager { 30 public: 31 CPDF_CMapManager(); 32 ~CPDF_CMapManager(); 33 34 CFX_MaybeOwned<CPDF_CMap> GetPredefinedCMap(const CFX_ByteString& name, 35 bool bPromptCJK); 36 CPDF_CID2UnicodeMap* GetCID2UnicodeMap(CIDSet charset, bool bPromptCJK); 37 38 private: 39 std::unique_ptr<CPDF_CMap> LoadPredefinedCMap(const CFX_ByteString& name, 40 bool bPromptCJK); 41 std::unique_ptr<CPDF_CID2UnicodeMap> LoadCID2UnicodeMap(CIDSet charset, 42 bool bPromptCJK); 43 44 std::map<CFX_ByteString, std::unique_ptr<CPDF_CMap>> m_CMaps; 45 std::unique_ptr<CPDF_CID2UnicodeMap> m_CID2UnicodeMaps[6]; 46 }; 47 48 class CFX_StockFontArray { 49 public: 50 CFX_StockFontArray(); 51 ~CFX_StockFontArray(); 52 53 // Takes ownership of |pFont|, returns unowned pointer to it. 54 CPDF_Font* SetFont(uint32_t index, std::unique_ptr<CPDF_Font> pFont); 55 CPDF_Font* GetFont(uint32_t index) const; 56 57 private: 58 std::unique_ptr<CPDF_Font> m_StockFonts[14]; 59 }; 60 61 class CPDF_FontGlobals { 62 public: 63 CPDF_FontGlobals(); 64 ~CPDF_FontGlobals(); 65 66 void Clear(CPDF_Document* pDoc); 67 CPDF_Font* Find(CPDF_Document* pDoc, uint32_t index); 68 69 // Takes ownership of |pFont|, returns unowned pointer to it. 70 CPDF_Font* Set(CPDF_Document* key, 71 uint32_t index, 72 std::unique_ptr<CPDF_Font> pFont); 73 74 CPDF_CMapManager m_CMapManager; 75 struct { 76 const struct FXCMAP_CMap* m_pMapList; 77 uint32_t m_Count; 78 } m_EmbeddedCharsets[CIDSET_NUM_SETS]; 79 struct { 80 const uint16_t* m_pMap; 81 uint32_t m_Count; 82 } m_EmbeddedToUnicodes[CIDSET_NUM_SETS]; 83 84 private: 85 std::map<CPDF_Document*, std::unique_ptr<CFX_StockFontArray>> m_StockMap; 86 }; 87 88 struct CMap_CodeRange { 89 int m_CharSize; 90 uint8_t m_Lower[4]; 91 uint8_t m_Upper[4]; 92 }; 93 94 class CPDF_CMapParser { 95 public: 96 CPDF_CMapParser(); 97 ~CPDF_CMapParser(); 98 void Initialize(CPDF_CMap* pMap); 99 void ParseWord(const CFX_ByteStringC& str); 100 CFX_BinaryBuf m_AddMaps; 101 102 private: 103 friend class fpdf_font_cid_CMap_GetCode_Test; 104 friend class fpdf_font_cid_CMap_GetCodeRange_Test; 105 106 static uint32_t CMap_GetCode(const CFX_ByteStringC& word); 107 static bool CMap_GetCodeRange(CMap_CodeRange& range, 108 const CFX_ByteStringC& first, 109 const CFX_ByteStringC& second); 110 111 CPDF_CMap* m_pCMap; 112 int m_Status; 113 int m_CodeSeq; 114 uint32_t m_CodePoints[4]; 115 std::vector<CMap_CodeRange> m_CodeRanges; 116 CFX_ByteString m_LastWord; 117 }; 118 119 enum CIDCoding : uint8_t { 120 CIDCODING_UNKNOWN = 0, 121 CIDCODING_GB, 122 CIDCODING_BIG5, 123 CIDCODING_JIS, 124 CIDCODING_KOREA, 125 CIDCODING_UCS2, 126 CIDCODING_CID, 127 CIDCODING_UTF16, 128 }; 129 130 class CPDF_CMap { 131 public: 132 enum CodingScheme : uint8_t { 133 OneByte, 134 TwoBytes, 135 MixedTwoBytes, 136 MixedFourBytes 137 }; 138 139 CPDF_CMap(); 140 ~CPDF_CMap(); 141 142 void LoadPredefined(CPDF_CMapManager* pMgr, 143 const CFX_ByteString& name, 144 bool bPromptCJK); 145 void LoadEmbedded(const uint8_t* pData, uint32_t dwSize); 146 147 bool IsLoaded() const; 148 bool IsVertWriting() const; 149 uint16_t CIDFromCharCode(uint32_t charcode) const; 150 int GetCharSize(uint32_t charcode) const; 151 uint32_t GetNextChar(const FX_CHAR* pString, int nStrLen, int& offset) const; 152 int CountChar(const FX_CHAR* pString, int size) const; 153 int AppendChar(FX_CHAR* str, uint32_t charcode) const; 154 155 private: 156 friend class CPDF_CMapParser; 157 friend class CPDF_CIDFont; 158 159 CFX_ByteString m_PredefinedCMap; 160 bool m_bVertical; 161 CIDSet m_Charset; 162 int m_Coding; 163 CodingScheme m_CodingScheme; 164 int m_nCodeRanges; 165 uint8_t* m_pLeadingBytes; 166 uint16_t* m_pMapping; 167 uint8_t* m_pAddMapping; 168 bool m_bLoaded; 169 const FXCMAP_CMap* m_pEmbedMap; 170 }; 171 172 class CPDF_CID2UnicodeMap { 173 public: 174 CPDF_CID2UnicodeMap(); 175 ~CPDF_CID2UnicodeMap(); 176 177 bool IsLoaded(); 178 void Load(CPDF_CMapManager* pMgr, CIDSet charset, bool bPromptCJK); 179 FX_WCHAR UnicodeFromCID(uint16_t CID); 180 181 private: 182 CIDSet m_Charset; 183 const uint16_t* m_pEmbeddedMap; 184 uint32_t m_EmbeddedCount; 185 }; 186 187 class CPDF_ToUnicodeMap { 188 public: 189 CPDF_ToUnicodeMap(); 190 ~CPDF_ToUnicodeMap(); 191 192 void Load(CPDF_Stream* pStream); 193 194 CFX_WideString Lookup(uint32_t charcode) const; 195 uint32_t ReverseLookup(FX_WCHAR unicode) const; 196 197 private: 198 friend class fpdf_font_StringToCode_Test; 199 friend class fpdf_font_StringToWideString_Test; 200 201 static uint32_t StringToCode(const CFX_ByteStringC& str); 202 static CFX_WideString StringToWideString(const CFX_ByteStringC& str); 203 204 uint32_t GetUnicode(); 205 206 std::map<uint32_t, uint32_t> m_Map; 207 CPDF_CID2UnicodeMap* m_pBaseMap; 208 CFX_WideTextBuf m_MultiCharBuf; 209 }; 210 211 #endif // CORE_FPDFAPI_FONT_FONT_INT_H_ 212