1 // Copyright 2016 PDFium Authors. All rights reserved.
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 <memory>
11 #include <utility>
12 #include <vector>
13 
14 #include "build/build_config.h"
15 #include "core/fpdfapi/parser/cpdf_dictionary.h"
16 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
17 #include "core/fxcrt/fx_string.h"
18 #include "core/fxcrt/fx_system.h"
19 #include "core/fxcrt/observed_ptr.h"
20 #include "core/fxcrt/retain_ptr.h"
21 #include "core/fxcrt/unowned_ptr.h"
22 #include "core/fxge/cfx_font.h"
23 
24 class CFX_DIBitmap;
25 class CFX_SubstFont;
26 class CPDF_CIDFont;
27 class CPDF_Document;
28 class CPDF_Object;
29 class CPDF_TrueTypeFont;
30 class CPDF_Type1Font;
31 class CPDF_Type3Char;
32 class CPDF_Type3Font;
33 class CPDF_ToUnicodeMap;
34 
35 class CPDF_Font : public Retainable, public Observable {
36  public:
37   // Callback mechanism for Type3 fonts to get pixels from forms.
38   class FormIface {
39    public:
~FormIface()40     virtual ~FormIface() {}
41 
42     virtual void ParseContentForType3Char(CPDF_Type3Char* pChar) = 0;
43     virtual bool HasPageObjects() const = 0;
44     virtual CFX_FloatRect CalcBoundingBox() const = 0;
45     virtual Optional<std::pair<RetainPtr<CFX_DIBitmap>, CFX_Matrix>>
46     GetBitmapAndMatrixFromSoleImageOfForm() const = 0;
47   };
48 
49   // Callback mechanism for Type3 fonts to get new forms from upper layers.
50   class FormFactoryIface {
51    public:
~FormFactoryIface()52     virtual ~FormFactoryIface() {}
53 
54     virtual std::unique_ptr<FormIface> CreateForm(
55         CPDF_Document* pDocument,
56         CPDF_Dictionary* pPageResources,
57         CPDF_Stream* pFormStream) = 0;
58   };
59 
60   static const uint32_t kInvalidCharCode = static_cast<uint32_t>(-1);
61 
62   // |pFactory| only required for Type3 fonts.
63   static RetainPtr<CPDF_Font> Create(CPDF_Document* pDoc,
64                                      CPDF_Dictionary* pFontDict,
65                                      FormFactoryIface* pFactory);
66   static RetainPtr<CPDF_Font> GetStockFont(CPDF_Document* pDoc,
67                                            ByteStringView fontname);
68 
69   ~CPDF_Font() override;
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;
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 defined(OS_MACOSX)
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; }
GetSubstFont()99   CFX_SubstFont* GetSubstFont() const { return m_Font.GetSubstFont(); }
IsEmbedded()100   bool IsEmbedded() const { return IsType3Font() || m_pFontFile != nullptr; }
GetFontDict()101   CPDF_Dictionary* GetFontDict() const { return m_pFontDict.Get(); }
ClearFontDict()102   void ClearFontDict() { m_pFontDict = nullptr; }
103   bool IsStandardFont() const;
HasFace()104   bool HasFace() const { return !!m_Font.GetFaceRec(); }
105   void AppendChar(ByteString* str, uint32_t charcode) const;
106 
GetFontBBox()107   const FX_RECT& GetFontBBox() const { return m_FontBBox; }
GetTypeAscent()108   int GetTypeAscent() const { return m_Ascent; }
GetTypeDescent()109   int GetTypeDescent() const { return m_Descent; }
110   uint32_t GetStringWidth(ByteStringView pString);
111   uint32_t FallbackFontFromCharcode(uint32_t charcode);
112   int FallbackGlyphFromCharcode(int fallbackFont, uint32_t charcode);
GetFontFlags()113   int GetFontFlags() const { return m_Flags; }
114   int GetFontWeight() const;
115 
116   virtual uint32_t GetCharWidthF(uint32_t charcode) = 0;
117   virtual FX_RECT GetCharBBox(uint32_t charcode) = 0;
118 
119   // Can return nullptr for stock Type1 fonts. Always returns non-null for other
120   // font types.
GetDocument()121   CPDF_Document* GetDocument() const { return m_pDocument.Get(); }
122 
GetFont()123   CFX_Font* GetFont() { return &m_Font; }
GetFont()124   const CFX_Font* GetFont() const { return &m_Font; }
125 
126   CFX_Font* GetFontFallback(int position);
127 
128  protected:
129   CPDF_Font(CPDF_Document* pDocument, CPDF_Dictionary* pFontDict);
130 
131   static int TT2PDF(int m, FXFT_FaceRec* face);
132   static bool FT_UseTTCharmap(FXFT_FaceRec* face,
133                               int platform_id,
134                               int encoding_id);
135   static const char* GetAdobeCharName(int iBaseEncoding,
136                                       const std::vector<ByteString>& charnames,
137                                       uint32_t charcode);
138 
139   virtual bool Load() = 0;
140 
141   void LoadUnicodeMap() const;  // logically const only.
142   void LoadFontDescriptor(const CPDF_Dictionary* pFontDesc);
143   void CheckFontMetrics();
144 
145   UnownedPtr<CPDF_Document> const m_pDocument;
146   CFX_Font m_Font;
147   std::vector<std::unique_ptr<CFX_Font>> m_FontFallbacks;
148   RetainPtr<CPDF_StreamAcc> m_pFontFile;
149   RetainPtr<CPDF_Dictionary> m_pFontDict;
150   ByteString m_BaseFontName;
151   mutable std::unique_ptr<CPDF_ToUnicodeMap> m_pToUnicodeMap;
152   mutable bool m_bToUnicodeLoaded = false;
153   int m_Flags = 0;
154   int m_StemV = 0;
155   int m_Ascent = 0;
156   int m_Descent = 0;
157   int m_ItalicAngle = 0;
158   FX_RECT m_FontBBox;
159 };
160 
161 #endif  // CORE_FPDFAPI_FONT_CPDF_FONT_H_
162