diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 5d8515c6ce..ff6bfc9b73 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -2130,7 +2130,7 @@ DEF_CONSOLE_CMD(ConFont) 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, "{}: \"{}\" {} {} [\"{}\" {} {}]", FontSizeToName(fs), fc->GetFontName(), fc->GetFontSize(), GetFontAAState(fs) ? "aa" : "noaa", setting->font, setting->size, setting->aa ? "aa" : "noaa"); diff --git a/src/fontcache.cpp b/src/fontcache.cpp index 2a5772fc3b..5bba534b44 100644 --- a/src/fontcache.cpp +++ b/src/fontcache.cpp @@ -121,21 +121,10 @@ void SetFont(FontSize fontsize, const std::string &font, uint size, bool aa) if (!changed) return; - if (fontsize != FS_MONO) { - /* Try to reload only the modified font. */ - FontCacheSettings backup = _fcsettings; - for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { - if (fs == fontsize) continue; - FontCache *fc = FontCache::Get(fs); - GetFontCacheSubSetting(fs)->font = fc->HasParent() ? fc->GetFontName() : ""; - } - CheckForMissingGlyphs(); - _fcsettings = backup; - } else { - InitFontCache(true); - } + InitFontCache(fontsize); + if (fontsize != FS_MONO) CheckForMissingGlyphs(); - LoadStringWidthTable(); + LoadStringWidthTable(fontsize); UpdateAllVirtCoords(); ReInitAllWindows(true); @@ -191,37 +180,42 @@ static void TryLoadDefaultTrueTypeFont([[maybe_unused]] 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. + * (Re)initialise the font cache related things for a specific font size. + * @param fs Font size to (re)initialise. */ -void InitFontCache(bool monospace) +void InitFontCache(FontSize fs) +{ + FontCache *fc = FontCache::Get(fs); + if (fc->HasParent()) delete fc; + + if (!_fcsettings.prefer_sprite && GetFontCacheSubSetting(fs)->font.empty()) { + TryLoadDefaultTrueTypeFont(fs); + } else { +#ifdef WITH_FREETYPE + LoadFreeTypeFont(fs); +#elif defined(_WIN32) + LoadWin32Font(fs); +#elif defined(WITH_COCOA) + LoadCoreTextFont(fs); +#endif + } +} + +/** + * (Re)initialise the font cache related things, i.e. load the non-sprite fonts, for all font sizes. + */ +void InitFontCaches() { FontCache::InitializeFontCaches(); - for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { - if (monospace != (fs == FS_MONO)) continue; - - FontCache *fc = FontCache::Get(fs); - if (fc->HasParent()) delete fc; - - if (!_fcsettings.prefer_sprite && GetFontCacheSubSetting(fs)->font.empty()) { - TryLoadDefaultTrueTypeFont(fs); - } else { -#ifdef WITH_FREETYPE - LoadFreeTypeFont(fs); -#elif defined(_WIN32) - LoadWin32Font(fs); -#elif defined(WITH_COCOA) - LoadCoreTextFont(fs); -#endif - } + InitFontCache(fs); } } /** * Free everything allocated w.r.t. fonts. */ -void UninitFontCache() +void UninitFontCaches() { for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { FontCache *fc = FontCache::Get(fs); diff --git a/src/fontcache.h b/src/fontcache.h index 9ba6055504..210a63924d 100644 --- a/src/fontcache.h +++ b/src/fontcache.h @@ -177,10 +177,15 @@ inline void InitializeUnicodeGlyphMap() } } -inline void ClearFontCache() +inline void ClearFontCache(FontSize fs) +{ + FontCache::Get(fs)->ClearFontCache(); +} + +inline void ClearFontCaches() { for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { - FontCache::Get(fs)->ClearFontCache(); + ClearFontCache(fs); } } @@ -240,8 +245,9 @@ inline FontCacheSubSetting *GetFontCacheSubSetting(FontSize fs) } } -void InitFontCache(bool monospace); -void UninitFontCache(); +void InitFontCaches(); +void InitFontCache(FontSize fs); +void UninitFontCaches(); bool HasAntialiasedFonts(); bool GetFontAAState(FontSize size, bool check_blitter = true); diff --git a/src/gfx.cpp b/src/gfx.cpp index 1cf74769e6..989535e45a 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -1227,20 +1227,25 @@ 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 a specific font size. + * @param fs Font size to initialize stringwidth table for. */ -void LoadStringWidthTable(bool monospace) +void LoadStringWidthTable(FontSize fs) { - ClearFontCache(); - - for (FontSize fs = monospace ? FS_MONO : FS_BEGIN; fs < (monospace ? FS_END : FS_MONO); fs++) { - for (uint i = 0; i != 224; i++) { - _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32); - } + ClearFontCache(fs); + for (uint i = 0; i != 224; i++) { + _stringwidth_table[fs][i] = GetGlyphWidth(fs, i + 32); } } +/** + * Initialize _stringwidth_table cache for all font sizes. + */ +void LoadStringWidthTable() +{ + for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) LoadStringWidthTable(fs); +} + /** * Return width of character glyph. * @param size Font of the character @@ -1827,7 +1832,7 @@ bool AdjustGUIZoom(bool automatic) if (old_font_zoom != _font_zoom) { GfxClearFontSpriteCache(); } - ClearFontCache(); + ClearFontCaches(); LoadStringWidthTable(); SetupWidgetDimensions(); diff --git a/src/gfx_func.h b/src/gfx_func.h index 9681158768..5c055f1a7d 100644 --- a/src/gfx_func.h +++ b/src/gfx_func.h @@ -139,7 +139,8 @@ int GetStringHeight(StringID str, int maxw); int GetStringLineCount(StringID str, int maxw); Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion); Dimension GetStringMultiLineBoundingBox(std::string_view str, const Dimension &suggestion); -void LoadStringWidthTable(bool monospace = false); +void LoadStringWidthTable(); +void LoadStringWidthTable(FontSize fs); Point GetCharPosInString(std::string_view str, const char *ch, FontSize start_fontsize = FS_NORMAL); ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize = FS_NORMAL); diff --git a/src/gfxinit.cpp b/src/gfxinit.cpp index 785dbde159..0d87a6169a 100644 --- a/src/gfxinit.cpp +++ b/src/gfxinit.cpp @@ -236,7 +236,7 @@ static void RealChangeBlitter(const char *repl_blitter) /* Clear caches that might have sprites for another blitter. */ VideoDriver::GetInstance()->ClearSystemSprites(); - ClearFontCache(); + ClearFontCaches(); GfxClearSpriteCache(); ReInitAllWindows(false); } @@ -318,7 +318,7 @@ void CheckBlitter() { if (!SwitchNewGRFBlitter()) return; - ClearFontCache(); + ClearFontCaches(); GfxClearSpriteCache(); ReInitAllWindows(false); } @@ -330,7 +330,7 @@ void GfxLoadSprites() SwitchNewGRFBlitter(); VideoDriver::GetInstance()->ClearSystemSprites(); - ClearFontCache(); + ClearFontCaches(); GfxInitSpriteMem(); LoadSpriteTables(); GfxInitPalettes(); diff --git a/src/openttd.cpp b/src/openttd.cpp index 31db6b0d60..26051d9ae3 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -308,7 +308,7 @@ static void ShutdownGame() /* No NewGRFs were loaded when it was still bootstrapping. */ if (_game_mode != GM_BOOTSTRAP) ResetNewGRFData(); - UninitFontCache(); + UninitFontCaches(); } /** @@ -705,7 +705,7 @@ int openttd_main(int argc, char *argv[]) InitializeLanguagePacks(); /* Initialize the font cache */ - InitFontCache(false); + InitFontCaches(); /* 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 548fd410cc..a442288e89 100644 --- a/src/os/unix/font_unix.cpp +++ b/src/os/unix/font_unix.cpp @@ -169,7 +169,7 @@ bool SetFallbackFont(FontCacheSettings *settings, const std::string &language_is if (best_font != nullptr) { ret = true; callback->SetFontNames(settings, best_font, &best_index); - InitFontCache(callback->Monospace()); + InitFontCache(callback->DefaultSize()); } /* Clean up the list of filenames. */ diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 07ec5ac1e6..4bf7af8051 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -764,9 +764,8 @@ struct GameOptionsWindow : Window { this->SetWidgetDisabledState(WID_GO_GUI_FONT_AA, _fcsettings.prefer_sprite); this->SetDirty(); - InitFontCache(false); - InitFontCache(true); - ClearFontCache(); + InitFontCaches(); + ClearFontCaches(); CheckForMissingGlyphs(); SetupWidgetDimensions(); UpdateAllVirtCoords(); @@ -779,7 +778,7 @@ struct GameOptionsWindow : Window { this->SetWidgetLoweredState(WID_GO_GUI_FONT_AA, _fcsettings.global_aa); MarkWholeScreenDirty(); - ClearFontCache(); + ClearFontCaches(); break; #endif /* HAS_TRUETYPE_FONT */ diff --git a/src/strings.cpp b/src/strings.cpp index 428f4a5293..dce684cc9c 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -2165,7 +2165,7 @@ const char *GetCurrentLanguageIsoCode() */ bool MissingGlyphSearcher::FindMissingGlyphs() { - InitFontCache(this->Monospace()); + InitFontCache(this->DefaultSize()); const Sprite *question_mark[FS_END]; for (FontSize size = this->Monospace() ? FS_MONO : FS_BEGIN; size < (this->Monospace() ? FS_END : FS_MONO); size++) { @@ -2301,7 +2301,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->DefaultSize()); } } #endif @@ -2318,12 +2318,12 @@ void CheckForMissingGlyphs(bool base_font, MissingGlyphSearcher *searcher) ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_WARNING); /* Reset the font width */ - LoadStringWidthTable(searcher->Monospace()); + LoadStringWidthTable(searcher->DefaultSize()); return; } /* Update the font with cache */ - LoadStringWidthTable(searcher->Monospace()); + LoadStringWidthTable(searcher->DefaultSize()); #if !(defined(WITH_ICU_I18N) && defined(WITH_HARFBUZZ)) && !defined(WITH_UNISCRIBE) && !defined(WITH_COCOA) /*