From 6eda94d78313f18a8f0708c4492d0962649fa709 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Tue, 5 Dec 2017 20:34:36 +0000 Subject: Replace uses of bsearch(). Either switch to std::lower_bound(), or brute force when the search space is very small. Change-Id: I54db346aeb6f2e8000e95a6a9c8fbdd512df500a Reviewed-on: https://pdfium-review.googlesource.com/20431 Reviewed-by: dsinclair Commit-Queue: Lei Zhang --- core/fpdfapi/font/cpdf_type1font.cpp | 16 +++++----- core/fxge/cfx_fontmapper.cpp | 58 ++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 42 deletions(-) diff --git a/core/fpdfapi/font/cpdf_type1font.cpp b/core/fpdfapi/font/cpdf_type1font.cpp index 9975943d9a..21d9da42c8 100644 --- a/core/fpdfapi/font/cpdf_type1font.cpp +++ b/core/fpdfapi/font/cpdf_type1font.cpp @@ -6,6 +6,8 @@ #include "core/fpdfapi/font/cpdf_type1font.h" +#include + #include "core/fpdfapi/parser/cpdf_dictionary.h" #include "core/fxge/cfx_gemodule.h" #include "core/fxge/fx_freetype.h" @@ -28,16 +30,12 @@ const GlyphNameMap g_GlyphNameSubsts[] = {{"ff", "uniFB00"}, {"fi", "uniFB01"}, {"fl", "uniFB02"}}; -int compareString(const void* key, const void* element) { - return FXSYS_stricmp(static_cast(key), - static_cast(element)->m_pStrAdobe); -} - const char* GlyphNameRemap(const char* pStrAdobe) { - const GlyphNameMap* found = static_cast( - bsearch(pStrAdobe, g_GlyphNameSubsts, FX_ArraySize(g_GlyphNameSubsts), - sizeof(GlyphNameMap), compareString)); - return found ? found->m_pStrUnicode : nullptr; + for (const auto& element : g_GlyphNameSubsts) { + if (!FXSYS_stricmp(element.m_pStrAdobe, pStrAdobe)) + return element.m_pStrUnicode; + } + return nullptr; } #endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ diff --git a/core/fxge/cfx_fontmapper.cpp b/core/fxge/cfx_fontmapper.cpp index bb80c27d51..406a8222db 100644 --- a/core/fxge/cfx_fontmapper.cpp +++ b/core/fxge/cfx_fontmapper.cpp @@ -158,19 +158,6 @@ const struct CODEPAGE_MAP { {10081, 86}, }; -int CompareFontFamilyString(const void* key, const void* element) { - ByteString str_key((const char*)key); - const AltFontFamily* family = reinterpret_cast(element); - if (str_key.Contains(family->m_pFontName)) - return 0; - return FXSYS_stricmp(reinterpret_cast(key), family->m_pFontName); -} - -int CompareString(const void* key, const void* element) { - return FXSYS_stricmp(reinterpret_cast(key), - reinterpret_cast(element)->m_pName); -} - ByteString TT_NormalizeName(const char* family) { ByteString norm(family); norm.Remove(' '); @@ -196,22 +183,24 @@ uint8_t GetCharsetFromCodePage(uint16_t codepage) { return FX_CHARSET_Default; } -ByteString GetFontFamily(ByteString fontName, uint32_t nStyle) { - if (fontName.Contains("Script")) { +void GetFontFamily(uint32_t nStyle, ByteString* fontName) { + if (fontName->Contains("Script")) { if (FontStyleIsBold(nStyle)) - fontName = "ScriptMTBold"; - else if (fontName.Contains("Palace")) - fontName = "PalaceScriptMT"; - else if (fontName.Contains("French")) - fontName = "FrenchScriptMT"; - else if (fontName.Contains("FreeStyle")) - fontName = "FreeStyleScript"; - return fontName; - } - AltFontFamily* found = reinterpret_cast(bsearch( - fontName.c_str(), g_AltFontFamilies, FX_ArraySize(g_AltFontFamilies), - sizeof(AltFontFamily), CompareFontFamilyString)); - return found ? ByteString(found->m_pFontFamily) : fontName; + *fontName = "ScriptMTBold"; + else if (fontName->Contains("Palace")) + *fontName = "PalaceScriptMT"; + else if (fontName->Contains("French")) + *fontName = "FrenchScriptMT"; + else if (fontName->Contains("FreeStyle")) + *fontName = "FreeStyleScript"; + return; + } + for (const auto& alternate : g_AltFontFamilies) { + if (fontName->Contains(alternate.m_pFontName)) { + *fontName = alternate.m_pFontFamily; + return; + } + } } ByteString ParseStyle(const char* pStyle, int iLen, int iIndex) { @@ -566,7 +555,7 @@ FXFT_Face CFX_FontMapper::FindSubstFont(const ByteString& name, return UseInternalSubst(pSubstFont, iBaseFont, italic_angle, old_weight, PitchFamily); } - family = GetFontFamily(family, nStyle); + GetFontFamily(nStyle, &family); ByteString match = MatchInstalledFonts(TT_NormalizeName(family.c_str())); if (match.IsEmpty() && family != SubstName && (!bHasComma && (!bHasHyphen || (bHasHyphen && !bStyleAvail)))) { @@ -800,10 +789,13 @@ FXFT_Face CFX_FontMapper::GetCachedFace(void* hFont, } int PDF_GetStandardFontName(ByteString* name) { - AltFontName* found = static_cast( - bsearch(name->c_str(), g_AltFontNames, FX_ArraySize(g_AltFontNames), - sizeof(AltFontName), CompareString)); - if (!found) + const auto* end = std::end(g_AltFontNames); + const auto* found = + std::lower_bound(std::begin(g_AltFontNames), end, name->c_str(), + [](const AltFontName& element, const char* name) { + return FXSYS_stricmp(element.m_pName, name) < 0; + }); + if (found == end || FXSYS_stricmp(found->m_pName, name->c_str())) return -1; *name = g_Base14FontNames[found->m_Index]; -- cgit v1.2.3