From b505805bfe88834205164797e435e5ea058e6fe1 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Wed, 17 May 2023 14:54:14 +0100 Subject: [PATCH] Fix: Don't rely on static initialization to set up sprite font caches. The order of static initialization is undefined, so this can cause initalization before relevant caches are initializations. (cherry picked from commit f454ec8d63be9442e8cb03f0db072e66f2332fca) --- src/fontcache.cpp | 12 +++++++++++- src/fontcache.h | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/fontcache.cpp b/src/fontcache.cpp index 033e7b8976..6d1bb597b8 100644 --- a/src/fontcache.cpp +++ b/src/fontcache.cpp @@ -54,7 +54,6 @@ int FontCache::GetDefaultFontHeight(FontSize fs) return _default_font_height[fs]; } -/* static */ FontCache *FontCache::caches[FS_END] = { new SpriteFontCache(FS_NORMAL), new SpriteFontCache(FS_SMALL), new SpriteFontCache(FS_LARGE), new SpriteFontCache(FS_MONO) }; int font_height_cache[FS_END]; void UpdateFontHeightCache() @@ -64,6 +63,15 @@ void UpdateFontHeightCache() } } +/* static */ FontCache *FontCache::caches[FS_END]; + +/* static */ void FontCache::InitializeFontCaches() +{ + for (FontSize fs = FS_BEGIN; fs != FS_END; fs++) { + if (FontCache::caches[fs] == nullptr) new SpriteFontCache(fs); /* FontCache inserts itself into to the cache. */ + } +} + /* Check if a glyph should be rendered with anti-aliasing. */ bool GetFontAAState(FontSize size, bool check_blitter) { @@ -131,6 +139,8 @@ extern void LoadCoreTextFont(FontSize fs); */ void InitFontCache(bool monospace) { + FontCache::InitializeFontCaches(); + for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { if (monospace != (fs == FS_MONO)) continue; diff --git a/src/fontcache.h b/src/fontcache.h index 4b2bfb2337..a84eec5012 100644 --- a/src/fontcache.h +++ b/src/fontcache.h @@ -37,6 +37,8 @@ public: FontCache(FontSize fs); virtual ~FontCache(); + static void InitializeFontCaches(); + static int GetDefaultFontHeight(FontSize fs); /**