• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <vector>
12 
13 #include "core/fpdfapi/font/cpdf_tounicodemap.h"
14 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
15 #include "core/fxcrt/fx_string.h"
16 #include "core/fxcrt/fx_system.h"
17 #include "core/fxcrt/unowned_ptr.h"
18 #include "core/fxge/cfx_font.h"
19 #include "core/fxge/fx_font.h"
20 
21 class CFX_SubstFont;
22 class CPDF_CIDFont;
23 class CPDF_Dictionary;
24 class CPDF_Document;
25 class CPDF_Object;
26 class CPDF_TrueTypeFont;
27 class CPDF_Type1Font;
28 class CPDF_Type3Font;
29 class CPDF_ToUnicodeMap;
30 
31 class CPDF_Font {
32  public:
33   static std::unique_ptr<CPDF_Font> Create(CPDF_Document* pDoc,
34                                            CPDF_Dictionary* pFontDict);
35   static CPDF_Font* GetStockFont(CPDF_Document* pDoc,
36                                  const ByteStringView& fontname);
37   static const uint32_t kInvalidCharCode = static_cast<uint32_t>(-1);
38 
39   virtual ~CPDF_Font();
40 
41   virtual bool IsType1Font() const;
42   virtual bool IsTrueTypeFont() const;
43   virtual bool IsType3Font() const;
44   virtual bool IsCIDFont() const;
45   virtual const CPDF_Type1Font* AsType1Font() const;
46   virtual CPDF_Type1Font* AsType1Font();
47   virtual const CPDF_TrueTypeFont* AsTrueTypeFont() const;
48   virtual CPDF_TrueTypeFont* AsTrueTypeFont();
49   virtual const CPDF_Type3Font* AsType3Font() const;
50   virtual CPDF_Type3Font* AsType3Font();
51   virtual const CPDF_CIDFont* AsCIDFont() const;
52   virtual CPDF_CIDFont* AsCIDFont();
53 
54   virtual bool IsVertWriting() const;
55   virtual bool IsUnicodeCompatible() const;
56   virtual uint32_t GetNextChar(const char* pString,
57                                int nStrLen,
58                                int& offset) const;
59   virtual int CountChar(const char* pString, int size) const;
60   virtual int AppendChar(char* buf, uint32_t charcode) const;
61   virtual int GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) = 0;
62   virtual int GlyphFromCharCodeExt(uint32_t charcode);
63   virtual WideString UnicodeFromCharCode(uint32_t charcode) const;
64   virtual uint32_t CharCodeFromUnicode(wchar_t Unicode) const;
65   virtual bool HasFontWidths() const;
66 
GetBaseFont()67   const ByteString& GetBaseFont() const { return m_BaseFont; }
GetSubstFont()68   CFX_SubstFont* GetSubstFont() const { return m_Font.GetSubstFont(); }
IsEmbedded()69   bool IsEmbedded() const { return IsType3Font() || m_pFontFile != nullptr; }
GetFontDict()70   CPDF_Dictionary* GetFontDict() const { return m_pFontDict; }
71   bool IsStandardFont() const;
GetFace()72   FXFT_Face GetFace() const { return m_Font.GetFace(); }
73   void AppendChar(ByteString* str, uint32_t charcode) const;
74 
GetFontBBox(FX_RECT & rect)75   void GetFontBBox(FX_RECT& rect) const { rect = m_FontBBox; }
GetTypeAscent()76   int GetTypeAscent() const { return m_Ascent; }
GetTypeDescent()77   int GetTypeDescent() const { return m_Descent; }
78   int GetStringWidth(const char* pString, int size);
79   uint32_t FallbackFontFromCharcode(uint32_t charcode);
80   int FallbackGlyphFromCharcode(int fallbackFont, uint32_t charcode);
81 
82   virtual int GetCharWidthF(uint32_t charcode) = 0;
83   virtual FX_RECT GetCharBBox(uint32_t charcode) = 0;
84 
GetDocument()85   CPDF_Document* GetDocument() const { return m_pDocument.Get(); }
GetFont()86   CFX_Font* GetFont() { return &m_Font; }
GetFont()87   const CFX_Font* GetFont() const { return &m_Font; }
88   CFX_Font* GetFontFallback(int position);
89 
90  protected:
91   CPDF_Font();
92 
93   static int TT2PDF(int m, FXFT_Face face);
94   static bool FT_UseTTCharmap(FXFT_Face face, int platform_id, int encoding_id);
95 
96   virtual bool Load() = 0;
97 
98   void LoadUnicodeMap() const;  // logically const only.
99   void LoadPDFEncoding(CPDF_Object* pEncoding,
100                        int& iBaseEncoding,
101                        std::vector<ByteString>* pCharNames,
102                        bool bEmbedded,
103                        bool bTrueType);
104   void LoadFontDescriptor(CPDF_Dictionary* pDict);
105   void CheckFontMetrics();
106 
107   const char* GetAdobeCharName(int iBaseEncoding,
108                                const std::vector<ByteString>& charnames,
109                                int charcode);
110 
111   UnownedPtr<CPDF_Document> m_pDocument;
112   CFX_Font m_Font;
113   std::vector<std::unique_ptr<CFX_Font>> m_FontFallbacks;
114   ByteString m_BaseFont;
115   RetainPtr<CPDF_StreamAcc> m_pFontFile;
116   CPDF_Dictionary* m_pFontDict;
117   mutable std::unique_ptr<CPDF_ToUnicodeMap> m_pToUnicodeMap;
118   mutable bool m_bToUnicodeLoaded;
119   int m_Flags;
120   FX_RECT m_FontBBox;
121   int m_StemV;
122   int m_Ascent;
123   int m_Descent;
124   int m_ItalicAngle;
125 };
126 
127 #endif  // CORE_FPDFAPI_FONT_CPDF_FONT_H_
128