Add optional alpha threshold option for fonts

This commit is contained in:
Kae 2023-08-04 23:47:52 +10:00
parent 65bacddc67
commit 2a204b384f
4 changed files with 36 additions and 7 deletions

View File

@ -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);
}
}

View File

@ -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;
};

View File

@ -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;
}
}

View File

@ -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;