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