From 97073e359178992aef166f23b0c73f2c7cbce0a1 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 15 Jul 2025 19:46:07 +0100 Subject: [PATCH] Codechange: Initialise/reset font cache with FontSizes bitset. Instead of choosing either "Normal/Small/Large" or "Monospace", use an EnumBitSet to allow any combination. --- src/console_cmds.cpp | 2 +- src/fontcache.cpp | 12 +++++------- src/fontcache.h | 6 +++--- src/gfx.cpp | 13 +++++++------ src/gfx_func.h | 2 +- src/gfx_type.h | 7 +++++++ src/gfxinit.cpp | 6 +++--- src/openttd.cpp | 2 +- src/os/unix/font_unix.cpp | 2 +- src/settings_gui.cpp | 7 +++---- src/strings.cpp | 8 ++++---- 11 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 3293c4704f..52dfc9cffa 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -2369,7 +2369,7 @@ static bool ConFont(std::span argv) FontCacheSubSetting *setting = GetFontCacheSubSetting(fs); /* Make sure all non sprite fonts are loaded. */ if (!setting->font.empty() && !fc->HasParent()) { - InitFontCache(fs == FS_MONO); + InitFontCache(fs); fc = FontCache::Get(fs); } IConsolePrint(CC_DEFAULT, "{} font:", FontSizeToName(fs)); diff --git a/src/fontcache.cpp b/src/fontcache.cpp index dcdc1804c9..d1be5e6ac2 100644 --- a/src/fontcache.cpp +++ b/src/fontcache.cpp @@ -126,10 +126,10 @@ void SetFont(FontSize fontsize, const std::string &font, uint size) CheckForMissingGlyphs(); _fcsettings = std::move(backup); } else { - InitFontCache(true); + InitFontCache(fontsize); } - LoadStringWidthTable(fontsize == FS_MONO); + LoadStringWidthTable(fontsize); UpdateAllVirtCoords(); ReInitAllWindows(true); @@ -213,15 +213,13 @@ std::string GetFontCacheFontName(FontSize fs) /** * (Re)initialize the font cache related things, i.e. load the non-sprite fonts. - * @param monospace Whether to initialise the monospace or regular fonts. + * @param fontsizes Font sizes to be initialised. */ -void InitFontCache(bool monospace) +void InitFontCache(FontSizes fontsizes) { FontCache::InitializeFontCaches(); - for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { - if (monospace != (fs == FS_MONO)) continue; - + for (FontSize fs : fontsizes) { FontCache *fc = FontCache::Get(fs); if (fc->HasParent()) delete fc; diff --git a/src/fontcache.h b/src/fontcache.h index 5026694f58..20dbfff074 100644 --- a/src/fontcache.h +++ b/src/fontcache.h @@ -167,9 +167,9 @@ inline void InitializeUnicodeGlyphMap() } } -inline void ClearFontCache() +inline void ClearFontCache(FontSizes fontsizes) { - for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { + for (FontSize fs : fontsizes) { FontCache::Get(fs)->ClearFontCache(); } } @@ -231,7 +231,7 @@ inline FontCacheSubSetting *GetFontCacheSubSetting(FontSize fs) uint GetFontCacheFontSize(FontSize fs); std::string GetFontCacheFontName(FontSize fs); -void InitFontCache(bool monospace); +void InitFontCache(FontSizes fontsizes); void UninitFontCache(); bool GetFontAAState(); diff --git a/src/gfx.cpp b/src/gfx.cpp index 1add357877..9ba985e971 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -8,6 +8,7 @@ /** @file gfx.cpp Handling of drawing text and other gfx related stuff. */ #include "stdafx.h" +#include "gfx_func.h" #include "gfx_layout.h" #include "progress.h" #include "zoom_func.h" @@ -1240,14 +1241,14 @@ static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, } /** - * Initialize _stringwidth_table cache - * @param monospace Whether to load the monospace cache or the normal fonts. + * Initialize _stringwidth_table cache for the specified font sizes. + * @param fontsizes Font sizes to initialise. */ -void LoadStringWidthTable(bool monospace) +void LoadStringWidthTable(FontSizes fontsizes) { - ClearFontCache(); + ClearFontCache(fontsizes); - for (FontSize fs = monospace ? FS_MONO : FS_BEGIN; fs < (monospace ? FS_END : FS_MONO); fs++) { + for (FontSize fs : fontsizes) { for (uint i = 0; i != 224; i++) { _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32); } @@ -1812,7 +1813,7 @@ bool AdjustGUIZoom(bool automatic) if (old_font_zoom != _font_zoom) { GfxClearFontSpriteCache(); } - ClearFontCache(); + ClearFontCache(FONTSIZES_ALL); LoadStringWidthTable(); SetupWidgetDimensions(); diff --git a/src/gfx_func.h b/src/gfx_func.h index 655b167c37..edd5b03d82 100644 --- a/src/gfx_func.h +++ b/src/gfx_func.h @@ -149,7 +149,7 @@ int GetStringHeight(StringID str, int maxw); int GetStringLineCount(std::string_view str, int maxw); Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion); Dimension GetStringMultiLineBoundingBox(std::string_view str, const Dimension &suggestion, FontSize fontsize = FS_NORMAL); -void LoadStringWidthTable(bool monospace = false); +void LoadStringWidthTable(FontSizes fontsizes = FONTSIZES_REQUIRED); void DrawDirtyBlocks(); void AddDirtyBlock(int left, int top, int right, int bottom); diff --git a/src/gfx_type.h b/src/gfx_type.h index 664aecaed9..718f7a455d 100644 --- a/src/gfx_type.h +++ b/src/gfx_type.h @@ -258,6 +258,13 @@ enum FontSize : uint8_t { }; DECLARE_INCREMENT_DECREMENT_OPERATORS(FontSize) +using FontSizes = EnumBitSet; + +/** Mask of all possible font sizes. */ +constexpr FontSizes FONTSIZES_ALL{FS_NORMAL, FS_SMALL, FS_LARGE, FS_MONO}; +/** Mask of font sizes required to be present. */ +constexpr FontSizes FONTSIZES_REQUIRED{FS_NORMAL, FS_SMALL, FS_LARGE}; + inline std::string_view FontSizeToName(FontSize fs) { static const std::string_view SIZE_TO_NAME[] = { "medium", "small", "large", "mono" }; diff --git a/src/gfxinit.cpp b/src/gfxinit.cpp index deaaf405b2..20c5e69f11 100644 --- a/src/gfxinit.cpp +++ b/src/gfxinit.cpp @@ -245,7 +245,7 @@ static void RealChangeBlitter(std::string_view repl_blitter) /* Clear caches that might have sprites for another blitter. */ VideoDriver::GetInstance()->ClearSystemSprites(); - ClearFontCache(); + ClearFontCache(FONTSIZES_ALL); GfxClearSpriteCache(); ReInitAllWindows(false); } @@ -326,7 +326,7 @@ void CheckBlitter() { if (!SwitchNewGRFBlitter()) return; - ClearFontCache(); + ClearFontCache(FONTSIZES_ALL); GfxClearSpriteCache(); ReInitAllWindows(false); } @@ -338,7 +338,7 @@ void GfxLoadSprites() SwitchNewGRFBlitter(); VideoDriver::GetInstance()->ClearSystemSprites(); - ClearFontCache(); + ClearFontCache(FONTSIZES_ALL); GfxInitSpriteMem(); LoadSpriteTables(); GfxInitPalettes(); diff --git a/src/openttd.cpp b/src/openttd.cpp index 10ea0951e0..533ee0446a 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -700,7 +700,7 @@ int openttd_main(std::span arguments) InitializeLanguagePacks(); /* Initialize the font cache */ - InitFontCache(false); + InitFontCache(FONTSIZES_REQUIRED); /* This must be done early, since functions use the SetWindowDirty* calls */ InitWindowSystem(); diff --git a/src/os/unix/font_unix.cpp b/src/os/unix/font_unix.cpp index 37567537e1..feb96713e5 100644 --- a/src/os/unix/font_unix.cpp +++ b/src/os/unix/font_unix.cpp @@ -182,6 +182,6 @@ bool SetFallbackFont(FontCacheSettings *settings, const std::string &language_is if (best_font == nullptr) return false; callback->SetFontNames(settings, best_font, &best_index); - InitFontCache(callback->Monospace()); + InitFontCache(callback->Monospace() ? FontSizes{FS_MONO} : FONTSIZES_REQUIRED); return true; } diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index d5f8d50e19..3e9353d488 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -1036,9 +1036,8 @@ struct GameOptionsWindow : Window { this->SetWidgetDisabledState(WID_GO_GUI_FONT_AA, _fcsettings.prefer_sprite); this->SetDirty(); - InitFontCache(false); - InitFontCache(true); - ClearFontCache(); + InitFontCache(FONTSIZES_ALL); + ClearFontCache(FONTSIZES_ALL); CheckForMissingGlyphs(); SetupWidgetDimensions(); UpdateAllVirtCoords(); @@ -1051,7 +1050,7 @@ struct GameOptionsWindow : Window { this->SetWidgetLoweredState(WID_GO_GUI_FONT_AA, _fcsettings.global_aa); MarkWholeScreenDirty(); - ClearFontCache(); + ClearFontCache(FONTSIZES_ALL); break; #endif /* HAS_TRUETYPE_FONT */ diff --git a/src/strings.cpp b/src/strings.cpp index d33535ba92..69d605e86d 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -2278,7 +2278,7 @@ std::string_view GetCurrentLanguageIsoCode() */ bool MissingGlyphSearcher::FindMissingGlyphs() { - InitFontCache(this->Monospace()); + InitFontCache(this->Monospace() ? FontSizes{FS_MONO} : FONTSIZES_REQUIRED); this->Reset(); for (auto text = this->NextString(); text.has_value(); text = this->NextString()) { @@ -2395,7 +2395,7 @@ void CheckForMissingGlyphs(bool base_font, MissingGlyphSearcher *searcher) /* Our fallback font does miss characters too, so keep the * user chosen font as that is more likely to be any good than * the wild guess we made */ - InitFontCache(searcher->Monospace()); + InitFontCache(searcher->Monospace() ? FontSizes{FS_MONO} : FONTSIZES_REQUIRED); } } #endif @@ -2412,12 +2412,12 @@ void CheckForMissingGlyphs(bool base_font, MissingGlyphSearcher *searcher) ShowErrorMessage(GetEncodedString(STR_JUST_RAW_STRING, std::move(err_str)), {}, WL_WARNING); /* Reset the font width */ - LoadStringWidthTable(searcher->Monospace()); + LoadStringWidthTable(searcher->Monospace() ? FontSizes{FS_MONO} : FONTSIZES_REQUIRED); return; } /* Update the font with cache */ - LoadStringWidthTable(searcher->Monospace()); + LoadStringWidthTable(searcher->Monospace() ? FontSizes{FS_MONO} : FONTSIZES_REQUIRED); #if !(defined(WITH_ICU_I18N) && defined(WITH_HARFBUZZ)) && !defined(WITH_UNISCRIBE) && !defined(WITH_COCOA) /*