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 | |
parent | 65bacddc67942e8678626016960431cdecef2030 (diff) |
Add optional alpha threshold option for fonts
-rw-r--r-- | source/core/StarFont.cpp | 16 | ||||
-rw-r--r-- | source/core/StarFont.hpp | 2 | ||||
-rw-r--r-- | source/rendering/StarTextPainter.cpp | 24 | ||||
-rw-r--r-- | source/rendering/StarTextPainter.hpp | 1 |
4 files changed, 36 insertions, 7 deletions
diff --git a/source/core/StarFont.cpp b/source/core/StarFont.cpp index 8b531a0..89503ed 100644 --- a/source/core/StarFont.cpp +++ b/source/core/StarFont.cpp @@ -51,7 +51,7 @@ FontPtr Font::loadTrueTypeFont(ByteArrayConstPtr const& bytes, unsigned pixelSiz return font; } -Font::Font() : m_pixelSize(0) {} +Font::Font() : m_pixelSize(0), m_alphaThreshold(0) {} FontPtr Font::clone() const { return Font::loadTrueTypeFont(m_fontBuffer, m_pixelSize); @@ -70,6 +70,10 @@ void Font::setPixelSize(unsigned pixelSize) { m_pixelSize = pixelSize; } +void Font::setAlphaThreshold(uint8_t alphaThreshold) { + m_alphaThreshold = alphaThreshold; +} + unsigned Font::height() const { return m_pixelSize; } @@ -112,8 +116,14 @@ std::pair<Image, Vec2I> Font::render(String::Char c) { uint8_t* p = slot->bitmap.buffer + y * slot->bitmap.pitch; for (unsigned x = 0; x != width; ++x) { if (x >= 0 && y >= 0 && x < width && y < height) { - if (uint8_t val = *(p + x)) { - white.setW(val); + uint8_t value = *(p + x); + if (m_alphaThreshold) { + if (value >= m_alphaThreshold) { + white[3] = 255; + image.set(x + 1, height - y, white); + } + } else if (value) { + white[3] = value; image.set(x + 1, height - y, white); } } diff --git a/source/core/StarFont.hpp b/source/core/StarFont.hpp index e4786b6..a3b3ced 100644 --- a/source/core/StarFont.hpp +++ b/source/core/StarFont.hpp @@ -27,6 +27,7 @@ public: FontPtr clone() const; void setPixelSize(unsigned pixelSize); + void setAlphaThreshold(uint8_t alphaThreshold = 0); unsigned height() const; unsigned width(String::Char c); @@ -40,6 +41,7 @@ private: FontImplPtr m_fontImpl; ByteArrayConstPtr m_fontBuffer; unsigned m_pixelSize; + uint8_t m_alphaThreshold; HashMap<pair<String::Char, unsigned>, unsigned> m_widthCache; }; 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; |