From eda3defcb5e6653c4c4bc4d53c21d3725bed7fd8 Mon Sep 17 00:00:00 2001 From: PeterN Date: Tue, 6 Jun 2023 21:55:56 +0100 Subject: [PATCH] Codechange: Pass language for font detection as std::string. (#10964) --- src/fontcache.cpp | 2 +- src/fontdetection.h | 2 +- src/os/macosx/font_osx.cpp | 18 ++++++++---------- src/os/unix/font_unix.cpp | 9 +++------ src/os/windows/font_win32.cpp | 2 +- 5 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/fontcache.cpp b/src/fontcache.cpp index 93c3f01b26..dcaa05d25a 100644 --- a/src/fontcache.cpp +++ b/src/fontcache.cpp @@ -189,5 +189,5 @@ bool HasAntialiasedFonts() #if !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA) -bool SetFallbackFont(FontCacheSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback) { return false; } +bool SetFallbackFont(FontCacheSettings *settings, const std::string &language_isocode, int winlangid, MissingGlyphSearcher *callback) { return false; } #endif /* !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA) */ diff --git a/src/fontdetection.h b/src/fontdetection.h index a0242fda1f..2a316da505 100644 --- a/src/fontdetection.h +++ b/src/fontdetection.h @@ -37,6 +37,6 @@ FT_Error GetFontByFaceName(const char *font_name, FT_Face *face); * @param callback The function to call to check for missing glyphs. * @return true if a font has been set, false otherwise. */ -bool SetFallbackFont(struct FontCacheSettings *settings, const char *language_isocode, int winlangid, class MissingGlyphSearcher *callback); +bool SetFallbackFont(struct FontCacheSettings *settings, const std::string &language_isocode, int winlangid, class MissingGlyphSearcher *callback); #endif diff --git a/src/os/macosx/font_osx.cpp b/src/os/macosx/font_osx.cpp index 6179169a86..7897d48141 100644 --- a/src/os/macosx/font_osx.cpp +++ b/src/os/macosx/font_osx.cpp @@ -24,28 +24,26 @@ #include "safeguards.h" -bool SetFallbackFont(FontCacheSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback) +bool SetFallbackFont(FontCacheSettings *settings, const std::string &language_isocode, int winlangid, MissingGlyphSearcher *callback) { /* Determine fallback font using CoreText. This uses the language isocode * to find a suitable font. CoreText is available from 10.5 onwards. */ - char lang[16]; - if (strcmp(language_isocode, "zh_TW") == 0) { + std::string lang; + if (language_isocode == "zh_TW") { /* Traditional Chinese */ - strecpy(lang, "zh-Hant", lastof(lang)); - } else if (strcmp(language_isocode, "zh_CN") == 0) { + lang = "zh-Hant"; + } else if (language_isocode == "zh_CN") { /* Simplified Chinese */ - strecpy(lang, "zh-Hans", lastof(lang)); + lang = "zh-Hans"; } else { /* Just copy the first part of the isocode. */ - strecpy(lang, language_isocode, lastof(lang)); - char *sep = strchr(lang, '_'); - if (sep != nullptr) *sep = '\0'; + lang = language_isocode.substr(0, language_isocode.find('_')); } /* Create a font descriptor matching the wanted language and latin (english) glyphs. * Can't use CFAutoRelease here for everything due to the way the dictionary has to be created. */ CFStringRef lang_codes[2]; - lang_codes[0] = CFStringCreateWithCString(kCFAllocatorDefault, lang, kCFStringEncodingUTF8); + lang_codes[0] = CFStringCreateWithCString(kCFAllocatorDefault, lang.c_str(), kCFStringEncodingUTF8); lang_codes[1] = CFSTR("en"); CFArrayRef lang_arr = CFArrayCreate(kCFAllocatorDefault, (const void **)lang_codes, lengthof(lang_codes), &kCFTypeArrayCallBacks); CFAutoRelease lang_attribs(CFDictionaryCreate(kCFAllocatorDefault, const_cast(reinterpret_cast(&kCTFontLanguagesAttribute)), (const void **)&lang_arr, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)); diff --git a/src/os/unix/font_unix.cpp b/src/os/unix/font_unix.cpp index b4e8c6a0f6..cd428b8d33 100644 --- a/src/os/unix/font_unix.cpp +++ b/src/os/unix/font_unix.cpp @@ -95,7 +95,7 @@ FT_Error GetFontByFaceName(const char *font_name, FT_Face *face) return err; } -bool SetFallbackFont(FontCacheSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback) +bool SetFallbackFont(FontCacheSettings *settings, const std::string &language_isocode, int winlangid, MissingGlyphSearcher *callback) { bool ret = false; @@ -107,13 +107,10 @@ bool SetFallbackFont(FontCacheSettings *settings, const char *language_isocode, /* Fontconfig doesn't handle full language isocodes, only the part * before the _ of e.g. en_GB is used, so "remove" everything after * the _. */ - char lang[16]; - seprintf(lang, lastof(lang), ":lang=%s", language_isocode); - char *split = strchr(lang, '_'); - if (split != nullptr) *split = '\0'; + std::string lang = language_isocode.substr(0, language_isocode.find('_')); /* First create a pattern to match the wanted language. */ - FcPattern *pat = FcNameParse((FcChar8 *)lang); + FcPattern *pat = FcNameParse((const FcChar8 *)lang.c_str()); /* We only want to know the filename. */ FcObjectSet *os = FcObjectSetBuild(FC_FILE, FC_SPACING, FC_SLANT, FC_WEIGHT, nullptr); /* Get the list of filenames matching the wanted language. */ diff --git a/src/os/windows/font_win32.cpp b/src/os/windows/font_win32.cpp index e6f9147751..7379c09206 100644 --- a/src/os/windows/font_win32.cpp +++ b/src/os/windows/font_win32.cpp @@ -88,7 +88,7 @@ static int CALLBACK EnumFontCallback(const ENUMLOGFONTEX *logfont, const NEWTEXT return 0; // stop enumerating } -bool SetFallbackFont(FontCacheSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback) +bool SetFallbackFont(FontCacheSettings *settings, const std::string &language_isocode, int winlangid, MissingGlyphSearcher *callback) { Debug(fontcache, 1, "Trying fallback fonts"); EFCParam langInfo;