Home | History | Annotate | Download | only in fxge
      1 // Copyright 2014 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_FX_FONT_H_
      8 #define CORE_FXGE_FX_FONT_H_
      9 
     10 #include <memory>
     11 #include <utility>
     12 #include <vector>
     13 
     14 #include "core/fxcrt/fx_system.h"
     15 #include "core/fxge/cfx_substfont.h"
     16 #include "core/fxge/fx_dib.h"
     17 #include "core/fxge/fx_freetype.h"
     18 
     19 typedef struct FT_FaceRec_* FXFT_Face;
     20 typedef void* FXFT_Library;
     21 
     22 class CFX_FaceCache;
     23 class CFX_GlyphBitmap;
     24 class CFX_PathData;
     25 class CFX_SizeGlyphCache;
     26 
     27 #if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_
     28 class SkTypeface;
     29 
     30 using CFX_TypeFace = SkTypeface;
     31 #endif
     32 
     33 /* Character sets for the font */
     34 #define FXFONT_ANSI_CHARSET 0
     35 #define FXFONT_DEFAULT_CHARSET 1
     36 #define FXFONT_SYMBOL_CHARSET 2
     37 #define FXFONT_SHIFTJIS_CHARSET 128
     38 #define FXFONT_HANGUL_CHARSET 129
     39 #define FXFONT_GB2312_CHARSET 134
     40 #define FXFONT_CHINESEBIG5_CHARSET 136
     41 #define FXFONT_THAI_CHARSET 222
     42 #define FXFONT_EASTEUROPE_CHARSET 238
     43 #define FXFONT_RUSSIAN_CHARSET 204
     44 #define FXFONT_GREEK_CHARSET 161
     45 #define FXFONT_TURKISH_CHARSET 162
     46 #define FXFONT_HEBREW_CHARSET 177
     47 #define FXFONT_ARABIC_CHARSET 178
     48 #define FXFONT_BALTIC_CHARSET 186
     49 #define FXFONT_JOHAB_CHARSET 130
     50 #define FXFONT_VIETNAMESE_CHARSET 163
     51 
     52 /* Font pitch and family flags */
     53 #define FXFONT_FF_FIXEDPITCH 1
     54 #define FXFONT_FF_ROMAN (1 << 4)
     55 #define FXFONT_FF_SCRIPT (4 << 4)
     56 
     57 /* Typical weight values */
     58 #define FXFONT_FW_NORMAL 400
     59 #define FXFONT_FW_BOLD 700
     60 
     61 /* Font styles as defined in PDF 1.7 Table 5.20 */
     62 #define FXFONT_FIXED_PITCH (1 << 0)
     63 #define FXFONT_SERIF (1 << 1)
     64 #define FXFONT_SYMBOLIC (1 << 2)
     65 #define FXFONT_SCRIPT (1 << 3)
     66 #define FXFONT_NONSYMBOLIC (1 << 5)
     67 #define FXFONT_ITALIC (1 << 6)
     68 #define FXFONT_ALLCAP (1 << 16)
     69 #define FXFONT_SMALLCAP (1 << 17)
     70 #define FXFONT_BOLD (1 << 18)
     71 
     72 /* Other font flags */
     73 #define FXFONT_USEEXTERNATTR 0x80000
     74 #define FXFONT_CIDFONT 0x100000
     75 #ifdef PDF_ENABLE_XFA
     76 #define FXFONT_EXACTMATCH 0x80000000
     77 #endif  // PDF_ENABLE_XFA
     78 
     79 #define CHARSET_FLAG_ANSI 1
     80 #define CHARSET_FLAG_SYMBOL 2
     81 #define CHARSET_FLAG_SHIFTJIS 4
     82 #define CHARSET_FLAG_BIG5 8
     83 #define CHARSET_FLAG_GB 16
     84 #define CHARSET_FLAG_KOREAN 32
     85 
     86 #define GET_TT_SHORT(w) (uint16_t)(((w)[0] << 8) | (w)[1])
     87 #define GET_TT_LONG(w) \
     88   (uint32_t)(((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3])
     89 
     90 // Sets the given transform on the font, and resets it to the identity when it
     91 // goes out of scope.
     92 class ScopedFontTransform {
     93  public:
     94   ScopedFontTransform(FT_Face face, FXFT_Matrix* matrix);
     95   ~ScopedFontTransform();
     96 
     97  private:
     98   FT_Face m_Face;
     99 };
    100 
    101 class CFX_Font {
    102  public:
    103   CFX_Font();
    104   ~CFX_Font();
    105 
    106   void LoadSubst(const CFX_ByteString& face_name,
    107                  bool bTrueType,
    108                  uint32_t flags,
    109                  int weight,
    110                  int italic_angle,
    111                  int CharsetCP,
    112                  bool bVertical);
    113 
    114   bool LoadEmbedded(const uint8_t* data, uint32_t size);
    115   FXFT_Face GetFace() const { return m_Face; }
    116   CFX_SubstFont* GetSubstFont() const { return m_pSubstFont.get(); }
    117 
    118 #ifdef PDF_ENABLE_XFA
    119   bool LoadFile(const CFX_RetainPtr<IFX_SeekableReadStream>& pFile,
    120                 int nFaceIndex = 0,
    121                 int* pFaceCount = nullptr);
    122 
    123   bool LoadClone(const CFX_Font* pFont);
    124   void SetFace(FXFT_Face face);
    125   void SetSubstFont(std::unique_ptr<CFX_SubstFont> subst) {
    126     m_pSubstFont = std::move(subst);
    127   }
    128 #endif  // PDF_ENABLE_XFA
    129 
    130   const CFX_GlyphBitmap* LoadGlyphBitmap(uint32_t glyph_index,
    131                                          bool bFontStyle,
    132                                          const CFX_Matrix* pMatrix,
    133                                          int dest_width,
    134                                          int anti_alias,
    135                                          int& text_flags) const;
    136   const CFX_PathData* LoadGlyphPath(uint32_t glyph_index, int dest_width) const;
    137 
    138 #if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_
    139   CFX_TypeFace* GetDeviceCache() const;
    140 #endif
    141 
    142   int GetGlyphWidth(uint32_t glyph_index);
    143   int GetAscent() const;
    144   int GetDescent() const;
    145   bool GetGlyphBBox(uint32_t glyph_index, FX_RECT& bbox);
    146   bool IsItalic() const;
    147   bool IsBold() const;
    148   bool IsFixedWidth() const;
    149   bool IsVertical() const { return m_bVertical; }
    150   CFX_ByteString GetPsName() const;
    151   CFX_ByteString GetFamilyName() const;
    152   CFX_ByteString GetFaceName() const;
    153   bool IsTTFont() const;
    154   bool GetBBox(FX_RECT& bbox);
    155   int GetHeight() const;
    156   int GetULPos() const;
    157   int GetULthickness() const;
    158   int GetMaxAdvanceWidth() const;
    159   bool IsEmbedded() const { return m_bEmbedded; }
    160   uint8_t* GetSubData() const { return m_pGsubData; }
    161   void SetSubData(uint8_t* data) { m_pGsubData = data; }
    162 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
    163   void* GetPlatformFont() const { return m_pPlatformFont; }
    164   void SetPlatformFont(void* font) { m_pPlatformFont = font; }
    165 #endif
    166   uint8_t* GetFontData() const { return m_pFontData; }
    167   uint32_t GetSize() const { return m_dwSize; }
    168   void AdjustMMParams(int glyph_index, int width, int weight) const;
    169 
    170   static const size_t kAngleSkewArraySize = 30;
    171   static const char s_AngleSkew[kAngleSkewArraySize];
    172   static const size_t kWeightPowArraySize = 100;
    173   static const uint8_t s_WeightPow[kWeightPowArraySize];
    174   static const uint8_t s_WeightPow_11[kWeightPowArraySize];
    175   static const uint8_t s_WeightPow_SHIFTJIS[kWeightPowArraySize];
    176 
    177 #ifdef PDF_ENABLE_XFA
    178  protected:
    179   bool m_bShallowCopy;
    180   FXFT_StreamRec* m_pOwnedStream;
    181 #endif  // PDF_ENABLE_XFA
    182 
    183  private:
    184   friend class CFX_FaceCache;
    185   CFX_PathData* LoadGlyphPathImpl(uint32_t glyph_index,
    186                                   int dest_width = 0) const;
    187   CFX_FaceCache* GetFaceCache() const;
    188   void ReleasePlatformResource();
    189   void DeleteFace();
    190   void ClearFaceCache();
    191 
    192   FXFT_Face m_Face;
    193   mutable CFX_FaceCache* m_FaceCache;  // not owned.
    194   std::unique_ptr<CFX_SubstFont> m_pSubstFont;
    195   std::vector<uint8_t> m_pFontDataAllocation;
    196   uint8_t* m_pFontData;
    197   uint8_t* m_pGsubData;
    198   uint32_t m_dwSize;
    199 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
    200   void* m_pPlatformFont;
    201 #endif
    202   bool m_bEmbedded;
    203   bool m_bVertical;
    204 };
    205 
    206 class CFX_FontFaceInfo {
    207  public:
    208   CFX_FontFaceInfo(CFX_ByteString filePath,
    209                    CFX_ByteString faceName,
    210                    CFX_ByteString fontTables,
    211                    uint32_t fontOffset,
    212                    uint32_t fileSize);
    213 
    214   const CFX_ByteString m_FilePath;
    215   const CFX_ByteString m_FaceName;
    216   const CFX_ByteString m_FontTables;
    217   const uint32_t m_FontOffset;
    218   const uint32_t m_FileSize;
    219   uint32_t m_Styles;
    220   uint32_t m_Charsets;
    221 };
    222 
    223 class CFX_GlyphBitmap {
    224  public:
    225   int m_Top;
    226   int m_Left;
    227   CFX_DIBitmap m_Bitmap;
    228 };
    229 
    230 class FXTEXT_GLYPHPOS {
    231  public:
    232   FXTEXT_GLYPHPOS();
    233   FXTEXT_GLYPHPOS(const FXTEXT_GLYPHPOS&);
    234   ~FXTEXT_GLYPHPOS();
    235 
    236   const CFX_GlyphBitmap* m_pGlyph;
    237   CFX_Point m_Origin;
    238   CFX_PointF m_fOrigin;
    239 };
    240 
    241 FX_RECT FXGE_GetGlyphsBBox(const std::vector<FXTEXT_GLYPHPOS>& glyphs,
    242                            int anti_alias,
    243                            FX_FLOAT retinaScaleX = 1.0f,
    244                            FX_FLOAT retinaScaleY = 1.0f);
    245 
    246 CFX_ByteString GetNameFromTT(const uint8_t* name_table,
    247                              uint32_t name_table_size,
    248                              uint32_t name);
    249 
    250 int PDF_GetStandardFontName(CFX_ByteString* name);
    251 
    252 #endif  // CORE_FXGE_FX_FONT_H_
    253