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