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 _FX_FONTMGR_IMP
8 #define _FX_FONTMGR_IMP
9 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
10 int32_t FX_GetSimilarValue(FX_LPCFONTDESCRIPTOR pFont, FX_DWORD dwFontStyles);
11 FX_LPCFONTDESCRIPTOR FX_DefFontMatcher(FX_LPFONTMATCHPARAMS pParams,
12                                        const CFX_FontDescriptors& fonts,
13                                        void* pUserData);
14 class CFX_StdFontMgrImp : public IFX_FontMgr {
15  public:
16   CFX_StdFontMgrImp(FX_LPEnumAllFonts pEnumerator,
17                     FX_LPMatchFont pMatcher,
18                     void* pUserData);
19   ~CFX_StdFontMgrImp();
Release()20   virtual void Release() { delete this; }
21   virtual IFX_Font* GetDefFontByCodePage(FX_WORD wCodePage,
22                                          FX_DWORD dwFontStyles,
23                                          const FX_WCHAR* pszFontFamily = NULL);
24   virtual IFX_Font* GetDefFontByCharset(uint8_t nCharset,
25                                         FX_DWORD dwFontStyles,
26                                         const FX_WCHAR* pszFontFamily = NULL);
27   virtual IFX_Font* GetDefFontByUnicode(FX_WCHAR wUnicode,
28                                         FX_DWORD dwFontStyles,
29                                         const FX_WCHAR* pszFontFamily = NULL);
30   virtual IFX_Font* GetDefFontByLanguage(FX_WORD wLanguage,
31                                          FX_DWORD dwFontStyles,
32                                          const FX_WCHAR* pszFontFamily = NULL);
33   virtual IFX_Font* LoadFont(const FX_WCHAR* pszFontFamily,
34                              FX_DWORD dwFontStyles,
35                              FX_WORD wCodePage = 0xFFFF);
36   virtual IFX_Font* LoadFont(const uint8_t* pBuffer, int32_t iLength);
37   virtual IFX_Font* LoadFont(const FX_WCHAR* pszFileName);
38   virtual IFX_Font* LoadFont(IFX_Stream* pFontStream,
39                              const FX_WCHAR* pszFontAlias = NULL,
40                              FX_DWORD dwFontStyles = 0,
41                              FX_WORD wCodePage = 0,
42                              FX_BOOL bSaveStream = FALSE);
43   virtual IFX_Font* LoadFont(IFX_Font* pSrcFont,
44                              FX_DWORD dwFontStyles,
45                              FX_WORD wCodePage = 0xFFFF);
46   virtual void ClearFontCache();
47   virtual void RemoveFont(IFX_Font* pFont);
48 
49  protected:
50   FX_LPMatchFont m_pMatcher;
51   FX_LPEnumAllFonts m_pEnumerator;
52   CFX_FontDescriptors m_FontFaces;
53   CFX_PtrArray m_Fonts;
54   CFX_MapPtrToPtr m_CPFonts;
55   CFX_MapPtrToPtr m_FamilyFonts;
56   CFX_MapPtrToPtr m_UnicodeFonts;
57   CFX_MapPtrToPtr m_BufferFonts;
58   CFX_MapPtrToPtr m_FileFonts;
59   CFX_MapPtrToPtr m_StreamFonts;
60   CFX_MapPtrToPtr m_DeriveFonts;
61   void* m_pUserData;
62   void RemoveFont(CFX_MapPtrToPtr& fontMap, IFX_Font* pFont);
63   FX_LPCFONTDESCRIPTOR FindFont(const FX_WCHAR* pszFontFamily,
64                                 FX_DWORD dwFontStyles,
65                                 FX_DWORD dwMatchFlags,
66                                 FX_WORD wCodePage,
67                                 FX_DWORD dwUSB = 999,
68                                 FX_WCHAR wUnicode = 0);
69   IFX_Font* GetFont(FX_LPCFONTDESCRIPTOR pFD, FX_DWORD dwFontStyles);
70 };
71 FX_DWORD FX_GetGdiFontStyles(const LOGFONTW& lf);
72 #else
73 class CFX_FontDescriptor {
74  public:
CFX_FontDescriptor()75   CFX_FontDescriptor()
76       : m_pFileAccess(NULL), m_nFaceIndex(0), m_dwFontStyles(0) {
77     m_dwUsb[0] = m_dwUsb[1] = m_dwUsb[2] = m_dwUsb[3] = 0;
78     m_dwCsb[0] = m_dwCsb[1] = 0;
79   }
~CFX_FontDescriptor()80   ~CFX_FontDescriptor() {
81     if (NULL != m_pFileAccess) {
82       m_pFileAccess->Release();
83     }
84   }
85   IFX_FileAccess* m_pFileAccess;
86   int32_t m_nFaceIndex;
87   CFX_WideString m_wsFaceName;
88   CFX_WideStringArray m_wsFamilyNames;
89   FX_DWORD m_dwFontStyles;
90   FX_DWORD m_dwUsb[4];
91   FX_DWORD m_dwCsb[2];
92 };
93 typedef CFX_ArrayTemplate<CFX_FontDescriptor*> CFX_FontDescriptors;
94 struct FX_FontDescriptorInfo {
95  public:
96   CFX_FontDescriptor* pFont;
97   int32_t nPenalty;
98   FX_BOOL operator>(const FX_FontDescriptorInfo& x) {
99     return this->nPenalty > x.nPenalty;
100   };
101   FX_BOOL operator<(const FX_FontDescriptorInfo& x) {
102     return this->nPenalty < x.nPenalty;
103   };
104   FX_BOOL operator==(const FX_FontDescriptorInfo& x) {
105     return this->nPenalty == x.nPenalty;
106   };
107 };
108 typedef CFX_ArrayTemplate<FX_FontDescriptorInfo> CFX_FontDescriptorInfos;
109 struct FX_HandleParentPath {
FX_HandleParentPathFX_HandleParentPath110   FX_HandleParentPath() {}
FX_HandleParentPathFX_HandleParentPath111   FX_HandleParentPath(const FX_HandleParentPath& x) {
112     pFileHandle = x.pFileHandle;
113     bsParentPath = x.bsParentPath;
114   }
115   void* pFileHandle;
116   CFX_ByteString bsParentPath;
117 };
118 class CFX_FontSourceEnum_File : public IFX_FontSourceEnum {
119  public:
120   CFX_FontSourceEnum_File();
Release()121   virtual void Release() { delete this; };
122   virtual FX_POSITION GetStartPosition(void* pUserData = NULL);
123   virtual IFX_FileAccess* GetNext(FX_POSITION& pos, void* pUserData = NULL);
124 
125  private:
126   CFX_ByteString GetNextFile();
127   CFX_WideString m_wsNext;
128   CFX_ObjectArray<FX_HandleParentPath> m_FolderQueue;
129   CFX_ByteStringArray m_FolderPaths;
130 };
131 typedef CFX_MapPtrTemplate<FX_DWORD, IFX_FileAccess*> CFX_HashFileMap;
132 typedef CFX_MapPtrTemplate<FX_DWORD, IFX_Font*> CFX_HashFontMap;
133 typedef CFX_MapPtrTemplate<FX_DWORD, CFX_FontDescriptorInfos*>
134     CFX_HashFontDescsMap;
135 typedef CFX_MapPtrTemplate<FX_DWORD, CFX_ArrayTemplate<IFX_Font*>*>
136     CFX_HashFontsMap;
137 typedef CFX_MapPtrTemplate<FX_WCHAR, IFX_Font*> CFX_UnicodeFontMap;
138 typedef CFX_MapPtrTemplate<IFX_FileAccess*, CFX_ArrayTemplate<IFX_Font*>*>
139     CFX_FileFontMap;
140 typedef CFX_MapPtrTemplate<IFX_Font*, IFX_FileRead*> CFX_FonStreamtMap;
141 class CFX_FontMgrImp : public IFX_FontMgr {
142  public:
143   CFX_FontMgrImp(IFX_FontSourceEnum* pFontEnum,
144                  IFX_FontMgrDelegate* pDelegate = NULL,
145                  void* pUserData = NULL);
146   virtual void Release();
147   virtual IFX_Font* GetDefFontByCodePage(FX_WORD wCodePage,
148                                          FX_DWORD dwFontStyles,
149                                          const FX_WCHAR* pszFontFamily = NULL);
150   virtual IFX_Font* GetDefFontByCharset(uint8_t nCharset,
151                                         FX_DWORD dwFontStyles,
152                                         const FX_WCHAR* pszFontFamily = NULL);
153   virtual IFX_Font* GetDefFontByUnicode(FX_WCHAR wUnicode,
154                                         FX_DWORD dwFontStyles,
155                                         const FX_WCHAR* pszFontFamily = NULL);
156   virtual IFX_Font* GetDefFontByLanguage(FX_WORD wLanguage,
157                                          FX_DWORD dwFontStyles,
158                                          const FX_WCHAR* pszFontFamily = NULL);
159   virtual IFX_Font* GetFontByCodePage(FX_WORD wCodePage,
160                                       FX_DWORD dwFontStyles,
161                                       const FX_WCHAR* pszFontFamily = NULL);
162   virtual IFX_Font* GetFontByCharset(uint8_t nCharset,
163                                      FX_DWORD dwFontStyles,
164                                      const FX_WCHAR* pszFontFamily = NULL);
165   virtual IFX_Font* GetFontByUnicode(FX_WCHAR wUnicode,
166                                      FX_DWORD dwFontStyles,
167                                      const FX_WCHAR* pszFontFamily = NULL);
168   virtual IFX_Font* GetFontByLanguage(FX_WORD wLanguage,
169                                       FX_DWORD dwFontStyles,
170                                       const FX_WCHAR* pszFontFamily = NULL);
171   virtual IFX_Font* LoadFont(const uint8_t* pBuffer,
172                              int32_t iLength,
173                              int32_t iFaceIndex,
174                              int32_t* pFaceCount);
175   virtual IFX_Font* LoadFont(const FX_WCHAR* pszFileName,
176                              int32_t iFaceIndex,
177                              int32_t* pFaceCount);
178   virtual IFX_Font* LoadFont(IFX_Stream* pFontStream,
179                              int32_t iFaceIndex,
180                              int32_t* pFaceCount,
181                              FX_BOOL bSaveStream = FALSE);
182   virtual void ClearFontCache();
183   virtual void RemoveFont(IFX_Font* pFont);
184   FX_BOOL EnumFonts();
185 
186  protected:
187   void ReportFace(FXFT_Face pFace,
188                   CFX_FontDescriptors& Fonts,
189                   IFX_FileAccess* pFontAccess);
190   void GetNames(const uint8_t* name_table, CFX_WideStringArray& Names);
191   void GetCharsets(FXFT_Face pFace, CFX_WordArray& Charsets);
192   void GetUSBCSB(FXFT_Face pFace, FX_DWORD* USB, FX_DWORD* CSB);
193   FX_DWORD GetFlags(FXFT_Face pFace);
194   CFX_FontDescriptors m_InstalledFonts;
195   FX_BOOL VerifyUnicode(CFX_FontDescriptor* pDesc, FX_WCHAR wcUnicode);
196   FX_BOOL VerifyUnicode(IFX_Font* pFont, FX_WCHAR wcUnicode);
197   void NormalizeFontName(CFX_WideString& FontName);
198   int32_t IsPartName(const CFX_WideString& Name1, const CFX_WideString& Name2);
199   int32_t MatchFonts(CFX_FontDescriptorInfos& MatchedFonts,
200                      FX_WORD wCodePage,
201                      FX_DWORD dwFontStyles,
202                      const CFX_WideString& FontName,
203                      FX_WCHAR wcUnicode = 0xFFFE);
204   int32_t CalcPenalty(CFX_FontDescriptor* pInstalled,
205                       FX_WORD wCodePage,
206                       FX_DWORD dwFontStyles,
207                       const CFX_WideString& FontName,
208                       FX_WCHAR wcUnicode = 0xFFFE);
209   IFX_Font* LoadFont(IFX_FileAccess* pFontAccess,
210                      int32_t iFaceIndex,
211                      int32_t* pFaceCount,
212                      FX_BOOL bWantCache = FALSE);
213   FXFT_Face LoadFace(IFX_FileRead* pFontStream, int32_t iFaceIndex);
214   CFX_HashFontDescsMap m_Hash2CandidateList;
215   CFX_HashFontsMap m_Hash2Fonts;
216   CFX_HashFileMap m_Hash2FileAccess;
217   CFX_HashFontMap m_FileAccess2IFXFont;
218   CFX_FonStreamtMap m_IFXFont2FileRead;
219   CFX_UnicodeFontMap m_FailedUnicodes2NULL;
220   IFX_FontSourceEnum* m_pFontSource;
221   IFX_FontMgrDelegate* m_pDelegate;
222   void* m_pUserData;
223 };
224 #endif
225 #endif
226