• 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 <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   ~CPDF_Font() override;
72 
73   virtual bool IsType1Font() const;
74   virtual bool IsTrueTypeFont() const;
75   virtual bool IsType3Font() const;
76   virtual bool IsCIDFont() const;
77   virtual const CPDF_Type1Font* AsType1Font() const;
78   virtual CPDF_Type1Font* AsType1Font();
79   virtual const CPDF_TrueTypeFont* AsTrueTypeFont() const;
80   virtual CPDF_TrueTypeFont* AsTrueTypeFont();
81   virtual const CPDF_Type3Font* AsType3Font() const;
82   virtual CPDF_Type3Font* AsType3Font();
83   virtual const CPDF_CIDFont* AsCIDFont() const;
84   virtual CPDF_CIDFont* AsCIDFont();
85 
86   virtual void WillBeDestroyed();
87   virtual bool IsVertWriting() const;
88   virtual bool IsUnicodeCompatible() const = 0;
89   virtual uint32_t GetNextChar(ByteStringView pString, size_t* pOffset) const;
90   virtual size_t CountChar(ByteStringView pString) const;
91   virtual int AppendChar(char* buf, uint32_t charcode) const;
92   virtual int GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) = 0;
93 #if BUILDFLAG(IS_APPLE)
94   virtual int GlyphFromCharCodeExt(uint32_t charcode);
95 #endif
96   virtual WideString UnicodeFromCharCode(uint32_t charcode) const;
97   virtual uint32_t CharCodeFromUnicode(wchar_t Unicode) const;
98   virtual bool HasFontWidths() const;
99 
GetBaseFontName()100   ByteString GetBaseFontName() const { return m_BaseFontName; }
101   absl::optional<FX_Charset> GetSubstFontCharset() const;
IsEmbedded()102   bool IsEmbedded() const { return IsType3Font() || m_pFontFile != nullptr; }
GetMutableFontDict()103   RetainPtr<CPDF_Dictionary> GetMutableFontDict() { return m_pFontDict; }
GetFontDict()104   RetainPtr<const CPDF_Dictionary> GetFontDict() const { return m_pFontDict; }
GetFontDictObjNum()105   uint32_t GetFontDictObjNum() const { return m_pFontDict->GetObjNum(); }
FontDictIs(const CPDF_Dictionary * pThat)106   bool FontDictIs(const CPDF_Dictionary* pThat) const {
107     return m_pFontDict == pThat;
108   }
ClearFontDict()109   void ClearFontDict() { m_pFontDict = nullptr; }
110   bool IsStandardFont() const;
HasFace()111   bool HasFace() const { return !!m_Font.GetFaceRec(); }
112   void AppendChar(ByteString* str, uint32_t charcode) const;
113 
GetFontBBox()114   const FX_RECT& GetFontBBox() const { return m_FontBBox; }
GetTypeAscent()115   int GetTypeAscent() const { return m_Ascent; }
GetTypeDescent()116   int GetTypeDescent() const { return m_Descent; }
117   int GetStringWidth(ByteStringView pString);
118   uint32_t FallbackFontFromCharcode(uint32_t charcode);
119   int FallbackGlyphFromCharcode(int fallbackFont, uint32_t charcode);
GetFontFlags()120   int GetFontFlags() const { return m_Flags; }
GetItalicAngle()121   int GetItalicAngle() const { return m_ItalicAngle; }
122   int GetFontWeight() const;
123 
124   virtual int GetCharWidthF(uint32_t charcode) = 0;
125   virtual FX_RECT GetCharBBox(uint32_t charcode) = 0;
126 
127   // Can return nullptr for stock Type1 fonts. Always returns non-null for other
128   // font types.
GetDocument()129   CPDF_Document* GetDocument() const { return m_pDocument; }
130 
GetFont()131   CFX_Font* GetFont() { return &m_Font; }
GetFont()132   const CFX_Font* GetFont() const { return &m_Font; }
133 
134   CFX_Font* GetFontFallback(int position);
135 
GetResourceName()136   const ByteString& GetResourceName() const { return m_ResourceName; }
SetResourceName(const ByteString & name)137   void SetResourceName(const ByteString& name) { m_ResourceName = name; }
138 
139  protected:
140   CPDF_Font(CPDF_Document* pDocument, RetainPtr<CPDF_Dictionary> pFontDict);
141 
142   static int TT2PDF(FT_Pos m, FXFT_FaceRec* face);
143 
144   // Commonly used wrappers for UseTTCharmap().
UseTTCharmapMSUnicode(FXFT_FaceRec * face)145   static bool UseTTCharmapMSUnicode(FXFT_FaceRec* face) {
146     return UseTTCharmap(face, 3, 1);
147   }
UseTTCharmapMSSymbol(FXFT_FaceRec * face)148   static bool UseTTCharmapMSSymbol(FXFT_FaceRec* face) {
149     return UseTTCharmap(face, 3, 0);
150   }
UseTTCharmapMacRoman(FXFT_FaceRec * face)151   static bool UseTTCharmapMacRoman(FXFT_FaceRec* face) {
152     return UseTTCharmap(face, 1, 0);
153   }
154   static bool UseTTCharmap(FXFT_FaceRec* face,
155                            int platform_id,
156                            int encoding_id);
157 
158   static const char* GetAdobeCharName(FontEncoding base_encoding,
159                                       const std::vector<ByteString>& charnames,
160                                       uint32_t charcode);
161 
162   virtual bool Load() = 0;
163 
164   void LoadUnicodeMap() const;  // logically const only.
165   void LoadFontDescriptor(const CPDF_Dictionary* pFontDesc);
166   void CheckFontMetrics();
167 
168   UnownedPtr<CPDF_Document> const m_pDocument;
169   ByteString m_ResourceName;  // The resource name for this font.
170   CFX_Font m_Font;
171   std::vector<std::unique_ptr<CFX_Font>> m_FontFallbacks;
172   RetainPtr<CPDF_StreamAcc> m_pFontFile;
173   RetainPtr<CPDF_Dictionary> m_pFontDict;
174   ByteString m_BaseFontName;
175   mutable std::unique_ptr<CPDF_ToUnicodeMap> m_pToUnicodeMap;
176   mutable bool m_bToUnicodeLoaded = false;
177   int m_Flags = 0;
178   int m_StemV = 0;
179   int m_Ascent = 0;
180   int m_Descent = 0;
181   int m_ItalicAngle = 0;
182   FX_RECT m_FontBBox;
183 };
184 
185 #endif  // CORE_FPDFAPI_FONT_CPDF_FONT_H_
186