fonts can reload now

This commit is contained in:
Kae 2023-06-21 23:13:37 +10:00
parent f0fec34dc9
commit 92ef4dba0f
4 changed files with 30 additions and 11 deletions

View File

@ -34,6 +34,11 @@ void FontTextureGroup::addFont(FontPtr const& font, String const& name, bool def
m_defaultFont = m_font = font; m_defaultFont = m_font = font;
} }
void FontTextureGroup::clearFonts() {
m_fonts.clear();
m_font = m_defaultFont;
}
const FontTextureGroup::GlyphTexture& FontTextureGroup::glyphTexture(String::Char c, unsigned size, String const& processingDirectives) const FontTextureGroup::GlyphTexture& FontTextureGroup::glyphTexture(String::Char c, unsigned size, String const& processingDirectives)
{ {
auto res = m_glyphs.insert(GlyphDescriptor{c, size, processingDirectives, m_font.get() }, GlyphTexture()); auto res = m_glyphs.insert(GlyphDescriptor{c, size, processingDirectives, m_font.get() }, GlyphTexture());

View File

@ -36,6 +36,7 @@ public:
void switchFont(String const& font); void switchFont(String const& font);
String const& activeFont(); String const& activeFont();
void addFont(FontPtr const& font, String const& name, bool default = false); void addFont(FontPtr const& font, String const& name, bool default = false);
void clearFonts();
private: private:
StringMap<FontPtr> m_fonts; StringMap<FontPtr> m_fonts;
String m_fontName; String m_fontName;

View File

@ -92,17 +92,9 @@ TextPainter::TextPainter(RendererPtr renderer, TextureGroupPtr textureGroup)
m_splitIgnore(" \t"), m_splitIgnore(" \t"),
m_splitForce("\n\v"), m_splitForce("\n\v"),
m_nonRenderedCharacters("\n\v\r") { m_nonRenderedCharacters("\n\v\r") {
auto assets = Root::singleton().assets(); reloadFonts();
auto defaultFont = assets->font("/hobo.ttf"); m_reloadTracker = make_shared<TrackerListener>();
for (auto& fontPath : assets->scanExtension("ttf")) { Root::singleton().registerReloadListener(m_reloadTracker);
auto font = assets->font(fontPath);
if (font == defaultFont)
continue;
auto fileName = AssetPath::filename(fontPath);
addFont(font->clone(), fileName.substr(0, fileName.findLast(".")));
}
m_fontTextureGroup.addFont(defaultFont->clone(), "hobo", true);
} }
RectF TextPainter::renderText(String const& s, TextPositioning const& position) { RectF TextPainter::renderText(String const& s, TextPositioning const& position) {
@ -313,6 +305,22 @@ void TextPainter::addFont(FontPtr const& font, String const& name) {
m_fontTextureGroup.addFont(font, name); m_fontTextureGroup.addFont(font, name);
} }
void TextPainter::reloadFonts() {
m_fontTextureGroup.clearFonts();
m_fontTextureGroup.cleanup(0);
auto assets = Root::singleton().assets();
auto defaultFont = assets->font("/hobo.ttf");
for (auto& fontPath : assets->scanExtension("ttf")) {
auto font = assets->font(fontPath);
if (font == defaultFont)
continue;
auto fileName = AssetPath::filename(fontPath);
addFont(font->clone(), fileName.substr(0, fileName.findLast(".")));
}
m_fontTextureGroup.addFont(defaultFont->clone(), "hobo", true);
}
void TextPainter::cleanup(int64_t timeout) { void TextPainter::cleanup(int64_t timeout) {
m_fontTextureGroup.cleanup(timeout); m_fontTextureGroup.cleanup(timeout);
} }
@ -346,6 +354,8 @@ RectF TextPainter::doRenderText(String const& s, TextPositioning const& position
} }
RectF TextPainter::doRenderLine(String const& s, TextPositioning const& position, bool reallyRender, unsigned* charLimit) { RectF TextPainter::doRenderLine(String const& s, TextPositioning const& position, bool reallyRender, unsigned* charLimit) {
if (m_reloadTracker->pullTriggered())
reloadFonts();
String text = s; String text = s;
TextPositioning pos = position; TextPositioning pos = position;

View File

@ -76,6 +76,7 @@ public:
void setProcessingDirectives(String directives); void setProcessingDirectives(String directives);
void setFont(String const& font); void setFont(String const& font);
void addFont(FontPtr const& font, String const& name); void addFont(FontPtr const& font, String const& name);
void reloadFonts();
void cleanup(int64_t textureTimeout); void cleanup(int64_t textureTimeout);
@ -105,6 +106,8 @@ private:
String m_splitIgnore; String m_splitIgnore;
String m_splitForce; String m_splitForce;
String m_nonRenderedCharacters; String m_nonRenderedCharacters;
TrackerListenerPtr m_reloadTracker;
}; };
} }