Add: use our search-paths to find fonts based on relative filenames too

This allows "small_font = ./myfont.ttf", with "myfont.ttf" located
in "~/.openttd".
This commit is contained in:
Patric Stout 2021-01-02 21:53:50 +01:00 committed by Michael Lutz
parent 126f40e012
commit 4bd3d18f34

View File

@ -561,8 +561,19 @@ static void LoadFreeTypeFont(FontSize fs)
} }
FT_Face face = nullptr; 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, settings->font, 0, &face);
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);
if (!full_font.empty()) {
error = FT_New_Face(_library, full_font.c_str(), 0, &face);
}
}
/* 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(settings->font, &face);
if (error == FT_Err_Ok) { if (error == FT_Err_Ok) {
@ -970,11 +981,23 @@ static 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 && FileExists(settings->font)) { } else if (strchr(settings->font, '.') != nullptr) {
/* Might be a font file name, try load it. */ /* Might be a font file name, try load it. */
TCHAR fontPath[MAX_PATH];
convert_to_fs(settings->font, fontPath, lengthof(fontPath), false);
TCHAR fontPath[MAX_PATH] = {};
/* See if this is an absolute path. */
if (FileExists(settings->font)) {
convert_to_fs(settings->font, fontPath, lengthof(fontPath), false);
} else {
/* Scan the search-paths to see if it can be found. */
std::string full_font = FioFindFullPath(BASE_DIR, settings->font);
if (!full_font.empty()) {
convert_to_fs(full_font.c_str(), fontPath, lengthof(fontPath), false);
}
}
if (fontPath[0] != 0) {
if (AddFontResourceEx(fontPath, FR_PRIVATE, 0) != 0) { if (AddFontResourceEx(fontPath, FR_PRIVATE, 0) != 0) {
/* Try a nice little undocumented function first for getting the internal font name. /* Try a nice little undocumented function first for getting the internal font name.
* Some documentation is found at: http://www.undocprint.org/winspool/getfontresourceinfo */ * Some documentation is found at: http://www.undocprint.org/winspool/getfontresourceinfo */
@ -1008,6 +1031,7 @@ static void LoadWin32Font(FontSize fs)
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", settings->font, 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(settings->font, " bold") != nullptr ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts.