summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLei Zhang <thestig@chromium.org>2017-12-05 20:34:36 +0000
committerChromium commit bot <commit-bot@chromium.org>2017-12-05 20:34:36 +0000
commit6eda94d78313f18a8f0708c4492d0962649fa709 (patch)
tree29e0e340e47865b3ee2f2f3f45e40a606ec46198
parentc5352efd9c0f86a189181f93405ee10919915841 (diff)
downloadpdfium-6eda94d78313f18a8f0708c4492d0962649fa709.tar.xz
Replace uses of bsearch().chromium/3286
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 <dsinclair@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org>
-rw-r--r--core/fpdfapi/font/cpdf_type1font.cpp16
-rw-r--r--core/fxge/cfx_fontmapper.cpp58
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 <algorithm>
+
#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<const char*>(key),
- static_cast<const GlyphNameMap*>(element)->m_pStrAdobe);
-}
-
const char* GlyphNameRemap(const char* pStrAdobe) {
- const GlyphNameMap* found = static_cast<const GlyphNameMap*>(
- 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<const AltFontFamily*>(element);
- if (str_key.Contains(family->m_pFontName))
- return 0;
- return FXSYS_stricmp(reinterpret_cast<const char*>(key), family->m_pFontName);
-}
-
-int CompareString(const void* key, const void* element) {
- return FXSYS_stricmp(reinterpret_cast<const char*>(key),
- reinterpret_cast<const AltFontName*>(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<AltFontFamily*>(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<AltFontName*>(
- 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];