1 // Copyright 2016 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 CORE_FXGE_CFX_FONTMAPPER_H_
8 #define CORE_FXGE_CFX_FONTMAPPER_H_
9 
10 #include <memory>
11 #include <utility>
12 #include <vector>
13 
14 #include "core/fxcrt/fx_memory_wrappers.h"
15 #include "core/fxcrt/fx_string.h"
16 #include "core/fxge/cfx_face.h"
17 #include "third_party/base/optional.h"
18 
19 class CFX_FontMgr;
20 class CFX_SubstFont;
21 class SystemFontInfoIface;
22 
23 class CFX_FontMapper {
24  public:
25   enum StandardFont {
26     kCourier = 0,
27     kCourierBold,
28     kCourierBoldOblique,
29     kCourierOblique,
30     kHelvetica,
31     kHelveticaBold,
32     kHelveticaBoldOblique,
33     kHelveticaOblique,
34     kTimes,
35     kTimesBold,
36     kTimesBoldOblique,
37     kTimesOblique,
38     kSymbol,
39     kDingbats,
40   };
41 
42   explicit CFX_FontMapper(CFX_FontMgr* mgr);
43   ~CFX_FontMapper();
44 
45   static Optional<StandardFont> GetStandardFontName(ByteString* name);
46   static bool IsSymbolicFont(StandardFont font);
47   static bool IsFixedFont(StandardFont font);
MakeTag(char c1,char c2,char c3,char c4)48   static constexpr uint32_t MakeTag(char c1, char c2, char c3, char c4) {
49     return static_cast<uint8_t>(c1) << 24 | static_cast<uint8_t>(c2) << 16 |
50            static_cast<uint8_t>(c3) << 8 | static_cast<uint8_t>(c4);
51   }
52 
53   void SetSystemFontInfo(std::unique_ptr<SystemFontInfoIface> pFontInfo);
54   void AddInstalledFont(const ByteString& name, int charset);
55   void LoadInstalledFonts();
56 
57   RetainPtr<CFX_Face> FindSubstFont(const ByteString& face_name,
58                                     bool bTrueType,
59                                     uint32_t flags,
60                                     int weight,
61                                     int italic_angle,
62                                     int CharsetCP,
63                                     CFX_SubstFont* pSubstFont);
64 
65   bool IsBuiltinFace(const RetainPtr<CFX_Face>& face) const;
66   int GetFaceSize() const;
GetFaceName(int index)67   ByteString GetFaceName(int index) const { return m_FaceArray[index].name; }
68 
69 #ifdef PDF_ENABLE_XFA
70   std::unique_ptr<uint8_t, FxFreeDeleter> RawBytesForIndex(
71       uint32_t index,
72       size_t* returned_length);
73 #endif  // PDF_ENABLE_XFA
74 
75   std::vector<ByteString> m_InstalledTTFonts;
76   std::vector<std::pair<ByteString, ByteString>> m_LocalizedTTFonts;
77 
78  private:
79   static const size_t MM_FACE_COUNT = 2;
80   static const size_t FOXIT_FACE_COUNT = 14;
81 
82   uint32_t GetChecksumFromTT(void* hFont);
83   ByteString GetPSNameFromTT(void* hFont);
84   ByteString MatchInstalledFonts(const ByteString& norm_name);
85   RetainPtr<CFX_Face> UseInternalSubst(CFX_SubstFont* pSubstFont,
86                                        int iBaseFont,
87                                        int italic_angle,
88                                        int weight,
89                                        int pitch_family);
90   RetainPtr<CFX_Face> GetCachedTTCFace(void* hFont,
91                                        uint32_t ttc_size,
92                                        uint32_t font_size);
93   RetainPtr<CFX_Face> GetCachedFace(void* hFont,
94                                     ByteString SubstName,
95                                     int weight,
96                                     bool bItalic,
97                                     uint32_t font_size);
98 
99   struct FaceData {
100     ByteString name;
101     uint32_t charset;
102   };
103 
104   bool m_bListLoaded = false;
105   ByteString m_LastFamily;
106   std::vector<FaceData> m_FaceArray;
107   std::unique_ptr<SystemFontInfoIface> m_pFontInfo;
108   UnownedPtr<CFX_FontMgr> const m_pFontMgr;
109   RetainPtr<CFX_Face> m_MMFaces[MM_FACE_COUNT];
110   RetainPtr<CFX_Face> m_FoxitFaces[FOXIT_FACE_COUNT];
111 };
112 
113 #endif  // CORE_FXGE_CFX_FONTMAPPER_H_
114