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_INCLUDE_FXGE_FX_FONT_H_
      8 #define CORE_INCLUDE_FXGE_FX_FONT_H_
      9 
     10 #include "../fxcrt/fx_ext.h"
     11 #include "fx_dib.h"
     12 
     13 typedef struct FT_FaceRec_* FXFT_Face;
     14 typedef void* FXFT_Library;
     15 class IFX_FontEncoding;
     16 class CFX_PathData;
     17 class CFX_SubstFont;
     18 class CFX_FaceCache;
     19 class IFX_FontMapper;
     20 class CFX_FontMapper;
     21 class IFX_SystemFontInfo;
     22 class CFontFileFaceInfo;
     23 #define FXFONT_FIXED_PITCH		0x01
     24 #define FXFONT_SERIF			0x02
     25 #define FXFONT_SYMBOLIC			0x04
     26 #define FXFONT_SCRIPT			0x08
     27 #define FXFONT_ITALIC			0x40
     28 #define FXFONT_BOLD				0x40000
     29 #define FXFONT_USEEXTERNATTR	0x80000
     30 #define FXFONT_CIDFONT			0x100000
     31 #define FXFONT_ANSI_CHARSET		0
     32 #define FXFONT_DEFAULT_CHARSET	1
     33 #define FXFONT_SYMBOL_CHARSET	2
     34 #define FXFONT_SHIFTJIS_CHARSET	128
     35 #define FXFONT_HANGEUL_CHARSET	129
     36 #define FXFONT_GB2312_CHARSET	134
     37 #define FXFONT_CHINESEBIG5_CHARSET	136
     38 #define FXFONT_THAI_CHARSET		222
     39 #define FXFONT_EASTEUROPE_CHARSET	238
     40 #define FXFONT_RUSSIAN_CHARSET	204
     41 #define FXFONT_GREEK_CHARSET	161
     42 #define FXFONT_TURKISH_CHARSET	162
     43 #define FXFONT_HEBREW_CHARSET	177
     44 #define FXFONT_ARABIC_CHARSET	178
     45 #define FXFONT_BALTIC_CHARSET	186
     46 #define FXFONT_FF_FIXEDPITCH	1
     47 #define FXFONT_FF_ROMAN			(1<<4)
     48 #define FXFONT_FF_SCRIPT		(4<<4)
     49 #define FXFONT_FW_NORMAL		400
     50 #define FXFONT_FW_BOLD			700
     51 class CFX_Font
     52 {
     53 public:
     54     CFX_Font();
     55     ~CFX_Font();
     56 
     57     FX_BOOL					LoadSubst(const CFX_ByteString& face_name, FX_BOOL bTrueType, FX_DWORD flags,
     58                                       int weight, int italic_angle, int CharsetCP, FX_BOOL bVertical = FALSE);
     59 
     60     FX_BOOL					LoadEmbedded(FX_LPCBYTE data, FX_DWORD size);
     61 
     62     FX_BOOL					LoadFile(IFX_FileRead* pFile);
     63 
     64     FXFT_Face				GetFace() const
     65     {
     66         return m_Face;
     67     }
     68 
     69 
     70     const CFX_SubstFont*	GetSubstFont() const
     71     {
     72         return m_pSubstFont;
     73     }
     74 
     75     CFX_PathData*			LoadGlyphPath(FX_DWORD glyph_index, int dest_width = 0);
     76 
     77     int						GetGlyphWidth(FX_DWORD glyph_index);
     78 
     79     int						GetAscent() const;
     80 
     81     int						GetDescent() const;
     82 
     83     FX_BOOL                 GetGlyphBBox(FX_DWORD glyph_index, FX_RECT &bbox);
     84 
     85     FX_BOOL                 IsItalic();
     86 
     87     FX_BOOL                 IsBold();
     88 
     89     FX_BOOL                 IsFixedWidth();
     90 
     91     FX_BOOL					IsVertical() const
     92     {
     93         return m_bVertical;
     94     }
     95 
     96     CFX_WideString          GetPsName() const;
     97 
     98 
     99     CFX_ByteString          GetFamilyName() const;
    100 
    101     CFX_ByteString          GetFaceName() const;
    102 
    103 
    104     FX_BOOL                 IsTTFont();
    105 
    106     FX_BOOL                 GetBBox(FX_RECT &bbox);
    107 
    108     int                     GetHeight();
    109 
    110     int                     GetULPos();
    111 
    112     int                     GetULthickness();
    113 
    114     int                     GetMaxAdvanceWidth();
    115 
    116     FXFT_Face				m_Face;
    117 
    118     CFX_SubstFont*			m_pSubstFont;
    119     FX_BOOL					IsEmbedded()
    120     {
    121         return m_bEmbedded;
    122     }
    123 
    124     void					AdjustMMParams(int glyph_index, int width, int weight);
    125     FX_LPBYTE				m_pFontDataAllocation;
    126     FX_LPBYTE               m_pFontData;
    127     FX_LPBYTE				m_pGsubData;
    128     FX_DWORD                m_dwSize;
    129     CFX_BinaryBuf           m_OtfFontData;
    130     void*                   m_hHandle;
    131     void*                   m_pPlatformFont;
    132     void*                   m_pPlatformFontCollection;
    133     void*                   m_pDwFont;
    134     FX_BOOL                 m_bDwLoaded;
    135     void                    ReleasePlatformResource();
    136 
    137     void					DeleteFace();
    138 protected:
    139 
    140     FX_BOOL					m_bEmbedded;
    141     FX_BOOL					m_bVertical;
    142     void*					m_pOwnedStream;
    143 };
    144 #define ENCODING_INTERNAL		0
    145 #define ENCODING_UNICODE		1
    146 class IFX_FontEncoding
    147 {
    148 public:
    149     virtual ~IFX_FontEncoding() {}
    150 
    151     virtual FX_DWORD		GlyphFromCharCode(FX_DWORD charcode) = 0;
    152 
    153     virtual CFX_WideString	UnicodeFromCharCode(FX_DWORD charcode) const = 0;
    154 
    155     virtual FX_DWORD		CharCodeFromUnicode(FX_WCHAR Unicode) const = 0;
    156 };
    157 IFX_FontEncoding* FXGE_CreateUnicodeEncoding(CFX_Font* pFont);
    158 #define FXFONT_SUBST_MM				0x01
    159 #define FXFONT_SUBST_GLYPHPATH		0x04
    160 #define FXFONT_SUBST_CLEARTYPE		0x08
    161 #define FXFONT_SUBST_TRANSFORM		0x10
    162 #define FXFONT_SUBST_NONSYMBOL		0x20
    163 #define FXFONT_SUBST_EXACT			0x40
    164 #define FXFONT_SUBST_STANDARD		0x80
    165 class CFX_SubstFont
    166 {
    167 public:
    168 
    169     CFX_SubstFont();
    170 
    171     FX_LPVOID				m_ExtHandle;
    172 
    173     CFX_ByteString			m_Family;
    174 
    175     int						m_Charset;
    176 
    177     FX_DWORD				m_SubstFlags;
    178 
    179     int						m_Weight;
    180 
    181     int						m_ItalicAngle;
    182 
    183     FX_BOOL					m_bSubstOfCJK;
    184 
    185     int						m_WeightCJK;
    186 
    187     FX_BOOL					m_bItlicCJK;
    188 };
    189 #define FX_FONT_FLAG_SERIF              0x01
    190 #define FX_FONT_FLAG_FIXEDPITCH			0x02
    191 #define FX_FONT_FLAG_ITALIC				0x04
    192 #define FX_FONT_FLAG_BOLD				0x08
    193 #define FX_FONT_FLAG_SYMBOLIC_SYMBOL	0x10
    194 #define FX_FONT_FLAG_SYMBOLIC_DINGBATS	0x20
    195 #define FX_FONT_FLAG_MULTIPLEMASTER		0x40
    196 typedef struct {
    197     FX_LPCBYTE	m_pFontData;
    198     FX_DWORD	m_dwSize;
    199 } FoxitFonts;
    200 class CFX_FontMgr
    201 {
    202 public:
    203     CFX_FontMgr();
    204     ~CFX_FontMgr();
    205     void			InitFTLibrary();
    206     FXFT_Face		GetCachedFace(const CFX_ByteString& face_name,
    207                                   int weight, FX_BOOL bItalic, FX_LPBYTE& pFontData);
    208     FXFT_Face		AddCachedFace(const CFX_ByteString& face_name,
    209                                   int weight, FX_BOOL bItalic, FX_LPBYTE pData, FX_DWORD size, int face_index);
    210     FXFT_Face		GetCachedTTCFace(int ttc_size, FX_DWORD checksum,
    211                                      int font_offset, FX_LPBYTE& pFontData);
    212     FXFT_Face		AddCachedTTCFace(int ttc_size, FX_DWORD checksum,
    213                                      FX_LPBYTE pData, FX_DWORD size, int font_offset);
    214     FXFT_Face		GetFileFace(FX_LPCSTR filename, int face_index);
    215     FXFT_Face		GetFixedFace(FX_LPCBYTE pData, FX_DWORD size, int face_index);
    216     void			ReleaseFace(FXFT_Face face);
    217     void			SetSystemFontInfo(IFX_SystemFontInfo* pFontInfo);
    218     FXFT_Face		FindSubstFont(const CFX_ByteString& face_name, FX_BOOL bTrueType, FX_DWORD flags,
    219                                   int weight, int italic_angle, int CharsetCP, CFX_SubstFont* pSubstFont);
    220 
    221     void			FreeCache();
    222 
    223     FX_BOOL			GetStandardFont(FX_LPCBYTE& pFontData, FX_DWORD& size, int index);
    224     CFX_FontMapper*	m_pBuiltinMapper;
    225     IFX_FontMapper*	m_pExtMapper;
    226     CFX_MapByteStringToPtr	m_FaceMap;
    227     FXFT_Library	m_FTLibrary;
    228     FoxitFonts m_ExternalFonts[16];
    229 };
    230 class IFX_FontMapper
    231 {
    232 public:
    233 
    234     virtual ~IFX_FontMapper() {}
    235 
    236     virtual FXFT_Face	FindSubstFont(const CFX_ByteString& face_name, FX_BOOL bTrueType, FX_DWORD flags,
    237                                       int weight, int italic_angle, int CharsetCP, CFX_SubstFont* pSubstFont) = 0;
    238 
    239     CFX_FontMgr*		m_pFontMgr;
    240 };
    241 class IFX_FontEnumerator
    242 {
    243 public:
    244     virtual ~IFX_FontEnumerator() { }
    245 
    246     virtual void		HitFont() = 0;
    247 
    248     virtual void		Finish() = 0;
    249 };
    250 class IFX_AdditionalFontEnum
    251 {
    252 public:
    253     virtual ~IFX_AdditionalFontEnum() { }
    254     virtual int  CountFiles() = 0;
    255     virtual IFX_FileStream* GetFontFile(int index) = 0;
    256 };
    257 class CFX_FontMapper : public IFX_FontMapper
    258 {
    259 public:
    260     CFX_FontMapper();
    261     virtual ~CFX_FontMapper();
    262     void				SetSystemFontInfo(IFX_SystemFontInfo* pFontInfo);
    263     IFX_SystemFontInfo*	GetSystemFontInfo()
    264     {
    265         return m_pFontInfo;
    266     }
    267     void				AddInstalledFont(const CFX_ByteString& name, int charset);
    268     void				LoadInstalledFonts();
    269     CFX_ByteStringArray	m_InstalledTTFonts;
    270     void				SetFontEnumerator(IFX_FontEnumerator* pFontEnumerator)
    271     {
    272         m_pFontEnumerator = pFontEnumerator;
    273     }
    274     IFX_FontEnumerator*	GetFontEnumerator() const
    275     {
    276         return m_pFontEnumerator;
    277     }
    278     virtual FXFT_Face	FindSubstFont(const CFX_ByteString& face_name, FX_BOOL bTrueType, FX_DWORD flags,
    279                                       int weight, int italic_angle, int CharsetCP, CFX_SubstFont* pSubstFont);
    280 private:
    281     CFX_ByteString		GetPSNameFromTT(void* hFont);
    282     CFX_ByteString		MatchInstalledFonts(const CFX_ByteString& norm_name);
    283     FXFT_Face			UseInternalSubst(CFX_SubstFont* pSubstFont, int iBaseFont, int italic_angle, int weight, int picthfamily);
    284 
    285     FX_BOOL				m_bListLoaded;
    286     FXFT_Face			m_MMFaces[2];
    287     CFX_ByteString		m_LastFamily;
    288     CFX_DWordArray		m_CharsetArray;
    289     CFX_ByteStringArray	m_FaceArray;
    290     IFX_SystemFontInfo*	m_pFontInfo;
    291     FXFT_Face			m_FoxitFaces[14];
    292     IFX_FontEnumerator*		m_pFontEnumerator;
    293 };
    294 class IFX_SystemFontInfo
    295 {
    296 public:
    297     static IFX_SystemFontInfo*	CreateDefault();
    298     virtual void		Release() = 0;
    299 
    300     virtual	FX_BOOL		EnumFontList(CFX_FontMapper* pMapper) = 0;
    301     virtual void*		MapFont(int weight, FX_BOOL bItalic, int charset, int pitch_family, FX_LPCSTR face, FX_BOOL& bExact) = 0;
    302     virtual void*		GetFont(FX_LPCSTR face) = 0;
    303     virtual FX_DWORD	GetFontData(void* hFont, FX_DWORD table, FX_LPBYTE buffer, FX_DWORD size) = 0;
    304     virtual FX_BOOL		GetFaceName(void* hFont, CFX_ByteString& name) = 0;
    305     virtual FX_BOOL		GetFontCharset(void* hFont, int& charset) = 0;
    306     virtual int			GetFaceIndex(void* hFont)
    307     {
    308         return 0;
    309     }
    310     virtual void		DeleteFont(void* hFont) = 0;
    311     virtual void*       RetainFont(void* hFont)
    312     {
    313         return NULL;
    314     }
    315 protected:
    316     ~IFX_SystemFontInfo() { }
    317 };
    318 class CFX_FolderFontInfo : public IFX_SystemFontInfo
    319 {
    320 public:
    321     CFX_FolderFontInfo();
    322     virtual ~CFX_FolderFontInfo();
    323     void				AddPath(FX_BSTR path);
    324     virtual void		Release();
    325     virtual	FX_BOOL		EnumFontList(CFX_FontMapper* pMapper);
    326     virtual void*		MapFont(int weight, FX_BOOL bItalic, int charset, int pitch_family, FX_LPCSTR face, FX_BOOL& bExact);
    327     virtual void*		GetFont(FX_LPCSTR face);
    328     virtual FX_DWORD	GetFontData(void* hFont, FX_DWORD table, FX_LPBYTE buffer, FX_DWORD size);
    329     virtual void		DeleteFont(void* hFont);
    330     virtual	FX_BOOL		GetFaceName(void* hFont, CFX_ByteString& name);
    331     virtual FX_BOOL		GetFontCharset(void* hFont, int& charset);
    332 protected:
    333     CFX_MapByteStringToPtr	m_FontList;
    334     CFX_ByteStringArray	m_PathList;
    335     CFX_FontMapper*		m_pMapper;
    336     void				ScanPath(CFX_ByteString& path);
    337     void				ScanFile(CFX_ByteString& path);
    338     void				ReportFace(CFX_ByteString& path, FXSYS_FILE* pFile, FX_DWORD filesize, FX_DWORD offset);
    339 };
    340 class CFX_CountedFaceCache
    341 {
    342 public:
    343     CFX_FaceCache*	m_Obj;
    344     FX_DWORD		m_nCount;
    345 };
    346 typedef CFX_MapPtrTemplate<FXFT_Face, CFX_CountedFaceCache*> CFX_FTCacheMap;
    347 class CFX_FontCache
    348 {
    349 public:
    350     ~CFX_FontCache();
    351     CFX_FaceCache*			GetCachedFace(CFX_Font* pFont);
    352     void					ReleaseCachedFace(CFX_Font* pFont);
    353     void					FreeCache(FX_BOOL bRelease = FALSE);
    354 
    355 private:
    356     CFX_FTCacheMap			m_FTFaceMap;
    357     CFX_FTCacheMap			m_ExtFaceMap;
    358 };
    359 class CFX_AutoFontCache
    360 {
    361 public:
    362     CFX_AutoFontCache(CFX_FontCache* pFontCache, CFX_Font* pFont)
    363         : m_pFontCache(pFontCache)
    364         , m_pFont(pFont)
    365     {
    366     }
    367     ~CFX_AutoFontCache()
    368     {
    369         m_pFontCache->ReleaseCachedFace(m_pFont);
    370     }
    371     CFX_FontCache* m_pFontCache;
    372     CFX_Font* m_pFont;
    373 };
    374 #define FX_FONTCACHE_DEFINE(pFontCache, pFont) CFX_AutoFontCache autoFontCache((pFontCache), (pFont))
    375 class CFX_GlyphBitmap
    376 {
    377 public:
    378     int						m_Top;
    379     int						m_Left;
    380     CFX_DIBitmap			m_Bitmap;
    381 };
    382 class CFX_FaceCache
    383 {
    384 public:
    385     ~CFX_FaceCache();
    386     const CFX_GlyphBitmap*	LoadGlyphBitmap(CFX_Font* pFont, FX_DWORD glyph_index, FX_BOOL bFontStyle, const CFX_AffineMatrix* pMatrix,
    387                                             int dest_width, int anti_alias, int& text_flags);
    388     const CFX_PathData*		LoadGlyphPath(CFX_Font* pFont, FX_DWORD glyph_index, int dest_width);
    389 
    390 
    391     CFX_FaceCache(FXFT_Face face);
    392 private:
    393     FXFT_Face				m_Face;
    394     CFX_GlyphBitmap*		RenderGlyph(CFX_Font* pFont, FX_DWORD glyph_index, FX_BOOL bFontStyle,
    395                                         const CFX_AffineMatrix* pMatrix, int dest_width, int anti_alias);
    396     CFX_GlyphBitmap*		RenderGlyph_Nativetext(CFX_Font* pFont, FX_DWORD glyph_index,
    397             const CFX_AffineMatrix* pMatrix, int dest_width, int anti_alias);
    398     CFX_GlyphBitmap*        LookUpGlyphBitmap(CFX_Font* pFont, const CFX_AffineMatrix* pMatrix, CFX_ByteStringC& FaceGlyphsKey,
    399             FX_DWORD glyph_index, FX_BOOL bFontStyle, int dest_width, int anti_alias);
    400     CFX_MapByteStringToPtr	m_SizeMap;
    401     CFX_MapPtrToPtr			m_PathMap;
    402     CFX_DIBitmap*           m_pBitmap;
    403     void*                   m_pPlatformGraphics;
    404     void*                   m_pPlatformBitmap;
    405     void*                   m_hDC;
    406     void*                   m_hBitmap;
    407     void*                   m_hOldBitmap;
    408     void*                   m_hGdiFont;
    409     void*                   m_hOldGdiFont;
    410 
    411     void				    InitPlatform();
    412     void				    DestroyPlatform();
    413 };
    414 typedef struct {
    415     const CFX_GlyphBitmap*	m_pGlyph;
    416     int					m_OriginX, m_OriginY;
    417     FX_FLOAT			m_fOriginX, m_fOriginY;
    418 } FXTEXT_GLYPHPOS;
    419 FX_RECT FXGE_GetGlyphsBBox(FXTEXT_GLYPHPOS* pGlyphAndPos, int nChars, int anti_alias, FX_FLOAT retinaScaleX = 1.0f, FX_FLOAT retinaScaleY = 1.0f);
    420 FX_BOOL	OutputGlyph(void* dib, int x, int y, CFX_Font* pFont, double font_size,
    421                     CFX_AffineMatrix* pMatrix, unsigned long glyph_index, unsigned long argb);
    422 FX_BOOL	OutputText(void* dib, int x, int y, CFX_Font* pFont, double font_size,
    423                    CFX_AffineMatrix* pText_matrix, unsigned short const* text, unsigned long argb);
    424 class IFX_GSUBTable
    425 {
    426 public:
    427     virtual void	Release() = 0;
    428     virtual FX_BOOL GetVerticalGlyph(FX_DWORD glyphnum, FX_DWORD* vglyphnum) = 0;
    429 
    430 protected:
    431      ~IFX_GSUBTable() { }
    432 };
    433 IFX_GSUBTable* FXGE_CreateGSUBTable(CFX_Font* pFont);
    434 
    435 #endif  // CORE_INCLUDE_FXGE_FX_FONT_H_
    436