diff --git a/include/eepp/graphics/textlayout.hpp b/include/eepp/graphics/textlayout.hpp index 5d878f36f..a52986342 100644 --- a/include/eepp/graphics/textlayout.hpp +++ b/include/eepp/graphics/textlayout.hpp @@ -48,6 +48,7 @@ class EE_API TextLayout { LineWrapMode lineWrapMode = LineWrapMode::NoWrap, Uint32 wrapWidth = 0, bool keepIndentation = false, Float initialXOffset = 0 ); + static void clearLayoutCache(); protected: static void wrapLayout( const String::View& string, TextLayout&, LineWrapMode lineWrapMode, Float wrapWidth, Float vspace, bool keepIndentation, Font* font, diff --git a/src/eepp/graphics/textlayout.cpp b/src/eepp/graphics/textlayout.cpp index 41579a368..b267d00e9 100644 --- a/src/eepp/graphics/textlayout.cpp +++ b/src/eepp/graphics/textlayout.cpp @@ -236,6 +236,17 @@ static inline Uint64 textLayoutHash( const String::View& string, Font* font, std::hash()( initialXOffset ) ); } +static LRULayoutCache& getLayoutCache( bool invalidate = false ) { + static LRULayoutCache sLayoutCache; + if ( invalidate ) + sLayoutCache.clear(); + return sLayoutCache; +} + +void TextLayout::clearLayoutCache() { + getLayoutCache( true ); +} + TextLayout::Cache TextLayout::layout( const String::View& string, Font* font, const Uint32& characterSize, const Uint32& style, const Uint32& tabWidth, const Float& outlineThickness, @@ -243,7 +254,6 @@ TextLayout::Cache TextLayout::layout( const String::View& string, Font* font, TextDirection baseDirection, LineWrapMode wrapMode, Uint32 wrapWidth, bool keepIndentation, Float initialXOffset ) { - static LRULayoutCache sLayoutCache; if ( !font || string.empty() ) { auto layout = std::make_shared(); @@ -258,7 +268,7 @@ TextLayout::Cache TextLayout::layout( const String::View& string, Font* font, tabOffset, baseDirection, wrapMode, wrapWidth, keepIndentation, initialXOffset ); - auto cacheHit = sLayoutCache.get( hash ); + auto cacheHit = getLayoutCache().get( hash ); if ( cacheHit.has_value() ) return *cacheHit; } @@ -513,7 +523,7 @@ TextLayout::Cache TextLayout::layout( const String::View& string, Font* font, characterSize, style, tabWidth, outlineThickness, hspace ); } - sLayoutCache.put( hash, resultPtr ); + getLayoutCache().put( hash, resultPtr ); return resultPtr; } diff --git a/src/eepp/window/engine.cpp b/src/eepp/window/engine.cpp index 2300fdfd3..f8d9f3455 100644 --- a/src/eepp/window/engine.cpp +++ b/src/eepp/window/engine.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -117,6 +118,8 @@ Engine::~Engine() { ParserMatcherManager::destroySingleton(); Log::destroySingleton(); + + TextLayout::clearLayoutCache(); } void Engine::destroy() {