From dd604afc891dc8e8357ad5e2846768eb0864dda2 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Wed, 23 Dec 2015 02:56:47 +0900 Subject: [PATCH] Use FontConfig to find suitable TrueType fonts on Linux and OS X. Fixes #2537. --- CMakeLists.txt | 5 +++++ src/drawing/string.c | 7 +++++-- src/platform/platform.h | 2 ++ src/platform/posix.c | 28 ++++++++++++++++++++++++++++ src/platform/windows.c | 8 ++++++++ 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b88bc3f3c1..c54b8d22b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -161,6 +161,11 @@ if (UNIX) list(APPEND RCT2_SECTIONS "${CMAKE_BINARY_DIR}/openrct2_data_section.o" "${CMAKE_BINARY_DIR}/openrct2_text_section.o") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-T,\"${CMAKE_CURRENT_SOURCE_DIR}/distribution/linux/ld_script.xc\"") endif (APPLE) + + # FontConfig for TrueType fonts. + find_path(FONTCONFIG_INCLUDE_DIR fontconfig/fontconfig.h) + find_library(FONTCONFIG_LIBRARY NAMES fontconfig) + TARGET_LINK_LIBRARIES(${PROJECT} ${FONTCONFIG_LIBRARY}) endif (UNIX) # install into ${CMAKE_INSTALL_PREFIX}/bin/ diff --git a/src/drawing/string.c b/src/drawing/string.c index f4241b4b4e..f12f0af0e0 100644 --- a/src/drawing/string.c +++ b/src/drawing/string.c @@ -888,8 +888,11 @@ bool ttf_initialise() for (int i = 0; i < 4; i++) { TTFFontDescriptor *fontDesc = &(gCurrentTTFFontSet->size[i]); - utf8 fontPath[MAX_PATH] = "C:\\Windows\\Fonts\\"; - strcat(fontPath, fontDesc->filename); + utf8 fontPath[MAX_PATH]; + if (!platform_get_font_path(fontDesc, fontPath)) { + log_error("Unable to load font '%s'", fontDesc->font_name); + return false; + } fontDesc->font = TTF_OpenFont(fontPath, fontDesc->ptSize); if (fontDesc->font == NULL) { diff --git a/src/platform/platform.h b/src/platform/platform.h index 0d65b6c566..c9e3eb23ad 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -28,6 +28,7 @@ #include #include "../common.h" +#include "../drawing/font.h" #ifndef MAX_PATH #define MAX_PATH 260 @@ -161,6 +162,7 @@ uint8 platform_get_locale_currency(); uint16 platform_get_locale_language(); uint8 platform_get_locale_measurement_format(); uint8 platform_get_locale_temperature_format(); +bool platform_get_font_path(TTFFontDescriptor *font, utf8 *buffer); bool platform_check_steam_overlay_attached(); diff --git a/src/platform/posix.c b/src/platform/posix.c index 8a09b85b6f..8bfd8eb260 100644 --- a/src/platform/posix.c +++ b/src/platform/posix.c @@ -37,6 +37,7 @@ #include #include #include +#include "fontconfig/fontconfig.h" // The name of the mutex used to prevent multiple instances of the game from running #define SINGLE_INSTANCE_MUTEX_NAME "RollerCoaster Tycoon 2_GSKMUTEX" @@ -757,4 +758,31 @@ uint8 platform_get_locale_temperature_format(){ return TEMPERATURE_FORMAT_C; } +bool platform_get_font_path(TTFFontDescriptor *font, utf8 *buffer) +{ + FcConfig* config = FcInitLoadConfigAndFonts(); + FcPattern* pat = FcNameParse((const FcChar8*) font->font_name); + + FcConfigSubstitute(config, pat, FcMatchPattern); + FcDefaultSubstitute(pat); + + bool found = false; + FcResult result = FcResultNoMatch; + FcPattern* match = FcFontMatch(config, pat, &result); + + if (match) + { + FcChar8* filename = NULL; + if (FcPatternGetString(match, FC_FILE, 0, &filename) == FcResultMatch) + { + found = true; + strcpy(buffer, (utf8*) filename); + } + FcPatternDestroy(match); + } + + FcPatternDestroy(pat); + return found; +} + #endif diff --git a/src/platform/windows.c b/src/platform/windows.c index d07f765fcd..5b1476b769 100644 --- a/src/platform/windows.c +++ b/src/platform/windows.c @@ -897,4 +897,12 @@ void platform_get_exe_path(utf8 *outPath) _wfullpath(exePath, tempPath, MAX_PATH); WideCharToMultiByte(CP_UTF8, 0, exePath, countof(exePath), outPath, MAX_PATH, NULL, NULL); } + +void platform_get_font_path(TTFFontDescriptor *font, utf8 *buffer) +{ + strcpy(buffer, "C:\\Windows\\Fonts\\"); + strcat(buffer, font->filename); + return true; +} + #endif