Better text input rendering

Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
2025-10-10 15:29:18 +03:00
parent 86024e2c03
commit c4e13985ed
5 changed files with 381 additions and 287 deletions

View File

@@ -253,6 +253,9 @@ auto TextRenderer::generate_glyph(FontRuntime &rt, FontData &fd,
// }
//}
auto c1 { (int)std::round(msdf_bitmap(0, 0)[3]) };
auto c4 { (int)std::round(msdf_bitmap(bmp_w - 1, bmp_h - 1)[3]) };
auto sum_white = 0;
auto sum_black = 0;
for (int y = 0; y < bmp_h; ++y) {
@@ -266,6 +269,11 @@ auto TextRenderer::generate_glyph(FontRuntime &rt, FontData &fd,
}
}
}
bool flip { sum_white > sum_black && (float)bmp_w / (float)bmp_h > 0.6 };
if (c1 == c4) {
flip = false;
}
// This really isn't the most accurate thing in the world but should work
// for now. Things like commas might be fucked.
for (int y = 0; y < bmp_h; ++y) {
@@ -273,7 +281,7 @@ auto TextRenderer::generate_glyph(FontRuntime &rt, FontData &fd,
for (int x = 0; x < bmp_w; ++x) {
float const *px = msdf_bitmap(x, y);
auto const r = msdfgen::pixelFloatToByte(px[0]);
if (sum_white > sum_black && (float)bmp_w / (float)bmp_h > 0.6) {
if (flip) {
buffer[static_cast<usize>(dst_y) * bmp_w + x] = Color { 255,
255, 255, static_cast<unsigned char>(255 - r) };
} else {