• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <optional>
14 #include <utility>
15 #include <vector>
16 
17 #include "build/build_config.h"
18 #include "core/fpdfapi/parser/cpdf_dictionary.h"
19 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
20 #include "core/fxcrt/fx_coordinates.h"
21 #include "core/fxcrt/fx_string.h"
22 #include "core/fxcrt/observed_ptr.h"
23 #include "core/fxcrt/retain_ptr.h"
24 #include "core/fxcrt/unowned_ptr.h"
25 #include "core/fxge/cfx_font.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 std::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 void AppendChar(ByteString* 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   std::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.GetFace(); }
110 
GetFontBBox()111   const FX_RECT& GetFontBBox() const { return m_FontBBox; }
GetTypeAscent()112   int GetTypeAscent() const { return m_Ascent; }
GetTypeDescent()113   int GetTypeDescent() const { return m_Descent; }
114   int GetStringWidth(ByteStringView pString);
115   uint32_t FallbackFontFromCharcode(uint32_t charcode);
116   int FallbackGlyphFromCharcode(int fallbackFont, uint32_t charcode);
GetFontFlags()117   int GetFontFlags() const { return m_Flags; }
GetItalicAngle()118   int GetItalicAngle() const { return m_ItalicAngle; }
119   int GetFontWeight() const;
120 
121   virtual int GetCharWidthF(uint32_t charcode) = 0;
122   virtual FX_RECT GetCharBBox(uint32_t charcode) = 0;
123 
124   // Can return nullptr for stock Type1 fonts. Always returns non-null for other
125   // font types.
GetDocument()126   CPDF_Document* GetDocument() const { return m_pDocument; }
127 
GetFont()128   CFX_Font* GetFont() { return &m_Font; }
GetFont()129   const CFX_Font* GetFont() const { return &m_Font; }
130 
131   CFX_Font* GetFontFallback(int position);
132 
GetResourceName()133   const ByteString& GetResourceName() const { return m_ResourceName; }
SetResourceName(const ByteString & name)134   void SetResourceName(const ByteString& name) { m_ResourceName = name; }
135 
136  protected:
137   CPDF_Font(CPDF_Document* pDocument, RetainPtr<CPDF_Dictionary> pFontDict);
138   ~CPDF_Font() override;
139 
140   // Commonly used wrappers for UseTTCharmap().
UseTTCharmapMSUnicode(const RetainPtr<CFX_Face> & face)141   static bool UseTTCharmapMSUnicode(const RetainPtr<CFX_Face>& face) {
142     return UseTTCharmap(face, 3, 1);
143   }
UseTTCharmapMSSymbol(const RetainPtr<CFX_Face> & face)144   static bool UseTTCharmapMSSymbol(const RetainPtr<CFX_Face>& face) {
145     return UseTTCharmap(face, 3, 0);
146   }
UseTTCharmapMacRoman(const RetainPtr<CFX_Face> & face)147   static bool UseTTCharmapMacRoman(const RetainPtr<CFX_Face>& face) {
148     return UseTTCharmap(face, 1, 0);
149   }
150   static bool UseTTCharmap(const RetainPtr<CFX_Face>& face,
151                            int platform_id,
152                            int encoding_id);
153 
154   static const char* GetAdobeCharName(FontEncoding base_encoding,
155                                       const std::vector<ByteString>& charnames,
156                                       uint32_t charcode);
157 
158   virtual bool Load() = 0;
159 
160   void LoadUnicodeMap() const;  // logically const only.
161   void LoadFontDescriptor(const CPDF_Dictionary* pFontDesc);
162   void CheckFontMetrics();
163 
164   UnownedPtr<CPDF_Document> const m_pDocument;
165   ByteString m_ResourceName;  // The resource name for this font.
166   CFX_Font m_Font;
167   std::vector<std::unique_ptr<CFX_Font>> m_FontFallbacks;
168   RetainPtr<CPDF_StreamAcc> m_pFontFile;
169   RetainPtr<CPDF_Dictionary> m_pFontDict;
170   ByteString m_BaseFontName;
171   mutable std::unique_ptr<CPDF_ToUnicodeMap> m_pToUnicodeMap;
172   mutable bool m_bToUnicodeLoaded = false;
173   bool m_bWillBeDestroyed = false;
174   int m_Flags = 0;
175   int m_StemV = 0;
176   int m_Ascent = 0;
177   int m_Descent = 0;
178   int m_ItalicAngle = 0;
179   FX_RECT m_FontBBox;
180 };
181 
182 #endif  // CORE_FPDFAPI_FONT_CPDF_FONT_H_
183