1
0
mirror of https://github.com/OpenTTD/OpenTTD.git synced 2025-08-19 20:49:11 +00:00

Compare commits

..

2 Commits

Author SHA1 Message Date
517dab35b1 Codechange: Use range-for to iterate base media files. (#12721) 2024-05-26 16:46:18 +01:00
3c42f701d7 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.
2024-05-26 15:59:25 +01:00
10 changed files with 64 additions and 121 deletions

View File

@@ -158,8 +158,8 @@ struct BaseSet {
*/ */
std::optional<std::string> GetTextfile(TextfileType type) const std::optional<std::string> GetTextfile(TextfileType type) const
{ {
for (uint i = 0; i < NUM_FILES; i++) { for (const auto &file : this->files) {
auto textfile = ::GetTextfile(type, BASESET_DIR, this->files[i].filename); auto textfile = ::GetTextfile(type, BASESET_DIR, file.filename);
if (textfile.has_value()) { if (textfile.has_value()) {
return textfile; return textfile;
} }

View File

@@ -325,8 +325,8 @@ template <class Tbase_set> const char *TryGetBaseSetFile(const ContentInfo *ci,
if (!md5sum) return s->files[0].filename.c_str(); if (!md5sum) return s->files[0].filename.c_str();
MD5Hash md5; MD5Hash md5;
for (uint i = 0; i < Tbase_set::NUM_FILES; i++) { for (const auto &file : s->files) {
md5 ^= s->files[i].hash; md5 ^= file.hash;
} }
if (md5 == ci->md5sum) return s->files[0].filename.c_str(); if (md5 == ci->md5sum) return s->files[0].filename.c_str();
} }

View File

@@ -138,50 +138,57 @@ void SetFont(FontSize fontsize, const std::string &font, uint size)
#ifdef WITH_FREETYPE #ifdef WITH_FREETYPE
extern void LoadFreeTypeFont(FontSize fs); extern void LoadFreeTypeFont(FontSize fs);
extern void LoadFreeTypeFont(FontSize fs, const std::string &file_name, uint size);
extern void UninitFreeType(); extern void UninitFreeType();
#elif defined(_WIN32) #elif defined(_WIN32)
extern void LoadWin32Font(FontSize fs); extern void LoadWin32Font(FontSize fs);
extern void LoadWin32Font(FontSize fs, const std::string &file_name, uint size);
#elif defined(WITH_COCOA) #elif defined(WITH_COCOA)
extern void LoadCoreTextFont(FontSize fs); extern void LoadCoreTextFont(FontSize fs);
extern void LoadCoreTextFont(FontSize fs, const std::string &file_name, uint size);
#endif #endif
static void TryLoadDefaultTrueTypeFont([[maybe_unused]] FontSize fs)
{
#if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA) #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) { switch (fs) {
case FS_NORMAL: case FS_NORMAL: return "OpenTTD-Sans.ttf";
font_name = "OpenTTD-Sans.ttf"; case FS_SMALL: return "OpenTTD-Small.ttf";
break; case FS_LARGE: return "OpenTTD-Serif.ttf";
case FS_SMALL: case FS_MONO: return "OpenTTD-Mono.ttf";
font_name = "OpenTTD-Small.ttf";
break;
case FS_LARGE:
font_name = "OpenTTD-Serif.ttf";
break;
case FS_MONO:
font_name = "OpenTTD-Mono.ttf";
break;
default: NOT_REACHED(); 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) */ #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,9 +205,6 @@ void InitFontCache(bool monospace)
FontCache *fc = FontCache::Get(fs); FontCache *fc = FontCache::Get(fs);
if (fc->HasParent()) delete fc; if (fc->HasParent()) delete fc;
if (!_fcsettings.prefer_sprite && GetFontCacheSubSetting(fs)->font.empty()) {
TryLoadDefaultTrueTypeFont(fs);
} else {
#ifdef WITH_FREETYPE #ifdef WITH_FREETYPE
LoadFreeTypeFont(fs); LoadFreeTypeFont(fs);
#elif defined(_WIN32) #elif defined(_WIN32)
@@ -209,7 +213,6 @@ void InitFontCache(bool monospace)
LoadCoreTextFont(fs); LoadCoreTextFont(fs);
#endif #endif
} }
}
} }
/** /**

View File

@@ -224,6 +224,7 @@ inline FontCacheSubSetting *GetFontCacheSubSetting(FontSize fs)
} }
} }
std::string GetFontCacheFontName(FontSize fs);
void InitFontCache(bool monospace); void InitFontCache(bool monospace);
void UninitFontCache(); void UninitFontCache();

View File

@@ -160,7 +160,8 @@ void LoadFreeTypeFont(FontSize fs)
{ {
FontCacheSubSetting *settings = GetFontCacheSubSetting(fs); FontCacheSubSetting *settings = GetFontCacheSubSetting(fs);
if (settings->font.empty()) return; std::string font = GetFontCacheFontName(fs);
if (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) {
@@ -171,7 +172,7 @@ void LoadFreeTypeFont(FontSize fs)
Debug(fontcache, 2, "Initialized"); Debug(fontcache, 2, "Initialized");
} }
const char *font_name = settings->font.c_str(); const char *font_name = 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. */
@@ -200,34 +201,6 @@ void LoadFreeTypeFont(FontSize fs)
} }
} }
/**
* Load a TrueType font from a file.
* @param fs The font size to load.
* @param file_name Path to the font file.
* @param size Requested font size.
*/
void LoadFreeTypeFont(FontSize fs, const std::string &file_name, uint size)
{
if (_library == nullptr) {
if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
ShowInfo("Unable to initialize FreeType, using sprite fonts instead");
return;
}
Debug(fontcache, 2, "Initialized");
}
FT_Face face = nullptr;
int32_t index = 0;
FT_Error error = FT_New_Face(_library, file_name.c_str(), index, &face);
if (error == FT_Err_Ok) {
LoadFont(fs, face, file_name.c_str(), size);
} else {
FT_Done_Face(face);
}
}
/** /**
* Free everything that was allocated for this font cache. * Free everything that was allocated for this font cache.
*/ */

View File

@@ -127,9 +127,9 @@ void CheckExternalFiles()
if (used_set->GetNumInvalid() != 0) { if (used_set->GetNumInvalid() != 0) {
/* Not all files were loaded successfully, see which ones */ /* Not all files were loaded successfully, see which ones */
fmt::format_to(output_iterator, "Trying to load graphics set '{}', but it is incomplete. The game will probably not run correctly until you properly install this set or select another one. See section 4.1 of README.md.\n\nThe following files are corrupted or missing:\n", used_set->name); fmt::format_to(output_iterator, "Trying to load graphics set '{}', but it is incomplete. The game will probably not run correctly until you properly install this set or select another one. See section 4.1 of README.md.\n\nThe following files are corrupted or missing:\n", used_set->name);
for (uint i = 0; i < GraphicsSet::NUM_FILES; i++) { for (const auto &file : used_set->files) {
MD5File::ChecksumResult res = GraphicsSet::CheckMD5(&used_set->files[i], BASESET_DIR); MD5File::ChecksumResult res = GraphicsSet::CheckMD5(&file, BASESET_DIR);
if (res != MD5File::CR_MATCH) fmt::format_to(output_iterator, "\t{} is {} ({})\n", used_set->files[i].filename, res == MD5File::CR_MISMATCH ? "corrupt" : "missing", used_set->files[i].missing_warning); if (res != MD5File::CR_MATCH) fmt::format_to(output_iterator, "\t{} is {} ({})\n", file.filename, res == MD5File::CR_MISMATCH ? "corrupt" : "missing", file.missing_warning);
} }
fmt::format_to(output_iterator, "\n"); fmt::format_to(output_iterator, "\n");
} }

View File

@@ -328,7 +328,8 @@ void LoadCoreTextFont(FontSize fs)
{ {
FontCacheSubSetting *settings = GetFontCacheSubSetting(fs); FontCacheSubSetting *settings = GetFontCacheSubSetting(fs);
if (settings->font.empty()) return; std::string font = GetFontCacheFontName(fs);
if (font.empty()) return;
CFAutoRelease<CTFontDescriptorRef> font_ref; CFAutoRelease<CTFontDescriptorRef> font_ref;
@@ -339,12 +340,12 @@ void LoadCoreTextFont(FontSize fs)
if (!font_ref && MacOSVersionIsAtLeast(10, 6, 0)) { if (!font_ref && MacOSVersionIsAtLeast(10, 6, 0)) {
/* Might be a font file name, try load it. */ /* Might be a font file name, try load it. */
font_ref.reset(LoadFontFromFile(settings->font)); font_ref.reset(LoadFontFromFile(font));
if (!font_ref) ShowInfo("Unable to load file '{}' for {} font, using default OS font selection instead", settings->font, FontSizeToName(fs)); if (!font_ref) ShowInfo("Unable to load file '{}' for {} font, using default OS font selection instead", font, FontSizeToName(fs));
} }
if (!font_ref) { if (!font_ref) {
CFAutoRelease<CFStringRef> name(CFStringCreateWithCString(kCFAllocatorDefault, settings->font.c_str(), kCFStringEncodingUTF8)); CFAutoRelease<CFStringRef> name(CFStringCreateWithCString(kCFAllocatorDefault, 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.
@@ -362,23 +363,9 @@ void LoadCoreTextFont(FontSize fs)
} }
if (!font_ref) { if (!font_ref) {
ShowInfo("Unable to use '{}' for {} font, using sprite font instead", settings->font, FontSizeToName(fs)); ShowInfo("Unable to use '{}' for {} font, using sprite font instead", font, FontSizeToName(fs));
return; return;
} }
new CoreTextFontCache(fs, std::move(font_ref), settings->size); new CoreTextFontCache(fs, std::move(font_ref), settings->size);
} }
/**
* Load a TrueType font from a file.
* @param fs The font size to load.
* @param file_name Path to the font file.
* @param size Requested font size.
*/
void LoadCoreTextFont(FontSize fs, const std::string &file_name, uint size)
{
CFAutoRelease<CTFontDescriptorRef> font_ref{LoadFontFromFile(file_name)};
if (font_ref) {
new CoreTextFontCache(fs, std::move(font_ref), size);
}
}

View File

@@ -35,6 +35,5 @@ public:
}; };
void LoadCoreTextFont(FontSize fs); void LoadCoreTextFont(FontSize fs);
void LoadCoreTextFont(FontSize fs, const std::string &file_name, uint size);
#endif /* FONT_OSX_H */ #endif /* FONT_OSX_H */

View File

@@ -352,9 +352,10 @@ void LoadWin32Font(FontSize fs)
{ {
FontCacheSubSetting *settings = GetFontCacheSubSetting(fs); FontCacheSubSetting *settings = GetFontCacheSubSetting(fs);
if (settings->font.empty()) return; std::string font = GetFontCacheFontName(fs);
if (font.empty()) return;
const char *font_name = settings->font.c_str(); const char *font_name = 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;
@@ -366,8 +367,8 @@ void LoadWin32Font(FontSize fs)
logfont = *(const LOGFONT *)settings->os_handle; logfont = *(const LOGFONT *)settings->os_handle;
} else if (strchr(font_name, '.') != 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. */
if (!TryLoadFontFromFile(settings->font, logfont)) { if (!TryLoadFontFromFile(font, logfont)) {
ShowInfo("Unable to load file '{}' for {} font, using default windows font selection instead", font_name, FontSizeToName(fs)); ShowInfo("Unable to load file '{}' for {} font, using default windows font selection instead", font, FontSizeToName(fs));
} }
} }
@@ -378,23 +379,3 @@ void LoadWin32Font(FontSize fs)
LoadWin32Font(fs, logfont, settings->size, font_name); LoadWin32Font(fs, logfont, settings->size, font_name);
} }
/**
* Load a TrueType font from a file.
* @param fs The font size to load.
* @param file_name Path to the font file.
* @param size Requested font size.
*/
void LoadWin32Font(FontSize fs, const std::string &file_name, uint size)
{
LOGFONT logfont;
MemSetT(&logfont, 0);
logfont.lfPitchAndFamily = fs == FS_MONO ? FIXED_PITCH : VARIABLE_PITCH;
logfont.lfCharSet = DEFAULT_CHARSET;
logfont.lfOutPrecision = OUT_OUTLINE_PRECIS;
logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
if (TryLoadFontFromFile(file_name, logfont)) {
LoadWin32Font(fs, logfont, size, file_name.c_str());
}
}

View File

@@ -43,6 +43,5 @@ public:
}; };
void LoadWin32Font(FontSize fs); void LoadWin32Font(FontSize fs);
void LoadWin32Font(FontSize fs, const std::string &file_name, uint size);
#endif /* FONT_WIN32_H */ #endif /* FONT_WIN32_H */