1
0
Fork 0

Codechange: Font cache sizes can now be reset independently.

Instead of choosing either "Normal/Small/Large" or "Monospace", use a BitSet to allow any combination.
pull/13303/head
Peter Nelson 2025-06-12 18:30:23 +01:00
parent 1cd0391926
commit 9b55a040ac
No known key found for this signature in database
GPG Key ID: 8EF8F0A467DF75ED
13 changed files with 34 additions and 32 deletions

View File

@ -2365,7 +2365,7 @@ static bool ConFont(std::span<std::string_view> argv)
FontCacheSubSetting *setting = GetFontCacheSubSetting(fs); FontCacheSubSetting *setting = GetFontCacheSubSetting(fs);
/* Make sure all non sprite fonts are loaded. */ /* Make sure all non sprite fonts are loaded. */
if (!setting->font.empty() && !fc->HasParent()) { if (!setting->font.empty() && !fc->HasParent()) {
InitFontCache(fs == FS_MONO); InitFontCache(fs);
fc = FontCache::Get(fs); fc = FontCache::Get(fs);
} }
IConsolePrint(CC_DEFAULT, "{} font:", FontSizeToName(fs)); IConsolePrint(CC_DEFAULT, "{} font:", FontSizeToName(fs));

View File

@ -126,10 +126,10 @@ void SetFont(FontSize fontsize, const std::string &font, uint size)
CheckForMissingGlyphs(); CheckForMissingGlyphs();
_fcsettings = std::move(backup); _fcsettings = std::move(backup);
} else { } else {
InitFontCache(true); InitFontCache(fontsize);
} }
LoadStringWidthTable(fontsize == FS_MONO); LoadStringWidthTable(fontsize);
UpdateAllVirtCoords(); UpdateAllVirtCoords();
ReInitAllWindows(true); ReInitAllWindows(true);
@ -215,13 +215,11 @@ std::string GetFontCacheFontName(FontSize fs)
* (Re)initialize the font cache related things, i.e. load the non-sprite fonts. * (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 monospace Whether to initialise the monospace or regular fonts.
*/ */
void InitFontCache(bool monospace) void InitFontCache(FontSizes fontsizes)
{ {
FontCache::InitializeFontCaches(); FontCache::InitializeFontCaches();
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { for (FontSize fs : fontsizes) {
if (monospace != (fs == FS_MONO)) continue;
FontCache *fc = FontCache::Get(fs); FontCache *fc = FontCache::Get(fs);
if (fc->HasParent()) delete fc; if (fc->HasParent()) delete fc;

View File

@ -162,9 +162,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(); FontCache::Get(fs)->ClearFontCache();
} }
} }
@ -226,7 +226,7 @@ inline FontCacheSubSetting *GetFontCacheSubSetting(FontSize fs)
uint GetFontCacheFontSize(FontSize fs); uint GetFontCacheFontSize(FontSize fs);
std::string GetFontCacheFontName(FontSize fs); std::string GetFontCacheFontName(FontSize fs);
void InitFontCache(bool monospace); void InitFontCache(FontSizes fontsizes);
void UninitFontCache(); void UninitFontCache();
bool GetFontAAState(); bool GetFontAAState();

View File

@ -330,7 +330,7 @@ void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_setti
SetCurrentRailTypeLabelList(); SetCurrentRailTypeLabelList();
SetCurrentRoadTypeLabelList(); SetCurrentRoadTypeLabelList();
InitializeBuildingCounts(); InitializeBuildingCounts();
LoadStringWidthTable(); LoadStringWidthTable(FONTSIZES_REQUIRED);
/* Re-init the windowing system */ /* Re-init the windowing system */
ResetWindowSystem(); ResetWindowSystem();

View File

@ -1243,11 +1243,11 @@ static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode,
* Initialize _stringwidth_table cache * Initialize _stringwidth_table cache
* @param monospace Whether to load the monospace cache or the normal fonts. * @param monospace Whether to load the monospace cache or the normal fonts.
*/ */
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++) { for (uint i = 0; i != 224; i++) {
_stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32); _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32);
} }
@ -1812,8 +1812,8 @@ bool AdjustGUIZoom(bool automatic)
if (old_font_zoom != _font_zoom) { if (old_font_zoom != _font_zoom) {
GfxClearFontSpriteCache(); GfxClearFontSpriteCache();
} }
ClearFontCache(); ClearFontCache(FONTSIZES_ALL);
LoadStringWidthTable(); LoadStringWidthTable(FONTSIZES_REQUIRED);
SetupWidgetDimensions(); SetupWidgetDimensions();
UpdateAllVirtCoords(); UpdateAllVirtCoords();

View File

@ -149,7 +149,7 @@ int GetStringHeight(StringID str, int maxw);
int GetStringLineCount(std::string_view str, int maxw); int GetStringLineCount(std::string_view str, int maxw);
Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion); Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion);
Dimension GetStringMultiLineBoundingBox(std::string_view str, const Dimension &suggestion, FontSize fontsize = FS_NORMAL); Dimension GetStringMultiLineBoundingBox(std::string_view str, const Dimension &suggestion, FontSize fontsize = FS_NORMAL);
void LoadStringWidthTable(bool monospace = false); void LoadStringWidthTable(FontSizes fontsizes);
void DrawDirtyBlocks(); void DrawDirtyBlocks();
void AddDirtyBlock(int left, int top, int right, int bottom); void AddDirtyBlock(int left, int top, int right, int bottom);

View File

@ -258,6 +258,11 @@ enum FontSize : uint8_t {
}; };
DECLARE_INCREMENT_DECREMENT_OPERATORS(FontSize) DECLARE_INCREMENT_DECREMENT_OPERATORS(FontSize)
using FontSizes = EnumBitSet<FontSize, uint8_t>;
constexpr FontSizes FONTSIZES_ALL{FS_NORMAL, FS_SMALL, FS_LARGE, FS_MONO};
constexpr FontSizes FONTSIZES_REQUIRED{FS_NORMAL, FS_SMALL, FS_LARGE};
inline std::string_view FontSizeToName(FontSize fs) inline std::string_view FontSizeToName(FontSize fs)
{ {
static const std::string_view SIZE_TO_NAME[] = { "medium", "small", "large", "mono" }; static const std::string_view SIZE_TO_NAME[] = { "medium", "small", "large", "mono" };

View File

@ -245,7 +245,7 @@ static void RealChangeBlitter(std::string_view repl_blitter)
/* Clear caches that might have sprites for another blitter. */ /* Clear caches that might have sprites for another blitter. */
VideoDriver::GetInstance()->ClearSystemSprites(); VideoDriver::GetInstance()->ClearSystemSprites();
ClearFontCache(); ClearFontCache(FONTSIZES_ALL);
GfxClearSpriteCache(); GfxClearSpriteCache();
ReInitAllWindows(false); ReInitAllWindows(false);
} }
@ -326,7 +326,7 @@ void CheckBlitter()
{ {
if (!SwitchNewGRFBlitter()) return; if (!SwitchNewGRFBlitter()) return;
ClearFontCache(); ClearFontCache(FONTSIZES_ALL);
GfxClearSpriteCache(); GfxClearSpriteCache();
ReInitAllWindows(false); ReInitAllWindows(false);
} }
@ -338,7 +338,7 @@ void GfxLoadSprites()
SwitchNewGRFBlitter(); SwitchNewGRFBlitter();
VideoDriver::GetInstance()->ClearSystemSprites(); VideoDriver::GetInstance()->ClearSystemSprites();
ClearFontCache(); ClearFontCache(FONTSIZES_ALL);
GfxInitSpriteMem(); GfxInitSpriteMem();
LoadSpriteTables(); LoadSpriteTables();
GfxInitPalettes(); GfxInitPalettes();

View File

@ -700,7 +700,7 @@ int openttd_main(std::span<std::string_view> arguments)
InitializeLanguagePacks(); InitializeLanguagePacks();
/* Initialize the font cache */ /* Initialize the font cache */
InitFontCache(false); InitFontCache(FONTSIZES_REQUIRED);
/* This must be done early, since functions use the SetWindowDirty* calls */ /* This must be done early, since functions use the SetWindowDirty* calls */
InitWindowSystem(); InitWindowSystem();

View File

@ -182,6 +182,6 @@ bool SetFallbackFont(FontCacheSettings *settings, const std::string &language_is
if (best_font == nullptr) return false; if (best_font == nullptr) return false;
callback->SetFontNames(settings, best_font, &best_index); callback->SetFontNames(settings, best_font, &best_index);
InitFontCache(callback->Monospace()); InitFontCache(callback->Monospace() ? FontSizes{FS_MONO} : FONTSIZES_REQUIRED);
return true; return true;
} }

View File

@ -796,7 +796,7 @@ bool AfterLoadGame()
/* Load the sprites */ /* Load the sprites */
GfxLoadSprites(); GfxLoadSprites();
LoadStringWidthTable(); LoadStringWidthTable(FONTSIZES_REQUIRED);
/* Copy temporary data to Engine pool */ /* Copy temporary data to Engine pool */
CopyTempEngineData(); CopyTempEngineData();
@ -3421,7 +3421,7 @@ void ReloadNewGRFData()
{ {
/* reload grf data */ /* reload grf data */
GfxLoadSprites(); GfxLoadSprites();
LoadStringWidthTable(); LoadStringWidthTable(FONTSIZES_REQUIRED);
RecomputePrices(); RecomputePrices();
/* reload vehicles */ /* reload vehicles */
ResetVehicleHash(); ResetVehicleHash();

View File

@ -1036,9 +1036,8 @@ struct GameOptionsWindow : Window {
this->SetWidgetDisabledState(WID_GO_GUI_FONT_AA, _fcsettings.prefer_sprite); this->SetWidgetDisabledState(WID_GO_GUI_FONT_AA, _fcsettings.prefer_sprite);
this->SetDirty(); this->SetDirty();
InitFontCache(false); InitFontCache(FONTSIZES_ALL);
InitFontCache(true); ClearFontCache(FONTSIZES_ALL);
ClearFontCache();
CheckForMissingGlyphs(); CheckForMissingGlyphs();
SetupWidgetDimensions(); SetupWidgetDimensions();
UpdateAllVirtCoords(); UpdateAllVirtCoords();
@ -1051,7 +1050,7 @@ struct GameOptionsWindow : Window {
this->SetWidgetLoweredState(WID_GO_GUI_FONT_AA, _fcsettings.global_aa); this->SetWidgetLoweredState(WID_GO_GUI_FONT_AA, _fcsettings.global_aa);
MarkWholeScreenDirty(); MarkWholeScreenDirty();
ClearFontCache(); ClearFontCache(FONTSIZES_ALL);
break; break;
#endif /* HAS_TRUETYPE_FONT */ #endif /* HAS_TRUETYPE_FONT */

View File

@ -2257,7 +2257,7 @@ std::string_view GetCurrentLanguageIsoCode()
*/ */
bool MissingGlyphSearcher::FindMissingGlyphs() bool MissingGlyphSearcher::FindMissingGlyphs()
{ {
InitFontCache(this->Monospace()); InitFontCache(this->Monospace() ? FontSizes{FS_MONO} : FONTSIZES_REQUIRED);
this->Reset(); this->Reset();
for (auto text = this->NextString(); text.has_value(); text = this->NextString()) { for (auto text = this->NextString(); text.has_value(); text = this->NextString()) {
@ -2384,7 +2384,7 @@ void CheckForMissingGlyphs(bool base_font, MissingGlyphSearcher *searcher)
/* Our fallback font does miss characters too, so keep the /* Our fallback font does miss characters too, so keep the
* user chosen font as that is more likely to be any good than * user chosen font as that is more likely to be any good than
* the wild guess we made */ * the wild guess we made */
InitFontCache(searcher->Monospace()); InitFontCache(searcher->Monospace() ? FontSizes{FS_MONO} : FONTSIZES_REQUIRED);
} }
} }
#endif #endif
@ -2401,12 +2401,12 @@ void CheckForMissingGlyphs(bool base_font, MissingGlyphSearcher *searcher)
ShowErrorMessage(GetEncodedString(STR_JUST_RAW_STRING, std::move(err_str)), {}, WL_WARNING); ShowErrorMessage(GetEncodedString(STR_JUST_RAW_STRING, std::move(err_str)), {}, WL_WARNING);
/* Reset the font width */ /* Reset the font width */
LoadStringWidthTable(searcher->Monospace()); LoadStringWidthTable(searcher->Monospace() ? FontSizes{FS_MONO} : FONTSIZES_REQUIRED);
return; return;
} }
/* Update the font with cache */ /* 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) #if !(defined(WITH_ICU_I18N) && defined(WITH_HARFBUZZ)) && !defined(WITH_UNISCRIBE) && !defined(WITH_COCOA)
/* /*