Clear cached glyph layouts if too many

This commit is contained in:
Benau
2019-06-17 01:12:54 +08:00
parent ae3fb4b004
commit 8995342b94
3 changed files with 23 additions and 7 deletions

View File

@@ -25,6 +25,7 @@
#include "font/face_ttf.hpp"
#include "font/regular_face.hpp"
#include "modes/profile_world.hpp"
#include "states_screens/state_manager.hpp"
#include "utils/string_utils.hpp"
#include "utils/translation.hpp"
@@ -469,6 +470,23 @@ void FontManager::shape(const std::u32string& text,
}
} // shape
// ----------------------------------------------------------------------------
/* Return the cached glyph layouts for writing, it will clear all layouts if
* not in-game and when the cached sized exceed a certain number. */
std::vector<irr::gui::GlyphLayout>&
FontManager::getCachedLayouts(const irr::core::stringw& str)
{
const size_t MAX_LAYOUTS = 600;
if (StateManager::get()->getGameState() != GUIEngine::GAME &&
m_cached_gls.size() > MAX_LAYOUTS)
{
Log::debug("FontManager",
"Clearing cached glyph layouts because too many.");
clearCachedLayouts();
}
return m_cached_gls[str];
} // getCachedLayouts
// ----------------------------------------------------------------------------
/** Convert text to glyph layouts for fast rendering with caching enabled
* If line_data is not null, each broken line u32string will be saved and
@@ -489,7 +507,7 @@ void FontManager::initGlyphLayouts(const core::stringw& text,
return;
}
auto& cached_gls = m_cached_gls[text];
auto& cached_gls = getCachedLayouts(text);
if (cached_gls.empty())
shape(StringUtils::wideToUtf32(text), cached_gls);
gls = cached_gls;

View File

@@ -127,7 +127,9 @@ public:
std::vector<std::u32string>* line_data = NULL);
// ------------------------------------------------------------------------
std::vector<irr::gui::GlyphLayout>& getCachedLayouts
(const irr::core::stringw& str) { return m_cached_gls[str]; }
(const irr::core::stringw& str);
// ------------------------------------------------------------------------
void clearCachedLayouts() { m_cached_gls.clear(); }
// ------------------------------------------------------------------------
void initGlyphLayouts(const irr::core::stringw& text,
std::vector<irr::gui::GlyphLayout>& gls,

View File

@@ -183,11 +183,7 @@ void OptionsScreenLanguage::eventCallback(Widget* widget, const std::string& nam
font_manager->getFont<BoldFace>()->reset();
font_manager->getFont<RegularFace>()->reset();
GUIEngine::getFont()->updateRTL();
GUIEngine::getTitleFont()->updateRTL();
GUIEngine::getSmallFont()->updateRTL();
GUIEngine::getLargeFont()->updateRTL();
GUIEngine::getOutlineFont()->updateRTL();
font_manager->clearCachedLayouts();
UserConfigParams::m_language = selection.c_str();
user_config->saveConfig();