From 94a4956f7aadc10fe6dd8451e965bd7447985b76 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Mon, 9 Nov 2015 10:41:56 -0800 Subject: Cleanup some fxge font code. R=tsepez@chromium.org Review URL: https://codereview.chromium.org/1426403008 . --- core/include/fxge/fx_font.h | 24 +- core/src/fxge/ge/fx_ge_fontmap.cpp | 831 ++++++++++++++++++------------------- core/src/fxge/ge/fx_ge_text.cpp | 15 +- 3 files changed, 417 insertions(+), 453 deletions(-) diff --git a/core/include/fxge/fx_font.h b/core/include/fxge/fx_font.h index 3e4ca95299..bc76c6e2e2 100644 --- a/core/include/fxge/fx_font.h +++ b/core/include/fxge/fx_font.h @@ -139,6 +139,7 @@ class CFX_UnicodeEncoding { #define FXFONT_SUBST_NONSYMBOL 0x20 #define FXFONT_SUBST_EXACT 0x40 #define FXFONT_SUBST_STANDARD 0x80 + class CFX_SubstFont { public: CFX_SubstFont(); @@ -385,6 +386,7 @@ class CFX_GlyphBitmap { }; class CFX_FaceCache { public: + explicit CFX_FaceCache(FXFT_Face face); ~CFX_FaceCache(); const CFX_GlyphBitmap* LoadGlyphBitmap(CFX_Font* pFont, FX_DWORD glyph_index, @@ -397,10 +399,7 @@ class CFX_FaceCache { FX_DWORD glyph_index, int dest_width); - CFX_FaceCache(FXFT_Face face); - private: - FXFT_Face m_Face; CFX_GlyphBitmap* RenderGlyph(CFX_Font* pFont, FX_DWORD glyph_index, FX_BOOL bFontStyle, @@ -419,18 +418,23 @@ class CFX_FaceCache { FX_BOOL bFontStyle, int dest_width, int anti_alias); + void InitPlatform(); + void DestroyPlatform(); + + FXFT_Face const m_Face; std::map m_SizeMap; CFX_MapPtrToPtr m_PathMap; CFX_DIBitmap* m_pBitmap; - - void InitPlatform(); - void DestroyPlatform(); }; -typedef struct { + +struct FXTEXT_GLYPHPOS { const CFX_GlyphBitmap* m_pGlyph; - int m_OriginX, m_OriginY; - FX_FLOAT m_fOriginX, m_fOriginY; -} FXTEXT_GLYPHPOS; + int m_OriginX; + int m_OriginY; + FX_FLOAT m_fOriginX; + FX_FLOAT m_fOriginY; +}; + FX_RECT FXGE_GetGlyphsBBox(FXTEXT_GLYPHPOS* pGlyphAndPos, int nChars, int anti_alias, diff --git a/core/src/fxge/ge/fx_ge_fontmap.cpp b/core/src/fxge/ge/fx_ge_fontmap.cpp index 446744328a..ba3e28ea06 100644 --- a/core/src/fxge/ge/fx_ge_fontmap.cpp +++ b/core/src/fxge/ge/fx_ge_fontmap.cpp @@ -15,6 +15,11 @@ #define GET_TT_LONG(w) \ (FX_DWORD)(((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3]) +#define FX_FONT_STYLE_None 0x00 +#define FX_FONT_STYLE_Bold 0x01 +#define FX_FONT_STYLE_Italic 0x02 +#define FX_FONT_STYLE_BoldBold 0x04 + namespace { struct BuiltinFont { @@ -44,149 +49,6 @@ const BuiltinFont g_MMFonts[2] = { {g_FoxitSansMMFontData, 66919}, }; -CFX_ByteString KeyNameFromFace(const CFX_ByteString& face_name, - int weight, - FX_BOOL bItalic) { - CFX_ByteString key(face_name); - key += ','; - key += CFX_ByteString::FormatInteger(weight); - key += bItalic ? 'I' : 'N'; - return key; -} - -CFX_ByteString KeyNameFromSize(int ttc_size, FX_DWORD checksum) { - CFX_ByteString key; - key.Format("%d:%d", ttc_size, checksum); - return key; -} - -} // namespace - -CFX_SubstFont::CFX_SubstFont() { - m_ExtHandle = NULL; - m_Charset = 0; - m_SubstFlags = 0; - m_Weight = 0; - m_ItalicAngle = 0; - m_bSubstOfCJK = FALSE; - m_WeightCJK = 0; - m_bItlicCJK = FALSE; -} -CTTFontDesc::~CTTFontDesc() { - if (m_Type == 1) { - if (m_SingleFace.m_pFace) { - FXFT_Done_Face(m_SingleFace.m_pFace); - } - } else if (m_Type == 2) { - for (int i = 0; i < 16; i++) - if (m_TTCFace.m_pFaces[i]) { - FXFT_Done_Face(m_TTCFace.m_pFaces[i]); - } - } - FX_Free(m_pFontData); -} -FX_BOOL CTTFontDesc::ReleaseFace(FXFT_Face face) { - if (m_Type == 1) { - if (m_SingleFace.m_pFace != face) { - return FALSE; - } - } else if (m_Type == 2) { - int i; - for (i = 0; i < 16; i++) - if (m_TTCFace.m_pFaces[i] == face) { - break; - } - if (i == 16) { - return FALSE; - } - } - m_RefCount--; - if (m_RefCount) { - return FALSE; - } - delete this; - return TRUE; -} - -CFX_FontMgr::CFX_FontMgr() : m_FTLibrary(nullptr) { - m_pBuiltinMapper.reset(new CFX_FontMapper(this)); -} - -CFX_FontMgr::~CFX_FontMgr() { - for (const auto& pair : m_FaceMap) - delete pair.second; - - // |m_pBuiltinMapper| references |m_FTLibrary|, so it has to be destroyed - // first. - m_pBuiltinMapper.reset(); - FXFT_Done_FreeType(m_FTLibrary); -} - -void CFX_FontMgr::InitFTLibrary() { - if (m_FTLibrary) - return; - FXFT_Init_FreeType(&m_FTLibrary); -} - -void CFX_FontMgr::SetSystemFontInfo(IFX_SystemFontInfo* pFontInfo) { - m_pBuiltinMapper->SetSystemFontInfo(pFontInfo); -} - -FXFT_Face CFX_FontMgr::FindSubstFont(const CFX_ByteString& face_name, - FX_BOOL bTrueType, - FX_DWORD flags, - int weight, - int italic_angle, - int CharsetCP, - CFX_SubstFont* pSubstFont) { - InitFTLibrary(); - return m_pBuiltinMapper->FindSubstFont(face_name, bTrueType, flags, weight, - italic_angle, CharsetCP, pSubstFont); -} - -FXFT_Face CFX_FontMgr::GetCachedFace(const CFX_ByteString& face_name, - int weight, - FX_BOOL bItalic, - uint8_t*& pFontData) { - auto it = m_FaceMap.find(KeyNameFromFace(face_name, weight, bItalic)); - if (it == m_FaceMap.end()) - return nullptr; - - CTTFontDesc* pFontDesc = it->second; - pFontData = pFontDesc->m_pFontData; - pFontDesc->m_RefCount++; - return pFontDesc->m_SingleFace.m_pFace; -} -FXFT_Face CFX_FontMgr::AddCachedFace(const CFX_ByteString& face_name, - int weight, - FX_BOOL bItalic, - uint8_t* pData, - FX_DWORD size, - int face_index) { - CTTFontDesc* pFontDesc = new CTTFontDesc; - pFontDesc->m_Type = 1; - pFontDesc->m_SingleFace.m_pFace = NULL; - pFontDesc->m_SingleFace.m_bBold = weight; - pFontDesc->m_SingleFace.m_bItalic = bItalic; - pFontDesc->m_pFontData = pData; - pFontDesc->m_RefCount = 1; - - InitFTLibrary(); - FXFT_Library library = m_FTLibrary; - int ret = FXFT_New_Memory_Face(library, pData, size, face_index, - &pFontDesc->m_SingleFace.m_pFace); - if (ret) { - delete pFontDesc; - return NULL; - } - ret = FXFT_Set_Pixel_Sizes(pFontDesc->m_SingleFace.m_pFace, 64, 64); - if (ret) { - delete pFontDesc; - return NULL; - } - m_FaceMap[KeyNameFromFace(face_name, weight, bItalic)] = pFontDesc; - return pFontDesc->m_SingleFace.m_pFace; -} const FX_CHAR* const g_Base14FontNames[14] = { "Courier", "Courier-Bold", @@ -203,6 +65,7 @@ const FX_CHAR* const g_Base14FontNames[14] = { "Symbol", "ZapfDingbats", }; + const struct AltFontName { const FX_CHAR* m_pName; int m_Index; @@ -297,10 +160,380 @@ const struct AltFontName { {"TimesNewRomanPSMT,Italic", 11}, {"ZapfDingbats", 13}, }; -extern "C" { -static int compareString(const void* key, const void* element) { + +const struct { + const FX_CHAR* m_pName; + const FX_CHAR* m_pSubstName; +} Base14Substs[] = { + {"Courier", "Courier New"}, + {"Courier-Bold", "Courier New Bold"}, + {"Courier-BoldOblique", "Courier New Bold Italic"}, + {"Courier-Oblique", "Courier New Italic"}, + {"Helvetica", "Arial"}, + {"Helvetica-Bold", "Arial Bold"}, + {"Helvetica-BoldOblique", "Arial Bold Italic"}, + {"Helvetica-Oblique", "Arial Italic"}, + {"Times-Roman", "Times New Roman"}, + {"Times-Bold", "Times New Roman Bold"}, + {"Times-BoldItalic", "Times New Roman Bold Italic"}, + {"Times-Italic", "Times New Roman Italic"}, +}; + +const struct AltFontFamily { + const FX_CHAR* m_pFontName; + const FX_CHAR* m_pFontFamily; +} g_AltFontFamilies[] = { + {"AGaramondPro", "Adobe Garamond Pro"}, + {"BankGothicBT-Medium", "BankGothic Md BT"}, + {"ForteMT", "Forte"}, +}; + +const struct FX_FontStyle { + const FX_CHAR* style; + int32_t len; +} g_FontStyles[] = { + {"Bold", 4}, {"Italic", 6}, {"BoldItalic", 10}, {"Reg", 3}, {"Regular", 7}, +}; + +const struct CHARSET_MAP { + uint8_t charset; + FX_WORD codepage; +} g_Codepage2CharsetTable[] = { + {1, 0}, {2, 42}, {254, 437}, {255, 850}, {222, 874}, + {128, 932}, {134, 936}, {129, 949}, {136, 950}, {238, 1250}, + {204, 1251}, {0, 1252}, {161, 1253}, {162, 1254}, {177, 1255}, + {178, 1256}, {186, 1257}, {163, 1258}, {130, 1361}, {77, 10000}, + {78, 10001}, {79, 10003}, {80, 10008}, {81, 10002}, {83, 10005}, + {84, 10004}, {85, 10006}, {86, 10081}, {87, 10021}, {88, 10029}, + {89, 10007}, +}; + +int CompareFontFamilyString(const void* key, const void* element) { + CFX_ByteString str_key((const FX_CHAR*)key); + if (str_key.Find(((AltFontFamily*)element)->m_pFontName) != -1) { + return 0; + } + return FXSYS_stricmp((const FX_CHAR*)key, + ((AltFontFamily*)element)->m_pFontName); +} + +int CompareString(const void* key, const void* element) { return FXSYS_stricmp((const FX_CHAR*)key, ((AltFontName*)element)->m_pName); } + +CFX_ByteString KeyNameFromFace(const CFX_ByteString& face_name, + int weight, + FX_BOOL bItalic) { + CFX_ByteString key(face_name); + key += ','; + key += CFX_ByteString::FormatInteger(weight); + key += bItalic ? 'I' : 'N'; + return key; +} + +CFX_ByteString KeyNameFromSize(int ttc_size, FX_DWORD checksum) { + CFX_ByteString key; + key.Format("%d:%d", ttc_size, checksum); + return key; +} + +CFX_ByteString TT_NormalizeName(const FX_CHAR* family) { + CFX_ByteString norm(family); + norm.Remove(' '); + norm.Remove('-'); + norm.Remove(','); + int pos = norm.Find('+'); + if (pos > 0) { + norm = norm.Left(pos); + } + norm.MakeLower(); + return norm; +} + +CFX_ByteString FPDF_ReadStringFromFile(FXSYS_FILE* pFile, FX_DWORD size) { + CFX_ByteString buffer; + if (!FXSYS_fread(buffer.GetBuffer(size), size, 1, pFile)) { + return CFX_ByteString(); + } + buffer.ReleaseBuffer(size); + return buffer; +} + +CFX_ByteString FPDF_LoadTableFromTT(FXSYS_FILE* pFile, + const uint8_t* pTables, + FX_DWORD nTables, + FX_DWORD tag) { + for (FX_DWORD i = 0; i < nTables; i++) { + const uint8_t* p = pTables + i * 16; + if (GET_TT_LONG(p) == tag) { + FX_DWORD offset = GET_TT_LONG(p + 8); + FX_DWORD size = GET_TT_LONG(p + 12); + FXSYS_fseek(pFile, offset, FXSYS_SEEK_SET); + return FPDF_ReadStringFromFile(pFile, size); + } + } + return CFX_ByteString(); +} + +uint8_t GetCharsetFromCodePage(FX_WORD codepage) { + int32_t iEnd = sizeof(g_Codepage2CharsetTable) / sizeof(CHARSET_MAP) - 1; + FXSYS_assert(iEnd >= 0); + int32_t iStart = 0, iMid; + do { + iMid = (iStart + iEnd) / 2; + const CHARSET_MAP& cp = g_Codepage2CharsetTable[iMid]; + if (codepage == cp.codepage) { + return cp.charset; + } + if (codepage < cp.codepage) { + iEnd = iMid - 1; + } else { + iStart = iMid + 1; + } + } while (iStart <= iEnd); + return 1; +} + +CFX_ByteString GetFontFamily(CFX_ByteString fontName, int nStyle) { + if (fontName.Find("Script") >= 0) { + if ((nStyle & FX_FONT_STYLE_Bold) == FX_FONT_STYLE_Bold) { + fontName = "ScriptMTBold"; + } else if (fontName.Find("Palace") >= 0) { + fontName = "PalaceScriptMT"; + } else if (fontName.Find("French") >= 0) { + fontName = "FrenchScriptMT"; + } else if (fontName.Find("FreeStyle") >= 0) { + fontName = "FreeStyleScript"; + } + return fontName; + } + AltFontFamily* found = (AltFontFamily*)FXSYS_bsearch( + fontName.c_str(), g_AltFontFamilies, + sizeof g_AltFontFamilies / sizeof(AltFontFamily), sizeof(AltFontFamily), + CompareFontFamilyString); + if (found == NULL) { + return fontName; + } + return found->m_pFontFamily; +} + +CFX_ByteString ParseStyle(const FX_CHAR* pStyle, int iLen, int iIndex) { + CFX_ByteTextBuf buf; + if (!iLen || iLen <= iIndex) { + return buf.GetByteString(); + } + while (iIndex < iLen) { + if (pStyle[iIndex] == ',') { + break; + } + buf.AppendChar(pStyle[iIndex]); + ++iIndex; + } + return buf.GetByteString(); +} + +int32_t GetStyleType(const CFX_ByteString& bsStyle, FX_BOOL bRevert) { + int32_t iLen = bsStyle.GetLength(); + if (!iLen) { + return -1; + } + int iSize = sizeof(g_FontStyles) / sizeof(FX_FontStyle); + const FX_FontStyle* pStyle = NULL; + for (int i = iSize - 1; i >= 0; --i) { + pStyle = g_FontStyles + i; + if (!pStyle || pStyle->len > iLen) { + continue; + } + if (!bRevert) { + if (bsStyle.Left(pStyle->len).Compare(pStyle->style) == 0) { + return i; + } + } else { + if (bsStyle.Right(pStyle->len).Compare(pStyle->style) == 0) { + return i; + } + } + } + return -1; +} + +FX_BOOL CheckSupportThirdPartFont(CFX_ByteString name, int& PitchFamily) { + if (name == FX_BSTRC("MyriadPro")) { + PitchFamily &= ~FXFONT_FF_ROMAN; + return TRUE; + } + return FALSE; +} + +FX_DWORD GetCharset(int charset) { + switch (charset) { + case FXFONT_SHIFTJIS_CHARSET: + return CHARSET_FLAG_SHIFTJIS; + case FXFONT_GB2312_CHARSET: + return CHARSET_FLAG_GB; + case FXFONT_CHINESEBIG5_CHARSET: + return CHARSET_FLAG_BIG5; + case FXFONT_HANGEUL_CHARSET: + return CHARSET_FLAG_KOREAN; + case FXFONT_SYMBOL_CHARSET: + return CHARSET_FLAG_SYMBOL; + case FXFONT_ANSI_CHARSET: + return CHARSET_FLAG_ANSI; + default: + break; + } + return 0; +} + +int32_t GetSimilarValue(int weight, + FX_BOOL bItalic, + int pitch_family, + FX_DWORD style) { + int32_t iSimilarValue = 0; + if ((style & FXFONT_BOLD) == (weight > 400)) { + iSimilarValue += 16; + } + if ((style & FXFONT_ITALIC) == bItalic) { + iSimilarValue += 16; + } + if ((style & FXFONT_SERIF) == (pitch_family & FXFONT_FF_ROMAN)) { + iSimilarValue += 16; + } + if ((style & FXFONT_SCRIPT) == (pitch_family & FXFONT_FF_SCRIPT)) { + iSimilarValue += 8; + } + if ((style & FXFONT_FIXED_PITCH) == (pitch_family & FXFONT_FF_FIXEDPITCH)) { + iSimilarValue += 8; + } + return iSimilarValue; +} + +} // namespace + +CFX_SubstFont::CFX_SubstFont() { + m_ExtHandle = NULL; + m_Charset = 0; + m_SubstFlags = 0; + m_Weight = 0; + m_ItalicAngle = 0; + m_bSubstOfCJK = FALSE; + m_WeightCJK = 0; + m_bItlicCJK = FALSE; +} +CTTFontDesc::~CTTFontDesc() { + if (m_Type == 1) { + if (m_SingleFace.m_pFace) { + FXFT_Done_Face(m_SingleFace.m_pFace); + } + } else if (m_Type == 2) { + for (int i = 0; i < 16; i++) + if (m_TTCFace.m_pFaces[i]) { + FXFT_Done_Face(m_TTCFace.m_pFaces[i]); + } + } + FX_Free(m_pFontData); +} +FX_BOOL CTTFontDesc::ReleaseFace(FXFT_Face face) { + if (m_Type == 1) { + if (m_SingleFace.m_pFace != face) { + return FALSE; + } + } else if (m_Type == 2) { + int i; + for (i = 0; i < 16; i++) + if (m_TTCFace.m_pFaces[i] == face) { + break; + } + if (i == 16) { + return FALSE; + } + } + m_RefCount--; + if (m_RefCount) { + return FALSE; + } + delete this; + return TRUE; +} + +CFX_FontMgr::CFX_FontMgr() : m_FTLibrary(nullptr) { + m_pBuiltinMapper.reset(new CFX_FontMapper(this)); +} + +CFX_FontMgr::~CFX_FontMgr() { + for (const auto& pair : m_FaceMap) + delete pair.second; + + // |m_pBuiltinMapper| references |m_FTLibrary|, so it has to be destroyed + // first. + m_pBuiltinMapper.reset(); + FXFT_Done_FreeType(m_FTLibrary); +} + +void CFX_FontMgr::InitFTLibrary() { + if (m_FTLibrary) + return; + FXFT_Init_FreeType(&m_FTLibrary); +} + +void CFX_FontMgr::SetSystemFontInfo(IFX_SystemFontInfo* pFontInfo) { + m_pBuiltinMapper->SetSystemFontInfo(pFontInfo); +} + +FXFT_Face CFX_FontMgr::FindSubstFont(const CFX_ByteString& face_name, + FX_BOOL bTrueType, + FX_DWORD flags, + int weight, + int italic_angle, + int CharsetCP, + CFX_SubstFont* pSubstFont) { + InitFTLibrary(); + return m_pBuiltinMapper->FindSubstFont(face_name, bTrueType, flags, weight, + italic_angle, CharsetCP, pSubstFont); +} + +FXFT_Face CFX_FontMgr::GetCachedFace(const CFX_ByteString& face_name, + int weight, + FX_BOOL bItalic, + uint8_t*& pFontData) { + auto it = m_FaceMap.find(KeyNameFromFace(face_name, weight, bItalic)); + if (it == m_FaceMap.end()) + return nullptr; + + CTTFontDesc* pFontDesc = it->second; + pFontData = pFontDesc->m_pFontData; + pFontDesc->m_RefCount++; + return pFontDesc->m_SingleFace.m_pFace; +} +FXFT_Face CFX_FontMgr::AddCachedFace(const CFX_ByteString& face_name, + int weight, + FX_BOOL bItalic, + uint8_t* pData, + FX_DWORD size, + int face_index) { + CTTFontDesc* pFontDesc = new CTTFontDesc; + pFontDesc->m_Type = 1; + pFontDesc->m_SingleFace.m_pFace = NULL; + pFontDesc->m_SingleFace.m_bBold = weight; + pFontDesc->m_SingleFace.m_bItalic = bItalic; + pFontDesc->m_pFontData = pData; + pFontDesc->m_RefCount = 1; + + InitFTLibrary(); + FXFT_Library library = m_FTLibrary; + int ret = FXFT_New_Memory_Face(library, pData, size, face_index, + &pFontDesc->m_SingleFace.m_pFace); + if (ret) { + delete pFontDesc; + return NULL; + } + ret = FXFT_Set_Pixel_Sizes(pFontDesc->m_SingleFace.m_pFace, 64, 64); + if (ret) { + delete pFontDesc; + return NULL; + } + m_FaceMap[KeyNameFromFace(face_name, weight, bItalic)] = pFontDesc; + return pFontDesc->m_SingleFace.m_pFace; } int GetTTCIndex(const uint8_t* pFontData, @@ -443,18 +676,7 @@ void CFX_FontMapper::SetSystemFontInfo(IFX_SystemFontInfo* pFontInfo) { } m_pFontInfo = pFontInfo; } -static CFX_ByteString _TT_NormalizeName(const FX_CHAR* family) { - CFX_ByteString norm(family); - norm.Remove(' '); - norm.Remove('-'); - norm.Remove(','); - int pos = norm.Find('+'); - if (pos > 0) { - norm = norm.Left(pos); - } - norm.MakeLower(); - return norm; -} + CFX_ByteString GetNameFromTT(const uint8_t* name_table, FX_DWORD name_id) { const uint8_t* ptr = name_table + 2; int name_count = GET_TT_SHORT(ptr); @@ -471,49 +693,7 @@ CFX_ByteString GetNameFromTT(const uint8_t* name_table, FX_DWORD name_id) { } return CFX_ByteString(); } -static CFX_ByteString _FPDF_ReadStringFromFile(FXSYS_FILE* pFile, - FX_DWORD size) { - CFX_ByteString buffer; - if (!FXSYS_fread(buffer.GetBuffer(size), size, 1, pFile)) { - return CFX_ByteString(); - } - buffer.ReleaseBuffer(size); - return buffer; -} -CFX_ByteString _FPDF_LoadTableFromTT(FXSYS_FILE* pFile, - const uint8_t* pTables, - FX_DWORD nTables, - FX_DWORD tag) { - for (FX_DWORD i = 0; i < nTables; i++) { - const uint8_t* p = pTables + i * 16; - if (GET_TT_LONG(p) == tag) { - FX_DWORD offset = GET_TT_LONG(p + 8); - FX_DWORD size = GET_TT_LONG(p + 12); - FXSYS_fseek(pFile, offset, FXSYS_SEEK_SET); - return _FPDF_ReadStringFromFile(pFile, size); - } - } - return CFX_ByteString(); -} -CFX_ByteString _FPDF_LoadTableFromTTStreamFile(IFX_FileStream* pFile, - const uint8_t* pTables, - FX_DWORD nTables, - FX_DWORD tag) { - for (FX_DWORD i = 0; i < nTables; i++) { - const uint8_t* p = pTables + i * 16; - if (GET_TT_LONG(p) == tag) { - FX_DWORD offset = GET_TT_LONG(p + 8); - FX_DWORD size = GET_TT_LONG(p + 12); - CFX_ByteString buffer; - if (!pFile->ReadBlock(buffer.GetBuffer(size), offset, size)) { - return CFX_ByteString(); - } - buffer.ReleaseBuffer(size); - return buffer; - } - } - return CFX_ByteString(); -} + CFX_ByteString CFX_FontMapper::GetPSNameFromTT(void* hFont) { if (m_pFontInfo == NULL) { return CFX_ByteString(); @@ -584,7 +764,7 @@ CFX_ByteString CFX_FontMapper::MatchInstalledFonts( LoadInstalledFonts(); int i; for (i = m_InstalledTTFonts.GetSize() - 1; i >= 0; i--) { - CFX_ByteString norm1 = _TT_NormalizeName(m_InstalledTTFonts[i]); + CFX_ByteString norm1 = TT_NormalizeName(m_InstalledTTFonts[i]); if (norm1 == norm_name) { break; } @@ -598,76 +778,7 @@ CFX_ByteString CFX_FontMapper::MatchInstalledFonts( } return match; } -typedef struct _CHARSET_MAP_ { - uint8_t charset; - FX_WORD codepage; -} CHARSET_MAP; -static const CHARSET_MAP g_Codepage2CharsetTable[] = { - {1, 0}, {2, 42}, {254, 437}, {255, 850}, {222, 874}, - {128, 932}, {134, 936}, {129, 949}, {136, 950}, {238, 1250}, - {204, 1251}, {0, 1252}, {161, 1253}, {162, 1254}, {177, 1255}, - {178, 1256}, {186, 1257}, {163, 1258}, {130, 1361}, {77, 10000}, - {78, 10001}, {79, 10003}, {80, 10008}, {81, 10002}, {83, 10005}, - {84, 10004}, {85, 10006}, {86, 10081}, {87, 10021}, {88, 10029}, - {89, 10007}, -}; -uint8_t _GetCharsetFromCodePage(FX_WORD codepage) { - int32_t iEnd = sizeof(g_Codepage2CharsetTable) / sizeof(CHARSET_MAP) - 1; - FXSYS_assert(iEnd >= 0); - int32_t iStart = 0, iMid; - do { - iMid = (iStart + iEnd) / 2; - const CHARSET_MAP& cp = g_Codepage2CharsetTable[iMid]; - if (codepage == cp.codepage) { - return cp.charset; - } - if (codepage < cp.codepage) { - iEnd = iMid - 1; - } else { - iStart = iMid + 1; - } - } while (iStart <= iEnd); - return 1; -} -FX_DWORD _GetCodePageRangeFromCharset(int charset) { - if (charset == FXFONT_EASTEUROPE_CHARSET) { - return 1 << 1; - } - if (charset == FXFONT_GREEK_CHARSET) { - return 1 << 3; - } - if (charset == FXFONT_TURKISH_CHARSET) { - return 1 << 4; - } - if (charset == FXFONT_HEBREW_CHARSET) { - return 1 << 5; - } - if (charset == FXFONT_ARABIC_CHARSET) { - return 1 << 6; - } - if (charset == FXFONT_BALTIC_CHARSET) { - return 1 << 7; - } - if (charset == FXFONT_THAI_CHARSET) { - return 1 << 16; - } - if (charset == FXFONT_SHIFTJIS_CHARSET) { - return 1 << 17; - } - if (charset == FXFONT_GB2312_CHARSET) { - return 1 << 18; - } - if (charset == FXFONT_CHINESEBIG5_CHARSET) { - return 1 << 20; - } - if (charset == FXFONT_HANGEUL_CHARSET) { - return 1 << 19; - } - if (charset == FXFONT_SYMBOL_CHARSET) { - return 1 << 31; - } - return 1 << 21; -} + FXFT_Face CFX_FontMapper::UseInternalSubst(CFX_SubstFont* pSubstFont, int iBaseFont, int italic_angle, @@ -711,102 +822,7 @@ FXFT_Face CFX_FontMapper::UseInternalSubst(CFX_SubstFont* pSubstFont, m_MMFaces[0] = m_pFontMgr->GetFixedFace(pFontData, size, 0); return m_MMFaces[0]; } -const struct _AltFontFamily { - const FX_CHAR* m_pFontName; - const FX_CHAR* m_pFontFamily; -} g_AltFontFamilies[] = { - {"AGaramondPro", "Adobe Garamond Pro"}, - {"BankGothicBT-Medium", "BankGothic Md BT"}, - {"ForteMT", "Forte"}, -}; -extern "C" { -static int compareFontFamilyString(const void* key, const void* element) { - CFX_ByteString str_key((const FX_CHAR*)key); - if (str_key.Find(((_AltFontFamily*)element)->m_pFontName) != -1) { - return 0; - } - return FXSYS_stricmp((const FX_CHAR*)key, - ((_AltFontFamily*)element)->m_pFontName); -} -} -#define FX_FONT_STYLE_None 0x00 -#define FX_FONT_STYLE_Bold 0x01 -#define FX_FONT_STYLE_Italic 0x02 -#define FX_FONT_STYLE_BoldBold 0x04 -static CFX_ByteString _GetFontFamily(CFX_ByteString fontName, int nStyle) { - if (fontName.Find("Script") >= 0) { - if ((nStyle & FX_FONT_STYLE_Bold) == FX_FONT_STYLE_Bold) { - fontName = "ScriptMTBold"; - } else if (fontName.Find("Palace") >= 0) { - fontName = "PalaceScriptMT"; - } else if (fontName.Find("French") >= 0) { - fontName = "FrenchScriptMT"; - } else if (fontName.Find("FreeStyle") >= 0) { - fontName = "FreeStyleScript"; - } - return fontName; - } - _AltFontFamily* found = (_AltFontFamily*)FXSYS_bsearch( - fontName.c_str(), g_AltFontFamilies, - sizeof g_AltFontFamilies / sizeof(_AltFontFamily), sizeof(_AltFontFamily), - compareFontFamilyString); - if (found == NULL) { - return fontName; - } - return found->m_pFontFamily; -}; -typedef struct _FX_FontStyle { - const FX_CHAR* style; - int32_t len; -} FX_FontStyle; -const FX_FontStyle g_FontStyles[] = { - {"Bold", 4}, {"Italic", 6}, {"BoldItalic", 10}, {"Reg", 3}, {"Regular", 7}, -}; -CFX_ByteString ParseStyle(const FX_CHAR* pStyle, int iLen, int iIndex) { - CFX_ByteTextBuf buf; - if (!iLen || iLen <= iIndex) { - return buf.GetByteString(); - } - while (iIndex < iLen) { - if (pStyle[iIndex] == ',') { - break; - } - buf.AppendChar(pStyle[iIndex]); - ++iIndex; - } - return buf.GetByteString(); -} -int32_t GetStyleType(const CFX_ByteString& bsStyle, FX_BOOL bRevert) { - int32_t iLen = bsStyle.GetLength(); - if (!iLen) { - return -1; - } - int iSize = sizeof(g_FontStyles) / sizeof(FX_FontStyle); - const FX_FontStyle* pStyle = NULL; - for (int i = iSize - 1; i >= 0; --i) { - pStyle = g_FontStyles + i; - if (!pStyle || pStyle->len > iLen) { - continue; - } - if (!bRevert) { - if (bsStyle.Left(pStyle->len).Compare(pStyle->style) == 0) { - return i; - } - } else { - if (bsStyle.Right(pStyle->len).Compare(pStyle->style) == 0) { - return i; - } - } - } - return -1; -} -FX_BOOL CheckSupportThirdPartFont(CFX_ByteString name, int& PitchFamily) { - if (name == FX_BSTRC("MyriadPro")) { - PitchFamily &= ~FXFONT_FF_ROMAN; - return TRUE; - } - return FALSE; -} + FXFT_Face CFX_FontMapper::FindSubstFont(const CFX_ByteString& name, FX_BOOL bTrueType, FX_DWORD flags, @@ -982,7 +998,7 @@ FXFT_Face CFX_FontMapper::FindSubstFont(const CFX_ByteString& name, int iExact = 0; int Charset = FXFONT_ANSI_CHARSET; if (WindowCP) { - Charset = _GetCharsetFromCodePage(WindowCP); + Charset = GetCharsetFromCodePage(WindowCP); } else if (iBaseFont == 12 && (flags & FXFONT_SYMBOLIC)) { Charset = FXFONT_SYMBOL_CHARSET; } @@ -996,11 +1012,11 @@ FXFT_Face CFX_FontMapper::FindSubstFont(const CFX_ByteString& name, return UseInternalSubst(pSubstFont, iBaseFont, italic_angle, old_weight, PitchFamily); } - family = _GetFontFamily(family, nStyle); - CFX_ByteString match = MatchInstalledFonts(_TT_NormalizeName(family)); + family = GetFontFamily(family, nStyle); + CFX_ByteString match = MatchInstalledFonts(TT_NormalizeName(family)); if (match.IsEmpty() && family != SubstName && (!bHasComma && (!bHasHypen || (bHasHypen && !bStyleAvail)))) { - match = MatchInstalledFonts(_TT_NormalizeName(SubstName)); + match = MatchInstalledFonts(TT_NormalizeName(SubstName)); } if (match.IsEmpty() && iBaseFont >= 12) { if (!bCJK) { @@ -1301,12 +1317,12 @@ void CFX_FolderFontInfo::ReportFace(CFX_ByteString& path, return; } FX_DWORD nTables = GET_TT_SHORT(buffer + 4); - CFX_ByteString tables = _FPDF_ReadStringFromFile(pFile, nTables * 16); + CFX_ByteString tables = FPDF_ReadStringFromFile(pFile, nTables * 16); if (tables.IsEmpty()) { return; } CFX_ByteString names = - _FPDF_LoadTableFromTT(pFile, tables, nTables, 0x6e616d65); + FPDF_LoadTableFromTT(pFile, tables, nTables, 0x6e616d65); CFX_ByteString facename = GetNameFromTT(names, 1); CFX_ByteString style = GetNameFromTT(names, 2); if (style != "Regular") { @@ -1317,8 +1333,7 @@ void CFX_FolderFontInfo::ReportFace(CFX_ByteString& path, } CFX_FontFaceInfo* pInfo = new CFX_FontFaceInfo(path, facename, tables, offset, filesize); - CFX_ByteString os2 = - _FPDF_LoadTableFromTT(pFile, tables, nTables, 0x4f532f32); + CFX_ByteString os2 = FPDF_LoadTableFromTT(pFile, tables, nTables, 0x4f532f32); if (os2.GetLength() >= 86) { const uint8_t* p = (const uint8_t*)os2 + 78; FX_DWORD codepages = GET_TT_LONG(p); @@ -1358,23 +1373,7 @@ void CFX_FolderFontInfo::ReportFace(CFX_ByteString& path, } m_FontList[facename] = pInfo; } -static const struct { - const FX_CHAR* m_pName; - const FX_CHAR* m_pSubstName; -} Base14Substs[] = { - {"Courier", "Courier New"}, - {"Courier-Bold", "Courier New Bold"}, - {"Courier-BoldOblique", "Courier New Bold Italic"}, - {"Courier-Oblique", "Courier New Italic"}, - {"Helvetica", "Arial"}, - {"Helvetica-Bold", "Arial Bold"}, - {"Helvetica-BoldOblique", "Arial Bold Italic"}, - {"Helvetica-Oblique", "Arial Italic"}, - {"Times-Roman", "Times New Roman"}, - {"Times-Bold", "Times New Roman Bold"}, - {"Times-BoldItalic", "Times New Roman Bold Italic"}, - {"Times-Italic", "Times New Roman Italic"}, -}; + void* CFX_FolderFontInfo::GetSubstFont(const CFX_ByteString& face) { for (size_t iBaseFont = 0; iBaseFont < FX_ArraySize(Base14Substs); iBaseFont++) { @@ -1384,47 +1383,7 @@ void* CFX_FolderFontInfo::GetSubstFont(const CFX_ByteString& face) { } return nullptr; } -static FX_DWORD _GetCharset(int charset) { - switch (charset) { - case FXFONT_SHIFTJIS_CHARSET: - return CHARSET_FLAG_SHIFTJIS; - case FXFONT_GB2312_CHARSET: - return CHARSET_FLAG_GB; - case FXFONT_CHINESEBIG5_CHARSET: - return CHARSET_FLAG_BIG5; - case FXFONT_HANGEUL_CHARSET: - return CHARSET_FLAG_KOREAN; - case FXFONT_SYMBOL_CHARSET: - return CHARSET_FLAG_SYMBOL; - case FXFONT_ANSI_CHARSET: - return CHARSET_FLAG_ANSI; - default: - break; - } - return 0; -} -static int32_t _GetSimilarValue(int weight, - FX_BOOL bItalic, - int pitch_family, - FX_DWORD style) { - int32_t iSimilarValue = 0; - if ((style & FXFONT_BOLD) == (weight > 400)) { - iSimilarValue += 16; - } - if ((style & FXFONT_ITALIC) == bItalic) { - iSimilarValue += 16; - } - if ((style & FXFONT_SERIF) == (pitch_family & FXFONT_FF_ROMAN)) { - iSimilarValue += 16; - } - if ((style & FXFONT_SCRIPT) == (pitch_family & FXFONT_FF_SCRIPT)) { - iSimilarValue += 8; - } - if ((style & FXFONT_FIXED_PITCH) == (pitch_family & FXFONT_FF_FIXEDPITCH)) { - iSimilarValue += 8; - } - return iSimilarValue; -} + void* CFX_FolderFontInfo::FindFont(int weight, FX_BOOL bItalic, int charset, @@ -1435,7 +1394,7 @@ void* CFX_FolderFontInfo::FindFont(int weight, if (charset == FXFONT_ANSI_CHARSET && (pitch_family & FXFONT_FF_FIXEDPITCH)) { return GetFont("Courier New"); } - FX_DWORD charset_flag = _GetCharset(charset); + FX_DWORD charset_flag = GetCharset(charset); int32_t iBestSimilar = 0; for (const auto& it : m_FontList) { const CFX_ByteString& bsName = it.first; @@ -1449,7 +1408,7 @@ void* CFX_FolderFontInfo::FindFont(int weight, continue; } int32_t iSimilarValue = - _GetSimilarValue(weight, bItalic, pitch_family, pFont->m_Styles); + GetSimilarValue(weight, bItalic, pitch_family, pFont->m_Styles); if (iSimilarValue > iBestSimilar) { iBestSimilar = iSimilarValue; pFind = pFont; @@ -1527,7 +1486,7 @@ FX_BOOL CFX_FolderFontInfo::GetFontCharset(void* hFont, int& charset) { int PDF_GetStandardFontName(CFX_ByteString* name) { AltFontName* found = static_cast( FXSYS_bsearch(name->c_str(), g_AltFontNames, FX_ArraySize(g_AltFontNames), - sizeof(AltFontName), compareString)); + sizeof(AltFontName), CompareString)); if (!found) return -1; diff --git a/core/src/fxge/ge/fx_ge_text.cpp b/core/src/fxge/ge/fx_ge_text.cpp index 6cc9790e56..efb4366423 100644 --- a/core/src/fxge/ge/fx_ge_text.cpp +++ b/core/src/fxge/ge/fx_ge_text.cpp @@ -37,7 +37,8 @@ class ScopedFontTransform { private: FT_Face m_Face; }; -} + +} // namespace FX_RECT FXGE_GetGlyphsBBox(FXTEXT_GLYPHPOS* pGlyphAndPos, int nChars, @@ -295,10 +296,11 @@ FX_BOOL CFX_RenderDevice::DrawNormalText(int nChars, glyph.m_pGlyph = pFaceCache->LoadGlyphBitmap( pFont, charpos.m_GlyphIndex, charpos.m_bFontStyle, &new_matrix, charpos.m_FontCharWidth, anti_alias, nativetext_flags); - } else + } else { glyph.m_pGlyph = pFaceCache->LoadGlyphBitmap( pFont, charpos.m_GlyphIndex, charpos.m_bFontStyle, &deviceCtm, charpos.m_FontCharWidth, anti_alias, nativetext_flags); + } } if (anti_alias < FXFT_RENDER_MODE_LCD && nChars > 1) { _AdjustGlyphSpace(pGlyphAndPos, nChars); @@ -1228,9 +1230,8 @@ void CFX_FontCache::FreeCache(FX_BOOL bRelease) { } } -CFX_FaceCache::CFX_FaceCache(FXFT_Face face) { - m_Face = face; -} +CFX_FaceCache::CFX_FaceCache(FXFT_Face face) : m_Face(face) {} + CFX_FaceCache::~CFX_FaceCache() { for (const auto& pair : m_SizeMap) { delete pair.second; @@ -1656,12 +1657,12 @@ const CFX_PathData* CFX_FaceCache::LoadGlyphPath(CFX_Font* pFont, } CFX_PathData* pGlyphPath = NULL; void* key; - if (pFont->GetSubstFont()) + if (pFont->GetSubstFont()) { key = (void*)(uintptr_t)( glyph_index + ((pFont->GetSubstFont()->m_Weight / 16) << 15) + ((pFont->GetSubstFont()->m_ItalicAngle / 2) << 21) + ((dest_width / 16) << 25) + (pFont->IsVertical() << 31)); - else { + } else { key = (void*)(uintptr_t)glyph_index; } if (m_PathMap.Lookup(key, (void*&)pGlyphPath)) { -- cgit v1.2.3