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