mirror of
https://github.com/OpenTTD/OpenTTD
synced 2026-01-20 02:42:42 +01:00
Fix: Allow changing size of default OpenTTD font. (#12641)
* Fix: Allow changing size of default OpenTTD font. Size configuration for default font was ignored as a different code path to load the font was followed. Resolved by removing this additional path and conditionally selecting the default font.
This commit is contained in:
@@ -138,50 +138,57 @@ void SetFont(FontSize fontsize, const std::string &font, uint size)
|
||||
|
||||
#ifdef WITH_FREETYPE
|
||||
extern void LoadFreeTypeFont(FontSize fs);
|
||||
extern void LoadFreeTypeFont(FontSize fs, const std::string &file_name, uint size);
|
||||
extern void UninitFreeType();
|
||||
#elif defined(_WIN32)
|
||||
extern void LoadWin32Font(FontSize fs);
|
||||
extern void LoadWin32Font(FontSize fs, const std::string &file_name, uint size);
|
||||
#elif defined(WITH_COCOA)
|
||||
extern void LoadCoreTextFont(FontSize fs);
|
||||
extern void LoadCoreTextFont(FontSize fs, const std::string &file_name, uint size);
|
||||
#endif
|
||||
|
||||
static void TryLoadDefaultTrueTypeFont([[maybe_unused]] FontSize fs)
|
||||
{
|
||||
#if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
|
||||
std::string font_name{};
|
||||
/**
|
||||
* Get name of default font file for a given font size.
|
||||
* @param fs Font size.
|
||||
* @return Name of default font file.
|
||||
*/
|
||||
static std::string GetDefaultTruetypeFont(FontSize fs)
|
||||
{
|
||||
switch (fs) {
|
||||
case FS_NORMAL:
|
||||
font_name = "OpenTTD-Sans.ttf";
|
||||
break;
|
||||
case FS_SMALL:
|
||||
font_name = "OpenTTD-Small.ttf";
|
||||
break;
|
||||
case FS_LARGE:
|
||||
font_name = "OpenTTD-Serif.ttf";
|
||||
break;
|
||||
case FS_MONO:
|
||||
font_name = "OpenTTD-Mono.ttf";
|
||||
break;
|
||||
|
||||
case FS_NORMAL: return "OpenTTD-Sans.ttf";
|
||||
case FS_SMALL: return "OpenTTD-Small.ttf";
|
||||
case FS_LARGE: return "OpenTTD-Serif.ttf";
|
||||
case FS_MONO: return "OpenTTD-Mono.ttf";
|
||||
default: NOT_REACHED();
|
||||
}
|
||||
|
||||
/* Find font file. */
|
||||
std::string full_font = FioFindFullPath(BASESET_DIR, font_name);
|
||||
if (!full_font.empty()) {
|
||||
int size = FontCache::GetDefaultFontHeight(fs);
|
||||
#ifdef WITH_FREETYPE
|
||||
LoadFreeTypeFont(fs, full_font, size);
|
||||
#elif defined(_WIN32)
|
||||
LoadWin32Font(fs, full_font, size);
|
||||
#elif defined(WITH_COCOA)
|
||||
LoadCoreTextFont(fs, full_font, size);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif /* defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA) */
|
||||
|
||||
/**
|
||||
* Get path of default font file for a given font size.
|
||||
* @param fs Font size.
|
||||
* @return Full path of default font file.
|
||||
*/
|
||||
static std::string GetDefaultTruetypeFontFile([[maybe_unused]] FontSize fs)
|
||||
{
|
||||
#if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
|
||||
/* Find font file. */
|
||||
return FioFindFullPath(BASESET_DIR, GetDefaultTruetypeFont(fs));
|
||||
#else
|
||||
return {};
|
||||
#endif /* defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA) */
|
||||
}
|
||||
|
||||
/**
|
||||
* Get font to use for a given font size.
|
||||
* @param fs Font size.
|
||||
* @return If configured, the font name to use, or the path of the default TrueType font if sprites are not preferred.
|
||||
*/
|
||||
std::string GetFontCacheFontName(FontSize fs)
|
||||
{
|
||||
const FontCacheSubSetting *settings = GetFontCacheSubSetting(fs);
|
||||
if (!settings->font.empty()) return settings->font;
|
||||
if (_fcsettings.prefer_sprite) return {};
|
||||
return GetDefaultTruetypeFontFile(fs);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -198,17 +205,13 @@ void InitFontCache(bool monospace)
|
||||
FontCache *fc = FontCache::Get(fs);
|
||||
if (fc->HasParent()) delete fc;
|
||||
|
||||
if (!_fcsettings.prefer_sprite && GetFontCacheSubSetting(fs)->font.empty()) {
|
||||
TryLoadDefaultTrueTypeFont(fs);
|
||||
} else {
|
||||
#ifdef WITH_FREETYPE
|
||||
LoadFreeTypeFont(fs);
|
||||
LoadFreeTypeFont(fs);
|
||||
#elif defined(_WIN32)
|
||||
LoadWin32Font(fs);
|
||||
LoadWin32Font(fs);
|
||||
#elif defined(WITH_COCOA)
|
||||
LoadCoreTextFont(fs);
|
||||
LoadCoreTextFont(fs);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user