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 <utility> 15 #include <vector> 16 17 #include "core/fxcrt/cfx_seekablestreamproxy.h" 18 #include "core/fxcrt/fx_extension.h" 19 #include "core/fxcrt/observable.h" 20 #include "core/fxcrt/retain_ptr.h" 21 #include "core/fxge/fx_freetype.h" 22 #include "core/fxge/ifx_systemfontinfo.h" 23 #include "xfa/fgas/font/cfgas_pdffontmgr.h" 24 25 class CFGAS_GEFont; 26 class CFX_FontMapper; 27 class CFX_FontSourceEnum_File; 28 29 #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ 30 struct FX_FONTMATCHPARAMS { 31 const wchar_t* pwsFamily; 32 uint32_t dwFontStyles; 33 uint32_t dwUSB; 34 bool matchParagraphStyle; 35 wchar_t wUnicode; 36 uint16_t wCodePage; 37 }; 38 39 struct FX_FONTSIGNATURE { 40 uint32_t fsUsb[4]; 41 uint32_t fsCsb[2]; 42 }; 43 44 inline bool operator==(const FX_FONTSIGNATURE& left, 45 const FX_FONTSIGNATURE& right) { 46 return left.fsUsb[0] == right.fsUsb[0] && left.fsUsb[1] == right.fsUsb[1] && 47 left.fsUsb[2] == right.fsUsb[2] && left.fsUsb[3] == right.fsUsb[3] && 48 left.fsCsb[0] == right.fsCsb[0] && left.fsCsb[1] == right.fsCsb[1]; 49 } 50 51 struct FX_FONTDESCRIPTOR { 52 wchar_t wsFontFace[32]; 53 uint32_t dwFontStyles; 54 uint8_t uCharSet; 55 FX_FONTSIGNATURE FontSignature; 56 }; 57 58 inline bool operator==(const FX_FONTDESCRIPTOR& left, 59 const FX_FONTDESCRIPTOR& right) { 60 return left.uCharSet == right.uCharSet && 61 left.dwFontStyles == right.dwFontStyles && 62 left.FontSignature == right.FontSignature && 63 wcscmp(left.wsFontFace, right.wsFontFace) == 0; 64 } 65 66 typedef void (*FX_LPEnumAllFonts)(std::deque<FX_FONTDESCRIPTOR>* fonts, 67 const wchar_t* pwsFaceName, 68 wchar_t wUnicode); 69 70 #else // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ 71 72 class CFX_FontDescriptor { 73 public: 74 CFX_FontDescriptor(); 75 ~CFX_FontDescriptor(); 76 77 int32_t m_nFaceIndex; 78 WideString m_wsFaceName; 79 std::vector<WideString> m_wsFamilyNames; 80 uint32_t m_dwFontStyles; 81 uint32_t m_dwUsb[4]; 82 uint32_t m_dwCsb[2]; 83 }; 84 85 class CFX_FontDescriptorInfo { 86 public: 87 CFX_FontDescriptor* pFont; 88 int32_t nPenalty; 89 90 bool operator>(const CFX_FontDescriptorInfo& other) const { 91 return nPenalty > other.nPenalty; 92 } 93 bool operator<(const CFX_FontDescriptorInfo& other) const { 94 return nPenalty < other.nPenalty; 95 } 96 bool operator==(const CFX_FontDescriptorInfo& other) const { 97 return nPenalty == other.nPenalty; 98 } 99 }; 100 101 class CFX_FontSourceEnum_File { 102 public: 103 CFX_FontSourceEnum_File(); 104 ~CFX_FontSourceEnum_File(); 105 106 bool HasStartPosition(); 107 std::pair<bool, RetainPtr<IFX_SeekableStream>> GetNext(); 108 109 private: 110 struct HandleParentPath { 111 HandleParentPath() = default; 112 HandleParentPath(const HandleParentPath& x) { 113 pFileHandle = x.pFileHandle; 114 bsParentPath = x.bsParentPath; 115 } 116 FX_FileHandle* pFileHandle; 117 ByteString bsParentPath; 118 }; 119 120 ByteString GetNextFile(); 121 122 WideString m_wsNext; 123 std::vector<HandleParentPath> m_FolderQueue; 124 std::vector<ByteString> m_FolderPaths; 125 }; 126 127 #endif // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ 128 129 class CFGAS_FontMgr : public Observable<CFGAS_FontMgr> { 130 public: 131 CFGAS_FontMgr(); 132 ~CFGAS_FontMgr(); 133 134 RetainPtr<CFGAS_GEFont> GetFontByCodePage(uint16_t wCodePage, 135 uint32_t dwFontStyles, 136 const wchar_t* pszFontFamily); 137 RetainPtr<CFGAS_GEFont> GetFontByUnicode(wchar_t wUnicode, 138 uint32_t dwFontStyles, 139 const wchar_t* pszFontFamily); 140 RetainPtr<CFGAS_GEFont> LoadFont(const wchar_t* pszFontFamily, 141 uint32_t dwFontStyles, 142 uint16_t wCodePage); 143 void RemoveFont(const RetainPtr<CFGAS_GEFont>& pFont); 144 145 bool EnumFonts(); 146 147 private: 148 #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ 149 const FX_FONTDESCRIPTOR* FindFont(const wchar_t* pszFontFamily, 150 uint32_t dwFontStyles, 151 bool matchParagraphStyle, 152 uint16_t wCodePage, 153 uint32_t dwUSB, 154 wchar_t wUnicode); 155 156 FX_LPEnumAllFonts m_pEnumerator; 157 std::deque<FX_FONTDESCRIPTOR> m_FontFaces; 158 #else // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ 159 bool EnumFontsFromFontMapper(); 160 bool EnumFontsFromFiles(); 161 void RegisterFace(FXFT_Face pFace, const WideString* pFaceName); 162 void RegisterFaces(const RetainPtr<IFX_SeekableReadStream>& pFontStream, 163 const WideString* pFaceName); 164 void GetNames(const uint8_t* name_table, std::vector<WideString>& Names); 165 std::vector<uint16_t> GetCharsets(FXFT_Face pFace) const; 166 void GetUSBCSB(FXFT_Face pFace, uint32_t* USB, uint32_t* CSB); 167 uint32_t GetFlags(FXFT_Face pFace); 168 bool VerifyUnicode(CFX_FontDescriptor* pDesc, wchar_t wcUnicode); 169 int32_t IsPartName(const WideString& Name1, const WideString& Name2); 170 void MatchFonts(std::vector<CFX_FontDescriptorInfo>* MatchedFonts, 171 uint16_t wCodePage, 172 uint32_t dwFontStyles, 173 const WideString& FontName, 174 wchar_t wcUnicode = 0xFFFE); 175 int32_t CalcPenalty(CFX_FontDescriptor* pInstalled, 176 uint16_t wCodePage, 177 uint32_t dwFontStyles, 178 const WideString& FontName, 179 wchar_t wcUnicode = 0xFFFE); 180 RetainPtr<CFGAS_GEFont> LoadFont(const WideString& wsFaceName, 181 int32_t iFaceIndex, 182 int32_t* pFaceCount); 183 FXFT_Face LoadFace(const RetainPtr<IFX_SeekableReadStream>& pFontStream, 184 int32_t iFaceIndex); 185 RetainPtr<IFX_SeekableReadStream> CreateFontStream( 186 CFX_FontMapper* pFontMapper, 187 IFX_SystemFontInfo* pSystemFontInfo, 188 uint32_t index); 189 RetainPtr<IFX_SeekableReadStream> CreateFontStream( 190 const ByteString& bsFaceName); 191 192 std::unique_ptr<CFX_FontSourceEnum_File> m_pFontSource; 193 std::vector<std::unique_ptr<CFX_FontDescriptor>> m_InstalledFonts; 194 std::map<uint32_t, std::unique_ptr<std::vector<CFX_FontDescriptorInfo>>> 195 m_Hash2CandidateList; 196 std::map<RetainPtr<CFGAS_GEFont>, RetainPtr<IFX_SeekableReadStream>> 197 m_IFXFont2FileRead; 198 std::set<wchar_t> m_FailedUnicodesSet; 199 #endif // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ 200 201 bool VerifyUnicode(const RetainPtr<CFGAS_GEFont>& pFont, wchar_t wcUnicode); 202 203 std::map<uint32_t, std::vector<RetainPtr<CFGAS_GEFont>>> m_Hash2Fonts; 204 }; 205 206 #endif // XFA_FGAS_FONT_CFGAS_FONTMGR_H_ 207