diff options
Diffstat (limited to 'xfa/src/fgas/font')
-rw-r--r-- | xfa/src/fgas/font/fgas_font.h | 285 | ||||
-rw-r--r-- | xfa/src/fgas/font/fgas_fontutils.cpp | 153 | ||||
-rw-r--r-- | xfa/src/fgas/font/fgas_fontutils.h | 25 | ||||
-rw-r--r-- | xfa/src/fgas/font/fgas_gefont.cpp | 572 | ||||
-rw-r--r-- | xfa/src/fgas/font/fgas_gefont.h | 92 | ||||
-rw-r--r-- | xfa/src/fgas/font/fgas_stdfontmgr.cpp | 1515 | ||||
-rw-r--r-- | xfa/src/fgas/font/fgas_stdfontmgr.h | 252 |
7 files changed, 2894 insertions, 0 deletions
diff --git a/xfa/src/fgas/font/fgas_font.h b/xfa/src/fgas/font/fgas_font.h new file mode 100644 index 0000000000..0484dee3e6 --- /dev/null +++ b/xfa/src/fgas/font/fgas_font.h @@ -0,0 +1,285 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_SRC_FGAS_FONT_FGAS_FONT_H_ +#define XFA_SRC_FGAS_FONT_FGAS_FONT_H_ + +#include "core/include/fxge/fx_font.h" +#include "xfa/src/fgas/crt/fgas_stream.h" + +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ +#include "xfa/src/fgas/crt/fgas_memory.h" +#include "xfa/src/fgas/crt/fgas_utils.h" +#endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ + +class IFX_Font; +class IFX_FontMgr; + +#define FX_FONTSTYLE_Normal 0x00 +#define FX_FONTSTYLE_FixedPitch 0x01 +#define FX_FONTSTYLE_Serif 0x02 +#define FX_FONTSTYLE_Symbolic 0x04 +#define FX_FONTSTYLE_Script 0x08 +#define FX_FONTSTYLE_Italic 0x40 +#define FX_FONTSTYLE_Bold 0x40000 +#define FX_FONTSTYLE_BoldItalic (FX_FONTSTYLE_Bold | FX_FONTSTYLE_Italic) +#define FX_FONTSTYLE_ExactMatch 0x80000000 +#define FX_FONTDECORATION_Underline 0x00000001 +#define FX_FONTDECORATION_Strikeout 0x00000002 +#define FX_FONTDECORATION_Overline 0x00000004 +#define FX_FONTDECORATION_Emphasis 0x00000008 +#define FX_FONTDECORATION_Superscript 0x00000010 +#define FX_FONTDECORATION_Subscript 0x00000020 +#define FX_FONTDECORATION_SmallCapital 0x00000040 +#define FX_FONTDECORATION_Capital 0x00000080 +#define FX_FONTDECORATION_Lowercase 0x000000C0 +#define FX_FONTDECORATION_Raised 0x00000100 +#define FX_FONTDECORATION_Sunken 0x00000200 +#define FX_FONTDECORATION_Shadow 0x00000400 +#define FX_FONTDECORATION_BoundingShape 0x20000000 +#define FX_FONTDECORATION_Hide 0x40000000 +#define FX_FONTDECORATION_StrokeFill 0x80000000 +#define FX_BOUNDINGSHAPE_None 0 +#define FX_BOUNDINGSHAPE_Circle 1 +#define FX_BOUNDINGSHAPE_Square 2 +#define FX_BOUNDINGSHAPE_Triangle 3 +#define FX_BOUNDINGSHAPE_Diamond 4 + +class IFX_FontProvider { + public: + virtual ~IFX_FontProvider() {} + virtual FX_BOOL GetCharWidth(IFX_Font* pFont, + FX_WCHAR wUnicode, + int32_t& iWidth, + FX_BOOL bCharCode = FALSE) = 0; +}; + +class IFX_Font { + public: + static IFX_Font* LoadFont(const FX_WCHAR* pszFontFamily, + FX_DWORD dwFontStyles, + FX_WORD wCodePage, + IFX_FontMgr* pFontMgr); + static IFX_Font* LoadFont(const uint8_t* pBuffer, + int32_t iLength, + IFX_FontMgr* pFontMgr); + static IFX_Font* LoadFont(const FX_WCHAR* pszFileName, IFX_FontMgr* pFontMgr); + static IFX_Font* LoadFont(IFX_Stream* pFontStream, + IFX_FontMgr* pFontMgr, + FX_BOOL bSaveStream = FALSE); + static IFX_Font* LoadFont(CFX_Font* pExtFont, + IFX_FontMgr* pFontMgr, + FX_BOOL bTakeOver = FALSE); + virtual ~IFX_Font() {} + virtual void Release() = 0; + virtual IFX_Font* Retain() = 0; + virtual IFX_Font* Derive(FX_DWORD dwFontStyles, FX_WORD wCodePage = 0) = 0; + virtual void GetFamilyName(CFX_WideString& wsFamily) const = 0; + virtual void GetPsName(CFX_WideString& wsName) const = 0; + virtual FX_DWORD GetFontStyles() const = 0; + virtual uint8_t GetCharSet() const = 0; + virtual FX_BOOL GetCharWidth(FX_WCHAR wUnicode, + int32_t& iWidth, + FX_BOOL bCharCode = FALSE) = 0; + virtual int32_t GetGlyphIndex(FX_WCHAR wUnicode, + FX_BOOL bCharCode = FALSE) = 0; + virtual int32_t GetAscent() const = 0; + virtual int32_t GetDescent() const = 0; + virtual FX_BOOL GetCharBBox(FX_WCHAR wUnicode, + CFX_Rect& bbox, + FX_BOOL bCharCode = FALSE) = 0; + virtual FX_BOOL GetBBox(CFX_Rect& bbox) = 0; + virtual int32_t GetItalicAngle() const = 0; + virtual void Reset() = 0; + virtual IFX_Font* GetSubstFont(int32_t iGlyphIndex) const = 0; + virtual void* GetDevFont() const = 0; + virtual void SetFontProvider(IFX_FontProvider* pProvider) = 0; +#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ + virtual void SetLogicalFontStyle(FX_DWORD dwLogFontStyle) = 0; +#endif +}; +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ +struct FX_FONTMATCHPARAMS { + const FX_WCHAR* pwsFamily; + FX_DWORD dwFontStyles; + FX_DWORD dwUSB; + FX_DWORD dwMatchFlags; + FX_WCHAR wUnicode; + FX_WORD wCodePage; +}; +typedef FX_FONTMATCHPARAMS* FX_LPFONTMATCHPARAMS; +typedef FX_FONTMATCHPARAMS const* FX_LPCFONTMATCHPARAMS; + +struct FX_FONTSIGNATURE { + FX_DWORD fsUsb[4]; + FX_DWORD fsCsb[2]; +}; +inline bool operator==(const FX_FONTSIGNATURE& left, + const FX_FONTSIGNATURE& right) { + return left.fsUsb[0] == right.fsUsb[0] && left.fsUsb[1] == right.fsUsb[1] && + left.fsUsb[2] == right.fsUsb[2] && left.fsUsb[3] == right.fsUsb[3] && + left.fsCsb[0] == right.fsCsb[0] && left.fsCsb[1] == right.fsCsb[1]; +} + +struct FX_FONTDESCRIPTOR { + FX_WCHAR wsFontFace[32]; + FX_DWORD dwFontStyles; + uint8_t uCharSet; + FX_FONTSIGNATURE FontSignature; +}; +typedef FX_FONTDESCRIPTOR* FX_LPFONTDESCRIPTOR; +typedef FX_FONTDESCRIPTOR const* FX_LPCFONTDESCRIPTOR; +typedef CFX_MassArrayTemplate<FX_FONTDESCRIPTOR> CFX_FontDescriptors; +inline bool operator==(const FX_FONTDESCRIPTOR& left, + const FX_FONTDESCRIPTOR& right) { + return left.uCharSet == right.uCharSet && + left.dwFontStyles == right.dwFontStyles && + left.FontSignature == right.FontSignature && + FXSYS_wcscmp(left.wsFontFace, right.wsFontFace) == 0; +} + +#define FX_FONTMATCHPARA_MacthStyle 0x01 +#define FX_FONTMATCHPARA_MacthFamily 0x02 +#define FX_FONTMATCHPARA_MacthUnicode 0x04 +typedef void (*FX_LPEnumAllFonts)(CFX_FontDescriptors& fonts, + void* pUserData, + const FX_WCHAR* pwsFaceName, + FX_WCHAR wUnicode); +FX_LPEnumAllFonts FX_GetDefFontEnumerator(); +typedef FX_LPCFONTDESCRIPTOR (*FX_LPMatchFont)(FX_LPFONTMATCHPARAMS pParams, + const CFX_FontDescriptors& fonts, + void* pUserData); +FX_LPMatchFont FX_GetDefFontMatchor(); +class IFX_FontMgr { + public: + static IFX_FontMgr* Create(FX_LPEnumAllFonts pEnumerator, + FX_LPMatchFont pMatcher = NULL, + void* pUserData = NULL); + virtual ~IFX_FontMgr() {} + virtual void Release() = 0; + virtual IFX_Font* GetDefFontByCodePage( + FX_WORD wCodePage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL) = 0; + virtual IFX_Font* GetDefFontByCharset( + uint8_t nCharset, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL) = 0; + virtual IFX_Font* GetDefFontByUnicode( + FX_WCHAR wUnicode, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL) = 0; + virtual IFX_Font* GetDefFontByLanguage( + FX_WORD wLanguage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL) = 0; + virtual IFX_Font* LoadFont(const FX_WCHAR* pszFontFamily, + FX_DWORD dwFontStyles, + FX_WORD wCodePage = 0xFFFF) = 0; + virtual IFX_Font* LoadFont(const uint8_t* pBuffer, int32_t iLength) = 0; + virtual IFX_Font* LoadFont(const FX_WCHAR* pszFileName) = 0; + virtual IFX_Font* LoadFont(IFX_Stream* pFontStream, + const FX_WCHAR* pszFontAlias = NULL, + FX_DWORD dwFontStyles = 0, + FX_WORD wCodePage = 0, + FX_BOOL bSaveStream = FALSE) = 0; + virtual IFX_Font* LoadFont(IFX_Font* pSrcFont, + FX_DWORD dwFontStyles, + FX_WORD wCodePage = 0xFFFF) = 0; + virtual void ClearFontCache() = 0; + virtual void RemoveFont(IFX_Font* pFont) = 0; +}; +#else +class IFX_FontMgrDelegate { + public: + virtual ~IFX_FontMgrDelegate() {} + virtual IFX_Font* GetDefFontByCodePage( + IFX_FontMgr* pFontMgr, + FX_WORD wCodePage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL) = 0; + virtual IFX_Font* GetDefFontByCharset( + IFX_FontMgr* pFontMgr, + uint8_t nCharset, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL) = 0; + virtual IFX_Font* GetDefFontByUnicode( + IFX_FontMgr* pFontMgr, + FX_WCHAR wUnicode, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL) = 0; + virtual IFX_Font* GetDefFontByLanguage( + IFX_FontMgr* pFontMgr, + FX_WORD wLanguage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL) = 0; +}; +class IFX_FontSourceEnum { + public: + virtual ~IFX_FontSourceEnum() {} + virtual void Release() = 0; + virtual FX_POSITION GetStartPosition(void* pUserData = NULL) = 0; + virtual IFX_FileAccess* GetNext(FX_POSITION& pos, void* pUserData = NULL) = 0; +}; +IFX_FontSourceEnum* FX_CreateDefaultFontSourceEnum(); +class IFX_FontMgr { + public: + static IFX_FontMgr* Create(IFX_FontSourceEnum* pFontEnum, + IFX_FontMgrDelegate* pDelegate = NULL, + void* pUserData = NULL); + virtual ~IFX_FontMgr() {} + virtual void Release() = 0; + virtual IFX_Font* GetDefFontByCodePage( + FX_WORD wCodePage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL) = 0; + virtual IFX_Font* GetDefFontByCharset( + uint8_t nCharset, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL) = 0; + virtual IFX_Font* GetDefFontByUnicode( + FX_WCHAR wUnicode, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL) = 0; + virtual IFX_Font* GetDefFontByLanguage( + FX_WORD wLanguage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL) = 0; + virtual IFX_Font* GetFontByCodePage(FX_WORD wCodePage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL) = 0; + inline IFX_Font* LoadFont(const FX_WCHAR* pszFontFamily, + FX_DWORD dwFontStyles, + FX_WORD wCodePage) { + return GetFontByCodePage(wCodePage, dwFontStyles, pszFontFamily); + } + virtual IFX_Font* GetFontByCharset(uint8_t nCharset, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL) = 0; + virtual IFX_Font* GetFontByUnicode(FX_WCHAR wUnicode, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL) = 0; + virtual IFX_Font* GetFontByLanguage(FX_WORD wLanguage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL) = 0; + virtual IFX_Font* LoadFont(const uint8_t* pBuffer, + int32_t iLength, + int32_t iFaceIndex, + int32_t* pFaceCount = NULL) = 0; + virtual IFX_Font* LoadFont(const FX_WCHAR* pszFileName, + int32_t iFaceIndex, + int32_t* pFaceCount = NULL) = 0; + virtual IFX_Font* LoadFont(IFX_Stream* pFontStream, + int32_t iFaceIndex, + int32_t* pFaceCount = NULL, + FX_BOOL bSaveStream = FALSE) = 0; + + virtual void ClearFontCache() = 0; + virtual void RemoveFont(IFX_Font* pFont) = 0; +}; +#endif + +#endif // XFA_SRC_FGAS_FONT_FGAS_FONT_H_ diff --git a/xfa/src/fgas/font/fgas_fontutils.cpp b/xfa/src/fgas/font/fgas_fontutils.cpp new file mode 100644 index 0000000000..62f26f515a --- /dev/null +++ b/xfa/src/fgas/font/fgas_fontutils.cpp @@ -0,0 +1,153 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "xfa/src/fgas/font/fgas_fontutils.h" + +#include "core/include/fxcrt/fx_ext.h" +#include "xfa/src/fgas/font/fgas_font.h" + +FX_DWORD FGAS_GetFontHashCode(FX_WORD wCodePage, FX_DWORD dwFontStyles) { + FX_DWORD dwHash = wCodePage; + if (dwFontStyles & FX_FONTSTYLE_FixedPitch) { + dwHash |= 0x00010000; + } + if (dwFontStyles & FX_FONTSTYLE_Serif) { + dwHash |= 0x00020000; + } + if (dwFontStyles & FX_FONTSTYLE_Symbolic) { + dwHash |= 0x00040000; + } + if (dwFontStyles & FX_FONTSTYLE_Script) { + dwHash |= 0x00080000; + } + if (dwFontStyles & FX_FONTSTYLE_Italic) { + dwHash |= 0x00100000; + } + if (dwFontStyles & FX_FONTSTYLE_Bold) { + dwHash |= 0x00200000; + } + return dwHash; +} +FX_DWORD FGAS_GetFontFamilyHash(const FX_WCHAR* pszFontFamily, + FX_DWORD dwFontStyles, + FX_WORD wCodePage) { + CFX_WideString wsFont(pszFontFamily); + if (dwFontStyles & FX_FONTSTYLE_Bold) { + wsFont += L"Bold"; + } + if (dwFontStyles & FX_FONTSTYLE_Italic) { + wsFont += L"Italic"; + } + wsFont += wCodePage; + return FX_HashCode_String_GetW((const FX_WCHAR*)wsFont, wsFont.GetLength()); +} +static const FGAS_FONTUSB g_FXGdiFontUSBTable[] = { + {0x0000, 0x007F, 0, 1252}, {0x0080, 0x00FF, 1, 1252}, + {0x0100, 0x017F, 2, 1250}, {0x0180, 0x024F, 3, 1250}, + {0x0250, 0x02AF, 4, 0xFFFF}, {0x02B0, 0x02FF, 5, 0xFFFF}, + {0x0300, 0x036F, 6, 0xFFFF}, {0x0370, 0x03FF, 7, 1253}, + {0x0400, 0x04FF, 9, 1251}, {0x0500, 0x052F, 9, 0xFFFF}, + {0x0530, 0x058F, 10, 0xFFFF}, {0x0590, 0x05FF, 11, 1255}, + {0x0600, 0x06FF, 13, 1256}, {0x0700, 0x074F, 71, 0xFFFF}, + {0x0750, 0x077F, 13, 0xFFFF}, {0x0780, 0x07BF, 72, 0xFFFF}, + {0x07C0, 0x07FF, 14, 0xFFFF}, {0x0800, 0x08FF, 999, 0xFFFF}, + {0x0900, 0x097F, 15, 0xFFFF}, {0x0980, 0x09FF, 16, 0xFFFF}, + {0x0A00, 0x0A7F, 17, 0xFFFF}, {0x0A80, 0x0AFF, 18, 0xFFFF}, + {0x0B00, 0x0B7F, 19, 0xFFFF}, {0x0B80, 0x0BFF, 20, 0xFFFF}, + {0x0C00, 0x0C7F, 21, 0xFFFF}, {0x0C80, 0x0CFF, 22, 0xFFFF}, + {0x0D00, 0x0D7F, 23, 0xFFFF}, {0x0D80, 0x0DFF, 73, 0xFFFF}, + {0x0E00, 0x0E7F, 24, 874}, {0x0E80, 0x0EFF, 25, 0xFFFF}, + {0x0F00, 0x0FFF, 70, 0xFFFF}, {0x1000, 0x109F, 74, 0xFFFF}, + {0x10A0, 0x10FF, 26, 0xFFFF}, {0x1100, 0x11FF, 28, 0xFFFF}, + {0x1200, 0x137F, 75, 0xFFFF}, {0x1380, 0x139F, 75, 0xFFFF}, + {0x13A0, 0x13FF, 76, 0xFFFF}, {0x1400, 0x167F, 77, 0xFFFF}, + {0x1680, 0x169F, 78, 0xFFFF}, {0x16A0, 0x16FF, 79, 0xFFFF}, + {0x1700, 0x171F, 84, 0xFFFF}, {0x1720, 0x173F, 84, 0xFFFF}, + {0x1740, 0x175F, 84, 0xFFFF}, {0x1760, 0x177F, 84, 0xFFFF}, + {0x1780, 0x17FF, 80, 0xFFFF}, {0x1800, 0x18AF, 81, 0xFFFF}, + {0x18B0, 0x18FF, 999, 0xFFFF}, {0x1900, 0x194F, 93, 0xFFFF}, + {0x1950, 0x197F, 94, 0xFFFF}, {0x1980, 0x19DF, 95, 0xFFFF}, + {0x19E0, 0x19FF, 80, 0xFFFF}, {0x1A00, 0x1A1F, 96, 0xFFFF}, + {0x1A20, 0x1AFF, 999, 0xFFFF}, {0x1B00, 0x1B7F, 27, 0xFFFF}, + {0x1B80, 0x1BBF, 112, 0xFFFF}, {0x1BC0, 0x1BFF, 999, 0xFFFF}, + {0x1C00, 0x1C4F, 113, 0xFFFF}, {0x1C50, 0x1C7F, 114, 0xFFFF}, + {0x1C80, 0x1CFF, 999, 0xFFFF}, {0x1D00, 0x1D7F, 4, 0xFFFF}, + {0x1D80, 0x1DBF, 4, 0xFFFF}, {0x1DC0, 0x1DFF, 6, 0xFFFF}, + {0x1E00, 0x1EFF, 29, 0xFFFF}, {0x1F00, 0x1FFF, 30, 0xFFFF}, + {0x2000, 0x206F, 31, 0xFFFF}, {0x2070, 0x209F, 32, 0xFFFF}, + {0x20A0, 0x20CF, 33, 0xFFFF}, {0x20D0, 0x20FF, 34, 0xFFFF}, + {0x2100, 0x214F, 35, 0xFFFF}, {0x2150, 0x215F, 36, 0xFFFF}, + {0x2160, 0x216B, 36, 936}, {0x216C, 0x216F, 36, 0xFFFF}, + {0x2170, 0x2179, 36, 936}, {0x217A, 0x218F, 36, 0xFFFF}, + {0x2190, 0x2199, 37, 949}, {0x219A, 0x21FF, 37, 0xFFFF}, + {0x2200, 0x22FF, 38, 0xFFFF}, {0x2300, 0x23FF, 39, 0xFFFF}, + {0x2400, 0x243F, 40, 0xFFFF}, {0x2440, 0x245F, 41, 0xFFFF}, + {0x2460, 0x2473, 42, 932}, {0x2474, 0x249B, 42, 936}, + {0x249C, 0x24E9, 42, 949}, {0x24EA, 0x24FF, 42, 0xFFFF}, + {0x2500, 0x2573, 43, 936}, {0x2574, 0x257F, 43, 0xFFFF}, + {0x2580, 0x2580, 44, 0xFFFF}, {0x2581, 0x258F, 44, 936}, + {0x2590, 0x259F, 44, 0xFFFF}, {0x25A0, 0x25FF, 45, 0xFFFF}, + {0x2600, 0x26FF, 46, 0xFFFF}, {0x2700, 0x27BF, 47, 0xFFFF}, + {0x27C0, 0x27EF, 38, 0xFFFF}, {0x27F0, 0x27FF, 37, 0xFFFF}, + {0x2800, 0x28FF, 82, 0xFFFF}, {0x2900, 0x297F, 37, 0xFFFF}, + {0x2980, 0x29FF, 38, 0xFFFF}, {0x2A00, 0x2AFF, 38, 0xFFFF}, + {0x2B00, 0x2BFF, 37, 0xFFFF}, {0x2C00, 0x2C5F, 97, 0xFFFF}, + {0x2C60, 0x2C7F, 29, 0xFFFF}, {0x2C80, 0x2CFF, 8, 0xFFFF}, + {0x2D00, 0x2D2F, 26, 0xFFFF}, {0x2D30, 0x2D7F, 98, 0xFFFF}, + {0x2D80, 0x2DDF, 75, 0xFFFF}, {0x2DE0, 0x2DFF, 9, 0xFFFF}, + {0x2E00, 0x2E7F, 31, 0xFFFF}, {0x2E80, 0x2EFF, 59, 0xFFFF}, + {0x2F00, 0x2FDF, 59, 0xFFFF}, {0x2FE0, 0x2FEF, 999, 0xFFFF}, + {0x2FF0, 0x2FFF, 59, 0xFFFF}, {0x3000, 0x303F, 48, 0xFFFF}, + {0x3040, 0x309F, 49, 932}, {0x30A0, 0x30FF, 50, 932}, + {0x3100, 0x3129, 51, 936}, {0x312A, 0x312F, 51, 0xFFFF}, + {0x3130, 0x318F, 52, 949}, {0x3190, 0x319F, 59, 0xFFFF}, + {0x31A0, 0x31BF, 51, 0xFFFF}, {0x31C0, 0x31EF, 61, 0xFFFF}, + {0x31F0, 0x31FF, 50, 0xFFFF}, {0x3200, 0x321C, 54, 949}, + {0x321D, 0x325F, 54, 0xFFFF}, {0x3260, 0x327F, 54, 949}, + {0x3280, 0x32FF, 54, 0xFFFF}, {0x3300, 0x3387, 55, 0xFFFF}, + {0x3388, 0x33D0, 55, 949}, {0x33D1, 0x33FF, 55, 0xFFFF}, + {0x3400, 0x4DBF, 59, 0xFFFF}, {0x4DC0, 0x4DFF, 99, 0xFFFF}, + {0x4E00, 0x9FA5, 59, 936}, {0x9FA6, 0x9FFF, 59, 0xFFFF}, + {0xA000, 0xA48F, 83, 0xFFFF}, {0xA490, 0xA4CF, 83, 0xFFFF}, + {0xA4D0, 0xA4FF, 999, 0xFFFF}, {0xA500, 0xA63F, 12, 0xFFFF}, + {0xA640, 0xA69F, 9, 0xFFFF}, {0xA6A0, 0xA6FF, 999, 0xFFFF}, + {0xA700, 0xA71F, 5, 0xFFFF}, {0xA720, 0xA7FF, 29, 0xFFFF}, + {0xA800, 0xA82F, 100, 0xFFFF}, {0xA830, 0xA8FF, 999, 0xFFFF}, + {0xA840, 0xA87F, 53, 0xFFFF}, {0xA880, 0xA8DF, 115, 0xFFFF}, + {0xA8E0, 0xA8FF, 999, 0xFFFF}, {0xA900, 0xA92F, 116, 0xFFFF}, + {0xA930, 0xA95F, 117, 0xFFFF}, {0xA960, 0xA9FF, 999, 0xFFFF}, + {0xAA00, 0xAA5F, 118, 0xFFFF}, {0xAA60, 0xABFF, 999, 0xFFFF}, + {0xAC00, 0xD7AF, 56, 949}, {0xD7B0, 0xD7FF, 999, 0xFFFF}, + {0xD800, 0xDB7F, 57, 0xFFFF}, {0xDB80, 0xDBFF, 57, 0xFFFF}, + {0xDC00, 0xDFFF, 57, 0xFFFF}, {0xE000, 0xE814, 60, 0xFFFF}, + {0xE815, 0xE864, 60, 936}, {0xE865, 0xF8FF, 60, 0xFFFF}, + {0xF900, 0xFA0B, 61, 949}, {0xFA0C, 0xFA0D, 61, 936}, + {0xFA0E, 0xFA2D, 61, 932}, {0xFA2E, 0xFAFF, 61, 0xFFFF}, + {0xFB00, 0xFB4F, 62, 0xFFFF}, {0xFB50, 0xFDFF, 63, 1256}, + {0xFE00, 0xFE0F, 91, 0xFFFF}, {0xFE10, 0xFE1F, 65, 0xFFFF}, + {0xFE20, 0xFE2F, 64, 0xFFFF}, {0xFE30, 0xFE4F, 65, 0xFFFF}, + {0xFE50, 0xFE6F, 66, 0xFFFF}, {0xFE70, 0xFEFF, 67, 1256}, + {0xFF00, 0xFF5F, 68, 936}, {0xFF60, 0xFF9F, 68, 932}, + {0xFFA0, 0xFFEF, 68, 0xFFFF}, +}; + +const FGAS_FONTUSB* FGAS_GetUnicodeBitField(FX_WCHAR wUnicode) { + int32_t iEnd = sizeof(g_FXGdiFontUSBTable) / sizeof(FGAS_FONTUSB) - 1; + FXSYS_assert(iEnd >= 0); + int32_t iStart = 0, iMid; + do { + iMid = (iStart + iEnd) / 2; + const FGAS_FONTUSB& usb = g_FXGdiFontUSBTable[iMid]; + if (wUnicode < usb.wStartUnicode) { + iEnd = iMid - 1; + } else if (wUnicode > usb.wEndUnicode) { + iStart = iMid + 1; + } else { + return &usb; + } + } while (iStart <= iEnd); + return NULL; +} diff --git a/xfa/src/fgas/font/fgas_fontutils.h b/xfa/src/fgas/font/fgas_fontutils.h new file mode 100644 index 0000000000..4cceb68574 --- /dev/null +++ b/xfa/src/fgas/font/fgas_fontutils.h @@ -0,0 +1,25 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_SRC_FGAS_FONT_FGAS_FONTUTILS_H_ +#define XFA_SRC_FGAS_FONT_FGAS_FONTUTILS_H_ + +#include "core/include/fxcrt/fx_string.h" + +struct FGAS_FONTUSB { + FX_WCHAR wStartUnicode; + FX_WCHAR wEndUnicode; + FX_WORD wBitField; + FX_WORD wCodePage; +}; + +FX_DWORD FGAS_GetFontHashCode(FX_WORD wCodePage, FX_DWORD dwFontStyles); +FX_DWORD FGAS_GetFontFamilyHash(const FX_WCHAR* pszFontFamily, + FX_DWORD dwFontStyles, + FX_WORD wCodePage); +const FGAS_FONTUSB* FGAS_GetUnicodeBitField(FX_WCHAR wUnicode); + +#endif // XFA_SRC_FGAS_FONT_FGAS_FONTUTILS_H_ diff --git a/xfa/src/fgas/font/fgas_gefont.cpp b/xfa/src/fgas/font/fgas_gefont.cpp new file mode 100644 index 0000000000..df614a57c3 --- /dev/null +++ b/xfa/src/fgas/font/fgas_gefont.cpp @@ -0,0 +1,572 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "xfa/src/fgas/font/fgas_gefont.h" + +#include "xfa/src/fgas/crt/fgas_codepage.h" +#include "xfa/src/fgas/font/fgas_fontutils.h" + +IFX_Font* IFX_Font::LoadFont(const FX_WCHAR* pszFontFamily, + FX_DWORD dwFontStyles, + FX_WORD wCodePage, + IFX_FontMgr* pFontMgr) { +#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ + if (NULL != pFontMgr) { + return pFontMgr->GetFontByCodePage(wCodePage, dwFontStyles, pszFontFamily); + } + return NULL; +#else + CFX_GEFont* pFont = new CFX_GEFont(pFontMgr); + if (!pFont->LoadFont(pszFontFamily, dwFontStyles, wCodePage)) { + pFont->Release(); + return NULL; + } + return pFont; +#endif +} +IFX_Font* IFX_Font::LoadFont(const uint8_t* pBuffer, + int32_t iLength, + IFX_FontMgr* pFontMgr) { +#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ + if (NULL != pFontMgr) { + return pFontMgr->LoadFont(pBuffer, iLength, 0, NULL); + } + return NULL; +#else + CFX_GEFont* pFont = new CFX_GEFont(pFontMgr); + if (!pFont->LoadFont(pBuffer, iLength)) { + pFont->Release(); + return NULL; + } + return pFont; +#endif +} +IFX_Font* IFX_Font::LoadFont(const FX_WCHAR* pszFileName, + IFX_FontMgr* pFontMgr) { +#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ + if (NULL != pFontMgr) { + return pFontMgr->LoadFont(pszFileName, 0, NULL); + } + return NULL; +#else + CFX_GEFont* pFont = new CFX_GEFont(pFontMgr); + if (!pFont->LoadFont(pszFileName)) { + pFont->Release(); + return NULL; + } + return pFont; +#endif +} +IFX_Font* IFX_Font::LoadFont(IFX_Stream* pFontStream, + IFX_FontMgr* pFontMgr, + FX_BOOL bSaveStream) { +#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ + if (NULL != pFontMgr) { + return pFontMgr->LoadFont(pFontStream, 0, NULL); + } + return NULL; +#else + CFX_GEFont* pFont = new CFX_GEFont(pFontMgr); + if (!pFont->LoadFont(pFontStream, bSaveStream)) { + pFont->Release(); + return NULL; + } + return pFont; +#endif +} +IFX_Font* IFX_Font::LoadFont(CFX_Font* pExtFont, + IFX_FontMgr* pFontMgr, + FX_BOOL bTakeOver) { + CFX_GEFont* pFont = new CFX_GEFont(pFontMgr); + if (!pFont->LoadFont(pExtFont, bTakeOver)) { + pFont->Release(); + return NULL; + } + return pFont; +} +CFX_GEFont::CFX_GEFont(IFX_FontMgr* pFontMgr) + : +#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ + m_bUseLogFontStyle(FALSE), + m_dwLogFontStyle(0), +#endif + m_pFont(NULL), + m_pFontMgr(pFontMgr), + m_iRefCount(1), + m_bExtFont(FALSE), + m_pStream(NULL), + m_pFileRead(NULL), + m_pFontEncoding(NULL), + m_pCharWidthMap(NULL), + m_pRectArray(NULL), + m_pBBoxMap(NULL), + m_pProvider(NULL), + m_wCharSet(0xFFFF), + m_SubstFonts(), + m_FontMapper(16) { +} + +CFX_GEFont::CFX_GEFont(const CFX_GEFont& src, FX_DWORD dwFontStyles) + : +#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ + m_bUseLogFontStyle(FALSE), + m_dwLogFontStyle(0), +#endif + m_pFont(NULL), + m_pFontMgr(src.m_pFontMgr), + m_iRefCount(1), + m_bExtFont(FALSE), + m_pStream(NULL), + m_pFileRead(NULL), + m_pFontEncoding(NULL), + m_pCharWidthMap(NULL), + m_pRectArray(NULL), + m_pBBoxMap(NULL), + m_pProvider(NULL), + m_wCharSet(0xFFFF), + m_SubstFonts(), + m_FontMapper(16) { + m_pFont = new CFX_Font; + FXSYS_assert(m_pFont != NULL); + FXSYS_assert(src.m_pFont != NULL); + m_pFont->LoadClone(src.m_pFont); + CFX_SubstFont* pSubst = m_pFont->GetSubstFont(); + if (!pSubst) { + pSubst = new CFX_SubstFont; + m_pFont->SetSubstFont(pSubst); + } + pSubst->m_Weight = + (dwFontStyles & FX_FONTSTYLE_Bold) ? FXFONT_FW_BOLD : FXFONT_FW_NORMAL; + if (dwFontStyles & FX_FONTSTYLE_Italic) { + pSubst->m_SubstFlags |= FXFONT_SUBST_ITALIC; + } + InitFont(); +} +CFX_GEFont::~CFX_GEFont() { + int32_t iCount = m_SubstFonts.GetSize(); + for (int32_t i = 0; i < iCount; i++) { + IFX_Font* pFont = (IFX_Font*)m_SubstFonts[i]; + pFont->Release(); + } + m_SubstFonts.RemoveAll(); + m_FontMapper.RemoveAll(); + if (m_pFileRead != NULL) { + m_pFileRead->Release(); + } + if (m_pStream != NULL) { + m_pStream->Release(); + } + if (m_pFontEncoding != NULL) { + delete m_pFontEncoding; + } + if (m_pCharWidthMap != NULL) { + delete m_pCharWidthMap; + } + if (m_pRectArray != NULL) { + delete m_pRectArray; + } + if (m_pBBoxMap != NULL) { + delete m_pBBoxMap; + } + if (m_pFont != NULL && !m_bExtFont) { + delete m_pFont; + } +} +void CFX_GEFont::Release() { + if (--m_iRefCount < 1) { + if (m_pFontMgr != NULL) { + m_pFontMgr->RemoveFont(this); + } + delete this; + } +} +IFX_Font* CFX_GEFont::Retain() { + ++m_iRefCount; + return this; +} +FX_BOOL CFX_GEFont::LoadFont(const FX_WCHAR* pszFontFamily, + FX_DWORD dwFontStyles, + FX_WORD wCodePage) { + if (m_pFont) { + return FALSE; + } + CFX_ByteString csFontFamily; + if (pszFontFamily != NULL) { + csFontFamily = CFX_ByteString::FromUnicode(pszFontFamily); + } + FX_DWORD dwFlags = 0; + if (dwFontStyles & FX_FONTSTYLE_FixedPitch) { + dwFlags |= FXFONT_FIXED_PITCH; + } + if (dwFontStyles & FX_FONTSTYLE_Serif) { + dwFlags |= FXFONT_SERIF; + } + if (dwFontStyles & FX_FONTSTYLE_Symbolic) { + dwFlags |= FXFONT_SYMBOLIC; + } + if (dwFontStyles & FX_FONTSTYLE_Script) { + dwFlags |= FXFONT_SCRIPT; + } + if (dwFontStyles & FX_FONTSTYLE_Italic) { + dwFlags |= FXFONT_ITALIC; + } + if (dwFontStyles & FX_FONTSTYLE_Bold) { + dwFlags |= FXFONT_BOLD; + } + if (dwFontStyles & FX_FONTSTYLE_ExactMatch) { + dwFlags |= FXFONT_EXACTMATCH; + } + int32_t iWeight = + (dwFontStyles & FX_FONTSTYLE_Bold) ? FXFONT_FW_BOLD : FXFONT_FW_NORMAL; + FX_WORD wCharSet = FX_GetCharsetFromCodePage(wCodePage); + if (wCharSet == 0xFFFF) { + wCharSet = FXSYS_GetACP(); + } + m_wCharSet = wCharSet; + m_pFont = new CFX_Font; + if ((dwFlags & FXFONT_ITALIC) && (dwFlags & FXFONT_BOLD)) { + csFontFamily += ",BoldItalic"; + } else if (dwFlags & FXFONT_BOLD) { + csFontFamily += ",Bold"; + } else if (dwFlags & FXFONT_ITALIC) { + csFontFamily += ",Italic"; + } + m_pFont->LoadSubst(csFontFamily, TRUE, dwFlags, iWeight, 0, wCodePage); + FX_BOOL bRet = m_pFont->GetFace() != nullptr; + if (bRet) { + bRet = InitFont(); + } + return bRet; +} +FX_BOOL CFX_GEFont::LoadFont(const uint8_t* pBuffer, int32_t length) { + if (m_pFont) { + return FALSE; + } + m_pFont = new CFX_Font; + FX_BOOL bRet = m_pFont->LoadEmbedded(pBuffer, length); + if (bRet) { + bRet = InitFont(); + } + m_wCharSet = 0xFFFF; + return bRet; +} +FX_BOOL CFX_GEFont::LoadFont(const FX_WCHAR* pszFileName) { + if (m_pFont || m_pStream || m_pFileRead) { + return FALSE; + } + m_pStream = IFX_Stream::CreateStream( + pszFileName, FX_STREAMACCESS_Binary | FX_STREAMACCESS_Read); + m_pFileRead = FX_CreateFileRead(m_pStream); + FX_BOOL bRet = FALSE; + if (m_pStream && m_pFileRead) { + m_pFont = new CFX_Font; + bRet = m_pFont->LoadFile(m_pFileRead); + if (bRet) { + bRet = InitFont(); + } else { + m_pFileRead->Release(); + m_pFileRead = nullptr; + } + } + m_wCharSet = 0xFFFF; + return bRet; +} +FX_BOOL CFX_GEFont::LoadFont(IFX_Stream* pFontStream, FX_BOOL bSaveStream) { + if (m_pFont || m_pFileRead || !pFontStream || pFontStream->GetLength() < 1) { + return FALSE; + } + if (bSaveStream) { + m_pStream = pFontStream; + } + m_pFileRead = FX_CreateFileRead(pFontStream); + m_pFont = new CFX_Font; + FX_BOOL bRet = m_pFont->LoadFile(m_pFileRead); + if (bRet) { + bRet = InitFont(); + } else { + m_pFileRead->Release(); + m_pFileRead = nullptr; + } + m_wCharSet = 0xFFFF; + return bRet; +} +FX_BOOL CFX_GEFont::LoadFont(CFX_Font* pExtFont, FX_BOOL bTakeOver) { + if (m_pFont || !pExtFont) { + return FALSE; + } + m_pFont = pExtFont; + FX_BOOL bRet = !!m_pFont; + if (bRet) { + m_bExtFont = !bTakeOver; + bRet = InitFont(); + } else { + m_bExtFont = TRUE; + } + m_wCharSet = 0xFFFF; + return bRet; +} +FX_BOOL CFX_GEFont::InitFont() { + if (!m_pFont) { + return FALSE; + } + if (!m_pFontEncoding) { + m_pFontEncoding = FX_CreateFontEncodingEx(m_pFont); + if (!m_pFontEncoding) { + return FALSE; + } + } + if (!m_pCharWidthMap) { + m_pCharWidthMap = new CFX_WordDiscreteArray(1024); + } + if (!m_pRectArray) { + m_pRectArray = new CFX_RectMassArray(16); + } + if (!m_pBBoxMap) { + m_pBBoxMap = new CFX_MapPtrToPtr(16); + } + return TRUE; +} +IFX_Font* CFX_GEFont::Derive(FX_DWORD dwFontStyles, FX_WORD wCodePage) { + if (GetFontStyles() == dwFontStyles) { + return Retain(); + } + return new CFX_GEFont(*this, dwFontStyles); +} +uint8_t CFX_GEFont::GetCharSet() const { + if (m_wCharSet != 0xFFFF) { + return (uint8_t)m_wCharSet; + } + if (!m_pFont->GetSubstFont()) { + return FX_CHARSET_Default; + } + return m_pFont->GetSubstFont()->m_Charset; +} +void CFX_GEFont::GetFamilyName(CFX_WideString& wsFamily) const { + if (!m_pFont->GetSubstFont() || + m_pFont->GetSubstFont()->m_Family.GetLength() == 0) { + wsFamily = CFX_WideString::FromLocal(m_pFont->GetFamilyName()); + } else { + wsFamily = CFX_WideString::FromLocal(m_pFont->GetSubstFont()->m_Family); + } +} +void CFX_GEFont::GetPsName(CFX_WideString& wsName) const { + wsName = m_pFont->GetPsName(); +} +FX_DWORD CFX_GEFont::GetFontStyles() const { + FXSYS_assert(m_pFont != NULL); +#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ + if (m_bUseLogFontStyle) { + return m_dwLogFontStyle; + } +#endif + FX_DWORD dwStyles = 0; + if (!m_pFont->GetSubstFont()) { + if (m_pFont->IsBold()) { + dwStyles |= FX_FONTSTYLE_Bold; + } + if (m_pFont->IsItalic()) { + dwStyles |= FX_FONTSTYLE_Italic; + } + } else { + if (m_pFont->GetSubstFont()->m_Weight == FXFONT_FW_BOLD) { + dwStyles |= FX_FONTSTYLE_Bold; + } + if (m_pFont->GetSubstFont()->m_SubstFlags & FXFONT_SUBST_ITALIC) { + dwStyles |= FX_FONTSTYLE_Italic; + } + } + return dwStyles; +} +FX_BOOL CFX_GEFont::GetCharWidth(FX_WCHAR wUnicode, + int32_t& iWidth, + FX_BOOL bCharCode) { + return GetCharWidth(wUnicode, iWidth, TRUE, bCharCode); +} +FX_BOOL CFX_GEFont::GetCharWidth(FX_WCHAR wUnicode, + int32_t& iWidth, + FX_BOOL bRecursive, + FX_BOOL bCharCode) { + FXSYS_assert(m_pCharWidthMap != NULL); + iWidth = m_pCharWidthMap->GetAt(wUnicode, 0); + if (iWidth < 1) { + if (!m_pProvider || + !m_pProvider->GetCharWidth(this, wUnicode, iWidth, bCharCode)) { + IFX_Font* pFont = NULL; + int32_t iGlyph = GetGlyphIndex(wUnicode, TRUE, &pFont, bCharCode); + if (iGlyph != 0xFFFF && pFont != NULL) { + if (pFont == (IFX_Font*)this) { + iWidth = m_pFont->GetGlyphWidth(iGlyph); + if (iWidth < 0) { + iWidth = -1; + } + } else if (((CFX_GEFont*)pFont) + ->GetCharWidth(wUnicode, iWidth, FALSE, bCharCode)) { + return TRUE; + } + } else { + iWidth = -1; + } + } + m_pCharWidthMap->SetAtGrow(wUnicode, (int16_t)iWidth); + } else if (iWidth == 65535) { + iWidth = -1; + } + return iWidth > 0; +} +FX_BOOL CFX_GEFont::GetCharBBox(FX_WCHAR wUnicode, + CFX_Rect& bbox, + FX_BOOL bCharCode) { + return GetCharBBox(wUnicode, bbox, TRUE, bCharCode); +} +FX_BOOL CFX_GEFont::GetCharBBox(FX_WCHAR wUnicode, + CFX_Rect& bbox, + FX_BOOL bRecursive, + FX_BOOL bCharCode) { + FXSYS_assert(m_pRectArray != NULL); + FXSYS_assert(m_pBBoxMap != NULL); + void* pRect = NULL; + if (!m_pBBoxMap->Lookup((void*)(uintptr_t)wUnicode, pRect)) { + IFX_Font* pFont = NULL; + int32_t iGlyph = GetGlyphIndex(wUnicode, TRUE, &pFont, bCharCode); + if (iGlyph != 0xFFFF && pFont != NULL) { + if (pFont == (IFX_Font*)this) { + FX_RECT rtBBox; + if (m_pFont->GetGlyphBBox(iGlyph, rtBBox)) { + CFX_Rect rt; + rt.Set(rtBBox.left, rtBBox.top, rtBBox.Width(), rtBBox.Height()); + int32_t index = m_pRectArray->Add(rt); + pRect = m_pRectArray->GetPtrAt(index); + m_pBBoxMap->SetAt((void*)(uintptr_t)wUnicode, pRect); + } + } else if (((CFX_GEFont*)pFont) + ->GetCharBBox(wUnicode, bbox, FALSE, bCharCode)) { + return TRUE; + } + } + } + if (!pRect) + return FALSE; + + bbox = *static_cast<const CFX_Rect*>(pRect); + return TRUE; +} +FX_BOOL CFX_GEFont::GetBBox(CFX_Rect& bbox) { + FX_RECT rt(0, 0, 0, 0); + FX_BOOL bRet = m_pFont->GetBBox(rt); + if (bRet) { + bbox.left = rt.left; + bbox.width = rt.Width(); + bbox.top = rt.bottom; + bbox.height = -rt.Height(); + } + return bRet; +} +int32_t CFX_GEFont::GetItalicAngle() const { + if (!m_pFont->GetSubstFont()) { + return 0; + } + return m_pFont->GetSubstFont()->m_ItalicAngle; +} +int32_t CFX_GEFont::GetGlyphIndex(FX_WCHAR wUnicode, FX_BOOL bCharCode) { + return GetGlyphIndex(wUnicode, TRUE, NULL, bCharCode); +} +int32_t CFX_GEFont::GetGlyphIndex(FX_WCHAR wUnicode, + FX_BOOL bRecursive, + IFX_Font** ppFont, + FX_BOOL bCharCode) { + FXSYS_assert(m_pFontEncoding != NULL); + int32_t iGlyphIndex = m_pFontEncoding->GlyphFromCharCode(wUnicode); + if (iGlyphIndex > 0) { + if (ppFont != NULL) { + *ppFont = (IFX_Font*)this; + } + return iGlyphIndex; + } + const FGAS_FONTUSB* pFontUSB = FGAS_GetUnicodeBitField(wUnicode); + if (pFontUSB == NULL) { + return 0xFFFF; + } + FX_WORD wBitField = pFontUSB->wBitField; + if (wBitField >= 128) { + return 0xFFFF; + } + IFX_Font* pFont = NULL; + m_FontMapper.Lookup((void*)(uintptr_t)wUnicode, (void*&)pFont); + if (pFont != NULL && pFont != (IFX_Font*)this) { + iGlyphIndex = + ((CFX_GEFont*)pFont)->GetGlyphIndex(wUnicode, FALSE, NULL, bCharCode); + if (iGlyphIndex != 0xFFFF) { + int32_t i = m_SubstFonts.Find(pFont); + if (i > -1) { + iGlyphIndex |= ((i + 1) << 24); + if (ppFont != NULL) { + *ppFont = pFont; + } + return iGlyphIndex; + } + } + } + if (m_pFontMgr != NULL && bRecursive) { + CFX_WideString wsFamily; + GetFamilyName(wsFamily); +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ + IFX_Font* pFont = m_pFontMgr->GetDefFontByUnicode( + wUnicode, GetFontStyles(), (const FX_WCHAR*)wsFamily); +#else + IFX_Font* pFont = m_pFontMgr->GetFontByUnicode(wUnicode, GetFontStyles(), + (const FX_WCHAR*)wsFamily); + if (NULL == pFont) { + pFont = m_pFontMgr->GetFontByUnicode(wUnicode, GetFontStyles(), NULL); + } +#endif + if (pFont != NULL) { + if (pFont == (IFX_Font*)this) { + pFont->Release(); + return 0xFFFF; + } + m_FontMapper.SetAt((void*)(uintptr_t)wUnicode, (void*)pFont); + int32_t i = m_SubstFonts.GetSize(); + m_SubstFonts.Add(pFont); + iGlyphIndex = + ((CFX_GEFont*)pFont)->GetGlyphIndex(wUnicode, FALSE, NULL, bCharCode); + if (iGlyphIndex != 0xFFFF) { + iGlyphIndex |= ((i + 1) << 24); + if (ppFont != NULL) { + *ppFont = pFont; + } + return iGlyphIndex; + } + } + } + return 0xFFFF; +} +int32_t CFX_GEFont::GetAscent() const { + return m_pFont->GetAscent(); +} +int32_t CFX_GEFont::GetDescent() const { + return m_pFont->GetDescent(); +} +void CFX_GEFont::Reset() { + int32_t iCount = m_SubstFonts.GetSize(); + for (int32_t i = 0; i < iCount; i++) { + IFX_Font* pFont = (IFX_Font*)m_SubstFonts[i]; + ((CFX_GEFont*)pFont)->Reset(); + } + if (m_pCharWidthMap != NULL) { + m_pCharWidthMap->RemoveAll(); + } + if (m_pBBoxMap != NULL) { + m_pBBoxMap->RemoveAll(); + } + if (m_pRectArray != NULL) { + m_pRectArray->RemoveAll(); + } +} +IFX_Font* CFX_GEFont::GetSubstFont(int32_t iGlyphIndex) const { + iGlyphIndex = ((FX_DWORD)iGlyphIndex) >> 24; + return iGlyphIndex == 0 ? (IFX_Font*)this + : (IFX_Font*)m_SubstFonts[iGlyphIndex - 1]; +} diff --git a/xfa/src/fgas/font/fgas_gefont.h b/xfa/src/fgas/font/fgas_gefont.h new file mode 100644 index 0000000000..1bc8a6a902 --- /dev/null +++ b/xfa/src/fgas/font/fgas_gefont.h @@ -0,0 +1,92 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_SRC_FGAS_FONT_FGAS_GEFONT_H_ +#define XFA_SRC_FGAS_FONT_FGAS_GEFONT_H_ + +#include "xfa/src/fgas/crt/fgas_utils.h" +#include "xfa/src/fgas/font/fgas_font.h" + +#define FXFONT_SUBST_ITALIC 0x02 + +class CFX_GEFont : public IFX_Font { + public: + CFX_GEFont(const CFX_GEFont& src, FX_DWORD dwFontStyles); + CFX_GEFont(IFX_FontMgr* pFontMgr); + ~CFX_GEFont(); + virtual void Release(); + virtual IFX_Font* Retain(); + FX_BOOL LoadFont(const FX_WCHAR* pszFontFamily, + FX_DWORD dwFontStyles, + FX_WORD wCodePage); + FX_BOOL LoadFont(const uint8_t* pBuffer, int32_t length); + FX_BOOL LoadFont(const FX_WCHAR* pszFileName); + FX_BOOL LoadFont(IFX_Stream* pFontStream, FX_BOOL bSaveStream); + FX_BOOL LoadFont(CFX_Font* pExtFont, FX_BOOL bTakeOver = FALSE); + virtual IFX_Font* Derive(FX_DWORD dwFontStyles, FX_WORD wCodePage = 0); + virtual void GetFamilyName(CFX_WideString& wsFamily) const; + virtual void GetPsName(CFX_WideString& wsName) const; + virtual FX_DWORD GetFontStyles() const; + virtual uint8_t GetCharSet() const; + virtual FX_BOOL GetCharWidth(FX_WCHAR wUnicode, + int32_t& iWidth, + FX_BOOL bCharCode = FALSE); + virtual int32_t GetGlyphIndex(FX_WCHAR wUnicode, FX_BOOL bCharCode = FALSE); + virtual int32_t GetAscent() const; + virtual int32_t GetDescent() const; + virtual FX_BOOL GetCharBBox(FX_WCHAR wUnicode, + CFX_Rect& bbox, + FX_BOOL bCharCode = FALSE); + virtual FX_BOOL GetBBox(CFX_Rect& bbox); + virtual int32_t GetItalicAngle() const; + virtual void Reset(); + virtual IFX_Font* GetSubstFont(int32_t iGlyphIndex) const; + virtual void* GetDevFont() const { return (void*)m_pFont; } + virtual void SetFontProvider(IFX_FontProvider* pProvider) { + m_pProvider = pProvider; + } +#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ + virtual void SetLogicalFontStyle(FX_DWORD dwLogFontStyle) { + m_bUseLogFontStyle = TRUE; + m_dwLogFontStyle = dwLogFontStyle; + } +#endif + + protected: +#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ + FX_BOOL m_bUseLogFontStyle; + FX_DWORD m_dwLogFontStyle; +#endif + CFX_Font* m_pFont; + IFX_FontMgr* m_pFontMgr; + int32_t m_iRefCount; + FX_BOOL m_bExtFont; + IFX_Stream* m_pStream; + IFX_FileRead* m_pFileRead; + CFX_UnicodeEncoding* m_pFontEncoding; + CFX_WordDiscreteArray* m_pCharWidthMap; + CFX_RectMassArray* m_pRectArray; + CFX_MapPtrToPtr* m_pBBoxMap; + IFX_FontProvider* m_pProvider; + FX_WORD m_wCharSet; + CFX_PtrArray m_SubstFonts; + CFX_MapPtrToPtr m_FontMapper; + FX_BOOL InitFont(); + FX_BOOL GetCharBBox(FX_WCHAR wUnicode, + CFX_Rect& bbox, + FX_BOOL bRecursive, + FX_BOOL bCharCode = FALSE); + FX_BOOL GetCharWidth(FX_WCHAR wUnicode, + int32_t& iWidth, + FX_BOOL bRecursive, + FX_BOOL bCharCode = FALSE); + int32_t GetGlyphIndex(FX_WCHAR wUnicode, + FX_BOOL bRecursive, + IFX_Font** ppFont, + FX_BOOL bCharCode = FALSE); +}; + +#endif // XFA_SRC_FGAS_FONT_FGAS_GEFONT_H_ diff --git a/xfa/src/fgas/font/fgas_stdfontmgr.cpp b/xfa/src/fgas/font/fgas_stdfontmgr.cpp new file mode 100644 index 0000000000..99f1fdda44 --- /dev/null +++ b/xfa/src/fgas/font/fgas_stdfontmgr.cpp @@ -0,0 +1,1515 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "xfa/src/fgas/font/fgas_stdfontmgr.h" + +#include "core/include/fxcrt/fx_stream.h" +#include "xfa/src/fgas/crt/fgas_codepage.h" +#include "xfa/src/fgas/font/fgas_fontutils.h" + +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ +IFX_FontMgr* IFX_FontMgr::Create(FX_LPEnumAllFonts pEnumerator, + FX_LPMatchFont pMatcher, + void* pUserData) { + return new CFX_StdFontMgrImp(pEnumerator, pMatcher, pUserData); +} +CFX_StdFontMgrImp::CFX_StdFontMgrImp(FX_LPEnumAllFonts pEnumerator, + FX_LPMatchFont pMatcher, + void* pUserData) + : m_pMatcher(pMatcher), + m_pEnumerator(pEnumerator), + m_FontFaces(), + m_Fonts(), + m_CPFonts(8), + m_FamilyFonts(16), + m_UnicodeFonts(16), + m_BufferFonts(4), + m_FileFonts(4), + m_StreamFonts(4), + m_DeriveFonts(4), + m_pUserData(pUserData) { + if (m_pEnumerator != NULL) { + m_pEnumerator(m_FontFaces, m_pUserData, NULL, 0xFEFF); + } + if (m_pMatcher == NULL) { + m_pMatcher = FX_DefFontMatcher; + } + FXSYS_assert(m_pMatcher != NULL); +} +CFX_StdFontMgrImp::~CFX_StdFontMgrImp() { + m_FontFaces.RemoveAll(); + m_CPFonts.RemoveAll(); + m_FamilyFonts.RemoveAll(); + m_UnicodeFonts.RemoveAll(); + m_BufferFonts.RemoveAll(); + m_FileFonts.RemoveAll(); + m_StreamFonts.RemoveAll(); + m_DeriveFonts.RemoveAll(); + for (int32_t i = m_Fonts.GetUpperBound(); i >= 0; i--) { + IFX_Font* pFont = (IFX_Font*)m_Fonts[i]; + if (pFont != NULL) { + pFont->Release(); + } + } + m_Fonts.RemoveAll(); +} +IFX_Font* CFX_StdFontMgrImp::GetDefFontByCodePage( + FX_WORD wCodePage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily) { + FX_DWORD dwHash = FGAS_GetFontHashCode(wCodePage, dwFontStyles); + IFX_Font* pFont = NULL; + if (m_CPFonts.Lookup((void*)(uintptr_t)dwHash, (void*&)pFont)) { + return pFont ? LoadFont(pFont, dwFontStyles, wCodePage) : NULL; + } + FX_LPCFONTDESCRIPTOR pFD; + if ((pFD = FindFont(pszFontFamily, dwFontStyles, TRUE, wCodePage)) == NULL) { + if ((pFD = FindFont(NULL, dwFontStyles, TRUE, wCodePage)) == NULL) { + if ((pFD = FindFont(NULL, dwFontStyles, FALSE, wCodePage)) == NULL) + return NULL; + } + } + FXSYS_assert(pFD != NULL); + pFont = IFX_Font::LoadFont(pFD->wsFontFace, dwFontStyles, wCodePage, this); + if (pFont != NULL) { + m_Fonts.Add(pFont); + m_CPFonts.SetAt((void*)(uintptr_t)dwHash, (void*)pFont); + dwHash = FGAS_GetFontFamilyHash(pFD->wsFontFace, dwFontStyles, wCodePage); + m_FamilyFonts.SetAt((void*)(uintptr_t)dwHash, (void*)pFont); + return LoadFont(pFont, dwFontStyles, wCodePage); + } + return NULL; +} +IFX_Font* CFX_StdFontMgrImp::GetDefFontByCharset( + uint8_t nCharset, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily) { + return GetDefFontByCodePage(FX_GetCodePageFromCharset(nCharset), dwFontStyles, + pszFontFamily); +} + +IFX_Font* CFX_StdFontMgrImp::GetDefFontByUnicode( + FX_WCHAR wUnicode, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily) { + const FGAS_FONTUSB* pRet = FGAS_GetUnicodeBitField(wUnicode); + if (pRet->wBitField == 999) + return nullptr; + + FX_DWORD dwHash = + FGAS_GetFontFamilyHash(pszFontFamily, dwFontStyles, pRet->wBitField); + IFX_Font* pFont = nullptr; + if (m_UnicodeFonts.Lookup((void*)(uintptr_t)dwHash, (void*&)pFont)) + return pFont ? LoadFont(pFont, dwFontStyles, pRet->wCodePage) : nullptr; + + FX_LPCFONTDESCRIPTOR pFD = + FindFont(pszFontFamily, dwFontStyles, FALSE, pRet->wCodePage, + pRet->wBitField, wUnicode); + if (!pFD && pszFontFamily) { + pFD = FindFont(nullptr, dwFontStyles, FALSE, pRet->wCodePage, + pRet->wBitField, wUnicode); + } + if (!pFD) + return nullptr; + + FX_WORD wCodePage = FX_GetCodePageFromCharset(pFD->uCharSet); + const FX_WCHAR* pFontFace = pFD->wsFontFace; + pFont = IFX_Font::LoadFont(pFontFace, dwFontStyles, wCodePage, this); + if (pFont) { + m_Fonts.Add(pFont); + m_UnicodeFonts.SetAt((void*)(uintptr_t)dwHash, (void*)pFont); + dwHash = FGAS_GetFontHashCode(wCodePage, dwFontStyles); + m_CPFonts.SetAt((void*)(uintptr_t)dwHash, (void*)pFont); + dwHash = FGAS_GetFontFamilyHash(pFontFace, dwFontStyles, wCodePage); + m_FamilyFonts.SetAt((void*)(uintptr_t)dwHash, (void*)pFont); + return LoadFont(pFont, dwFontStyles, wCodePage); + } + return nullptr; +} + +IFX_Font* CFX_StdFontMgrImp::GetDefFontByLanguage( + FX_WORD wLanguage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily) { + return GetDefFontByCodePage(FX_GetDefCodePageByLanguage(wLanguage), + dwFontStyles, pszFontFamily); +} +IFX_Font* CFX_StdFontMgrImp::LoadFont(const FX_WCHAR* pszFontFamily, + FX_DWORD dwFontStyles, + FX_WORD wCodePage) { + FX_DWORD dwHash = + FGAS_GetFontFamilyHash(pszFontFamily, dwFontStyles, wCodePage); + IFX_Font* pFont = NULL; + if (m_FamilyFonts.Lookup((void*)(uintptr_t)dwHash, (void*&)pFont)) { + return pFont ? LoadFont(pFont, dwFontStyles, wCodePage) : NULL; + } + FX_LPCFONTDESCRIPTOR pFD = NULL; + if ((pFD = FindFont(pszFontFamily, dwFontStyles, TRUE, wCodePage)) == NULL) { + if ((pFD = FindFont(pszFontFamily, dwFontStyles, FALSE, wCodePage)) == + NULL) { + return NULL; + } + } + FXSYS_assert(pFD != NULL); + if (wCodePage == 0xFFFF) { + wCodePage = FX_GetCodePageFromCharset(pFD->uCharSet); + } + pFont = IFX_Font::LoadFont(pFD->wsFontFace, dwFontStyles, wCodePage, this); + if (pFont != NULL) { + m_Fonts.Add(pFont); + m_FamilyFonts.SetAt((void*)(uintptr_t)dwHash, (void*)pFont); + dwHash = FGAS_GetFontHashCode(wCodePage, dwFontStyles); + m_CPFonts.SetAt((void*)(uintptr_t)dwHash, (void*)pFont); + return LoadFont(pFont, dwFontStyles, wCodePage); + } + return NULL; +} +IFX_Font* CFX_StdFontMgrImp::LoadFont(const uint8_t* pBuffer, int32_t iLength) { + FXSYS_assert(pBuffer != NULL && iLength > 0); + IFX_Font* pFont = NULL; + if (m_BufferFonts.Lookup((void*)pBuffer, (void*&)pFont)) { + if (pFont != NULL) { + return pFont->Retain(); + } + } + pFont = IFX_Font::LoadFont(pBuffer, iLength, this); + if (pFont != NULL) { + m_Fonts.Add(pFont); + m_BufferFonts.SetAt((void*)pBuffer, pFont); + return pFont->Retain(); + } + return NULL; +} +IFX_Font* CFX_StdFontMgrImp::LoadFont(const FX_WCHAR* pszFileName) { + FXSYS_assert(pszFileName != NULL); + FX_DWORD dwHash = FX_HashCode_String_GetW(pszFileName, -1); + IFX_Font* pFont = NULL; + if (m_FileFonts.Lookup((void*)(uintptr_t)dwHash, (void*&)pFont)) { + if (pFont != NULL) { + return pFont->Retain(); + } + } + pFont = IFX_Font::LoadFont(pszFileName, NULL); + if (pFont != NULL) { + m_Fonts.Add(pFont); + m_FileFonts.SetAt((void*)(uintptr_t)dwHash, (void*)pFont); + return pFont->Retain(); + } + return NULL; +} +IFX_Font* CFX_StdFontMgrImp::LoadFont(IFX_Stream* pFontStream, + const FX_WCHAR* pszFontAlias, + FX_DWORD dwFontStyles, + FX_WORD wCodePage, + FX_BOOL bSaveStream) { + FXSYS_assert(pFontStream != NULL && pFontStream->GetLength() > 0); + IFX_Font* pFont = NULL; + if (m_StreamFonts.Lookup((void*)pFontStream, (void*&)pFont)) { + if (pFont != NULL) { + if (pszFontAlias != NULL) { + FX_DWORD dwHash = + FGAS_GetFontFamilyHash(pszFontAlias, dwFontStyles, wCodePage); + m_FamilyFonts.SetAt((void*)(uintptr_t)dwHash, (void*)pFont); + } + return LoadFont(pFont, dwFontStyles, wCodePage); + } + } + pFont = IFX_Font::LoadFont(pFontStream, this, bSaveStream); + if (pFont != NULL) { + m_Fonts.Add(pFont); + m_StreamFonts.SetAt((void*)pFontStream, (void*)pFont); + if (pszFontAlias != NULL) { + FX_DWORD dwHash = + FGAS_GetFontFamilyHash(pszFontAlias, dwFontStyles, wCodePage); + m_FamilyFonts.SetAt((void*)(uintptr_t)dwHash, (void*)pFont); + } + return LoadFont(pFont, dwFontStyles, wCodePage); + } + return NULL; +} +IFX_Font* CFX_StdFontMgrImp::LoadFont(IFX_Font* pSrcFont, + FX_DWORD dwFontStyles, + FX_WORD wCodePage) { + FXSYS_assert(pSrcFont != NULL); + if (pSrcFont->GetFontStyles() == dwFontStyles) { + return pSrcFont->Retain(); + } + void* buffer[3] = {pSrcFont, (void*)(uintptr_t)dwFontStyles, + (void*)(uintptr_t)wCodePage}; + FX_DWORD dwHash = + FX_HashCode_String_GetA((const FX_CHAR*)buffer, 3 * sizeof(void*)); + IFX_Font* pFont = NULL; + if (m_DeriveFonts.GetCount() > 0) { + m_DeriveFonts.Lookup((void*)(uintptr_t)dwHash, (void*&)pFont); + if (pFont != NULL) { + return pFont->Retain(); + } + } + pFont = pSrcFont->Derive(dwFontStyles, wCodePage); + if (pFont != NULL) { + m_DeriveFonts.SetAt((void*)(uintptr_t)dwHash, (void*)pFont); + int32_t index = m_Fonts.Find(pFont); + if (index < 0) { + m_Fonts.Add(pFont); + pFont->Retain(); + } + return pFont; + } + return NULL; +} +void CFX_StdFontMgrImp::ClearFontCache() { + int32_t iCount = m_Fonts.GetSize(); + for (int32_t i = 0; i < iCount; i++) { + IFX_Font* pFont = (IFX_Font*)m_Fonts[i]; + if (pFont != NULL) { + pFont->Reset(); + } + } +} +void CFX_StdFontMgrImp::RemoveFont(CFX_MapPtrToPtr& fontMap, IFX_Font* pFont) { + FX_POSITION pos = fontMap.GetStartPosition(); + void* pKey; + void* pFind; + while (pos != NULL) { + pFind = NULL; + fontMap.GetNextAssoc(pos, pKey, pFind); + if (pFind != (void*)pFont) { + continue; + } + fontMap.RemoveKey(pKey); + break; + } +} +void CFX_StdFontMgrImp::RemoveFont(IFX_Font* pFont) { + RemoveFont(m_CPFonts, pFont); + RemoveFont(m_FamilyFonts, pFont); + RemoveFont(m_UnicodeFonts, pFont); + RemoveFont(m_BufferFonts, pFont); + RemoveFont(m_FileFonts, pFont); + RemoveFont(m_StreamFonts, pFont); + RemoveFont(m_DeriveFonts, pFont); + int32_t iFind = m_Fonts.Find(pFont); + if (iFind > -1) { + m_Fonts.RemoveAt(iFind, 1); + } +} +FX_LPCFONTDESCRIPTOR CFX_StdFontMgrImp::FindFont(const FX_WCHAR* pszFontFamily, + FX_DWORD dwFontStyles, + FX_DWORD dwMatchFlags, + FX_WORD wCodePage, + FX_DWORD dwUSB, + FX_WCHAR wUnicode) { + if (m_pMatcher == NULL) { + return NULL; + } + FX_FONTMATCHPARAMS params; + FXSYS_memset(¶ms, 0, sizeof(params)); + params.dwUSB = dwUSB; + params.wUnicode = wUnicode; + params.wCodePage = wCodePage; + params.pwsFamily = pszFontFamily; + params.dwFontStyles = dwFontStyles; + params.dwMatchFlags = dwMatchFlags; + FX_LPCFONTDESCRIPTOR pDesc = m_pMatcher(¶ms, m_FontFaces, m_pUserData); + if (pDesc) { + return pDesc; + } + if (pszFontFamily && m_pEnumerator) { + CFX_FontDescriptors namedFonts; + m_pEnumerator(namedFonts, m_pUserData, pszFontFamily, wUnicode); + params.pwsFamily = NULL; + pDesc = m_pMatcher(¶ms, namedFonts, m_pUserData); + if (pDesc == NULL) { + return NULL; + } + for (int32_t i = m_FontFaces.GetSize() - 1; i >= 0; i--) { + FX_LPCFONTDESCRIPTOR pMatch = m_FontFaces.GetPtrAt(i); + if (*pMatch == *pDesc) { + return pMatch; + } + } + int index = m_FontFaces.Add(*pDesc); + return m_FontFaces.GetPtrAt(index); + } + return NULL; +} +FX_LPCFONTDESCRIPTOR FX_DefFontMatcher(FX_LPFONTMATCHPARAMS pParams, + const CFX_FontDescriptors& fonts, + void* pUserData) { + FX_LPCFONTDESCRIPTOR pBestFont = NULL; + int32_t iBestSimilar = 0; + FX_BOOL bMatchStyle = + (pParams->dwMatchFlags & FX_FONTMATCHPARA_MacthStyle) > 0; + int32_t iCount = fonts.GetSize(); + for (int32_t i = 0; i < iCount; ++i) { + FX_LPCFONTDESCRIPTOR pFont = fonts.GetPtrAt(i); + if ((pFont->dwFontStyles & FX_FONTSTYLE_BoldItalic) == + FX_FONTSTYLE_BoldItalic) { + continue; + } + if (pParams->pwsFamily) { + if (FXSYS_wcsicmp(pParams->pwsFamily, pFont->wsFontFace)) { + continue; + } + if (pFont->uCharSet == FX_CHARSET_Symbol) { + return pFont; + } + } + if (pFont->uCharSet == FX_CHARSET_Symbol) { + continue; + } + if (pParams->wCodePage != 0xFFFF) { + if (FX_GetCodePageFromCharset(pFont->uCharSet) != pParams->wCodePage) { + continue; + } + } else { + if (pParams->dwUSB < 128) { + FX_DWORD dwByte = pParams->dwUSB / 32; + FX_DWORD dwUSB = 1 << (pParams->dwUSB % 32); + if ((pFont->FontSignature.fsUsb[dwByte] & dwUSB) == 0) { + continue; + } + } + } + if (bMatchStyle) { + if ((pFont->dwFontStyles & 0x0F) == (pParams->dwFontStyles & 0x0F)) { + return pFont; + } else { + continue; + } + } + if (pParams->pwsFamily != NULL) { + if (FXSYS_wcsicmp(pParams->pwsFamily, pFont->wsFontFace) == 0) { + return pFont; + } + } + int32_t iSimilarValue = FX_GetSimilarValue(pFont, pParams->dwFontStyles); + if (iBestSimilar < iSimilarValue) { + iBestSimilar = iSimilarValue; + pBestFont = pFont; + } + } + return iBestSimilar < 1 ? NULL : pBestFont; +} +int32_t FX_GetSimilarValue(FX_LPCFONTDESCRIPTOR pFont, FX_DWORD dwFontStyles) { + int32_t iValue = 0; + if ((dwFontStyles & FX_FONTSTYLE_Symbolic) == + (pFont->dwFontStyles & FX_FONTSTYLE_Symbolic)) { + iValue += 64; + } + if ((dwFontStyles & FX_FONTSTYLE_FixedPitch) == + (pFont->dwFontStyles & FX_FONTSTYLE_FixedPitch)) { + iValue += 32; + } + if ((dwFontStyles & FX_FONTSTYLE_Serif) == + (pFont->dwFontStyles & FX_FONTSTYLE_Serif)) { + iValue += 16; + } + if ((dwFontStyles & FX_FONTSTYLE_Script) == + (pFont->dwFontStyles & FX_FONTSTYLE_Script)) { + iValue += 8; + } + return iValue; +} +FX_LPMatchFont FX_GetDefFontMatchor() { + return FX_DefFontMatcher; +} +FX_DWORD FX_GetGdiFontStyles(const LOGFONTW& lf) { + FX_DWORD dwStyles = 0; + if ((lf.lfPitchAndFamily & 0x03) == FIXED_PITCH) { + dwStyles |= FX_FONTSTYLE_FixedPitch; + } + uint8_t nFamilies = lf.lfPitchAndFamily & 0xF0; + if (nFamilies == FF_ROMAN) { + dwStyles |= FX_FONTSTYLE_Serif; + } + if (nFamilies == FF_SCRIPT) { + dwStyles |= FX_FONTSTYLE_Script; + } + if (lf.lfCharSet == SYMBOL_CHARSET) { + dwStyles |= FX_FONTSTYLE_Symbolic; + } + return dwStyles; +} +static int32_t CALLBACK FX_GdiFontEnumProc(ENUMLOGFONTEX* lpelfe, + NEWTEXTMETRICEX* lpntme, + DWORD dwFontType, + LPARAM lParam) { + if (dwFontType != TRUETYPE_FONTTYPE) { + return 1; + } + const LOGFONTW& lf = ((LPENUMLOGFONTEXW)lpelfe)->elfLogFont; + if (lf.lfFaceName[0] == L'@') { + return 1; + } + FX_LPFONTDESCRIPTOR pFont = FX_Alloc(FX_FONTDESCRIPTOR, 1); + FXSYS_memset(pFont, 0, sizeof(FX_FONTDESCRIPTOR)); + pFont->uCharSet = lf.lfCharSet; + pFont->dwFontStyles = FX_GetGdiFontStyles(lf); + FXSYS_wcsncpy(pFont->wsFontFace, (const FX_WCHAR*)lf.lfFaceName, 31); + pFont->wsFontFace[31] = 0; + FXSYS_memcpy(&pFont->FontSignature, &lpntme->ntmFontSig, + sizeof(lpntme->ntmFontSig)); + ((CFX_FontDescriptors*)lParam)->Add(*pFont); + FX_Free(pFont); + return 1; +} +static void FX_EnumGdiFonts(CFX_FontDescriptors& fonts, + void* pUserData, + const FX_WCHAR* pwsFaceName, + FX_WCHAR wUnicode) { + HDC hDC = ::GetDC(NULL); + LOGFONTW lfFind; + FXSYS_memset(&lfFind, 0, sizeof(lfFind)); + lfFind.lfCharSet = DEFAULT_CHARSET; + if (pwsFaceName) { + FXSYS_wcsncpy((FX_WCHAR*)lfFind.lfFaceName, pwsFaceName, 31); + lfFind.lfFaceName[31] = 0; + } + EnumFontFamiliesExW(hDC, (LPLOGFONTW)&lfFind, + (FONTENUMPROCW)FX_GdiFontEnumProc, (LPARAM)&fonts, 0); + ::ReleaseDC(NULL, hDC); +} +FX_LPEnumAllFonts FX_GetDefFontEnumerator() { + return FX_EnumGdiFonts; +} +#else +const FX_CHAR* g_FontFolders[] = { +#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ + "/usr/share/fonts", "/usr/share/X11/fonts/Type1", + "/usr/share/X11/fonts/TTF", "/usr/local/share/fonts", +#elif _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ + "~/Library/Fonts", "/Library/Fonts", "/System/Library/Fonts", +#elif _FXM_PLATFORM_ == _FXM_PLATFORM_ANDROID_ + "/system/fonts", +#endif +}; + +CFX_FontSourceEnum_File::CFX_FontSourceEnum_File() { + for (size_t i = 0; i < FX_ArraySize(g_FontFolders); ++i) + m_FolderPaths.Add(g_FontFolders[i]); +} + +CFX_ByteString CFX_FontSourceEnum_File::GetNextFile() { +Restart: + void* pCurHandle = + m_FolderQueue.GetSize() == 0 + ? NULL + : m_FolderQueue.GetDataPtr(m_FolderQueue.GetSize() - 1)->pFileHandle; + if (NULL == pCurHandle) { + if (m_FolderPaths.GetSize() < 1) { + return ""; + } + pCurHandle = FX_OpenFolder(m_FolderPaths[m_FolderPaths.GetSize() - 1]); + FX_HandleParentPath hpp; + hpp.pFileHandle = pCurHandle; + hpp.bsParentPath = m_FolderPaths[m_FolderPaths.GetSize() - 1]; + m_FolderQueue.Add(hpp); + } + CFX_ByteString bsName; + FX_BOOL bFolder; + CFX_ByteString bsFolderSpearator = + CFX_ByteString::FromUnicode(CFX_WideString(FX_GetFolderSeparator())); + while (TRUE) { + if (!FX_GetNextFile(pCurHandle, bsName, bFolder)) { + FX_CloseFolder(pCurHandle); + m_FolderQueue.RemoveAt(m_FolderQueue.GetSize() - 1); + if (m_FolderQueue.GetSize() == 0) { + m_FolderPaths.RemoveAt(m_FolderPaths.GetSize() - 1); + if (m_FolderPaths.GetSize() == 0) { + return ""; + } else { + goto Restart; + } + } + pCurHandle = + m_FolderQueue.GetDataPtr(m_FolderQueue.GetSize() - 1)->pFileHandle; + continue; + } + if (bsName == "." || bsName == "..") { + continue; + } + if (bFolder) { + FX_HandleParentPath hpp; + hpp.bsParentPath = + m_FolderQueue.GetDataPtr(m_FolderQueue.GetSize() - 1)->bsParentPath + + bsFolderSpearator + bsName; + hpp.pFileHandle = FX_OpenFolder(hpp.bsParentPath); + if (hpp.pFileHandle == NULL) { + continue; + } + m_FolderQueue.Add(hpp); + pCurHandle = hpp.pFileHandle; + continue; + } + bsName = + m_FolderQueue.GetDataPtr(m_FolderQueue.GetSize() - 1)->bsParentPath + + bsFolderSpearator + bsName; + break; + } + return bsName; +} +FX_POSITION CFX_FontSourceEnum_File::GetStartPosition(void* pUserData) { + m_wsNext = GetNextFile().UTF8Decode(); + if (0 == m_wsNext.GetLength()) { + return (FX_POSITION)0; + } + return (FX_POSITION)-1; +} +IFX_FileAccess* CFX_FontSourceEnum_File::GetNext(FX_POSITION& pos, + void* pUserData) { + IFX_FileAccess* pAccess = FX_CreateDefaultFileAccess(m_wsNext); + m_wsNext = GetNextFile().UTF8Decode(); + pos = 0 != m_wsNext.GetLength() ? pAccess : NULL; + return (IFX_FileAccess*)pAccess; +} +IFX_FontSourceEnum* FX_CreateDefaultFontSourceEnum() { + return (IFX_FontSourceEnum*)new CFX_FontSourceEnum_File; +} +IFX_FontMgr* IFX_FontMgr::Create(IFX_FontSourceEnum* pFontEnum, + IFX_FontMgrDelegate* pDelegate, + void* pUserData) { + if (NULL == pFontEnum) { + return NULL; + } + CFX_FontMgrImp* pFontMgr = + new CFX_FontMgrImp(pFontEnum, pDelegate, pUserData); + if (pFontMgr->EnumFonts()) { + return pFontMgr; + } + delete pFontMgr; + return NULL; +} +CFX_FontMgrImp::CFX_FontMgrImp(IFX_FontSourceEnum* pFontEnum, + IFX_FontMgrDelegate* pDelegate, + void* pUserData) + : m_pFontSource(pFontEnum), + m_pDelegate(pDelegate), + m_pUserData(pUserData) {} + +FX_BOOL CFX_FontMgrImp::EnumFontsFromFontMapper() { + CFX_FontMapper* pFontMapper = + CFX_GEModule::Get()->GetFontMgr()->GetBuiltinMapper(); + if (!pFontMapper) + return FALSE; + IFX_SystemFontInfo* pSystemFontInfo = pFontMapper->GetSystemFontInfo(); + if (!pSystemFontInfo) + return FALSE; + pSystemFontInfo->EnumFontList(pFontMapper); + for (int32_t i = 0; i < pFontMapper->GetFaceSize(); ++i) { + IFX_FileRead* pFontStream = + CreateFontStream(pFontMapper, pSystemFontInfo, i); + if (!pFontStream) + continue; + CFX_WideString wsFaceName = + CFX_WideString::FromLocal(pFontMapper->GetFaceName(i).c_str()); + RegisterFaces(pFontStream, &wsFaceName); + pFontStream->Release(); + } + if (m_InstalledFonts.GetSize() == 0) + return FALSE; + return TRUE; +} +FX_BOOL CFX_FontMgrImp::EnumFontsFromFiles() { + CFX_GEModule::Get()->GetFontMgr()->InitFTLibrary(); + FX_POSITION pos = m_pFontSource->GetStartPosition(); + IFX_FileAccess* pFontSource = nullptr; + IFX_FileRead* pFontStream = nullptr; + while (pos) { + pFontSource = m_pFontSource->GetNext(pos); + pFontStream = pFontSource->CreateFileStream(FX_FILEMODE_ReadOnly); + if (!pFontStream) { + pFontSource->Release(); + continue; + } + RegisterFaces(pFontStream, nullptr); + pFontStream->Release(); + pFontSource->Release(); + } + if (m_InstalledFonts.GetSize() == 0) + return FALSE; + return TRUE; +} +FX_BOOL CFX_FontMgrImp::EnumFonts() { + if (EnumFontsFromFontMapper()) + return TRUE; + return EnumFontsFromFiles(); +} +void CFX_FontMgrImp::Release() { + for (int32_t i = 0; i < m_InstalledFonts.GetSize(); i++) { + delete m_InstalledFonts[i]; + } + FX_POSITION pos = m_Hash2CandidateList.GetStartPosition(); + while (pos) { + FX_DWORD dwHash; + CFX_FontDescriptorInfos* pDescs; + m_Hash2CandidateList.GetNextAssoc(pos, dwHash, pDescs); + if (NULL != pDescs) { + delete pDescs; + } + } + pos = m_Hash2Fonts.GetStartPosition(); + while (pos) { + FX_DWORD dwHash; + CFX_ArrayTemplate<IFX_Font*>* pFonts; + m_Hash2Fonts.GetNextAssoc(pos, dwHash, pFonts); + if (NULL != pFonts) { + delete pFonts; + } + } + m_Hash2Fonts.RemoveAll(); + pos = m_Hash2FileAccess.GetStartPosition(); + while (pos) { + FX_DWORD dwHash; + IFX_FileAccess* pFileAccess; + m_Hash2FileAccess.GetNextAssoc(pos, dwHash, pFileAccess); + if (NULL != pFileAccess) { + pFileAccess->Release(); + } + } + pos = m_FileAccess2IFXFont.GetStartPosition(); + while (pos) { + FX_DWORD dwHash; + IFX_Font* pFont; + m_FileAccess2IFXFont.GetNextAssoc(pos, dwHash, pFont); + if (NULL != pFont) { + pFont->Release(); + } + } + pos = m_IFXFont2FileRead.GetStartPosition(); + while (pos) { + IFX_Font* pFont; + IFX_FileRead* pFileRead; + m_IFXFont2FileRead.GetNextAssoc(pos, pFont, pFileRead); + pFileRead->Release(); + } + delete this; +} +IFX_Font* CFX_FontMgrImp::GetDefFontByCodePage(FX_WORD wCodePage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily) { + return NULL == m_pDelegate ? NULL : m_pDelegate->GetDefFontByCodePage( + this, wCodePage, dwFontStyles, + pszFontFamily); +} +IFX_Font* CFX_FontMgrImp::GetDefFontByCharset(uint8_t nCharset, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily) { + return NULL == m_pDelegate ? NULL + : m_pDelegate->GetDefFontByCharset( + this, nCharset, dwFontStyles, pszFontFamily); +} +IFX_Font* CFX_FontMgrImp::GetDefFontByUnicode(FX_WCHAR wUnicode, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily) { + return NULL == m_pDelegate ? NULL + : m_pDelegate->GetDefFontByUnicode( + this, wUnicode, dwFontStyles, pszFontFamily); +} +IFX_Font* CFX_FontMgrImp::GetDefFontByLanguage(FX_WORD wLanguage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily) { + return NULL == m_pDelegate ? NULL : m_pDelegate->GetDefFontByLanguage( + this, wLanguage, dwFontStyles, + pszFontFamily); +} +IFX_Font* CFX_FontMgrImp::GetFontByCodePage(FX_WORD wCodePage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily) { + CFX_ByteString bsHash; + bsHash.Format("%d, %d", wCodePage, dwFontStyles); + bsHash += CFX_WideString(pszFontFamily).UTF8Encode(); + FX_DWORD dwHash = FX_HashCode_String_GetA(bsHash, bsHash.GetLength()); + CFX_ArrayTemplate<IFX_Font*>* pFonts = NULL; + IFX_Font* pFont = NULL; + if (m_Hash2Fonts.Lookup(dwHash, pFonts)) { + if (NULL == pFonts) { + return NULL; + } + if (0 != pFonts->GetSize()) { + return pFonts->GetAt(0)->Retain(); + } + } + if (!pFonts) + pFonts = new CFX_ArrayTemplate<IFX_Font*>; + m_Hash2Fonts.SetAt(dwHash, pFonts); + CFX_FontDescriptorInfos* sortedFonts = NULL; + if (!m_Hash2CandidateList.Lookup(dwHash, sortedFonts)) { + sortedFonts = new CFX_FontDescriptorInfos; + MatchFonts(*sortedFonts, wCodePage, dwFontStyles, + CFX_WideString(pszFontFamily), 0); + m_Hash2CandidateList.SetAt(dwHash, sortedFonts); + } + if (sortedFonts->GetSize() == 0) { + return NULL; + } + CFX_FontDescriptor* pDesc = sortedFonts->GetAt(0).pFont; + if (pDesc->m_pFileAccess) + pFont = LoadFont(pDesc->m_pFileAccess, pDesc->m_nFaceIndex, nullptr); + else + pFont = LoadFont(pDesc->m_wsFaceName, pDesc->m_nFaceIndex, nullptr); + if (NULL != pFont) { + pFont->SetLogicalFontStyle(dwFontStyles); + } + pFonts->Add(pFont); + return pFont; +} +IFX_Font* CFX_FontMgrImp::GetFontByCharset(uint8_t nCharset, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily) { + return GetFontByCodePage(FX_GetCodePageFromCharset(nCharset), dwFontStyles, + pszFontFamily); +} +IFX_Font* CFX_FontMgrImp::GetFontByUnicode(FX_WCHAR wUnicode, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily) { + IFX_Font* pFont = nullptr; + if (m_FailedUnicodes2NULL.Lookup(wUnicode, pFont)) + return nullptr; + const FGAS_FONTUSB* x = FGAS_GetUnicodeBitField(wUnicode); + FX_WORD wCodePage = x ? x->wCodePage : 0xFFFF; + FX_WORD wBitField = x ? x->wBitField : 0x03E7; + CFX_ByteString bsHash; + if (wCodePage == 0xFFFF) + bsHash.Format("%d, %d, %d", wCodePage, wBitField, dwFontStyles); + else + bsHash.Format("%d, %d", wCodePage, dwFontStyles); + bsHash += CFX_WideString(pszFontFamily).UTF8Encode(); + FX_DWORD dwHash = FX_HashCode_String_GetA(bsHash, bsHash.GetLength()); + CFX_ArrayTemplate<IFX_Font*>* pFonts = nullptr; + if (m_Hash2Fonts.Lookup(dwHash, pFonts)) { + if (!pFonts) + return nullptr; + for (int32_t i = 0; i < pFonts->GetSize(); ++i) { + if (VerifyUnicode(pFonts->GetAt(i), wUnicode)) + return pFonts->GetAt(i)->Retain(); + } + } + if (!pFonts) + pFonts = new CFX_ArrayTemplate<IFX_Font*>; + m_Hash2Fonts.SetAt(dwHash, pFonts); + CFX_FontDescriptorInfos* sortedFonts = nullptr; + if (!m_Hash2CandidateList.Lookup(dwHash, sortedFonts)) { + sortedFonts = new CFX_FontDescriptorInfos; + MatchFonts(*sortedFonts, wCodePage, dwFontStyles, + CFX_WideString(pszFontFamily), wUnicode); + m_Hash2CandidateList.SetAt(dwHash, sortedFonts); + } + for (int32_t i = 0; i < sortedFonts->GetSize(); ++i) { + CFX_FontDescriptor* pDesc = sortedFonts->GetAt(i).pFont; + if (!VerifyUnicode(pDesc, wUnicode)) + continue; + if (pDesc->m_pFileAccess) + pFont = LoadFont(pDesc->m_pFileAccess, pDesc->m_nFaceIndex, nullptr); + else + pFont = LoadFont(pDesc->m_wsFaceName, pDesc->m_nFaceIndex, nullptr); + if (!pFont) + continue; + pFont->SetLogicalFontStyle(dwFontStyles); + pFonts->Add(pFont); + return pFont; + } + if (!pszFontFamily) + m_FailedUnicodes2NULL.SetAt(wUnicode, nullptr); + return nullptr; +} +FX_BOOL CFX_FontMgrImp::VerifyUnicode(CFX_FontDescriptor* pDesc, + FX_WCHAR wcUnicode) { + IFX_FileRead* pFileRead = nullptr; + if (pDesc->m_pFileAccess) + pFileRead = pDesc->m_pFileAccess->CreateFileStream(FX_FILEMODE_ReadOnly); + else + pFileRead = CreateFontStream(pDesc->m_wsFaceName.UTF8Encode()); + if (!pFileRead) + return FALSE; + FXFT_Face pFace = LoadFace(pFileRead, pDesc->m_nFaceIndex); + FT_Error retCharmap = FXFT_Select_Charmap(pFace, FXFT_ENCODING_UNICODE); + FT_Error retIndex = FXFT_Get_Char_Index(pFace, wcUnicode); + pFileRead->Release(); + if (!pFace) + return FALSE; + if (FXFT_Get_Face_External_Stream(pFace)) + FXFT_Clear_Face_External_Stream(pFace); + FXFT_Done_Face(pFace); + return !retCharmap && retIndex; +} +FX_BOOL CFX_FontMgrImp::VerifyUnicode(IFX_Font* pFont, FX_WCHAR wcUnicode) { + if (NULL == pFont) { + return FALSE; + } + FXFT_Face pFace = ((CFX_Font*)pFont->GetDevFont())->GetFace(); + FXFT_CharMap charmap = FXFT_Get_Face_Charmap(pFace); + if (0 != FXFT_Select_Charmap(pFace, FXFT_ENCODING_UNICODE)) { + return FALSE; + } + if (0 == FXFT_Get_Char_Index(pFace, wcUnicode)) { + FXFT_Set_Charmap(pFace, charmap); + return FALSE; + } + return TRUE; +} +IFX_Font* CFX_FontMgrImp::GetFontByLanguage(FX_WORD wLanguage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily) { + return GetFontByCodePage(FX_GetDefCodePageByLanguage(wLanguage), dwFontStyles, + pszFontFamily); +} +IFX_Font* CFX_FontMgrImp::LoadFont(const uint8_t* pBuffer, + int32_t iLength, + int32_t iFaceIndex, + int32_t* pFaceCount) { + void* Hash[2] = {(void*)(uintptr_t)pBuffer, (void*)(uintptr_t)iLength}; + FX_DWORD dwHash = + FX_HashCode_String_GetA((const FX_CHAR*)Hash, 2 * sizeof(void*)); + IFX_FileAccess* pFontAccess = NULL; + if (!m_Hash2FileAccess.Lookup(dwHash, pFontAccess)) { + } + if (NULL != pFontAccess) { + return LoadFont(pFontAccess, iFaceIndex, pFaceCount, TRUE); + } else { + return NULL; + } +} +IFX_Font* CFX_FontMgrImp::LoadFont(const FX_WCHAR* pszFileName, + int32_t iFaceIndex, + int32_t* pFaceCount) { + CFX_ByteString bsHash; + bsHash += CFX_WideString(pszFileName).UTF8Encode(); + FX_DWORD dwHash = + FX_HashCode_String_GetA((const FX_CHAR*)bsHash, bsHash.GetLength()); + IFX_FileAccess* pFontAccess = NULL; + if (!m_Hash2FileAccess.Lookup(dwHash, pFontAccess)) { + pFontAccess = FX_CreateDefaultFileAccess(pszFileName); + m_Hash2FileAccess.SetAt(dwHash, pFontAccess); + } + if (NULL != pFontAccess) { + return LoadFont(pFontAccess, iFaceIndex, pFaceCount, TRUE); + } else { + return NULL; + } +} +IFX_Font* CFX_FontMgrImp::LoadFont(IFX_Stream* pFontStream, + int32_t iFaceIndex, + int32_t* pFaceCount, + FX_BOOL bSaveStream) { + void* Hash[1] = {(void*)(uintptr_t)pFontStream}; + FX_DWORD dwHash = + FX_HashCode_String_GetA((const FX_CHAR*)Hash, 1 * sizeof(void*)); + IFX_FileAccess* pFontAccess = NULL; + if (!m_Hash2FileAccess.Lookup(dwHash, pFontAccess)) { + } + if (NULL != pFontAccess) { + return LoadFont(pFontAccess, iFaceIndex, pFaceCount, TRUE); + } else { + return NULL; + } +} +IFX_Font* CFX_FontMgrImp::LoadFont(IFX_FileAccess* pFontAccess, + int32_t iFaceIndex, + int32_t* pFaceCount, + FX_BOOL bWantCache) { + FX_DWORD dwHash = 0; + IFX_Font* pFont = NULL; + if (bWantCache) { + CFX_ByteString bsHash; + bsHash.Format("%d, %d", (uintptr_t)pFontAccess, iFaceIndex); + dwHash = FX_HashCode_String_GetA(bsHash, bsHash.GetLength()); + if (m_FileAccess2IFXFont.Lookup(dwHash, pFont)) { + if (NULL != pFont) { + if (NULL != pFaceCount) { + *pFaceCount = ((CFX_Font*)pFont->GetDevFont())->GetFace()->num_faces; + } + return pFont->Retain(); + } + } + } + CFX_Font* pInternalFont = new CFX_Font; + IFX_FileRead* pFontStream = + pFontAccess->CreateFileStream(FX_FILEMODE_ReadOnly); + if (NULL == pFontStream) { + delete pInternalFont; + return NULL; + } + if (!pInternalFont->LoadFile(pFontStream, iFaceIndex)) { + delete pInternalFont; + pFontStream->Release(); + return NULL; + } + pFont = IFX_Font::LoadFont(pInternalFont, this, TRUE); + if (NULL == pFont) { + delete pInternalFont; + pFontStream->Release(); + return NULL; + } + if (bWantCache) { + m_FileAccess2IFXFont.SetAt(dwHash, pFont); + } + m_IFXFont2FileRead.SetAt(pFont, pFontStream); + if (NULL != pFaceCount) { + *pFaceCount = ((CFX_Font*)pFont->GetDevFont())->GetFace()->num_faces; + } + return pFont; +} +IFX_Font* CFX_FontMgrImp::LoadFont(const CFX_WideString& wsFaceName, + int32_t iFaceIndex, + int32_t* pFaceCount) { + CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr(); + CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper(); + if (!pFontMapper) + return nullptr; + IFX_SystemFontInfo* pSystemFontInfo = pFontMapper->GetSystemFontInfo(); + if (!pSystemFontInfo) + return nullptr; + IFX_FileRead* pFontStream = CreateFontStream(wsFaceName.UTF8Encode()); + if (!pFontStream) + return nullptr; + if (!LoadFace(pFontStream, 0)) { + pFontStream->Release(); + return nullptr; + } + CFX_Font* pInternalFont = new CFX_Font(); + if (!pInternalFont->LoadFile(pFontStream, iFaceIndex)) { + pFontStream->Release(); + return nullptr; + } + IFX_Font* pFont = IFX_Font::LoadFont(pInternalFont, this, FALSE); + if (!pFont) { + pFontStream->Release(); + return nullptr; + } + m_IFXFont2FileRead.SetAt(pFont, pFontStream); + if (pFaceCount) + *pFaceCount = ((CFX_Font*)pFont->GetDevFont())->GetFace()->num_faces; + return pFont; +} +extern "C" { +unsigned long _ftStreamRead(FXFT_Stream stream, + unsigned long offset, + unsigned char* buffer, + unsigned long count) { + if (count == 0) { + return 0; + } + IFX_FileRead* pFile = (IFX_FileRead*)stream->descriptor.pointer; + int res = pFile->ReadBlock(buffer, offset, count); + if (res) { + return count; + } + return 0; +} +void _ftStreamClose(FXFT_Stream stream) {} +}; + +FXFT_Face CFX_FontMgrImp::LoadFace(IFX_FileRead* pFontStream, + int32_t iFaceIndex) { + if (!pFontStream) + return nullptr; + + CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr(); + pFontMgr->InitFTLibrary(); + FXFT_Library library = pFontMgr->GetFTLibrary(); + if (!library) + return nullptr; + + FXFT_Stream ftStream = FX_Alloc(FXFT_StreamRec, 1); + FXSYS_memset(ftStream, 0, sizeof(FXFT_StreamRec)); + ftStream->base = NULL; + ftStream->descriptor.pointer = pFontStream; + ftStream->pos = 0; + ftStream->size = (unsigned long)pFontStream->GetSize(); + ftStream->read = _ftStreamRead; + ftStream->close = _ftStreamClose; + + FXFT_Open_Args ftArgs; + FXSYS_memset(&ftArgs, 0, sizeof(FXFT_Open_Args)); + ftArgs.flags |= FT_OPEN_STREAM; + ftArgs.stream = ftStream; + + FXFT_Face pFace = NULL; + if (FXFT_Open_Face(library, &ftArgs, iFaceIndex, &pFace)) { + FX_Free(ftStream); + return nullptr; + } + + FXFT_Set_Pixel_Sizes(pFace, 0, 64); + return pFace; +} + +IFX_FileRead* CFX_FontMgrImp::CreateFontStream( + CFX_FontMapper* pFontMapper, + IFX_SystemFontInfo* pSystemFontInfo, + FX_DWORD index) { + int iExact = 0; + void* hFont = pSystemFontInfo->MapFont( + 0, 0, FXFONT_DEFAULT_CHARSET, 0, pFontMapper->GetFaceName(index), iExact); + if (!hFont) + return nullptr; + FX_DWORD dwFileSize = pSystemFontInfo->GetFontData(hFont, 0, nullptr, 0); + if (dwFileSize == 0) + return nullptr; + uint8_t* pBuffer = FX_Alloc(uint8_t, dwFileSize + 1); + dwFileSize = pSystemFontInfo->GetFontData(hFont, 0, pBuffer, dwFileSize); + return FX_CreateMemoryStream(pBuffer, dwFileSize, TRUE); +} + +IFX_FileRead* CFX_FontMgrImp::CreateFontStream( + const CFX_ByteString& bsFaceName) { + CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr(); + CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper(); + if (!pFontMapper) + return nullptr; + IFX_SystemFontInfo* pSystemFontInfo = pFontMapper->GetSystemFontInfo(); + if (!pSystemFontInfo) + return nullptr; + pSystemFontInfo->EnumFontList(pFontMapper); + for (int32_t i = 0; i < pFontMapper->GetFaceSize(); ++i) { + if (pFontMapper->GetFaceName(i) == bsFaceName) + return CreateFontStream(pFontMapper, pSystemFontInfo, i); + } + return nullptr; +} +int32_t CFX_FontMgrImp::MatchFonts(CFX_FontDescriptorInfos& MatchedFonts, + FX_WORD wCodePage, + FX_DWORD dwFontStyles, + const CFX_WideString& FontName, + FX_WCHAR wcUnicode) { + MatchedFonts.RemoveAll(); + CFX_WideString wsNormalizedFontName = FontName; + static const int32_t nMax = 0xffff; + CFX_FontDescriptor* pFont = NULL; + int32_t nCount = m_InstalledFonts.GetSize(); + for (int32_t i = 0; i < nCount; i++) { + pFont = m_InstalledFonts[i]; + int32_t nPenalty = CalcPenalty(pFont, wCodePage, dwFontStyles, + wsNormalizedFontName, wcUnicode); + if (nPenalty >= 0xFFFF) { + continue; + } + FX_FontDescriptorInfo FontInfo; + FontInfo.pFont = pFont; + FontInfo.nPenalty = nPenalty; + MatchedFonts.Add(FontInfo); + if (MatchedFonts.GetSize() == nMax) { + break; + } + } + if (MatchedFonts.GetSize() == 0) { + return 0; + } + CFX_SSortTemplate<FX_FontDescriptorInfo> ssort; + ssort.ShellSort(MatchedFonts.GetData(), MatchedFonts.GetSize()); + return MatchedFonts.GetSize(); +} +struct FX_BitCodePage { + FX_WORD wBit; + FX_WORD wCodePage; +}; +static const FX_BitCodePage g_Bit2CodePage[] = { + {0, 1252}, {1, 1250}, {2, 1251}, {3, 1253}, {4, 1254}, {5, 1255}, + {6, 1256}, {7, 1257}, {8, 1258}, {9, 0}, {10, 0}, {11, 0}, + {12, 0}, {13, 0}, {14, 0}, {15, 0}, {16, 874}, {17, 932}, + {18, 936}, {19, 949}, {20, 950}, {21, 1361}, {22, 0}, {23, 0}, + {24, 0}, {25, 0}, {26, 0}, {27, 0}, {28, 0}, {29, 0}, + {30, 0}, {31, 0}, {32, 0}, {33, 0}, {34, 0}, {35, 0}, + {36, 0}, {37, 0}, {38, 0}, {39, 0}, {40, 0}, {41, 0}, + {42, 0}, {43, 0}, {44, 0}, {45, 0}, {46, 0}, {47, 0}, + {48, 869}, {49, 866}, {50, 865}, {51, 864}, {52, 863}, {53, 862}, + {54, 861}, {55, 860}, {56, 857}, {57, 855}, {58, 852}, {59, 775}, + {60, 737}, {61, 708}, {62, 850}, {63, 437}, +}; + +FX_WORD FX_GetCodePageBit(FX_WORD wCodePage) { + for (size_t i = 0; i < FX_ArraySize(g_Bit2CodePage); ++i) { + if (g_Bit2CodePage[i].wCodePage == wCodePage) + return g_Bit2CodePage[i].wBit; + } + return (FX_WORD)-1; +} + +FX_WORD FX_GetUnicodeBit(FX_WCHAR wcUnicode) { + const FGAS_FONTUSB* x = FGAS_GetUnicodeBitField(wcUnicode); + return x ? x->wBitField : 999; +} + +int32_t CFX_FontMgrImp::CalcPenalty(CFX_FontDescriptor* pInstalled, + FX_WORD wCodePage, + FX_DWORD dwFontStyles, + const CFX_WideString& FontName, + FX_WCHAR wcUnicode) { + int32_t nPenalty = 30000; + if (0 != FontName.GetLength()) { + if (FontName != pInstalled->m_wsFaceName) { + int32_t i; + for (i = 0; i < pInstalled->m_wsFamilyNames.GetSize(); i++) { + if (pInstalled->m_wsFamilyNames[i] == FontName) { + break; + } + } + if (i == pInstalled->m_wsFamilyNames.GetSize()) { + nPenalty += 0xFFFF; + } else { + nPenalty -= 28000; + } + } else { + nPenalty -= 30000; + } + if (30000 == nPenalty && + 0 == IsPartName(pInstalled->m_wsFaceName, FontName)) { + int32_t i; + for (i = 0; i < pInstalled->m_wsFamilyNames.GetSize(); i++) { + if (0 != IsPartName(pInstalled->m_wsFamilyNames[i], FontName)) { + break; + } + } + if (i == pInstalled->m_wsFamilyNames.GetSize()) { + nPenalty += 0xFFFF; + } else { + nPenalty -= 26000; + } + } else { + nPenalty -= 27000; + } + } + FX_DWORD dwStyleMask = pInstalled->m_dwFontStyles ^ dwFontStyles; + if (dwStyleMask & FX_FONTSTYLE_Bold) { + nPenalty += 4500; + } + if (dwStyleMask & FX_FONTSTYLE_FixedPitch) { + nPenalty += 10000; + } + if (dwStyleMask & FX_FONTSTYLE_Italic) { + nPenalty += 10000; + } + if (dwStyleMask & FX_FONTSTYLE_Serif) { + nPenalty += 500; + } + if (dwStyleMask & FX_FONTSTYLE_Symbolic) { + nPenalty += 0xFFFF; + } + if (nPenalty >= 0xFFFF) { + return 0xFFFF; + } + FX_WORD wBit = + ((0 == wCodePage || 0xFFFF == wCodePage) ? (FX_WORD)-1 + : FX_GetCodePageBit(wCodePage)); + if (wBit != (FX_WORD)-1) { + FXSYS_assert(wBit < 64); + if (0 == (pInstalled->m_dwCsb[wBit / 32] & (1 << (wBit % 32)))) { + nPenalty += 0xFFFF; + } else { + nPenalty -= 60000; + } + } + wBit = + ((0 == wcUnicode || 0xFFFE == wcUnicode) ? (FX_WORD)999 + : FX_GetUnicodeBit(wcUnicode)); + if (wBit != (FX_WORD)999) { + FXSYS_assert(wBit < 128); + if (0 == (pInstalled->m_dwUsb[wBit / 32] & (1 << (wBit % 32)))) { + nPenalty += 0xFFFF; + } else { + nPenalty -= 60000; + } + } + return nPenalty; +} +void CFX_FontMgrImp::ClearFontCache() { + FX_POSITION pos = m_Hash2CandidateList.GetStartPosition(); + while (pos) { + FX_DWORD dwHash; + CFX_FontDescriptorInfos* pDescs; + m_Hash2CandidateList.GetNextAssoc(pos, dwHash, pDescs); + if (NULL != pDescs) { + delete pDescs; + } + } + pos = m_FileAccess2IFXFont.GetStartPosition(); + while (pos) { + FX_DWORD dwHash; + IFX_Font* pFont; + m_FileAccess2IFXFont.GetNextAssoc(pos, dwHash, pFont); + if (NULL != pFont) { + pFont->Release(); + } + } + pos = m_IFXFont2FileRead.GetStartPosition(); + while (pos) { + IFX_Font* pFont; + IFX_FileRead* pFileRead; + m_IFXFont2FileRead.GetNextAssoc(pos, pFont, pFileRead); + pFileRead->Release(); + } +} +void CFX_FontMgrImp::RemoveFont(IFX_Font* pEFont) { + if (NULL == pEFont) { + return; + } + IFX_FileRead* pFileRead; + if (m_IFXFont2FileRead.Lookup(pEFont, pFileRead)) { + pFileRead->Release(); + m_IFXFont2FileRead.RemoveKey(pEFont); + } + FX_POSITION pos; + pos = m_FileAccess2IFXFont.GetStartPosition(); + while (pos) { + FX_DWORD dwHash; + IFX_Font* pCFont; + m_FileAccess2IFXFont.GetNextAssoc(pos, dwHash, pCFont); + if (pCFont == pEFont) { + m_FileAccess2IFXFont.RemoveKey(dwHash); + break; + } + } + pos = m_Hash2Fonts.GetStartPosition(); + while (pos) { + FX_DWORD dwHash; + CFX_ArrayTemplate<IFX_Font*>* pFonts; + m_Hash2Fonts.GetNextAssoc(pos, dwHash, pFonts); + if (NULL != pFonts) { + for (int32_t i = 0; i < pFonts->GetSize(); i++) { + if (pFonts->GetAt(i) == pEFont) { + pFonts->SetAt(i, NULL); + } + } + } else { + m_Hash2Fonts.RemoveKey(dwHash); + } + } +} +void CFX_FontMgrImp::RegisterFace(FXFT_Face pFace, + CFX_FontDescriptors& Fonts, + const CFX_WideString* pFaceName, + IFX_FileAccess* pFontAccess) { + if (0 == (pFace->face_flags & FT_FACE_FLAG_SCALABLE)) { + return; + } + CFX_FontDescriptor* pFont = new CFX_FontDescriptor; + pFont->m_dwFontStyles |= FXFT_Is_Face_Bold(pFace) ? FX_FONTSTYLE_Bold : 0; + pFont->m_dwFontStyles |= FXFT_Is_Face_Italic(pFace) ? FX_FONTSTYLE_Italic : 0; + pFont->m_dwFontStyles |= GetFlags(pFace); + CFX_WordArray Charsets; + GetCharsets(pFace, Charsets); + GetUSBCSB(pFace, pFont->m_dwUsb, pFont->m_dwCsb); + unsigned long nLength = 0; + FT_ULong dwTag; + uint8_t* pTable = NULL; + FT_ENC_TAG(dwTag, 'n', 'a', 'm', 'e'); + unsigned int error = FXFT_Load_Sfnt_Table(pFace, dwTag, 0, NULL, &nLength); + if (0 == error && 0 != nLength) { + pTable = FX_Alloc(uint8_t, nLength); + error = FXFT_Load_Sfnt_Table(pFace, dwTag, 0, pTable, NULL); + if (0 != error) { + FX_Free(pTable); + pTable = NULL; + } + } + GetNames(pTable, pFont->m_wsFamilyNames); + if (NULL != pTable) { + FX_Free(pTable); + } + pFont->m_wsFamilyNames.Add(CFX_ByteString(pFace->family_name).UTF8Decode()); + if (pFaceName) { + pFont->m_wsFaceName = *pFaceName; + } else { + pFont->m_wsFaceName = + CFX_WideString::FromLocal(FXFT_Get_Postscript_Name(pFace)); + } + pFont->m_nFaceIndex = pFace->face_index; + if (pFontAccess) + pFont->m_pFileAccess = pFontAccess->Retain(); + else + pFont->m_pFileAccess = nullptr; + Fonts.Add(pFont); +} +void CFX_FontMgrImp::RegisterFaces(IFX_FileRead* pFontStream, + const CFX_WideString* pFaceName) { + int32_t index = 0; + int32_t num_faces = 0; + do { + FXFT_Face pFace = LoadFace(pFontStream, index++); + if (!pFace) + continue; + // All faces keep number of faces. It can be retrieved from any one face. + if (!num_faces) + num_faces = pFace->num_faces; + RegisterFace(pFace, m_InstalledFonts, pFaceName, nullptr); + if (FXFT_Get_Face_External_Stream(pFace)) + FXFT_Clear_Face_External_Stream(pFace); + FXFT_Done_Face(pFace); + } while (index < num_faces); +} +FX_DWORD CFX_FontMgrImp::GetFlags(FXFT_Face pFace) { + FX_DWORD flag = 0; + if (FT_IS_FIXED_WIDTH(pFace)) { + flag |= FX_FONTSTYLE_FixedPitch; + } + TT_OS2* pOS2 = (TT_OS2*)FT_Get_Sfnt_Table(pFace, ft_sfnt_os2); + if (!pOS2) { + return flag; + } + if (pOS2->ulCodePageRange1 & (1 << 31)) { + flag |= FX_FONTSTYLE_Symbolic; + } + if (pOS2->panose[0] == 2) { + uint8_t uSerif = pOS2->panose[1]; + if ((uSerif > 1 && uSerif < 10) || uSerif > 13) { + flag |= FX_FONTSTYLE_Serif; + } + } + return flag; +} +#define GetUInt8(p) ((uint8_t)((p)[0])) +#define GetUInt16(p) ((uint16_t)((p)[0] << 8 | (p)[1])) +#define GetUInt32(p) \ + ((uint32_t)((p)[0] << 24 | (p)[1] << 16 | (p)[2] << 8 | (p)[3])) +void CFX_FontMgrImp::GetNames(const uint8_t* name_table, + CFX_WideStringArray& Names) { + if (NULL == name_table) { + return; + } + uint8_t* lpTable = (uint8_t*)name_table; + CFX_WideString wsFamily; + uint8_t* sp = lpTable + 2; + uint8_t* lpNameRecord = lpTable + 6; + uint16_t nNameCount = GetUInt16(sp); + uint8_t* lpStr = lpTable + GetUInt16(sp + 2); + for (uint16_t j = 0; j < nNameCount; j++) { + uint16_t nNameID = GetUInt16(lpNameRecord + j * 12 + 6); + if (nNameID != 1) { + continue; + } + uint16_t nPlatformID = GetUInt16(lpNameRecord + j * 12 + 0); + uint16_t nNameLength = GetUInt16(lpNameRecord + j * 12 + 8); + uint16_t nNameOffset = GetUInt16(lpNameRecord + j * 12 + 10); + wsFamily.Empty(); + if (nPlatformID != 1) { + for (uint16_t k = 0; k < nNameLength / 2; k++) { + FX_WCHAR wcTemp = GetUInt16(lpStr + nNameOffset + k * 2); + wsFamily += wcTemp; + } + Names.Add(wsFamily); + } else { + for (uint16_t k = 0; k < nNameLength; k++) { + FX_WCHAR wcTemp = GetUInt8(lpStr + nNameOffset + k); + wsFamily += wcTemp; + } + Names.Add(wsFamily); + } + } +} +#undef GetUInt8 +#undef GetUInt16 +#undef GetUInt32 +struct FX_BIT2CHARSET { + FX_WORD wBit; + FX_WORD wCharset; +}; +FX_BIT2CHARSET g_FX_Bit2Charset1[16] = { + {1 << 0, FX_CHARSET_ANSI}, + {1 << 1, FX_CHARSET_MSWin_EasterEuropean}, + {1 << 2, FX_CHARSET_MSWin_Cyrillic}, + {1 << 3, FX_CHARSET_MSWin_Greek}, + {1 << 4, FX_CHARSET_MSWin_Turkish}, + {1 << 5, FX_CHARSET_MSWin_Hebrew}, + {1 << 6, FX_CHARSET_MSWin_Arabic}, + {1 << 7, FX_CHARSET_MSWin_Baltic}, + {1 << 8, FX_CHARSET_MSWin_Vietnamese}, + {1 << 9, FX_CHARSET_Default}, + {1 << 10, FX_CHARSET_Default}, + {1 << 11, FX_CHARSET_Default}, + {1 << 12, FX_CHARSET_Default}, + {1 << 13, FX_CHARSET_Default}, + {1 << 14, FX_CHARSET_Default}, + {1 << 15, FX_CHARSET_Default}, +}; +FX_BIT2CHARSET g_FX_Bit2Charset2[16] = { + {1 << 0, FX_CHARSET_Thai}, + {1 << 1, FX_CHARSET_ShiftJIS}, + {1 << 2, FX_CHARSET_ChineseSimplified}, + {1 << 3, FX_CHARSET_Korean}, + {1 << 4, FX_CHARSET_ChineseTriditional}, + {1 << 5, FX_CHARSET_Johab}, + {1 << 6, FX_CHARSET_Default}, + {1 << 7, FX_CHARSET_Default}, + {1 << 8, FX_CHARSET_Default}, + {1 << 9, FX_CHARSET_Default}, + {1 << 10, FX_CHARSET_Default}, + {1 << 11, FX_CHARSET_Default}, + {1 << 12, FX_CHARSET_Default}, + {1 << 13, FX_CHARSET_Default}, + {1 << 14, FX_CHARSET_OEM}, + {1 << 15, FX_CHARSET_Symbol}, +}; +FX_BIT2CHARSET g_FX_Bit2Charset3[16] = { + {1 << 0, FX_CHARSET_Default}, {1 << 1, FX_CHARSET_Default}, + {1 << 2, FX_CHARSET_Default}, {1 << 3, FX_CHARSET_Default}, + {1 << 4, FX_CHARSET_Default}, {1 << 5, FX_CHARSET_Default}, + {1 << 6, FX_CHARSET_Default}, {1 << 7, FX_CHARSET_Default}, + {1 << 8, FX_CHARSET_Default}, {1 << 9, FX_CHARSET_Default}, + {1 << 10, FX_CHARSET_Default}, {1 << 11, FX_CHARSET_Default}, + {1 << 12, FX_CHARSET_Default}, {1 << 13, FX_CHARSET_Default}, + {1 << 14, FX_CHARSET_Default}, {1 << 15, FX_CHARSET_Default}, +}; +FX_BIT2CHARSET g_FX_Bit2Charset4[16] = { + {1 << 0, FX_CHARSET_Default}, {1 << 1, FX_CHARSET_Default}, + {1 << 2, FX_CHARSET_Default}, {1 << 3, FX_CHARSET_Default}, + {1 << 4, FX_CHARSET_Default}, {1 << 5, FX_CHARSET_Default}, + {1 << 6, FX_CHARSET_Default}, {1 << 7, FX_CHARSET_Default}, + {1 << 8, FX_CHARSET_Default}, {1 << 9, FX_CHARSET_Default}, + {1 << 10, FX_CHARSET_Default}, {1 << 11, FX_CHARSET_Default}, + {1 << 12, FX_CHARSET_Default}, {1 << 13, FX_CHARSET_Default}, + {1 << 14, FX_CHARSET_Default}, {1 << 15, FX_CHARSET_US}, +}; +#define CODEPAGERANGE_IMPLEMENT(n) \ + for (int32_t i = 0; i < 16; i++) { \ + if ((a##n & g_FX_Bit2Charset##n[i].wBit) != 0) { \ + Charsets.Add(g_FX_Bit2Charset##n[i].wCharset); \ + } \ + } +void CFX_FontMgrImp::GetCharsets(FXFT_Face pFace, CFX_WordArray& Charsets) { + Charsets.RemoveAll(); + TT_OS2* pOS2 = (TT_OS2*)FT_Get_Sfnt_Table(pFace, ft_sfnt_os2); + if (NULL != pOS2) { + FX_WORD a1, a2, a3, a4; + a1 = pOS2->ulCodePageRange1 & 0x0000ffff; + CODEPAGERANGE_IMPLEMENT(1); + a2 = (pOS2->ulCodePageRange1 >> 16) & 0x0000ffff; + CODEPAGERANGE_IMPLEMENT(2); + a3 = pOS2->ulCodePageRange2 & 0x0000ffff; + CODEPAGERANGE_IMPLEMENT(3); + a4 = (pOS2->ulCodePageRange2 >> 16) & 0x0000ffff; + CODEPAGERANGE_IMPLEMENT(4); + } else { + Charsets.Add(FX_CHARSET_Default); + } +} +#undef CODEPAGERANGE_IMPLEMENT +void CFX_FontMgrImp::GetUSBCSB(FXFT_Face pFace, FX_DWORD* USB, FX_DWORD* CSB) { + TT_OS2* pOS2 = (TT_OS2*)FT_Get_Sfnt_Table(pFace, ft_sfnt_os2); + if (NULL != pOS2) { + USB[0] = pOS2->ulUnicodeRange1; + USB[1] = pOS2->ulUnicodeRange2; + USB[2] = pOS2->ulUnicodeRange3; + USB[3] = pOS2->ulUnicodeRange4; + CSB[0] = pOS2->ulCodePageRange1; + CSB[1] = pOS2->ulCodePageRange2; + } else { + USB[0] = 0; + USB[1] = 0; + USB[2] = 0; + USB[3] = 0; + CSB[0] = 0; + CSB[1] = 0; + } +} +int32_t CFX_FontMgrImp::IsPartName(const CFX_WideString& Name1, + const CFX_WideString& Name2) { + if (Name1.Find((const FX_WCHAR*)Name2) != -1) { + return 1; + } + return 0; +} +#endif diff --git a/xfa/src/fgas/font/fgas_stdfontmgr.h b/xfa/src/fgas/font/fgas_stdfontmgr.h new file mode 100644 index 0000000000..cf16033919 --- /dev/null +++ b/xfa/src/fgas/font/fgas_stdfontmgr.h @@ -0,0 +1,252 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_SRC_FGAS_FONT_FGAS_STDFONTMGR_H_ +#define XFA_SRC_FGAS_FONT_FGAS_STDFONTMGR_H_ + +#include "core/include/fxcrt/fx_ext.h" +#include "core/include/fxge/fx_freetype.h" +#include "core/include/fxge/fx_ge.h" +#include "third_party/freetype/include/freetype/fttypes.h" +#include "xfa/src/fgas/font/fgas_font.h" + +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ +int32_t FX_GetSimilarValue(FX_LPCFONTDESCRIPTOR pFont, FX_DWORD dwFontStyles); +FX_LPCFONTDESCRIPTOR FX_DefFontMatcher(FX_LPFONTMATCHPARAMS pParams, + const CFX_FontDescriptors& fonts, + void* pUserData); + +class CFX_StdFontMgrImp : public IFX_FontMgr { + public: + CFX_StdFontMgrImp(FX_LPEnumAllFonts pEnumerator, + FX_LPMatchFont pMatcher, + void* pUserData); + ~CFX_StdFontMgrImp(); + virtual void Release() { delete this; } + virtual IFX_Font* GetDefFontByCodePage(FX_WORD wCodePage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL); + virtual IFX_Font* GetDefFontByCharset(uint8_t nCharset, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL); + virtual IFX_Font* GetDefFontByUnicode(FX_WCHAR wUnicode, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL); + virtual IFX_Font* GetDefFontByLanguage(FX_WORD wLanguage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL); + virtual IFX_Font* LoadFont(const FX_WCHAR* pszFontFamily, + FX_DWORD dwFontStyles, + FX_WORD wCodePage = 0xFFFF); + virtual IFX_Font* LoadFont(const uint8_t* pBuffer, int32_t iLength); + virtual IFX_Font* LoadFont(const FX_WCHAR* pszFileName); + virtual IFX_Font* LoadFont(IFX_Stream* pFontStream, + const FX_WCHAR* pszFontAlias = NULL, + FX_DWORD dwFontStyles = 0, + FX_WORD wCodePage = 0, + FX_BOOL bSaveStream = FALSE); + virtual IFX_Font* LoadFont(IFX_Font* pSrcFont, + FX_DWORD dwFontStyles, + FX_WORD wCodePage = 0xFFFF); + virtual void ClearFontCache(); + virtual void RemoveFont(IFX_Font* pFont); + + protected: + FX_LPMatchFont m_pMatcher; + FX_LPEnumAllFonts m_pEnumerator; + CFX_FontDescriptors m_FontFaces; + CFX_PtrArray m_Fonts; + CFX_MapPtrToPtr m_CPFonts; + CFX_MapPtrToPtr m_FamilyFonts; + CFX_MapPtrToPtr m_UnicodeFonts; + CFX_MapPtrToPtr m_BufferFonts; + CFX_MapPtrToPtr m_FileFonts; + CFX_MapPtrToPtr m_StreamFonts; + CFX_MapPtrToPtr m_DeriveFonts; + void* m_pUserData; + void RemoveFont(CFX_MapPtrToPtr& fontMap, IFX_Font* pFont); + FX_LPCFONTDESCRIPTOR FindFont(const FX_WCHAR* pszFontFamily, + FX_DWORD dwFontStyles, + FX_DWORD dwMatchFlags, + FX_WORD wCodePage, + FX_DWORD dwUSB = 999, + FX_WCHAR wUnicode = 0); + IFX_Font* GetFont(FX_LPCFONTDESCRIPTOR pFD, FX_DWORD dwFontStyles); +}; +FX_DWORD FX_GetGdiFontStyles(const LOGFONTW& lf); + +#else + +class CFX_FontDescriptor { + public: + CFX_FontDescriptor() + : m_pFileAccess(NULL), m_nFaceIndex(0), m_dwFontStyles(0) { + m_dwUsb[0] = m_dwUsb[1] = m_dwUsb[2] = m_dwUsb[3] = 0; + m_dwCsb[0] = m_dwCsb[1] = 0; + } + ~CFX_FontDescriptor() { + if (NULL != m_pFileAccess) { + m_pFileAccess->Release(); + } + } + IFX_FileAccess* m_pFileAccess; + int32_t m_nFaceIndex; + CFX_WideString m_wsFaceName; + CFX_WideStringArray m_wsFamilyNames; + FX_DWORD m_dwFontStyles; + FX_DWORD m_dwUsb[4]; + FX_DWORD m_dwCsb[2]; +}; +typedef CFX_ArrayTemplate<CFX_FontDescriptor*> CFX_FontDescriptors; + +struct FX_FontDescriptorInfo { + public: + CFX_FontDescriptor* pFont; + int32_t nPenalty; + + bool operator>(const FX_FontDescriptorInfo& other) const { + return nPenalty > other.nPenalty; + } + bool operator<(const FX_FontDescriptorInfo& other) const { + return nPenalty < other.nPenalty; + } + bool operator==(const FX_FontDescriptorInfo& other) const { + return nPenalty == other.nPenalty; + } +}; +typedef CFX_ArrayTemplate<FX_FontDescriptorInfo> CFX_FontDescriptorInfos; + +struct FX_HandleParentPath { + FX_HandleParentPath() {} + FX_HandleParentPath(const FX_HandleParentPath& x) { + pFileHandle = x.pFileHandle; + bsParentPath = x.bsParentPath; + } + void* pFileHandle; + CFX_ByteString bsParentPath; +}; + +class CFX_FontSourceEnum_File : public IFX_FontSourceEnum { + public: + CFX_FontSourceEnum_File(); + virtual void Release() { delete this; } + virtual FX_POSITION GetStartPosition(void* pUserData = NULL); + virtual IFX_FileAccess* GetNext(FX_POSITION& pos, void* pUserData = NULL); + + private: + CFX_ByteString GetNextFile(); + CFX_WideString m_wsNext; + CFX_ObjectArray<FX_HandleParentPath> m_FolderQueue; + CFX_ByteStringArray m_FolderPaths; +}; +typedef CFX_MapPtrTemplate<FX_DWORD, IFX_FileAccess*> CFX_HashFileMap; +typedef CFX_MapPtrTemplate<FX_DWORD, IFX_Font*> CFX_HashFontMap; +typedef CFX_MapPtrTemplate<FX_DWORD, CFX_FontDescriptorInfos*> + CFX_HashFontDescsMap; +typedef CFX_MapPtrTemplate<FX_DWORD, CFX_ArrayTemplate<IFX_Font*>*> + CFX_HashFontsMap; +typedef CFX_MapPtrTemplate<FX_WCHAR, IFX_Font*> CFX_UnicodeFontMap; +typedef CFX_MapPtrTemplate<IFX_FileAccess*, CFX_ArrayTemplate<IFX_Font*>*> + CFX_FileFontMap; +typedef CFX_MapPtrTemplate<IFX_Font*, IFX_FileRead*> CFX_FonStreamtMap; + +class CFX_FontMgrImp : public IFX_FontMgr { + public: + CFX_FontMgrImp(IFX_FontSourceEnum* pFontEnum, + IFX_FontMgrDelegate* pDelegate = NULL, + void* pUserData = NULL); + virtual void Release(); + virtual IFX_Font* GetDefFontByCodePage(FX_WORD wCodePage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL); + virtual IFX_Font* GetDefFontByCharset(uint8_t nCharset, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL); + virtual IFX_Font* GetDefFontByUnicode(FX_WCHAR wUnicode, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL); + virtual IFX_Font* GetDefFontByLanguage(FX_WORD wLanguage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL); + virtual IFX_Font* GetFontByCodePage(FX_WORD wCodePage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL); + virtual IFX_Font* GetFontByCharset(uint8_t nCharset, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL); + virtual IFX_Font* GetFontByUnicode(FX_WCHAR wUnicode, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL); + virtual IFX_Font* GetFontByLanguage(FX_WORD wLanguage, + FX_DWORD dwFontStyles, + const FX_WCHAR* pszFontFamily = NULL); + virtual IFX_Font* LoadFont(const uint8_t* pBuffer, + int32_t iLength, + int32_t iFaceIndex, + int32_t* pFaceCount); + virtual IFX_Font* LoadFont(const FX_WCHAR* pszFileName, + int32_t iFaceIndex, + int32_t* pFaceCount); + virtual IFX_Font* LoadFont(IFX_Stream* pFontStream, + int32_t iFaceIndex, + int32_t* pFaceCount, + FX_BOOL bSaveStream = FALSE); + virtual IFX_Font* LoadFont(const CFX_WideString& wsFaceName, + int32_t iFaceIndex, + int32_t* pFaceCount); + virtual void ClearFontCache(); + virtual void RemoveFont(IFX_Font* pFont); + FX_BOOL EnumFonts(); + FX_BOOL EnumFontsFromFontMapper(); + FX_BOOL EnumFontsFromFiles(); + + protected: + void RegisterFace(FXFT_Face pFace, + CFX_FontDescriptors& Fonts, + const CFX_WideString* pFaceName, + IFX_FileAccess* pFontAccess); + void RegisterFaces(IFX_FileRead* pFontStream, + const CFX_WideString* pFaceName); + void GetNames(const uint8_t* name_table, CFX_WideStringArray& Names); + void GetCharsets(FXFT_Face pFace, CFX_WordArray& Charsets); + void GetUSBCSB(FXFT_Face pFace, FX_DWORD* USB, FX_DWORD* CSB); + FX_DWORD GetFlags(FXFT_Face pFace); + CFX_FontDescriptors m_InstalledFonts; + FX_BOOL VerifyUnicode(CFX_FontDescriptor* pDesc, FX_WCHAR wcUnicode); + FX_BOOL VerifyUnicode(IFX_Font* pFont, FX_WCHAR wcUnicode); + int32_t IsPartName(const CFX_WideString& Name1, const CFX_WideString& Name2); + int32_t MatchFonts(CFX_FontDescriptorInfos& MatchedFonts, + FX_WORD wCodePage, + FX_DWORD dwFontStyles, + const CFX_WideString& FontName, + FX_WCHAR wcUnicode = 0xFFFE); + int32_t CalcPenalty(CFX_FontDescriptor* pInstalled, + FX_WORD wCodePage, + FX_DWORD dwFontStyles, + const CFX_WideString& FontName, + FX_WCHAR wcUnicode = 0xFFFE); + IFX_Font* LoadFont(IFX_FileAccess* pFontAccess, + int32_t iFaceIndex, + int32_t* pFaceCount, + FX_BOOL bWantCache = FALSE); + FXFT_Face LoadFace(IFX_FileRead* pFontStream, int32_t iFaceIndex); + IFX_FileRead* CreateFontStream(CFX_FontMapper* pFontMapper, + IFX_SystemFontInfo* pSystemFontInfo, + FX_DWORD index); + IFX_FileRead* CreateFontStream(const CFX_ByteString& bsFaceName); + CFX_HashFontDescsMap m_Hash2CandidateList; + CFX_HashFontsMap m_Hash2Fonts; + CFX_HashFileMap m_Hash2FileAccess; + CFX_HashFontMap m_FileAccess2IFXFont; + CFX_FonStreamtMap m_IFXFont2FileRead; + CFX_UnicodeFontMap m_FailedUnicodes2NULL; + IFX_FontSourceEnum* m_pFontSource; + IFX_FontMgrDelegate* m_pDelegate; + void* m_pUserData; +}; +#endif + +#endif // XFA_SRC_FGAS_FONT_FGAS_STDFONTMGR_H_ |