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