summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium42 <rubidium@openttd.org>2021-04-28 17:10:15 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-05-13 23:13:17 +0200
commit77330d09fd8307261e94fe0eb0260f5ca5cc3898 (patch)
tree0a2d3efe86b4f0047b558bb668dc9ed17ac63d3f
parent65cbde4b30f8fdf6d4cf1196f6a596a5550c9aee (diff)
downloadopenttd-77330d09fd8307261e94fe0eb0260f5ca5cc3898.tar.xz
Codechange: move font settings to std::string
-rw-r--r--src/fontcache.cpp15
-rw-r--r--src/fontcache.h6
-rw-r--r--src/os/macosx/font_osx.cpp12
-rw-r--r--src/os/windows/font_win32.cpp21
-rw-r--r--src/strings.cpp11
-rw-r--r--src/table/misc_settings.ini16
-rw-r--r--src/textfile_gui.cpp2
7 files changed, 42 insertions, 41 deletions
diff --git a/src/fontcache.cpp b/src/fontcache.cpp
index 530dc7cf8..2535ac69f 100644
--- a/src/fontcache.cpp
+++ b/src/fontcache.cpp
@@ -490,7 +490,7 @@ static void LoadFreeTypeFont(FontSize fs)
case FS_MONO: settings = &_freetype.mono; break;
}
- if (StrEmpty(settings->font)) return;
+ if (settings->font.empty()) return;
if (_library == nullptr) {
if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
@@ -501,19 +501,20 @@ static void LoadFreeTypeFont(FontSize fs)
DEBUG(freetype, 2, "Initialized");
}
+ const char *font_name = settings->font.c_str();
FT_Face face = nullptr;
/* 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)
extern void MacOSRegisterExternalFont(const char *file_path);
- if (error == FT_Err_Ok) MacOSRegisterExternalFont(settings->font);
+ if (error == FT_Err_Ok) MacOSRegisterExternalFont(font_name);
#endif
if (error != FT_Err_Ok) {
/* 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()) {
error = FT_New_Face(_library, full_font.c_str(), 0, &face);
#if defined(WITH_COCOA)
@@ -523,10 +524,10 @@ static void LoadFreeTypeFont(FontSize fs)
}
/* 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) {
- 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 */
error = FT_Select_Charmap(face, ft_encoding_unicode);
@@ -556,7 +557,7 @@ static void LoadFreeTypeFont(FontSize fs)
FT_Done_Face(face);
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;
found_face:
diff --git a/src/fontcache.h b/src/fontcache.h
index c3b20cb4f..2eb5634cf 100644
--- a/src/fontcache.h
+++ b/src/fontcache.h
@@ -214,9 +214,9 @@ static inline bool GetDrawGlyphShadow(FontSize size)
/** Settings for a single freetype font. */
struct FreeTypeSubSetting {
- char font[MAX_PATH]; ///< The name of the font, or path to the font.
- uint size; ///< The (requested) size of the font.
- bool aa; ///< Whether to do anti aliasing or not.
+ std::string font; ///< The name of the font, or path to the font.
+ uint size; ///< The (requested) size of the font.
+ bool aa; ///< Whether to do anti aliasing or not.
const void *os_handle = nullptr; ///< Optional native OS font info. Only valid during font search.
};
diff --git a/src/os/macosx/font_osx.cpp b/src/os/macosx/font_osx.cpp
index f5ceed850..295fcf257 100644
--- a/src/os/macosx/font_osx.cpp
+++ b/src/os/macosx/font_osx.cpp
@@ -359,7 +359,7 @@ void LoadCoreTextFont(FontSize fs)
case FS_MONO: settings = &_freetype.mono; break;
}
- if (StrEmpty(settings->font)) return;
+ if (settings->font.empty()) return;
CFAutoRelease<CTFontDescriptorRef> font_ref;
@@ -375,10 +375,10 @@ void LoadCoreTextFont(FontSize fs)
/* See if this is an absolute path. */
if (FileExists(settings->font)) {
- path.reset(CFStringCreateWithCString(kCFAllocatorDefault, settings->font, kCFStringEncodingUTF8));
+ path.reset(CFStringCreateWithCString(kCFAllocatorDefault, settings->font.c_str(), kCFStringEncodingUTF8));
} else {
/* 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()) {
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));
CFRetain(font_ref.get());
} 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) {
- 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
* 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) {
- 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;
}
diff --git a/src/os/windows/font_win32.cpp b/src/os/windows/font_win32.cpp
index 55d4193e8..6c4e67deb 100644
--- a/src/os/windows/font_win32.cpp
+++ b/src/os/windows/font_win32.cpp
@@ -588,8 +588,9 @@ void LoadWin32Font(FontSize fs)
default: NOT_REACHED();
}
- if (StrEmpty(settings->font)) return;
+ if (settings->font.empty()) return;
+ const char *font_name = settings->font.c_str();
LOGFONT logfont;
MemSetT(&logfont, 0);
logfont.lfPitchAndFamily = fs == FS_MONO ? FIXED_PITCH : VARIABLE_PITCH;
@@ -599,19 +600,19 @@ void LoadWin32Font(FontSize fs)
if (settings->os_handle != nullptr) {
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. */
wchar_t fontPath[MAX_PATH] = {};
/* See if this is an absolute path. */
if (FileExists(settings->font)) {
- convert_to_fs(settings->font, fontPath, lengthof(fontPath));
+ convert_to_fs(font_name, fontPath, lengthof(fontPath));
} else {
/* 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()) {
- 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);
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 {
- 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) {
- logfont.lfWeight = strcasestr(settings->font, " 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));
+ logfont.lfWeight = strcasestr(font_name, " bold") != nullptr ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts.
+ convert_to_fs(font_name, logfont.lfFaceName, lengthof(logfont.lfFaceName));
}
HFONT font = CreateFontIndirect(&logfont);
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;
}
DeleteObject(font);
diff --git a/src/strings.cpp b/src/strings.cpp
index b5cb59d20..fb1943870 100644
--- a/src/strings.cpp
+++ b/src/strings.cpp
@@ -2090,9 +2090,9 @@ class LanguagePackGlyphSearcher : public MissingGlyphSearcher {
void SetFontNames(FreeTypeSettings *settings, const char *font_name, const void *os_data) override
{
#if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
- strecpy(settings->small.font, font_name, lastof(settings->small.font));
- strecpy(settings->medium.font, font_name, lastof(settings->medium.font));
- strecpy(settings->large.font, font_name, lastof(settings->large.font));
+ settings->small.font = font_name;
+ settings->medium.font = font_name;
+ settings->large.font = font_name;
settings->small.os_handle = os_data;
settings->medium.os_handle = os_data;
@@ -2123,15 +2123,14 @@ void CheckForMissingGlyphs(bool base_font, MissingGlyphSearcher *searcher)
if (bad_font) {
/* We found an unprintable character... lets try whether we can find
* a fallback font that can print the characters in the current language. */
- FreeTypeSettings backup;
- memcpy(&backup, &_freetype, sizeof(backup));
+ FreeTypeSettings backup = _freetype;
_freetype.mono.os_handle = nullptr;
_freetype.medium.os_handle = nullptr;
bad_font = !SetFallbackFont(&_freetype, _langpack.langpack->isocode, _langpack.langpack->winlangid, searcher);
- memcpy(&_freetype, &backup, sizeof(backup));
+ _freetype = backup;
if (!bad_font) {
/* Show that we loaded fallback font. To do this properly we have
diff --git a/src/table/misc_settings.ini b/src/table/misc_settings.ini
index 49a4caa14..39df34b78 100644
--- a/src/table/misc_settings.ini
+++ b/src/table/misc_settings.ini
@@ -175,31 +175,31 @@ name = ""rightclick_emulate""
var = _rightclick_emulate
def = false
-[SDTG_STR]
+[SDTG_SSTR]
ifdef = HAS_TRUETYPE_FONT
name = ""small_font""
-type = SLE_STRB
+type = SLE_STR
var = _freetype.small.font
def = nullptr
-[SDTG_STR]
+[SDTG_SSTR]
ifdef = HAS_TRUETYPE_FONT
name = ""medium_font""
-type = SLE_STRB
+type = SLE_STR
var = _freetype.medium.font
def = nullptr
-[SDTG_STR]
+[SDTG_SSTR]
ifdef = HAS_TRUETYPE_FONT
name = ""large_font""
-type = SLE_STRB
+type = SLE_STR
var = _freetype.large.font
def = nullptr
-[SDTG_STR]
+[SDTG_SSTR]
ifdef = HAS_TRUETYPE_FONT
name = ""mono_font""
-type = SLE_STRB
+type = SLE_STR
var = _freetype.mono.font
def = nullptr
diff --git a/src/textfile_gui.cpp b/src/textfile_gui.cpp
index ac8da5c2e..d6c37a458 100644
--- a/src/textfile_gui.cpp
+++ b/src/textfile_gui.cpp
@@ -218,7 +218,7 @@ void TextfileWindow::SetupScrollbars(bool force_reflow)
/* virtual */ void TextfileWindow::SetFontNames(FreeTypeSettings *settings, const char *font_name, const void *os_data)
{
#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;
#endif
}