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 XFA_FGAS_FONT_CFGAS_FONTMGR_H_ 8 #define XFA_FGAS_FONT_CFGAS_FONTMGR_H_ 9 10 #include <deque> 11 #include <map> 12 #include <memory> 13 #include <set> 14 #include <vector> 15 16 #include "core/fxcrt/cfx_retain_ptr.h" 17 #include "core/fxcrt/fx_ext.h" 18 #include "core/fxge/cfx_fontmapper.h" 19 #include "core/fxge/fx_freetype.h" 20 #include "core/fxge/ifx_systemfontinfo.h" 21 #include "third_party/freetype/include/freetype/fttypes.h" 22 #include "xfa/fgas/crt/fgas_stream.h" 23 24 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 25 #include "xfa/fgas/crt/fgas_utils.h" 26 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 27 28 #define FX_FONTSTYLE_Normal 0x00 29 #define FX_FONTSTYLE_FixedPitch 0x01 30 #define FX_FONTSTYLE_Serif 0x02 31 #define FX_FONTSTYLE_Symbolic 0x04 32 #define FX_FONTSTYLE_Script 0x08 33 #define FX_FONTSTYLE_Italic 0x40 34 #define FX_FONTSTYLE_Bold 0x40000 35 #define FX_FONTSTYLE_BoldItalic (FX_FONTSTYLE_Bold | FX_FONTSTYLE_Italic) 36 #define FX_FONTSTYLE_ExactMatch 0x80000000 37 38 class CFX_FontSourceEnum_File; 39 class CXFA_PDFFontMgr; 40 class CFGAS_FontMgr; 41 class CFGAS_GEFont; 42 43 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 44 #define FX_FONTMATCHPARA_MatchStyle 0x01 45 46 struct FX_FONTMATCHPARAMS { 47 const FX_WCHAR* pwsFamily; 48 uint32_t dwFontStyles; 49 uint32_t dwUSB; 50 uint32_t dwMatchFlags; 51 FX_WCHAR wUnicode; 52 uint16_t wCodePage; 53 }; 54 55 struct FX_FONTSIGNATURE { 56 uint32_t fsUsb[4]; 57 uint32_t fsCsb[2]; 58 }; 59 60 inline bool operator==(const FX_FONTSIGNATURE& left, 61 const FX_FONTSIGNATURE& right) { 62 return left.fsUsb[0] == right.fsUsb[0] && left.fsUsb[1] == right.fsUsb[1] && 63 left.fsUsb[2] == right.fsUsb[2] && left.fsUsb[3] == right.fsUsb[3] && 64 left.fsCsb[0] == right.fsCsb[0] && left.fsCsb[1] == right.fsCsb[1]; 65 } 66 67 struct FX_FONTDESCRIPTOR { 68 FX_WCHAR wsFontFace[32]; 69 uint32_t dwFontStyles; 70 uint8_t uCharSet; 71 FX_FONTSIGNATURE FontSignature; 72 }; 73 74 inline bool operator==(const FX_FONTDESCRIPTOR& left, 75 const FX_FONTDESCRIPTOR& right) { 76 return left.uCharSet == right.uCharSet && 77 left.dwFontStyles == right.dwFontStyles && 78 left.FontSignature == right.FontSignature && 79 FXSYS_wcscmp(left.wsFontFace, right.wsFontFace) == 0; 80 } 81 82 typedef void (*FX_LPEnumAllFonts)(std::deque<FX_FONTDESCRIPTOR>* fonts, 83 const FX_WCHAR* pwsFaceName, 84 FX_WCHAR wUnicode); 85 86 FX_LPEnumAllFonts FX_GetDefFontEnumerator(); 87 88 class CFGAS_FontMgr { 89 public: 90 static std::unique_ptr<CFGAS_FontMgr> Create(FX_LPEnumAllFonts pEnumerator); 91 92 explicit CFGAS_FontMgr(FX_LPEnumAllFonts pEnumerator); 93 ~CFGAS_FontMgr(); 94 95 CFX_RetainPtr<CFGAS_GEFont> GetFontByCodePage(uint16_t wCodePage, 96 uint32_t dwFontStyles, 97 const FX_WCHAR* pszFontFamily); 98 CFX_RetainPtr<CFGAS_GEFont> GetFontByUnicode(FX_WCHAR wUnicode, 99 uint32_t dwFontStyles, 100 const FX_WCHAR* pszFontFamily); 101 CFX_RetainPtr<CFGAS_GEFont> LoadFont(const FX_WCHAR* pszFontFamily, 102 uint32_t dwFontStyles, 103 uint16_t wCodePage); 104 void RemoveFont(const CFX_RetainPtr<CFGAS_GEFont>& pFont); 105 106 private: 107 CFX_RetainPtr<CFGAS_GEFont> LoadFont( 108 const CFX_RetainPtr<CFGAS_GEFont>& pSrcFont, 109 uint32_t dwFontStyles, 110 uint16_t wCodePage); 111 void RemoveFont(std::map<uint32_t, CFX_RetainPtr<CFGAS_GEFont>>* pFontMap, 112 const CFX_RetainPtr<CFGAS_GEFont>& pFont); 113 const FX_FONTDESCRIPTOR* FindFont(const FX_WCHAR* pszFontFamily, 114 uint32_t dwFontStyles, 115 uint32_t dwMatchFlags, 116 uint16_t wCodePage, 117 uint32_t dwUSB = 999, 118 FX_WCHAR wUnicode = 0); 119 120 FX_LPEnumAllFonts m_pEnumerator; 121 std::deque<FX_FONTDESCRIPTOR> m_FontFaces; 122 std::vector<CFX_RetainPtr<CFGAS_GEFont>> m_Fonts; 123 std::map<uint32_t, CFX_RetainPtr<CFGAS_GEFont>> m_CPFonts; 124 std::map<uint32_t, CFX_RetainPtr<CFGAS_GEFont>> m_FamilyFonts; 125 std::map<uint32_t, CFX_RetainPtr<CFGAS_GEFont>> m_UnicodeFonts; 126 std::map<uint32_t, CFX_RetainPtr<CFGAS_GEFont>> m_BufferFonts; 127 std::map<uint32_t, CFX_RetainPtr<CFGAS_GEFont>> m_StreamFonts; 128 std::map<uint32_t, CFX_RetainPtr<CFGAS_GEFont>> m_DeriveFonts; 129 }; 130 131 #else // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 132 class CFX_FontDescriptor { 133 public: 134 CFX_FontDescriptor(); 135 ~CFX_FontDescriptor(); 136 137 int32_t m_nFaceIndex; 138 CFX_WideString m_wsFaceName; 139 std::vector<CFX_WideString> m_wsFamilyNames; 140 uint32_t m_dwFontStyles; 141 uint32_t m_dwUsb[4]; 142 uint32_t m_dwCsb[2]; 143 }; 144 145 class CFX_FontDescriptorInfo { 146 public: 147 CFX_FontDescriptor* pFont; 148 int32_t nPenalty; 149 150 bool operator>(const CFX_FontDescriptorInfo& other) const { 151 return nPenalty > other.nPenalty; 152 } 153 bool operator<(const CFX_FontDescriptorInfo& other) const { 154 return nPenalty < other.nPenalty; 155 } 156 bool operator==(const CFX_FontDescriptorInfo& other) const { 157 return nPenalty == other.nPenalty; 158 } 159 }; 160 161 struct FX_HandleParentPath { FX_HandleParentPathFX_HandleParentPath162 FX_HandleParentPath() {} FX_HandleParentPathFX_HandleParentPath163 FX_HandleParentPath(const FX_HandleParentPath& x) { 164 pFileHandle = x.pFileHandle; 165 bsParentPath = x.bsParentPath; 166 } 167 FX_FileHandle* pFileHandle; 168 CFX_ByteString bsParentPath; 169 }; 170 171 class CFX_FontSourceEnum_File { 172 public: 173 CFX_FontSourceEnum_File(); 174 ~CFX_FontSourceEnum_File(); 175 176 FX_POSITION GetStartPosition(); 177 CFX_RetainPtr<IFX_FileAccess> GetNext(FX_POSITION& pos); 178 179 private: 180 CFX_ByteString GetNextFile(); 181 182 CFX_WideString m_wsNext; 183 std::vector<FX_HandleParentPath> m_FolderQueue; 184 std::vector<CFX_ByteString> m_FolderPaths; 185 }; 186 187 class CFGAS_FontMgr { 188 public: 189 static std::unique_ptr<CFGAS_FontMgr> Create( 190 CFX_FontSourceEnum_File* pFontEnum); 191 192 explicit CFGAS_FontMgr(CFX_FontSourceEnum_File* pFontEnum); 193 ~CFGAS_FontMgr(); 194 195 CFX_RetainPtr<CFGAS_GEFont> GetFontByCodePage(uint16_t wCodePage, 196 uint32_t dwFontStyles, 197 const FX_WCHAR* pszFontFamily); 198 CFX_RetainPtr<CFGAS_GEFont> GetFontByUnicode(FX_WCHAR wUnicode, 199 uint32_t dwFontStyles, 200 const FX_WCHAR* pszFontFamily); 201 CFX_RetainPtr<CFGAS_GEFont> LoadFont(const FX_WCHAR* pszFontFamily, 202 uint32_t dwFontStyles, 203 uint16_t wCodePage); 204 void RemoveFont(const CFX_RetainPtr<CFGAS_GEFont>& pFont); 205 206 private: 207 bool EnumFonts(); 208 bool EnumFontsFromFontMapper(); 209 bool EnumFontsFromFiles(); 210 void RegisterFace(FXFT_Face pFace, const CFX_WideString* pFaceName); 211 void RegisterFaces(const CFX_RetainPtr<IFX_SeekableReadStream>& pFontStream, 212 const CFX_WideString* pFaceName); 213 void GetNames(const uint8_t* name_table, std::vector<CFX_WideString>& Names); 214 std::vector<uint16_t> GetCharsets(FXFT_Face pFace) const; 215 void GetUSBCSB(FXFT_Face pFace, uint32_t* USB, uint32_t* CSB); 216 uint32_t GetFlags(FXFT_Face pFace); 217 bool VerifyUnicode(CFX_FontDescriptor* pDesc, FX_WCHAR wcUnicode); 218 bool VerifyUnicode(const CFX_RetainPtr<CFGAS_GEFont>& pFont, 219 FX_WCHAR wcUnicode); 220 int32_t IsPartName(const CFX_WideString& Name1, const CFX_WideString& Name2); 221 void MatchFonts(std::vector<CFX_FontDescriptorInfo>* MatchedFonts, 222 uint16_t wCodePage, 223 uint32_t dwFontStyles, 224 const CFX_WideString& FontName, 225 FX_WCHAR wcUnicode = 0xFFFE); 226 int32_t CalcPenalty(CFX_FontDescriptor* pInstalled, 227 uint16_t wCodePage, 228 uint32_t dwFontStyles, 229 const CFX_WideString& FontName, 230 FX_WCHAR wcUnicode = 0xFFFE); 231 CFX_RetainPtr<CFGAS_GEFont> LoadFont(const CFX_WideString& wsFaceName, 232 int32_t iFaceIndex, 233 int32_t* pFaceCount); 234 FXFT_Face LoadFace(const CFX_RetainPtr<IFX_SeekableReadStream>& pFontStream, 235 int32_t iFaceIndex); 236 CFX_RetainPtr<IFX_SeekableReadStream> CreateFontStream( 237 CFX_FontMapper* pFontMapper, 238 IFX_SystemFontInfo* pSystemFontInfo, 239 uint32_t index); 240 CFX_RetainPtr<IFX_SeekableReadStream> CreateFontStream( 241 const CFX_ByteString& bsFaceName); 242 243 CFX_FontSourceEnum_File* const m_pFontSource; 244 std::vector<std::unique_ptr<CFX_FontDescriptor>> m_InstalledFonts; 245 std::map<uint32_t, std::unique_ptr<std::vector<CFX_FontDescriptorInfo>>> 246 m_Hash2CandidateList; 247 std::map<uint32_t, std::vector<CFX_RetainPtr<CFGAS_GEFont>>> m_Hash2Fonts; 248 std::map<CFX_RetainPtr<CFGAS_GEFont>, CFX_RetainPtr<IFX_SeekableReadStream>> 249 m_IFXFont2FileRead; 250 std::set<FX_WCHAR> m_FailedUnicodesSet; 251 }; 252 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 253 254 #endif // XFA_FGAS_FONT_CFGAS_FONTMGR_H_ 255