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_FPDF_FONT_INCLUDE_CPDF_FONT_H_
8 #define CORE_FPDFAPI_FPDF_FONT_INCLUDE_CPDF_FONT_H_
9 
10 #include <memory>
11 
12 #include "core/fxcrt/include/fx_string.h"
13 #include "core/fxcrt/include/fx_system.h"
14 #include "core/fxge/include/fx_font.h"
15 
16 #define PDFFONT_FIXEDPITCH 1
17 #define PDFFONT_SERIF 2
18 #define PDFFONT_SYMBOLIC 4
19 #define PDFFONT_SCRIPT 8
20 #define PDFFONT_NONSYMBOLIC 32
21 #define PDFFONT_ITALIC 64
22 #define PDFFONT_ALLCAP 0x10000
23 #define PDFFONT_SMALLCAP 0x20000
24 #define PDFFONT_FORCEBOLD 0x40000
25 #define PDFFONT_USEEXTERNATTR 0x80000
26 
27 class CFX_SubstFont;
28 class CPDF_CIDFont;
29 class CPDF_Dictionary;
30 class CPDF_Document;
31 class CPDF_Object;
32 class CPDF_StreamAcc;
33 class CPDF_TrueTypeFont;
34 class CPDF_Type1Font;
35 class CPDF_Type3Font;
36 class CPDF_ToUnicodeMap;
37 
38 class CPDF_Font {
39  public:
40   static CPDF_Font* CreateFontF(CPDF_Document* pDoc,
41                                 CPDF_Dictionary* pFontDict);
42   static CPDF_Font* GetStockFont(CPDF_Document* pDoc,
43                                  const CFX_ByteStringC& fontname);
44   static const uint32_t kInvalidCharCode = static_cast<uint32_t>(-1);
45 
46   virtual ~CPDF_Font();
47 
48   virtual bool IsType1Font() const;
49   virtual bool IsTrueTypeFont() const;
50   virtual bool IsType3Font() const;
51   virtual bool IsCIDFont() const;
52   virtual const CPDF_Type1Font* AsType1Font() const;
53   virtual CPDF_Type1Font* AsType1Font();
54   virtual const CPDF_TrueTypeFont* AsTrueTypeFont() const;
55   virtual CPDF_TrueTypeFont* AsTrueTypeFont();
56   virtual const CPDF_Type3Font* AsType3Font() const;
57   virtual CPDF_Type3Font* AsType3Font();
58   virtual const CPDF_CIDFont* AsCIDFont() const;
59   virtual CPDF_CIDFont* AsCIDFont();
60 
61   virtual FX_BOOL IsVertWriting() const;
62   virtual FX_BOOL IsUnicodeCompatible() const;
63   virtual uint32_t GetNextChar(const FX_CHAR* pString,
64                                int nStrLen,
65                                int& offset) const;
66   virtual int CountChar(const FX_CHAR* pString, int size) const;
67   virtual int AppendChar(FX_CHAR* buf, uint32_t charcode) const;
68   virtual int GetCharSize(uint32_t charcode) const;
69   virtual int GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) = 0;
70   virtual int GlyphFromCharCodeExt(uint32_t charcode);
71   virtual CFX_WideString UnicodeFromCharCode(uint32_t charcode) const;
72   virtual uint32_t CharCodeFromUnicode(FX_WCHAR Unicode) const;
73 
GetBaseFont()74   const CFX_ByteString& GetBaseFont() const { return m_BaseFont; }
GetSubstFont()75   CFX_SubstFont* GetSubstFont() const { return m_Font.GetSubstFont(); }
GetFlags()76   uint32_t GetFlags() const { return m_Flags; }
IsEmbedded()77   FX_BOOL IsEmbedded() const { return IsType3Font() || m_pFontFile != nullptr; }
GetFontFile()78   CPDF_StreamAcc* GetFontFile() const { return m_pFontFile; }
GetFontDict()79   CPDF_Dictionary* GetFontDict() const { return m_pFontDict; }
80   FX_BOOL IsStandardFont() const;
GetFace()81   FXFT_Face GetFace() const { return m_Font.GetFace(); }
82   void AppendChar(CFX_ByteString& str, uint32_t charcode) const;
83 
GetFontBBox(FX_RECT & rect)84   void GetFontBBox(FX_RECT& rect) const { rect = m_FontBBox; }
GetTypeAscent()85   int GetTypeAscent() const { return m_Ascent; }
GetTypeDescent()86   int GetTypeDescent() const { return m_Descent; }
GetItalicAngle()87   int GetItalicAngle() const { return m_ItalicAngle; }
GetStemV()88   int GetStemV() const { return m_StemV; }
89   int GetStringWidth(const FX_CHAR* pString, int size);
90 
91   virtual int GetCharWidthF(uint32_t charcode, int level = 0) = 0;
92   virtual FX_RECT GetCharBBox(uint32_t charcode, int level = 0) = 0;
93 
94   CPDF_Document* m_pDocument;
95   CFX_Font m_Font;
96 
97  protected:
98   CPDF_Font();
99 
100   virtual FX_BOOL Load() = 0;
101 
102   FX_BOOL Initialize();
103   void LoadUnicodeMap() const;  // logically const only.
104   void LoadPDFEncoding(CPDF_Object* pEncoding,
105                        int& iBaseEncoding,
106                        CFX_ByteString*& pCharNames,
107                        FX_BOOL bEmbedded,
108                        FX_BOOL bTrueType);
109   void LoadFontDescriptor(CPDF_Dictionary* pDict);
110   void CheckFontMetrics();
111 
112   const FX_CHAR* GetAdobeCharName(int iBaseEncoding,
113                                   const CFX_ByteString* pCharNames,
114                                   int charcode);
115 
116   CFX_ByteString m_BaseFont;
117   CPDF_StreamAcc* m_pFontFile;
118   CPDF_Dictionary* m_pFontDict;
119   mutable std::unique_ptr<CPDF_ToUnicodeMap> m_pToUnicodeMap;
120   mutable FX_BOOL m_bToUnicodeLoaded;
121   int m_Flags;
122   FX_RECT m_FontBBox;
123   int m_StemV;
124   int m_Ascent;
125   int m_Descent;
126   int m_ItalicAngle;
127 };
128 
129 #endif  // CORE_FPDFAPI_FPDF_FONT_INCLUDE_CPDF_FONT_H_
130