• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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 CORE_FXGE_CFX_FONTMAPPER_H_
8 #define CORE_FXGE_CFX_FONTMAPPER_H_
9 
10 #include <array>
11 #include <memory>
12 #include <optional>
13 #include <utility>
14 #include <vector>
15 
16 #include "build/build_config.h"
17 #include "core/fxcrt/bytestring.h"
18 #include "core/fxcrt/fx_codepage_forward.h"
19 #include "core/fxcrt/retain_ptr.h"
20 #include "core/fxcrt/unowned_ptr.h"
21 #include "core/fxge/cfx_face.h"
22 
23 #ifdef PDF_ENABLE_XFA
24 #include "core/fxcrt/fixed_size_data_vector.h"
25 #endif
26 
27 class CFX_FontMgr;
28 class CFX_SubstFont;
29 class SystemFontInfoIface;
30 
31 class CFX_FontMapper {
32  public:
33   enum StandardFont : uint8_t {
34     kCourier = 0,
35     kCourierBold,
36     kCourierBoldOblique,
37     kCourierOblique,
38     kHelvetica,
39     kHelveticaBold,
40     kHelveticaBoldOblique,
41     kHelveticaOblique,
42     kTimes,
43     kTimesBold,
44     kTimesBoldOblique,
45     kTimesOblique,
46     kSymbol,
47     kDingbats,
48     kLast = kDingbats
49   };
50   static constexpr int kNumStandardFonts = 14;
51 
52   explicit CFX_FontMapper(CFX_FontMgr* mgr);
53   ~CFX_FontMapper();
54 
55   static std::optional<StandardFont> GetStandardFontName(ByteString* name);
56   static bool IsStandardFontName(const ByteString& name);
57   static bool IsSymbolicFont(StandardFont font);
58   static bool IsFixedFont(StandardFont font);
MakeTag(char c1,char c2,char c3,char c4)59   static constexpr uint32_t MakeTag(char c1, char c2, char c3, char c4) {
60     return static_cast<uint8_t>(c1) << 24 | static_cast<uint8_t>(c2) << 16 |
61            static_cast<uint8_t>(c3) << 8 | static_cast<uint8_t>(c4);
62   }
63 
64   void SetSystemFontInfo(std::unique_ptr<SystemFontInfoIface> pFontInfo);
65   std::unique_ptr<SystemFontInfoIface> TakeSystemFontInfo();
66   void AddInstalledFont(const ByteString& name, FX_Charset charset);
67   void LoadInstalledFonts();
68 
69   RetainPtr<CFX_Face> FindSubstFont(const ByteString& face_name,
70                                     bool is_truetype,
71                                     uint32_t flags,
72                                     int weight,
73                                     int italic_angle,
74                                     FX_CodePage code_page,
75                                     CFX_SubstFont* subst_font);
76 
77   size_t GetFaceSize() const;
78   // `index` must be less than GetFaceSize().
79   ByteString GetFaceName(size_t index) const;
80   bool HasInstalledFont(ByteStringView name) const;
81   bool HasLocalizedFont(ByteStringView name) const;
82 
83 #if BUILDFLAG(IS_WIN)
84   std::optional<ByteString> InstalledFontNameStartingWith(
85       const ByteString& name) const;
86   std::optional<ByteString> LocalizedFontNameStartingWith(
87       const ByteString& name) const;
88 #endif  // BUILDFLAG(IS_WIN)
89 
90 #ifdef PDF_ENABLE_XFA
91   // `index` must be less than GetFaceSize().
92   FixedSizeDataVector<uint8_t> RawBytesForIndex(size_t index);
93 #endif  // PDF_ENABLE_XFA
94 
95  private:
96   friend class TestFontMapper;
97 
98   uint32_t GetChecksumFromTT(void* font_handle);
99   ByteString GetPSNameFromTT(void* font_handle);
100   ByteString MatchInstalledFonts(const ByteString& norm_name);
101   RetainPtr<CFX_Face> UseInternalSubst(int base_font,
102                                        int weight,
103                                        int italic_angle,
104                                        int pitch_family,
105                                        CFX_SubstFont* subst_font);
106   RetainPtr<CFX_Face> UseExternalSubst(void* font_handle,
107                                        ByteString face_name,
108                                        int weight,
109                                        bool is_italic,
110                                        int italic_angle,
111                                        FX_Charset charset,
112                                        CFX_SubstFont* subst_font);
113   RetainPtr<CFX_Face> GetCachedTTCFace(void* font_handle,
114                                        size_t ttc_size,
115                                        size_t data_size);
116   RetainPtr<CFX_Face> GetCachedFace(void* font_handle,
117                                     ByteString subst_name,
118                                     int weight,
119                                     bool is_italic,
120                                     size_t data_size);
121 
122   struct FaceData {
123     ByteString name;
124     uint32_t charset;
125   };
126 
127   bool m_bListLoaded = false;
128   ByteString m_LastFamily;
129   std::vector<FaceData> m_FaceArray;
130   std::unique_ptr<SystemFontInfoIface> m_pFontInfo;
131   UnownedPtr<CFX_FontMgr> const m_pFontMgr;
132   std::vector<ByteString> m_InstalledTTFonts;
133   std::vector<std::pair<ByteString, ByteString>> m_LocalizedTTFonts;
134   std::array<RetainPtr<CFX_Face>, kNumStandardFonts> m_StandardFaces;
135   RetainPtr<CFX_Face> m_GenericSansFace;
136   RetainPtr<CFX_Face> m_GenericSerifFace;
137 };
138 
139 #endif  // CORE_FXGE_CFX_FONTMAPPER_H_
140