• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The PDFium Authors
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 <array>
11 #include <deque>
12 #include <map>
13 #include <memory>
14 #include <set>
15 #include <vector>
16 
17 #include "build/build_config.h"
18 #include "core/fxcrt/compiler_specific.h"
19 #include "core/fxcrt/fx_codepage_forward.h"
20 #include "core/fxcrt/retain_ptr.h"
21 #include "core/fxcrt/widestring.h"
22 #include "core/fxge/cfx_face.h"
23 
24 class CFGAS_GEFont;
25 class IFX_SeekableReadStream;
26 
27 #if BUILDFLAG(IS_WIN)
28 struct FX_FONTSIGNATURE {
29   std::array<uint32_t, 4> fsUsb;
30   std::array<uint32_t, 2> fsCsb;
31 };
32 
33 inline bool operator==(const FX_FONTSIGNATURE& left,
34                        const FX_FONTSIGNATURE& right) {
35   return left.fsUsb[0] == right.fsUsb[0] && left.fsUsb[1] == right.fsUsb[1] &&
36          left.fsUsb[2] == right.fsUsb[2] && left.fsUsb[3] == right.fsUsb[3] &&
37          left.fsCsb[0] == right.fsCsb[0] && left.fsCsb[1] == right.fsCsb[1];
38 }
39 
40 struct FX_FONTDESCRIPTOR {
41   wchar_t wsFontFace[32];
42   uint32_t dwFontStyles;
43   FX_Charset uCharSet;
44   FX_FONTSIGNATURE FontSignature;
45 };
46 
47 inline bool operator==(const FX_FONTDESCRIPTOR& left,
48                        const FX_FONTDESCRIPTOR& right) {
49   return left.uCharSet == right.uCharSet &&
50          left.dwFontStyles == right.dwFontStyles &&
51          left.FontSignature == right.FontSignature &&
52          wcscmp(left.wsFontFace, right.wsFontFace) == 0;
53 }
54 
55 #else  // BUILDFLAG(IS_WIN)
56 
57 class CFGAS_FontDescriptor {
58  public:
59   CFGAS_FontDescriptor();
60   ~CFGAS_FontDescriptor();
61 
62   int32_t m_nFaceIndex = 0;
63   uint32_t m_dwFontStyles = 0;
64   WideString m_wsFaceName;
65   std::vector<WideString> m_wsFamilyNames;
66   std::array<uint32_t, 4> m_dwUsb = {};
67   std::array<uint32_t, 2> m_dwCsb = {};
68 };
69 
70 struct CFGAS_FontDescriptorInfo {
71  public:
72   UNOWNED_PTR_EXCLUSION CFGAS_FontDescriptor* pFont;  // POD struct.
73   int32_t nPenalty;
74 
75   bool operator>(const CFGAS_FontDescriptorInfo& other) const {
76     return nPenalty > other.nPenalty;
77   }
78   bool operator<(const CFGAS_FontDescriptorInfo& other) const {
79     return nPenalty < other.nPenalty;
80   }
81   bool operator==(const CFGAS_FontDescriptorInfo& other) const {
82     return nPenalty == other.nPenalty;
83   }
84 };
85 
86 #endif  // BUILDFLAG(IS_WIN)
87 
88 class CFGAS_FontMgr {
89  public:
90   CFGAS_FontMgr();
91   ~CFGAS_FontMgr();
92 
93   bool EnumFonts();
94   RetainPtr<CFGAS_GEFont> GetFontByCodePage(FX_CodePage wCodePage,
95                                             uint32_t dwFontStyles,
96                                             const wchar_t* pszFontFamily);
97   RetainPtr<CFGAS_GEFont> GetFontByUnicode(wchar_t wUnicode,
98                                            uint32_t dwFontStyles,
99                                            const wchar_t* pszFontFamily);
100   RetainPtr<CFGAS_GEFont> LoadFont(const wchar_t* pszFontFamily,
101                                    uint32_t dwFontStyles,
102                                    FX_CodePage wCodePage);
103 
104  private:
105   RetainPtr<CFGAS_GEFont> GetFontByUnicodeImpl(wchar_t wUnicode,
106                                                uint32_t dwFontStyles,
107                                                const wchar_t* pszFontFamily,
108                                                uint32_t dwHash,
109                                                FX_CodePage wCodePage,
110                                                uint16_t wBitField);
111 
112 #if BUILDFLAG(IS_WIN)
113   const FX_FONTDESCRIPTOR* FindFont(const wchar_t* pszFontFamily,
114                                     uint32_t dwFontStyles,
115                                     bool matchParagraphStyle,
116                                     FX_CodePage wCodePage,
117                                     uint32_t dwUSB,
118                                     wchar_t wUnicode);
119 
120 #else   // BUILDFLAG(IS_WIN)
121   bool EnumFontsFromFontMapper();
122   void RegisterFace(RetainPtr<CFX_Face> pFace, const WideString& wsFaceName);
123   void RegisterFaces(const RetainPtr<IFX_SeekableReadStream>& pFontStream,
124                      const WideString& wsFaceName);
125   std::vector<CFGAS_FontDescriptorInfo> MatchFonts(FX_CodePage wCodePage,
126                                                    uint32_t dwFontStyles,
127                                                    const WideString& FontName,
128                                                    wchar_t wcUnicode);
129   RetainPtr<CFGAS_GEFont> LoadFontInternal(const WideString& wsFaceName,
130                                            int32_t iFaceIndex);
131 #endif  // BUILDFLAG(IS_WIN)
132 
133   std::map<uint32_t, std::vector<RetainPtr<CFGAS_GEFont>>> m_Hash2Fonts;
134   std::set<wchar_t> m_FailedUnicodesSet;
135 
136 #if BUILDFLAG(IS_WIN)
137   std::deque<FX_FONTDESCRIPTOR> m_FontFaces;
138 #else
139   std::vector<std::unique_ptr<CFGAS_FontDescriptor>> m_InstalledFonts;
140   std::map<uint32_t, std::vector<CFGAS_FontDescriptorInfo>>
141       m_Hash2CandidateList;
142 #endif  // BUILDFLAG(IS_WIN)
143 };
144 
145 #endif  // XFA_FGAS_FONT_CFGAS_FONTMGR_H_
146