Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix: Correct MSDF font glyph positioning and TextField vertical centering

esengine486b941c0051
diff --git a/src/esengine/ui/font/MSDFFont.cpp b/src/esengine/ui/font/MSDFFont.cpp
index 43e390967..5e324f41b 100644
--- a/src/esengine/ui/font/MSDFFont.cpp
+++ b/src/esengine/ui/font/MSDFFont.cpp
@@ -212,10 +212,12 @@ MSDFGlyphInfo* MSDFFont::loadGlyph(u32 codepoint) {
shape.orientContours();
msdfgen::edgeColoringSimple(shape, 3.0);
+ msdfgen::Shape::Bounds tightBounds = shape.getBounds();
msdfgen::Shape::Bounds bounds = shape.getBounds(pixelRange_ / fontSize_);
- i32 glyphWidth = static_cast<i32>(std::ceil((bounds.r - bounds.l) * fontSize_)) + 2;
- i32 glyphHeight = static_cast<i32>(std::ceil((bounds.t - bounds.b) * fontSize_)) + 2;
+ constexpr i32 BITMAP_PADDING = 1;
+ i32 glyphWidth = static_cast<i32>(std::ceil((bounds.r - bounds.l) * fontSize_)) + BITMAP_PADDING * 2;
+ i32 glyphHeight = static_cast<i32>(std::ceil((bounds.t - bounds.b) * fontSize_)) + BITMAP_PADDING * 2;
glyphWidth = std::max(glyphWidth, 1);
glyphHeight = std::max(glyphHeight, 1);
@@ -239,7 +241,8 @@ MSDFGlyphInfo* MSDFFont::loadGlyph(u32 codepoint) {
msdfgen::Bitmap<float, 3> msdf(glyphWidth, glyphHeight);
msdfgen::Vector2 scale(fontSize_, fontSize_);
- msdfgen::Vector2 translate(1.0 / fontSize_ - bounds.l, 1.0 / fontSize_ - bounds.b);
+ double paddingInEm = BITMAP_PADDING / static_cast<double>(fontSize_);
+ msdfgen::Vector2 translate(paddingInEm - bounds.l, paddingInEm - bounds.b);
double rangeInEm = pixelRange_ / fontSize_;
msdfgen::Range range(-rangeInEm / 2.0, rangeInEm / 2.0);
@@ -270,8 +273,9 @@ MSDFGlyphInfo* MSDFFont::loadGlyph(u32 codepoint) {
MSDFGlyphInfo& info = glyphs_[codepoint];
info.width = static_cast<f32>(glyphWidth);
info.height = static_cast<f32>(glyphHeight);
- info.bearingX = static_cast<f32>(bounds.l) * fontSize_ - 1.0f;
- info.bearingY = static_cast<f32>(bounds.t) * fontSize_ + 1.0f;
+ f32 spreadPixels = static_cast<f32>(bounds.t - tightBounds.t) * fontSize_;
+ info.bearingX = static_cast<f32>(tightBounds.l) * fontSize_ - spreadPixels - BITMAP_PADDING;
+ info.bearingY = static_cast<f32>(tightBounds.t) * fontSize_ + spreadPixels + BITMAP_PADDING;
info.advance = static_cast<f32>(advance) * fontSize_;
info.atlasX = atlasX;
diff --git a/src/esengine/ui/widgets/TextField.cpp b/src/esengine/ui/widgets/TextField.cpp
index cf4f16a95..36d11aa3d 100644
--- a/src/esengine/ui/widgets/TextField.cpp
+++ b/src/esengine/ui/widgets/TextField.cpp
@@ -145,7 +145,16 @@ void TextField::render(UIBatchRenderer& renderer) {
}
f32 textX = bounds.x + padding.left + TEXT_PADDING - textOffsetX_;
- f32 textY = bounds.y + (bounds.height - fontSize) * 0.5f;
+
+ f32 lineHeight = fontSize;
+#if ES_FEATURE_SDF_FONT
+ if (getContext() && getContext()->getDefaultMSDFFont()) {
+ MSDFFont* font = getContext()->getDefaultMSDFFont();
+ f32 scale = fontSize / font->getFontSize();
+ lineHeight = font->getLineHeight() * scale;
+ }
+#endif
+ f32 textY = bounds.y + (bounds.height - lineHeight) * 0.5f;
if (text_.empty() && !isFocused() && !placeholder_.empty()) {
glm::vec4 placeholderColor = textColor;