• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "build/build_config.h"
17 #include "core/fxcrt/fx_extension.h"
18 #include "core/fxcrt/observed_ptr.h"
19 #include "core/fxcrt/retain_ptr.h"
20 #include "core/fxge/cfx_face.h"
21 #include "core/fxge/fx_freetype.h"
22 #include "xfa/fgas/font/cfgas_pdffontmgr.h"
23 
24 class CFGAS_GEFont;
25 class CFX_FontMapper;
26 class CFX_FontSourceEnum_File;
27 class IFX_SeekableReadStream;
28 
29 #if defined(OS_WIN)
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 #else  // defined(OS_WIN)
67 
68 class CFX_FontDescriptor {
69  public:
70   CFX_FontDescriptor();
71   ~CFX_FontDescriptor();
72 
73   int32_t m_nFaceIndex;
74   uint32_t m_dwFontStyles;
75   WideString m_wsFaceName;
76   std::vector<WideString> m_wsFamilyNames;
77   uint32_t m_dwUsb[4];
78   uint32_t m_dwCsb[2];
79 };
80 
81 class CFX_FontDescriptorInfo {
82  public:
83   CFX_FontDescriptor* pFont;
84   int32_t nPenalty;
85 
86   bool operator>(const CFX_FontDescriptorInfo& other) const {
87     return nPenalty > other.nPenalty;
88   }
89   bool operator<(const CFX_FontDescriptorInfo& other) const {
90     return nPenalty < other.nPenalty;
91   }
92   bool operator==(const CFX_FontDescriptorInfo& other) const {
93     return nPenalty == other.nPenalty;
94   }
95 };
96 
97 #endif  // defined(OS_WIN)
98 
99 class CFGAS_FontMgr final : public Observable {
100  public:
101   CFGAS_FontMgr();
102   ~CFGAS_FontMgr();
103 
104   RetainPtr<CFGAS_GEFont> GetFontByCodePage(uint16_t wCodePage,
105                                             uint32_t dwFontStyles,
106                                             const wchar_t* pszFontFamily);
107   RetainPtr<CFGAS_GEFont> GetFontByUnicode(wchar_t wUnicode,
108                                            uint32_t dwFontStyles,
109                                            const wchar_t* pszFontFamily);
110   RetainPtr<CFGAS_GEFont> LoadFont(const wchar_t* pszFontFamily,
111                                    uint32_t dwFontStyles,
112                                    uint16_t wCodePage);
113   void RemoveFont(const RetainPtr<CFGAS_GEFont>& pFont);
114 
115   bool EnumFonts();
116 
117  private:
118   RetainPtr<CFGAS_GEFont> GetFontByUnicodeImpl(wchar_t wUnicode,
119                                                uint32_t dwFontStyles,
120                                                const wchar_t* pszFontFamily,
121                                                uint32_t dwHash,
122                                                uint16_t wCodePage,
123                                                uint16_t wBitField);
124 
125 #if defined(OS_WIN)
126   const FX_FONTDESCRIPTOR* FindFont(const wchar_t* pszFontFamily,
127                                     uint32_t dwFontStyles,
128                                     bool matchParagraphStyle,
129                                     uint16_t wCodePage,
130                                     uint32_t dwUSB,
131                                     wchar_t wUnicode);
132 
133 #else   // defined(OS_WIN)
134   bool EnumFontsFromFontMapper();
135   bool EnumFontsFromFiles();
136   void RegisterFace(RetainPtr<CFX_Face> pFace, const WideString* pFaceName);
137   void RegisterFaces(const RetainPtr<IFX_SeekableReadStream>& pFontStream,
138                      const WideString* pFaceName);
139   void MatchFonts(std::vector<CFX_FontDescriptorInfo>* MatchedFonts,
140                   uint16_t wCodePage,
141                   uint32_t dwFontStyles,
142                   const WideString& FontName,
143                   wchar_t wcUnicode);
144   RetainPtr<CFGAS_GEFont> LoadFontInternal(const WideString& wsFaceName,
145                                            int32_t iFaceIndex);
146 #endif  // defined(OS_WIN)
147 
148   std::map<uint32_t, std::vector<RetainPtr<CFGAS_GEFont>>> m_Hash2Fonts;
149   std::set<wchar_t> m_FailedUnicodesSet;
150 
151 #if defined(OS_WIN)
152   std::deque<FX_FONTDESCRIPTOR> m_FontFaces;
153 #else
154   std::unique_ptr<CFX_FontSourceEnum_File> m_pFontSource;
155   std::vector<std::unique_ptr<CFX_FontDescriptor>> m_InstalledFonts;
156   std::map<uint32_t, std::unique_ptr<std::vector<CFX_FontDescriptorInfo>>>
157       m_Hash2CandidateList;
158   std::map<RetainPtr<CFGAS_GEFont>, RetainPtr<IFX_SeekableReadStream>>
159       m_IFXFont2FileRead;
160 #endif  // defined(OS_WIN)
161 };
162 
163 #endif  // XFA_FGAS_FONT_CFGAS_FONTMGR_H_
164