xref: /aosp_15_r20/external/pdfium/core/fxge/cfx_font.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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/retain_ptr.h"
20 #include "core/fxcrt/unowned_ptr_exclusion.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/containers/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; }
GetFontType()131   FontType GetFontType() const { return m_FontType; }
SetFontType(FontType type)132   void SetFontType(FontType type) { m_FontType = type; }
GetObjectTag()133   uint64_t GetObjectTag() const { return m_ObjectTag; }
GetFontSpan()134   pdfium::span<uint8_t> GetFontSpan() const { return m_FontData; }
135   void AdjustMMParams(int glyph_index, int dest_width, int weight) const;
136   std::unique_ptr<CFX_Path> LoadGlyphPathImpl(uint32_t glyph_index,
137                                               int dest_width) const;
138   int GetGlyphWidthImpl(uint32_t glyph_index, int dest_width, int weight) const;
139 
140 #if defined(_SKIA_SUPPORT_)
141   CFX_TypeFace* GetDeviceCache() const;
142   bool IsSubstFontBold() const;
143 #endif
144 
145 #if BUILDFLAG(IS_APPLE)
GetPlatformFont()146   void* GetPlatformFont() const { return m_pPlatformFont; }
SetPlatformFont(void * font)147   void SetPlatformFont(void* font) { m_pPlatformFont = font; }
148 #endif
149 
150  private:
151   RetainPtr<CFX_GlyphCache> GetOrCreateGlyphCache() const;
152   void ClearGlyphCache();
153 #if BUILDFLAG(IS_APPLE)
154   void ReleasePlatformResource();
155 #endif
156   ByteString GetFamilyNameOrUntitled() const;
157 
158 #if defined(PDF_ENABLE_XFA)
159   // |m_pOwnedFile| must outlive |m_pOwnedStreamRec|.
160   RetainPtr<IFX_SeekableReadStream> m_pOwnedFile;
161   std::unique_ptr<FXFT_StreamRec> m_pOwnedStreamRec;  // Must outlive |m_Face|.
162 #endif
163 
164   mutable RetainPtr<CFX_Face> m_Face;
165   mutable RetainPtr<CFX_GlyphCache> m_GlyphCache;
166   std::unique_ptr<CFX_SubstFont> m_pSubstFont;
167   DataVector<uint8_t> m_FontDataAllocation;
168   pdfium::span<uint8_t> m_FontData;
169   FontType m_FontType = FontType::kUnknown;
170   uint64_t m_ObjectTag = 0;
171   bool m_bEmbedded = false;
172   bool m_bVertical = false;
173 #if BUILDFLAG(IS_APPLE)
174   UNOWNED_PTR_EXCLUSION void* m_pPlatformFont = nullptr;
175 #endif
176 };
177 
178 #endif  // CORE_FXGE_CFX_FONT_H_
179