xref: /aosp_15_r20/external/pdfium/core/fpdfapi/font/cpdf_font.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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_FPDFAPI_FONT_CPDF_FONT_H_
8 #define CORE_FPDFAPI_FONT_CPDF_FONT_H_
9 
10 #include <stdint.h>
11 
12 #include <memory>
13 #include <utility>
14 #include <vector>
15 
16 #include "build/build_config.h"
17 #include "core/fpdfapi/parser/cpdf_dictionary.h"
18 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
19 #include "core/fxcrt/fx_string.h"
20 #include "core/fxcrt/observed_ptr.h"
21 #include "core/fxcrt/retain_ptr.h"
22 #include "core/fxcrt/unowned_ptr.h"
23 #include "core/fxge/cfx_font.h"
24 #include "core/fxge/freetype/fx_freetype.h"
25 #include "third_party/abseil-cpp/absl/types/optional.h"
26 
27 class CFX_DIBitmap;
28 class CPDF_CIDFont;
29 class CPDF_Document;
30 class CPDF_TrueTypeFont;
31 class CPDF_Type1Font;
32 class CPDF_Type3Char;
33 class CPDF_Type3Font;
34 class CPDF_ToUnicodeMap;
35 enum class FontEncoding;
36 
37 class CPDF_Font : public Retainable, public Observable {
38  public:
39   // Callback mechanism for Type3 fonts to get pixels from forms.
40   class FormIface {
41    public:
42     virtual ~FormIface() = default;
43 
44     virtual void ParseContentForType3Char(CPDF_Type3Char* pChar) = 0;
45     virtual bool HasPageObjects() const = 0;
46     virtual CFX_FloatRect CalcBoundingBox() const = 0;
47     virtual absl::optional<std::pair<RetainPtr<CFX_DIBitmap>, CFX_Matrix>>
48     GetBitmapAndMatrixFromSoleImageOfForm() const = 0;
49   };
50 
51   // Callback mechanism for Type3 fonts to get new forms from upper layers.
52   class FormFactoryIface {
53    public:
54     virtual ~FormFactoryIface() = default;
55 
56     virtual std::unique_ptr<FormIface> CreateForm(
57         CPDF_Document* pDocument,
58         RetainPtr<CPDF_Dictionary> pPageResources,
59         RetainPtr<CPDF_Stream> pFormStream) = 0;
60   };
61 
62   static constexpr uint32_t kInvalidCharCode = static_cast<uint32_t>(-1);
63 
64   // |pFactory| only required for Type3 fonts.
65   static RetainPtr<CPDF_Font> Create(CPDF_Document* pDoc,
66                                      RetainPtr<CPDF_Dictionary> pFontDict,
67                                      FormFactoryIface* pFactory);
68   static RetainPtr<CPDF_Font> GetStockFont(CPDF_Document* pDoc,
69                                            ByteStringView fontname);
70 
71   virtual bool IsType1Font() const;
72   virtual bool IsTrueTypeFont() const;
73   virtual bool IsType3Font() const;
74   virtual bool IsCIDFont() const;
75   virtual const CPDF_Type1Font* AsType1Font() const;
76   virtual CPDF_Type1Font* AsType1Font();
77   virtual const CPDF_TrueTypeFont* AsTrueTypeFont() const;
78   virtual CPDF_TrueTypeFont* AsTrueTypeFont();
79   virtual const CPDF_Type3Font* AsType3Font() const;
80   virtual CPDF_Type3Font* AsType3Font();
81   virtual const CPDF_CIDFont* AsCIDFont() const;
82   virtual CPDF_CIDFont* AsCIDFont();
83 
84   virtual void WillBeDestroyed();
85   virtual bool IsVertWriting() const;
86   virtual bool IsUnicodeCompatible() const = 0;
87   virtual uint32_t GetNextChar(ByteStringView pString, size_t* pOffset) const;
88   virtual size_t CountChar(ByteStringView pString) const;
89   virtual int AppendChar(char* buf, uint32_t charcode) const;
90   virtual int GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) = 0;
91 #if BUILDFLAG(IS_APPLE)
92   virtual int GlyphFromCharCodeExt(uint32_t charcode);
93 #endif
94   virtual WideString UnicodeFromCharCode(uint32_t charcode) const;
95   virtual uint32_t CharCodeFromUnicode(wchar_t Unicode) const;
96   virtual bool HasFontWidths() const;
97 
GetBaseFontName()98   ByteString GetBaseFontName() const { return m_BaseFontName; }
99   absl::optional<FX_Charset> GetSubstFontCharset() const;
IsEmbedded()100   bool IsEmbedded() const { return IsType3Font() || m_pFontFile != nullptr; }
GetMutableFontDict()101   RetainPtr<CPDF_Dictionary> GetMutableFontDict() { return m_pFontDict; }
GetFontDict()102   RetainPtr<const CPDF_Dictionary> GetFontDict() const { return m_pFontDict; }
GetFontDictObjNum()103   uint32_t GetFontDictObjNum() const { return m_pFontDict->GetObjNum(); }
FontDictIs(const CPDF_Dictionary * pThat)104   bool FontDictIs(const CPDF_Dictionary* pThat) const {
105     return m_pFontDict == pThat;
106   }
ClearFontDict()107   void ClearFontDict() { m_pFontDict = nullptr; }
108   bool IsStandardFont() const;
HasFace()109   bool HasFace() const { return !!m_Font.GetFaceRec(); }
110   void AppendChar(ByteString* str, uint32_t charcode) const;
111 
GetFontBBox()112   const FX_RECT& GetFontBBox() const { return m_FontBBox; }
GetTypeAscent()113   int GetTypeAscent() const { return m_Ascent; }
GetTypeDescent()114   int GetTypeDescent() const { return m_Descent; }
115   int GetStringWidth(ByteStringView pString);
116   uint32_t FallbackFontFromCharcode(uint32_t charcode);
117   int FallbackGlyphFromCharcode(int fallbackFont, uint32_t charcode);
GetFontFlags()118   int GetFontFlags() const { return m_Flags; }
GetItalicAngle()119   int GetItalicAngle() const { return m_ItalicAngle; }
120   int GetFontWeight() const;
121 
122   virtual int GetCharWidthF(uint32_t charcode) = 0;
123   virtual FX_RECT GetCharBBox(uint32_t charcode) = 0;
124 
125   // Can return nullptr for stock Type1 fonts. Always returns non-null for other
126   // font types.
GetDocument()127   CPDF_Document* GetDocument() const { return m_pDocument; }
128 
GetFont()129   CFX_Font* GetFont() { return &m_Font; }
GetFont()130   const CFX_Font* GetFont() const { return &m_Font; }
131 
132   CFX_Font* GetFontFallback(int position);
133 
GetResourceName()134   const ByteString& GetResourceName() const { return m_ResourceName; }
SetResourceName(const ByteString & name)135   void SetResourceName(const ByteString& name) { m_ResourceName = name; }
136 
137  protected:
138   CPDF_Font(CPDF_Document* pDocument, RetainPtr<CPDF_Dictionary> pFontDict);
139   ~CPDF_Font() override;
140 
141   static int TT2PDF(FT_Pos m, FXFT_FaceRec* face);
142 
143   // Commonly used wrappers for UseTTCharmap().
UseTTCharmapMSUnicode(FXFT_FaceRec * face)144   static bool UseTTCharmapMSUnicode(FXFT_FaceRec* face) {
145     return UseTTCharmap(face, 3, 1);
146   }
UseTTCharmapMSSymbol(FXFT_FaceRec * face)147   static bool UseTTCharmapMSSymbol(FXFT_FaceRec* face) {
148     return UseTTCharmap(face, 3, 0);
149   }
UseTTCharmapMacRoman(FXFT_FaceRec * face)150   static bool UseTTCharmapMacRoman(FXFT_FaceRec* face) {
151     return UseTTCharmap(face, 1, 0);
152   }
153   static bool UseTTCharmap(FXFT_FaceRec* face,
154                            int platform_id,
155                            int encoding_id);
156 
157   static const char* GetAdobeCharName(FontEncoding base_encoding,
158                                       const std::vector<ByteString>& charnames,
159                                       uint32_t charcode);
160 
161   virtual bool Load() = 0;
162 
163   void LoadUnicodeMap() const;  // logically const only.
164   void LoadFontDescriptor(const CPDF_Dictionary* pFontDesc);
165   void CheckFontMetrics();
166 
167   UnownedPtr<CPDF_Document> const m_pDocument;
168   ByteString m_ResourceName;  // The resource name for this font.
169   CFX_Font m_Font;
170   std::vector<std::unique_ptr<CFX_Font>> m_FontFallbacks;
171   RetainPtr<CPDF_StreamAcc> m_pFontFile;
172   RetainPtr<CPDF_Dictionary> m_pFontDict;
173   ByteString m_BaseFontName;
174   mutable std::unique_ptr<CPDF_ToUnicodeMap> m_pToUnicodeMap;
175   mutable bool m_bToUnicodeLoaded = false;
176   int m_Flags = 0;
177   int m_StemV = 0;
178   int m_Ascent = 0;
179   int m_Descent = 0;
180   int m_ItalicAngle = 0;
181   FX_RECT m_FontBBox;
182 };
183 
184 #endif  // CORE_FPDFAPI_FONT_CPDF_FONT_H_
185