From c432d8fa2dfaedc3a96e04e91d3ffe597162d17c Mon Sep 17 00:00:00 2001 From: Nunuhara Cabbage Date: Fri, 4 Sep 2020 20:48:22 -0700 Subject: [PATCH] Provide fallback for missing fonts If the user-provided font can't be loaded, try to load the default font. If the default font can't be loaded (e.g. xsystem4 isn't installed), try to load the default font from the project directory. --- src/text.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/text.c b/src/text.c index 3f07da0..a78e6ee 100644 --- a/src/text.c +++ b/src/text.c @@ -35,11 +35,22 @@ */ #define MAX_FONT_SIZE 128 +#define DEFAULT_FONT_GOTHIC "fonts/VL-Gothic-Regular.ttf" +#define DEFAULT_FONT_MINCHO "fonts/HanaMinA.ttf" + +static const char *local_font_paths[] = { + [FONT_GOTHIC] = DEFAULT_FONT_GOTHIC, + [FONT_MINCHO] = DEFAULT_FONT_MINCHO +}; + +static const char *default_font_paths[] = { + [FONT_GOTHIC] = XSYS4_DATA_DIR "/" DEFAULT_FONT_GOTHIC, + [FONT_MINCHO] = XSYS4_DATA_DIR "/" DEFAULT_FONT_MINCHO +}; -// TODO: install fonts on system, and provide run-time configuration option const char *font_paths[] = { - [FONT_GOTHIC] = XSYS4_DATA_DIR "/fonts/VL-Gothic-Regular.ttf", - [FONT_MINCHO] = XSYS4_DATA_DIR "/fonts/HanaMinA.ttf" + [FONT_GOTHIC] = XSYS4_DATA_DIR "/" DEFAULT_FONT_GOTHIC, + [FONT_MINCHO] = XSYS4_DATA_DIR "/" DEFAULT_FONT_MINCHO }; struct font { @@ -74,6 +85,16 @@ static struct font_metrics font_metrics = { } }; +static TTF_Font *open_font(enum font_face face, unsigned int size) +{ + TTF_Font *font; + if ((font = TTF_OpenFont(font_paths[face], size))) + return font; + if ((font = TTF_OpenFont(default_font_paths[face], size))) + return font; + return TTF_OpenFont(local_font_paths[face], size); +} + bool gfx_set_font(enum font_face face, unsigned int size) { struct _font *ff = &font_table[face]; @@ -92,7 +113,7 @@ bool gfx_set_font(enum font_face face, unsigned int size) // open font if needed if (!ff->fonts[size].size) { - if (!(ff->fonts[size].font = TTF_OpenFont(font_paths[face], size))) { + if (!(ff->fonts[size].font = open_font(face, size))) { WARNING("Error opening font: %s:%d", font_paths[face], size); return false; }