diff --git a/workspace/all/common/api.c b/workspace/all/common/api.c index 19f3f5439..900e9fc69 100644 --- a/workspace/all/common/api.c +++ b/workspace/all/common/api.c @@ -95,6 +95,7 @@ static SDL_Rect asset_rects[ASSET_COUNT]; static uint32_t asset_rgbs[ASSET_COLORS]; static SDL_Rect input_rects[INPUT_COUNT]; GFX_Fonts font; +static int system_font_face_style = TTF_STYLE_NORMAL; /////////////////////////////// @@ -324,6 +325,9 @@ int GFX_loadSystemFont(const char *fontPath) font.tiny = TTF_OpenFont(fontPath, SCALE1(FONT_TINY)); font.micro = TTF_OpenFont(fontPath, SCALE1(FONT_MICRO)); + // This value is only intrinsic before the configured style is applied. + system_font_face_style = font.large ? TTF_GetFontStyle(font.large) : TTF_STYLE_NORMAL; + TTF_SetFontStyle(font.large, CFG_getFontStyle()); TTF_SetFontStyle(font.medium, CFG_getFontStyle()); TTF_SetFontStyle(font.small, CFG_getFontStyle()); @@ -333,6 +337,11 @@ int GFX_loadSystemFont(const char *fontPath) return 0; } +int GFX_getSystemFontFaceStyle(void) +{ + return system_font_face_style; +} + int GFX_updateColors(void) { // We are currently micro managing all of these screen-mapped colors, diff --git a/workspace/all/common/api.h b/workspace/all/common/api.h index 828af8271..a7a986726 100644 --- a/workspace/all/common/api.h +++ b/workspace/all/common/api.h @@ -308,6 +308,9 @@ enum { }; SDL_Surface* GFX_init(int mode); +// Style reported by SDL_ttf immediately after opening the current font face, +// before NextUI applies its configured synthetic style. +int GFX_getSystemFontFaceStyle(void); #define GFX_resize PLAT_resizeVideo // (int w, int h, int pitch); #define GFX_setSharpness PLAT_setSharpness // (int sharpness) #define GFX_setEffectColor PLAT_setEffectColor // (int color) diff --git a/workspace/all/common/config.c b/workspace/all/common/config.c index e1ec3ba7b..6dd676161 100644 --- a/workspace/all/common/config.c +++ b/workspace/all/common/config.c @@ -1286,7 +1286,9 @@ int CFG_getFontStyle(void) void CFG_setFontStyle(int style) { - settings.fontStyle = clamp(style, 0x00, 0x01); + // SDL_ttf defines TTF_STYLE_NORMAL as 0x00 and TTF_STYLE_BOLD as 0x01. + // Use the values directly to keep config.c free of SDL dependencies. + settings.fontStyle = (style & 0x01) ? 0x01 : 0x00; CFG_sync(); // reload the font to apply the new style (if the font supports it) CFG_setFontFile(CFG_getFontFile()); diff --git a/workspace/all/settings/settings.cpp b/workspace/all/settings/settings.cpp index 7a3eeda49..5202c7c81 100644 --- a/workspace/all/settings/settings.cpp +++ b/workspace/all/settings/settings.cpp @@ -421,14 +421,31 @@ int main(int argc, char *argv[]) font_values.push_back(f.filename); font_labels.push_back(f.label); } + + MenuItem *fontStyleItem = nullptr; + auto resetFontStyleToFaceDefault = [&fontStyleItem]() { + const int faceStyle = GFX_getSystemFontFaceStyle(); + const int style = (faceStyle & TTF_STYLE_BOLD) + ? TTF_STYLE_BOLD + : TTF_STYLE_NORMAL; + CFG_setFontStyle(style); + if (fontStyleItem) + fontStyleItem->reselect(); + }; + auto selectFont = [&resetFontStyleToFaceDefault](const char *filename) { + CFG_setFontFile(filename); + resetFontStyleToFaceDefault(); + }; + appearanceItems.push_back(new MenuItem{ListItemType::Generic, "Font", "The font to render all UI text.", font_values, font_labels, []() -> std::any { return std::string(CFG_getFontFile()); }, - [](const std::any &value) { CFG_setFontFile(std::any_cast(value).c_str()); }, - []() { CFG_setFontFile(CFG_DEFAULT_FONT_FILE); }}); - appearanceItems.push_back(new MenuItem{ListItemType::Generic, "Font style", "The style to render the UI font (e.g. bold)", std::vector{0, 1}, std::vector{"Normal", "Bold"}, + [&selectFont](const std::any &value) { selectFont(std::any_cast(value).c_str()); }, + [&selectFont]() { selectFont(CFG_DEFAULT_FONT_FILE); }}); + fontStyleItem = new MenuItem{ListItemType::Generic, "Font style", "The style to render the UI font (e.g. bold)", std::vector{TTF_STYLE_NORMAL, TTF_STYLE_BOLD}, std::vector{"Normal", "Bold"}, []() -> std::any { return CFG_getFontStyle(); }, [](const std::any &value) { CFG_setFontStyle(std::any_cast(value)); }, - []() { CFG_setFontStyle(CFG_DEFAULT_FONT_STYLE); }}); + [&resetFontStyleToFaceDefault]() { resetFontStyleToFaceDefault(); }}; + appearanceItems.push_back(fontStyleItem); appearanceItems.push_back(buildPaletteMenuItem()); for (auto *item : colorMenuItems) appearanceItems.push_back(item);