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