1 // Copyright 2017 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_FXGE_CFX_FONT_H_
8 #define CORE_FXGE_CFX_FONT_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "build/build_config.h"
14 #include "core/fxcrt/bytestring.h"
15 #include "core/fxcrt/fx_coordinates.h"
16 #include "core/fxcrt/fx_memory_wrappers.h"
17 #include "core/fxge/cfx_face.h"
18 #include "core/fxge/fx_freetype.h"
19 #include "third_party/base/span.h"
20 
21 #if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_
22 #include "core/fxge/fx_font.h"
23 #endif
24 
25 class CFX_GlyphCache;
26 class CFX_GlyphBitmap;
27 class CFX_PathData;
28 class CFX_SubstFont;
29 class IFX_SeekableReadStream;
30 
31 class CFX_Font {
32  public:
33   CFX_Font();
34   ~CFX_Font();
35 
36   // Used when the font name is empty.
37   static const char kUntitledFontName[];
38 
39   static const char kDefaultAnsiFontName[];
40   static const char kUniversalDefaultFontName[];
41   static ByteString GetDefaultFontNameByCharset(uint8_t nCharset);
42   static uint8_t GetCharSetFromUnicode(uint16_t word);
43 
44   void LoadSubst(const ByteString& face_name,
45                  bool bTrueType,
46                  uint32_t flags,
47                  int weight,
48                  int italic_angle,
49                  int CharsetCP,
50                  bool bVertical);
51 
52   bool LoadEmbedded(pdfium::span<const uint8_t> src_span,
53                     bool bForceAsVertical);
GetFace()54   RetainPtr<CFX_Face> GetFace() const { return m_Face; }
GetFaceRec()55   FXFT_FaceRec* GetFaceRec() const {
56     return m_Face ? m_Face->GetRec() : nullptr;
57   }
GetSubstFont()58   CFX_SubstFont* GetSubstFont() const { return m_pSubstFont.get(); }
59   int GetSubstFontItalicAngle() const;
60 
61 #if defined(PDF_ENABLE_XFA)
62   bool LoadFile(const RetainPtr<IFX_SeekableReadStream>& pFile, int nFaceIndex);
63 
64 #if !defined(OS_WIN)
65   void SetFace(RetainPtr<CFX_Face> face);
SetFontSpan(pdfium::span<uint8_t> pSpan)66   void SetFontSpan(pdfium::span<uint8_t> pSpan) { m_FontData = pSpan; }
67   void SetSubstFont(std::unique_ptr<CFX_SubstFont> subst);
68 #endif  // !defined(OS_WIN)
69 #endif  // defined(PDF_ENABLE_XFA)
70 
71   const CFX_GlyphBitmap* LoadGlyphBitmap(uint32_t glyph_index,
72                                          bool bFontStyle,
73                                          const CFX_Matrix& matrix,
74                                          uint32_t dest_width,
75                                          int anti_alias,
76                                          int* pTextFlags) const;
77   const CFX_PathData* LoadGlyphPath(uint32_t glyph_index,
78                                     uint32_t dest_width) const;
79 
80 #if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_
81   CFX_TypeFace* GetDeviceCache() const;
82 #endif
83 
84   uint32_t GetGlyphWidth(uint32_t glyph_index);
85   int GetAscent() const;
86   int GetDescent() const;
87   bool GetGlyphBBox(uint32_t glyph_index, FX_RECT* pBBox);
88   bool IsItalic() const;
89   bool IsBold() const;
90   bool IsFixedWidth() const;
IsVertical()91   bool IsVertical() const { return m_bVertical; }
92   ByteString GetPsName() const;
93   ByteString GetFamilyName() const;
94   ByteString GetFaceName() const;
95   ByteString GetBaseFontName(bool restrict_to_psname) const;
96   bool IsTTFont() const;
97   bool GetBBox(FX_RECT* pBBox);
IsEmbedded()98   bool IsEmbedded() const { return m_bEmbedded; }
GetSubData()99   uint8_t* GetSubData() const { return m_pGsubData.get(); }
SetSubData(uint8_t * data)100   void SetSubData(uint8_t* data) { m_pGsubData.reset(data); }
GetFontSpan()101   pdfium::span<uint8_t> GetFontSpan() const { return m_FontData; }
102   void AdjustMMParams(int glyph_index, int dest_width, int weight) const;
103   CFX_PathData* LoadGlyphPathImpl(uint32_t glyph_index,
104                                   uint32_t dest_width) const;
105 #if defined(OS_MACOSX)
GetPlatformFont()106   void* GetPlatformFont() const { return m_pPlatformFont; }
SetPlatformFont(void * font)107   void SetPlatformFont(void* font) { m_pPlatformFont = font; }
108 #endif
109 
110   static const size_t kAngleSkewArraySize = 30;
111   static const char s_AngleSkew[kAngleSkewArraySize];
112   static const size_t kWeightPowArraySize = 100;
113   static const uint8_t s_WeightPow[kWeightPowArraySize];
114   static const uint8_t s_WeightPow_11[kWeightPowArraySize];
115   static const uint8_t s_WeightPow_SHIFTJIS[kWeightPowArraySize];
116 
117   // This struct should be the same as FPDF_CharsetFontMap.
118   struct CharsetFontMap {
119     int charset;           // Character Set Enum value, see FX_CHARSET_XXX.
120     const char* fontname;  // Name of default font to use with that charset.
121   };
122 
123   /**
124    *    Pointer to the default character set to TT Font name map. The
125    *    map is an array of CharsetFontMap structs, with its end indicated
126    *    by a { -1, NULL } entry.
127    **/
128   static const CharsetFontMap defaultTTFMap[];
129 
130  private:
131   RetainPtr<CFX_GlyphCache> GetOrCreateGlyphCache() const;
132   void ClearGlyphCache();
133 #if defined(OS_MACOSX)
134   void ReleasePlatformResource();
135 #endif
136   ByteString GetFamilyNameOrUntitled() const;
137 
138 #if defined(PDF_ENABLE_XFA)
139   std::unique_ptr<FXFT_StreamRec> m_pOwnedStream;  // Must outlive |m_Face|.
140 #endif
141   mutable RetainPtr<CFX_Face> m_Face;
142   mutable RetainPtr<CFX_GlyphCache> m_GlyphCache;
143   std::unique_ptr<CFX_SubstFont> m_pSubstFont;
144   std::unique_ptr<uint8_t, FxFreeDeleter> m_pGsubData;
145   std::vector<uint8_t, FxAllocAllocator<uint8_t>> m_FontDataAllocation;
146   pdfium::span<uint8_t> m_FontData;
147   bool m_bEmbedded = false;
148   bool m_bVertical = false;
149 #if defined(OS_MACOSX)
150   void* m_pPlatformFont = nullptr;
151 #endif
152 };
153 
154 #endif  // CORE_FXGE_CFX_FONT_H_
155