diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-08-04 23:47:52 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-08-04 23:47:52 +1000 |
commit | 2a204b384fc61b59aa6b2f9998c3af28013f7d17 (patch) | |
tree | 4c5ea83fe2936ae1b0cc02cd3fe411189b240882 /source/rendering | |
parent | 65bacddc67942e8678626016960431cdecef2030 (diff) |
Add optional alpha threshold option for fonts
Diffstat (limited to 'source/rendering')
-rw-r--r-- | source/rendering/StarTextPainter.cpp | 24 | ||||
-rw-r--r-- | source/rendering/StarTextPainter.hpp | 1 |
2 files changed, 21 insertions, 4 deletions
diff --git a/source/rendering/StarTextPainter.cpp b/source/rendering/StarTextPainter.cpp index f0ef53d..748aa52 100644 --- a/source/rendering/StarTextPainter.cpp +++ b/source/rendering/StarTextPainter.cpp @@ -338,16 +338,18 @@ void TextPainter::reloadFonts() { m_fontTextureGroup.clearFonts(); m_fontTextureGroup.cleanup(0); auto assets = Root::singleton().assets(); - auto defaultFont = assets->font("/hobo.ttf"); + String defaultName = "hobo"; + auto defaultFont = loadFont("/hobo.ttf", defaultName); 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("."))); + auto name = AssetPath::filename(fontPath); + name = name.substr(0, name.findLast(".")); + addFont(loadFont(fontPath, name), name); } - m_fontTextureGroup.addFont(defaultFont->clone(), "hobo", true); + m_fontTextureGroup.addFont(defaultFont, defaultName, true); } void TextPainter::cleanup(int64_t timeout) { @@ -512,4 +514,18 @@ void TextPainter::renderGlyph(String::Char c, Vec2F const& screenPos, unsigned f m_renderer->immediatePrimitives().emplace_back(std::in_place_type_t<RenderQuad>(), glyphTexture.texture, Vec2F::round(screenPos + offset), scale, color, 0.0f); } +FontPtr TextPainter::loadFont(String const& fontPath, Maybe<String> fontName) { + if (!fontName) { + auto name = AssetPath::filename(fontPath); + fontName.emplace(name.substr(0, name.findLast("."))); + } + + auto assets = Root::singleton().assets(); + + auto font = assets->font(fontPath)->clone(); + if (auto fontConfig = assets->json("/interface.config:font").opt(*fontName)) { + font->setAlphaThreshold(fontConfig->getUInt("alphaThreshold", 0)); + } + return font; +} } diff --git a/source/rendering/StarTextPainter.hpp b/source/rendering/StarTextPainter.hpp index 381a358..e71c385 100644 --- a/source/rendering/StarTextPainter.hpp +++ b/source/rendering/StarTextPainter.hpp @@ -87,6 +87,7 @@ private: RectF doRenderGlyph(String::Char c, TextPositioning const& position, bool reallyRender); void renderGlyph(String::Char c, Vec2F const& screenPos, unsigned fontSize, float scale, Vec4B const& color, Directives const* processingDirectives = nullptr); + static FontPtr loadFont(String const& fontPath, Maybe<String> fontName = {}); RendererPtr m_renderer; FontTextureGroup m_fontTextureGroup; |