Add optional alpha threshold option for fonts
This commit is contained in:
parent
65bacddc67
commit
2a204b384f
@ -51,7 +51,7 @@ FontPtr Font::loadTrueTypeFont(ByteArrayConstPtr const& bytes, unsigned pixelSiz
|
|||||||
return font;
|
return font;
|
||||||
}
|
}
|
||||||
|
|
||||||
Font::Font() : m_pixelSize(0) {}
|
Font::Font() : m_pixelSize(0), m_alphaThreshold(0) {}
|
||||||
|
|
||||||
FontPtr Font::clone() const {
|
FontPtr Font::clone() const {
|
||||||
return Font::loadTrueTypeFont(m_fontBuffer, m_pixelSize);
|
return Font::loadTrueTypeFont(m_fontBuffer, m_pixelSize);
|
||||||
@ -70,6 +70,10 @@ void Font::setPixelSize(unsigned pixelSize) {
|
|||||||
m_pixelSize = pixelSize;
|
m_pixelSize = pixelSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Font::setAlphaThreshold(uint8_t alphaThreshold) {
|
||||||
|
m_alphaThreshold = alphaThreshold;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned Font::height() const {
|
unsigned Font::height() const {
|
||||||
return m_pixelSize;
|
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;
|
uint8_t* p = slot->bitmap.buffer + y * slot->bitmap.pitch;
|
||||||
for (unsigned x = 0; x != width; ++x) {
|
for (unsigned x = 0; x != width; ++x) {
|
||||||
if (x >= 0 && y >= 0 && x < width && y < height) {
|
if (x >= 0 && y >= 0 && x < width && y < height) {
|
||||||
if (uint8_t val = *(p + x)) {
|
uint8_t value = *(p + x);
|
||||||
white.setW(val);
|
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);
|
image.set(x + 1, height - y, white);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ public:
|
|||||||
FontPtr clone() const;
|
FontPtr clone() const;
|
||||||
|
|
||||||
void setPixelSize(unsigned pixelSize);
|
void setPixelSize(unsigned pixelSize);
|
||||||
|
void setAlphaThreshold(uint8_t alphaThreshold = 0);
|
||||||
|
|
||||||
unsigned height() const;
|
unsigned height() const;
|
||||||
unsigned width(String::Char c);
|
unsigned width(String::Char c);
|
||||||
@ -40,6 +41,7 @@ private:
|
|||||||
FontImplPtr m_fontImpl;
|
FontImplPtr m_fontImpl;
|
||||||
ByteArrayConstPtr m_fontBuffer;
|
ByteArrayConstPtr m_fontBuffer;
|
||||||
unsigned m_pixelSize;
|
unsigned m_pixelSize;
|
||||||
|
uint8_t m_alphaThreshold;
|
||||||
|
|
||||||
HashMap<pair<String::Char, unsigned>, unsigned> m_widthCache;
|
HashMap<pair<String::Char, unsigned>, unsigned> m_widthCache;
|
||||||
};
|
};
|
||||||
|
@ -338,16 +338,18 @@ void TextPainter::reloadFonts() {
|
|||||||
m_fontTextureGroup.clearFonts();
|
m_fontTextureGroup.clearFonts();
|
||||||
m_fontTextureGroup.cleanup(0);
|
m_fontTextureGroup.cleanup(0);
|
||||||
auto assets = Root::singleton().assets();
|
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")) {
|
for (auto& fontPath : assets->scanExtension("ttf")) {
|
||||||
auto font = assets->font(fontPath);
|
auto font = assets->font(fontPath);
|
||||||
if (font == defaultFont)
|
if (font == defaultFont)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
auto fileName = AssetPath::filename(fontPath);
|
auto name = AssetPath::filename(fontPath);
|
||||||
addFont(font->clone(), fileName.substr(0, fileName.findLast(".")));
|
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) {
|
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);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,6 +87,7 @@ private:
|
|||||||
RectF doRenderGlyph(String::Char c, TextPositioning const& position, bool reallyRender);
|
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);
|
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;
|
RendererPtr m_renderer;
|
||||||
FontTextureGroup m_fontTextureGroup;
|
FontTextureGroup m_fontTextureGroup;
|
||||||
|
Loading…
Reference in New Issue
Block a user