Codechange: move font settings to std::string

pull/332/head
rubidium42 3 years ago committed by rubidium42
parent 65cbde4b30
commit 77330d09fd

@ -490,7 +490,7 @@ static void LoadFreeTypeFont(FontSize fs)
case FS_MONO: settings = &_freetype.mono; break; case FS_MONO: settings = &_freetype.mono; break;
} }
if (StrEmpty(settings->font)) return; if (settings->font.empty()) return;
if (_library == nullptr) { if (_library == nullptr) {
if (FT_Init_FreeType(&_library) != FT_Err_Ok) { if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
@ -501,19 +501,20 @@ static void LoadFreeTypeFont(FontSize fs)
DEBUG(freetype, 2, "Initialized"); DEBUG(freetype, 2, "Initialized");
} }
const char *font_name = settings->font.c_str();
FT_Face face = nullptr; FT_Face face = nullptr;
/* If font is an absolute path to a ttf, try loading that first. */ /* If font is an absolute path to a ttf, try loading that first. */
FT_Error error = FT_New_Face(_library, settings->font, 0, &face); FT_Error error = FT_New_Face(_library, font_name, 0, &face);
#if defined(WITH_COCOA) #if defined(WITH_COCOA)
extern void MacOSRegisterExternalFont(const char *file_path); extern void MacOSRegisterExternalFont(const char *file_path);
if (error == FT_Err_Ok) MacOSRegisterExternalFont(settings->font); if (error == FT_Err_Ok) MacOSRegisterExternalFont(font_name);
#endif #endif
if (error != FT_Err_Ok) { if (error != FT_Err_Ok) {
/* Check if font is a relative filename in one of our search-paths. */ /* Check if font is a relative filename in one of our search-paths. */
std::string full_font = FioFindFullPath(BASE_DIR, settings->font); std::string full_font = FioFindFullPath(BASE_DIR, font_name);
if (!full_font.empty()) { if (!full_font.empty()) {
error = FT_New_Face(_library, full_font.c_str(), 0, &face); error = FT_New_Face(_library, full_font.c_str(), 0, &face);
#if defined(WITH_COCOA) #if defined(WITH_COCOA)
@ -523,10 +524,10 @@ static void LoadFreeTypeFont(FontSize fs)
} }
/* Try loading based on font face name (OS-wide fonts). */ /* Try loading based on font face name (OS-wide fonts). */
if (error != FT_Err_Ok) error = GetFontByFaceName(settings->font, &face); if (error != FT_Err_Ok) error = GetFontByFaceName(font_name, &face);
if (error == FT_Err_Ok) { if (error == FT_Err_Ok) {
DEBUG(freetype, 2, "Requested '%s', using '%s %s'", settings->font, face->family_name, face->style_name); DEBUG(freetype, 2, "Requested '%s', using '%s %s'", font_name, face->family_name, face->style_name);
/* Attempt to select the unicode character map */ /* Attempt to select the unicode character map */
error = FT_Select_Charmap(face, ft_encoding_unicode); error = FT_Select_Charmap(face, ft_encoding_unicode);
@ -556,7 +557,7 @@ static void LoadFreeTypeFont(FontSize fs)
FT_Done_Face(face); FT_Done_Face(face);
static const char *SIZE_TO_NAME[] = { "medium", "small", "large", "mono" }; static const char *SIZE_TO_NAME[] = { "medium", "small", "large", "mono" };
ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", settings->font, SIZE_TO_NAME[fs], error); ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", font_name, SIZE_TO_NAME[fs], error);
return; return;
found_face: found_face:

@ -214,9 +214,9 @@ static inline bool GetDrawGlyphShadow(FontSize size)
/** Settings for a single freetype font. */ /** Settings for a single freetype font. */
struct FreeTypeSubSetting { struct FreeTypeSubSetting {
char font[MAX_PATH]; ///< The name of the font, or path to the font. std::string font; ///< The name of the font, or path to the font.
uint size; ///< The (requested) size of the font. uint size; ///< The (requested) size of the font.
bool aa; ///< Whether to do anti aliasing or not. bool aa; ///< Whether to do anti aliasing or not.
const void *os_handle = nullptr; ///< Optional native OS font info. Only valid during font search. const void *os_handle = nullptr; ///< Optional native OS font info. Only valid during font search.
}; };

@ -359,7 +359,7 @@ void LoadCoreTextFont(FontSize fs)
case FS_MONO: settings = &_freetype.mono; break; case FS_MONO: settings = &_freetype.mono; break;
} }
if (StrEmpty(settings->font)) return; if (settings->font.empty()) return;
CFAutoRelease<CTFontDescriptorRef> font_ref; CFAutoRelease<CTFontDescriptorRef> font_ref;
@ -375,10 +375,10 @@ void LoadCoreTextFont(FontSize fs)
/* See if this is an absolute path. */ /* See if this is an absolute path. */
if (FileExists(settings->font)) { if (FileExists(settings->font)) {
path.reset(CFStringCreateWithCString(kCFAllocatorDefault, settings->font, kCFStringEncodingUTF8)); path.reset(CFStringCreateWithCString(kCFAllocatorDefault, settings->font.c_str(), kCFStringEncodingUTF8));
} else { } else {
/* Scan the search-paths to see if it can be found. */ /* Scan the search-paths to see if it can be found. */
std::string full_font = FioFindFullPath(BASE_DIR, settings->font); std::string full_font = FioFindFullPath(BASE_DIR, settings->font.c_str());
if (!full_font.empty()) { if (!full_font.empty()) {
path.reset(CFStringCreateWithCString(kCFAllocatorDefault, full_font.c_str(), kCFStringEncodingUTF8)); path.reset(CFStringCreateWithCString(kCFAllocatorDefault, full_font.c_str(), kCFStringEncodingUTF8));
} }
@ -393,13 +393,13 @@ void LoadCoreTextFont(FontSize fs)
font_ref.reset((CTFontDescriptorRef)CFArrayGetValueAtIndex(descs.get(), 0)); font_ref.reset((CTFontDescriptorRef)CFArrayGetValueAtIndex(descs.get(), 0));
CFRetain(font_ref.get()); CFRetain(font_ref.get());
} else { } else {
ShowInfoF("Unable to load file '%s' for %s font, using default OS font selection instead", settings->font, SIZE_TO_NAME[fs]); ShowInfoF("Unable to load file '%s' for %s font, using default OS font selection instead", settings->font.c_str(), SIZE_TO_NAME[fs]);
} }
} }
} }
if (!font_ref) { if (!font_ref) {
CFAutoRelease<CFStringRef> name(CFStringCreateWithCString(kCFAllocatorDefault, settings->font, kCFStringEncodingUTF8)); CFAutoRelease<CFStringRef> name(CFStringCreateWithCString(kCFAllocatorDefault, settings->font.c_str(), kCFStringEncodingUTF8));
/* Simply creating the font using CTFontCreateWithNameAndSize will *always* return /* Simply creating the font using CTFontCreateWithNameAndSize will *always* return
* something, no matter the name. As such, we can't use it to check for existence. * something, no matter the name. As such, we can't use it to check for existence.
@ -417,7 +417,7 @@ void LoadCoreTextFont(FontSize fs)
} }
if (!font_ref) { if (!font_ref) {
ShowInfoF("Unable to use '%s' for %s font, using sprite font instead", settings->font, SIZE_TO_NAME[fs]); ShowInfoF("Unable to use '%s' for %s font, using sprite font instead", settings->font.c_str(), SIZE_TO_NAME[fs]);
return; return;
} }

@ -588,8 +588,9 @@ void LoadWin32Font(FontSize fs)
default: NOT_REACHED(); default: NOT_REACHED();
} }
if (StrEmpty(settings->font)) return; if (settings->font.empty()) return;
const char *font_name = settings->font.c_str();
LOGFONT logfont; LOGFONT logfont;
MemSetT(&logfont, 0); MemSetT(&logfont, 0);
logfont.lfPitchAndFamily = fs == FS_MONO ? FIXED_PITCH : VARIABLE_PITCH; logfont.lfPitchAndFamily = fs == FS_MONO ? FIXED_PITCH : VARIABLE_PITCH;
@ -599,19 +600,19 @@ void LoadWin32Font(FontSize fs)
if (settings->os_handle != nullptr) { if (settings->os_handle != nullptr) {
logfont = *(const LOGFONT *)settings->os_handle; logfont = *(const LOGFONT *)settings->os_handle;
} else if (strchr(settings->font, '.') != nullptr) { } else if (strchr(font_name, '.') != nullptr) {
/* Might be a font file name, try load it. */ /* Might be a font file name, try load it. */
wchar_t fontPath[MAX_PATH] = {}; wchar_t fontPath[MAX_PATH] = {};
/* See if this is an absolute path. */ /* See if this is an absolute path. */
if (FileExists(settings->font)) { if (FileExists(settings->font)) {
convert_to_fs(settings->font, fontPath, lengthof(fontPath)); convert_to_fs(font_name, fontPath, lengthof(fontPath));
} else { } else {
/* Scan the search-paths to see if it can be found. */ /* Scan the search-paths to see if it can be found. */
std::string full_font = FioFindFullPath(BASE_DIR, settings->font); std::string full_font = FioFindFullPath(BASE_DIR, font_name);
if (!full_font.empty()) { if (!full_font.empty()) {
convert_to_fs(full_font.c_str(), fontPath, lengthof(fontPath)); convert_to_fs(font_name, fontPath, lengthof(fontPath));
} }
} }
@ -639,22 +640,22 @@ void LoadWin32Font(FontSize fs)
_wsplitpath(fontPath, nullptr, nullptr, fname, nullptr); _wsplitpath(fontPath, nullptr, nullptr, fname, nullptr);
wcsncpy_s(logfont.lfFaceName, lengthof(logfont.lfFaceName), fname, _TRUNCATE); wcsncpy_s(logfont.lfFaceName, lengthof(logfont.lfFaceName), fname, _TRUNCATE);
logfont.lfWeight = strcasestr(settings->font, " bold") != nullptr || strcasestr(settings->font, "-bold") != nullptr ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts. logfont.lfWeight = strcasestr(font_name, " bold") != nullptr || strcasestr(font_name, "-bold") != nullptr ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts.
} }
} else { } else {
ShowInfoF("Unable to load file '%s' for %s font, using default windows font selection instead", settings->font, SIZE_TO_NAME[fs]); ShowInfoF("Unable to load file '%s' for %s font, using default windows font selection instead", font_name, SIZE_TO_NAME[fs]);
} }
} }
} }
if (logfont.lfFaceName[0] == 0) { if (logfont.lfFaceName[0] == 0) {
logfont.lfWeight = strcasestr(settings->font, " bold") != nullptr ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts. logfont.lfWeight = strcasestr(font_name, " bold") != nullptr ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts.
convert_to_fs(settings->font, logfont.lfFaceName, lengthof(logfont.lfFaceName)); convert_to_fs(font_name, logfont.lfFaceName, lengthof(logfont.lfFaceName));
} }
HFONT font = CreateFontIndirect(&logfont); HFONT font = CreateFontIndirect(&logfont);
if (font == nullptr) { if (font == nullptr) {
ShowInfoF("Unable to use '%s' for %s font, Win32 reported error 0x%lX, using sprite font instead", settings->font, SIZE_TO_NAME[fs], GetLastError()); ShowInfoF("Unable to use '%s' for %s font, Win32 reported error 0x%lX, using sprite font instead", font_name, SIZE_TO_NAME[fs], GetLastError());
return; return;
} }
DeleteObject(font); DeleteObject(font);

@ -2090,9 +2090,9 @@ class LanguagePackGlyphSearcher : public MissingGlyphSearcher {
void SetFontNames(FreeTypeSettings *settings, const char *font_name, const void *os_data) override void SetFontNames(FreeTypeSettings *settings, const char *font_name, const void *os_data) override
{ {
#if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA) #if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
strecpy(settings->small.font, font_name, lastof(settings->small.font)); settings->small.font = font_name;
strecpy(settings->medium.font, font_name, lastof(settings->medium.font)); settings->medium.font = font_name;
strecpy(settings->large.font, font_name, lastof(settings->large.font)); settings->large.font = font_name;
settings->small.os_handle = os_data; settings->small.os_handle = os_data;
settings->medium.os_handle = os_data; settings->medium.os_handle = os_data;
@ -2123,15 +2123,14 @@ void CheckForMissingGlyphs(bool base_font, MissingGlyphSearcher *searcher)
if (bad_font) { if (bad_font) {
/* We found an unprintable character... lets try whether we can find /* We found an unprintable character... lets try whether we can find
* a fallback font that can print the characters in the current language. */ * a fallback font that can print the characters in the current language. */
FreeTypeSettings backup; FreeTypeSettings backup = _freetype;
memcpy(&backup, &_freetype, sizeof(backup));
_freetype.mono.os_handle = nullptr; _freetype.mono.os_handle = nullptr;
_freetype.medium.os_handle = nullptr; _freetype.medium.os_handle = nullptr;
bad_font = !SetFallbackFont(&_freetype, _langpack.langpack->isocode, _langpack.langpack->winlangid, searcher); bad_font = !SetFallbackFont(&_freetype, _langpack.langpack->isocode, _langpack.langpack->winlangid, searcher);
memcpy(&_freetype, &backup, sizeof(backup)); _freetype = backup;
if (!bad_font) { if (!bad_font) {
/* Show that we loaded fallback font. To do this properly we have /* Show that we loaded fallback font. To do this properly we have

@ -175,31 +175,31 @@ name = ""rightclick_emulate""
var = _rightclick_emulate var = _rightclick_emulate
def = false def = false
[SDTG_STR] [SDTG_SSTR]
ifdef = HAS_TRUETYPE_FONT ifdef = HAS_TRUETYPE_FONT
name = ""small_font"" name = ""small_font""
type = SLE_STRB type = SLE_STR
var = _freetype.small.font var = _freetype.small.font
def = nullptr def = nullptr
[SDTG_STR] [SDTG_SSTR]
ifdef = HAS_TRUETYPE_FONT ifdef = HAS_TRUETYPE_FONT
name = ""medium_font"" name = ""medium_font""
type = SLE_STRB type = SLE_STR
var = _freetype.medium.font var = _freetype.medium.font
def = nullptr def = nullptr
[SDTG_STR] [SDTG_SSTR]
ifdef = HAS_TRUETYPE_FONT ifdef = HAS_TRUETYPE_FONT
name = ""large_font"" name = ""large_font""
type = SLE_STRB type = SLE_STR
var = _freetype.large.font var = _freetype.large.font
def = nullptr def = nullptr
[SDTG_STR] [SDTG_SSTR]
ifdef = HAS_TRUETYPE_FONT ifdef = HAS_TRUETYPE_FONT
name = ""mono_font"" name = ""mono_font""
type = SLE_STRB type = SLE_STR
var = _freetype.mono.font var = _freetype.mono.font
def = nullptr def = nullptr

@ -218,7 +218,7 @@ void TextfileWindow::SetupScrollbars(bool force_reflow)
/* virtual */ void TextfileWindow::SetFontNames(FreeTypeSettings *settings, const char *font_name, const void *os_data) /* virtual */ void TextfileWindow::SetFontNames(FreeTypeSettings *settings, const char *font_name, const void *os_data)
{ {
#if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA) #if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
strecpy(settings->mono.font, font_name, lastof(settings->mono.font)); settings->mono.font = font_name;
settings->mono.os_handle = os_data; settings->mono.os_handle = os_data;
#endif #endif
} }

Loading…
Cancel
Save