1 // Copyright 2017 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_FONT_H_ 8 #define CORE_FXGE_CFX_FONT_H_ 9 10 #include <stdint.h> 11 12 #include <memory> 13 14 #include "build/build_config.h" 15 #include "core/fxcrt/bytestring.h" 16 #include "core/fxcrt/data_vector.h" 17 #include "core/fxcrt/fx_codepage_forward.h" 18 #include "core/fxcrt/fx_coordinates.h" 19 #include "core/fxcrt/fx_memory_wrappers.h" 20 #include "core/fxcrt/retain_ptr.h" 21 #include "core/fxge/cfx_face.h" 22 #include "core/fxge/freetype/fx_freetype.h" 23 #include "third_party/abseil-cpp/absl/types/optional.h" 24 #include "third_party/base/span.h" 25 26 #if defined(_SKIA_SUPPORT_) 27 #include "core/fxge/fx_font.h" 28 #endif 29 30 class CFX_GlyphBitmap; 31 class CFX_GlyphCache; 32 class CFX_Path; 33 class CFX_SubstFont; 34 class IFX_SeekableReadStream; 35 struct CFX_TextRenderOptions; 36 37 class CFX_Font { 38 public: 39 // This struct should be the same as FPDF_CharsetFontMap. 40 struct CharsetFontMap { 41 int charset; // Character Set Enum value, see FX_Charset::kXXX. 42 const char* fontname; // Name of default font to use with that charset. 43 }; 44 45 enum class FontType { 46 kUnknown, 47 kCIDTrueType, 48 }; 49 50 // Pointer to the default character set to TT Font name map. The map is an 51 // array of CharsetFontMap structs, with its end indicated by a {-1, nullptr} 52 // entry. 53 static const CharsetFontMap kDefaultTTFMap[]; 54 55 // Used when the font name is empty. 56 static const char kUntitledFontName[]; 57 58 static const char kDefaultAnsiFontName[]; 59 static const char kUniversalDefaultFontName[]; 60 61 // Returns negative values on failure. 62 static int GetWeightLevel(FX_Charset charset, size_t index); 63 64 // |angle| is typically negative. 65 static int GetSkewFromAngle(int angle); 66 67 static ByteString GetDefaultFontNameByCharset(FX_Charset nCharset); 68 static FX_Charset GetCharSetFromUnicode(uint16_t word); 69 70 CFX_Font(); 71 ~CFX_Font(); 72 73 void LoadSubst(const ByteString& face_name, 74 bool bTrueType, 75 uint32_t flags, 76 int weight, 77 int italic_angle, 78 FX_CodePage code_page, 79 bool bVertical); 80 81 bool LoadEmbedded(pdfium::span<const uint8_t> src_span, 82 bool force_vertical, 83 uint64_t object_tag); GetFace()84 RetainPtr<CFX_Face> GetFace() const { return m_Face; } GetFaceRec()85 FXFT_FaceRec* GetFaceRec() const { 86 return m_Face ? m_Face->GetRec() : nullptr; 87 } GetSubstFont()88 CFX_SubstFont* GetSubstFont() const { return m_pSubstFont.get(); } 89 int GetSubstFontItalicAngle() const; 90 91 #if defined(PDF_ENABLE_XFA) 92 bool LoadFile(RetainPtr<IFX_SeekableReadStream> pFile, int nFaceIndex); 93 94 #if !BUILDFLAG(IS_WIN) 95 void SetFace(RetainPtr<CFX_Face> face); SetFontSpan(pdfium::span<uint8_t> pSpan)96 void SetFontSpan(pdfium::span<uint8_t> pSpan) { m_FontData = pSpan; } 97 void SetSubstFont(std::unique_ptr<CFX_SubstFont> subst); 98 #endif // !BUILDFLAG(IS_WIN) 99 #endif // defined(PDF_ENABLE_XFA) 100 101 const CFX_GlyphBitmap* LoadGlyphBitmap( 102 uint32_t glyph_index, 103 bool bFontStyle, 104 const CFX_Matrix& matrix, 105 int dest_width, 106 int anti_alias, 107 CFX_TextRenderOptions* text_options) const; 108 const CFX_Path* LoadGlyphPath(uint32_t glyph_index, int dest_width) const; 109 int GetGlyphWidth(uint32_t glyph_index) const; 110 int GetGlyphWidth(uint32_t glyph_index, int dest_width, int weight) const; 111 int GetAscent() const; 112 int GetDescent() const; 113 absl::optional<FX_RECT> GetGlyphBBox(uint32_t glyph_index); 114 bool IsItalic() const; 115 bool IsBold() const; 116 bool IsFixedWidth() const; IsVertical()117 bool IsVertical() const { return m_bVertical; } 118 ByteString GetPsName() const; 119 ByteString GetFamilyName() const; 120 ByteString GetFaceName() const; 121 ByteString GetBaseFontName() const; 122 bool IsTTFont() const; 123 124 // Raw bounding box. 125 absl::optional<FX_RECT> GetRawBBox() const; 126 127 // Bounding box adjusted for font units. 128 absl::optional<FX_RECT> GetBBox() const; 129 IsEmbedded()130 bool IsEmbedded() const { return m_bEmbedded; } 131 void AllocSubData(size_t size); GetSubData()132 uint8_t* GetSubData() const { return m_pSubData.get(); } GetFontType()133 FontType GetFontType() const { return m_FontType; } SetFontType(FontType type)134 void SetFontType(FontType type) { m_FontType = type; } GetObjectTag()135 uint64_t GetObjectTag() const { return m_ObjectTag; } GetFontSpan()136 pdfium::span<uint8_t> GetFontSpan() const { return m_FontData; } 137 void AdjustMMParams(int glyph_index, int dest_width, int weight) const; 138 std::unique_ptr<CFX_Path> LoadGlyphPathImpl(uint32_t glyph_index, 139 int dest_width) const; 140 int GetGlyphWidthImpl(uint32_t glyph_index, int dest_width, int weight) const; 141 142 #if defined(_SKIA_SUPPORT_) 143 CFX_TypeFace* GetDeviceCache() const; 144 bool IsSubstFontBold() const; 145 #endif 146 147 #if BUILDFLAG(IS_APPLE) GetPlatformFont()148 void* GetPlatformFont() const { return m_pPlatformFont; } SetPlatformFont(void * font)149 void SetPlatformFont(void* font) { m_pPlatformFont = font; } 150 #endif 151 152 private: 153 RetainPtr<CFX_GlyphCache> GetOrCreateGlyphCache() const; 154 void ClearGlyphCache(); 155 #if BUILDFLAG(IS_APPLE) 156 void ReleasePlatformResource(); 157 #endif 158 ByteString GetFamilyNameOrUntitled() const; 159 160 #if defined(PDF_ENABLE_XFA) 161 // |m_pOwnedFile| must outlive |m_pOwnedStreamRec|. 162 RetainPtr<IFX_SeekableReadStream> m_pOwnedFile; 163 std::unique_ptr<FXFT_StreamRec> m_pOwnedStreamRec; // Must outlive |m_Face|. 164 #endif 165 166 mutable RetainPtr<CFX_Face> m_Face; 167 mutable RetainPtr<CFX_GlyphCache> m_GlyphCache; 168 std::unique_ptr<CFX_SubstFont> m_pSubstFont; 169 std::unique_ptr<uint8_t, FxFreeDeleter> m_pSubData; 170 DataVector<uint8_t> m_FontDataAllocation; 171 pdfium::span<uint8_t> m_FontData; 172 FontType m_FontType = FontType::kUnknown; 173 uint64_t m_ObjectTag = 0; 174 bool m_bEmbedded = false; 175 bool m_bVertical = false; 176 #if BUILDFLAG(IS_APPLE) 177 void* m_pPlatformFont = nullptr; 178 #endif 179 }; 180 181 #endif // CORE_FXGE_CFX_FONT_H_ 182