Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Fix MSDF glyph quad anchor mismatched with its rasterized bitmap

Rémi Verscheldefb86bc0cf906
diff --git a/modules/text_server_adv/text_server_adv.cpp b/modules/text_server_adv/text_server_adv.cpp
index b5591184a95..73636b5567f 100644
--- a/modules/text_server_adv/text_server_adv.cpp
+++ b/modules/text_server_adv/text_server_adv.cpp
@@ -1022,8 +1022,9 @@ _FORCE_INLINE_ TextServerAdvanced::FontGlyph TextServerAdvanced::rasterize_msdf(
chr.advance = p_advance;
if (shape.validate() && shape.contours.size() > 0) {
- int w = (bounds.r - bounds.l);
- int h = (bounds.t - bounds.b);
+ // Round the glyph size up to whole pixels so the bitmap fully covers the shape.
+ int w = Math::ceil(bounds.r - bounds.l);
+ int h = Math::ceil(bounds.t - bounds.b);
if (w == 0 || h == 0) {
chr.texture_idx = -1;
@@ -1080,8 +1081,9 @@ _FORCE_INLINE_ TextServerAdvanced::FontGlyph TextServerAdvanced::rasterize_msdf(
chr.texture_idx = tex_pos.index;
chr.uv_rect = Rect2(tex_pos.x + p_rect_margin, tex_pos.y + p_rect_margin, w + p_rect_margin * 2, h + p_rect_margin * 2);
- chr.rect.position = Vector2(bounds.l - p_rect_margin, -bounds.t - p_rect_margin);
-
+ // Derive the glyph position from the same bottom-left anchor the rasterizer uses,
+ // rather than top-left, so the two agree about glyph placement.
+ chr.rect.position = Vector2(bounds.l - p_rect_margin, -(bounds.b + h) - p_rect_margin);
chr.rect.size = chr.uv_rect.size;
}
return chr;
diff --git a/modules/text_server_fb/text_server_fb.cpp b/modules/text_server_fb/text_server_fb.cpp
index 7857e2f15a0..2f5e9558a84 100644
--- a/modules/text_server_fb/text_server_fb.cpp
+++ b/modules/text_server_fb/text_server_fb.cpp
@@ -439,8 +439,9 @@ _FORCE_INLINE_ TextServerFallback::FontGlyph TextServerFallback::rasterize_msdf(
chr.advance = p_advance;
if (shape.validate() && shape.contours.size() > 0) {
- int w = (bounds.r - bounds.l);
- int h = (bounds.t - bounds.b);
+ // Round the glyph size up to whole pixels so the bitmap fully covers the shape.
+ int w = Math::ceil(bounds.r - bounds.l);
+ int h = Math::ceil(bounds.t - bounds.b);
if (w == 0 || h == 0) {
chr.texture_idx = -1;
@@ -496,7 +497,9 @@ _FORCE_INLINE_ TextServerFallback::FontGlyph TextServerFallback::rasterize_msdf(
chr.texture_idx = tex_pos.index;
chr.uv_rect = Rect2(tex_pos.x + p_rect_margin, tex_pos.y + p_rect_margin, w + p_rect_margin * 2, h + p_rect_margin * 2);
- chr.rect.position = Vector2(bounds.l - p_rect_margin, -bounds.t - p_rect_margin);
+ // Derive the glyph position from the same bottom-left anchor the rasterizer uses,
+ // rather than top-left, so the two agree about glyph placement.
+ chr.rect.position = Vector2(bounds.l - p_rect_margin, -(bounds.b + h) - p_rect_margin);
chr.rect.size = chr.uv_rect.size;
}
return chr;