[TextServer] Do not add extra spacing to zero-width glyphs.

This commit is contained in:
Pāvels Nadtočajevs 2025-09-08 10:06:28 +03:00
parent 8d8041bd4d
commit 2914a870af
No known key found for this signature in database
GPG key ID: 8413210218EF35D2
2 changed files with 36 additions and 5 deletions

View file

@ -4810,6 +4810,17 @@ bool TextServerFallback::_shaped_text_shape(const RID &p_shaped) {
sd->glyphs.push_back(gl);
} else {
// Text span.
int last_non_zero_w = sd->end - 1;
if (i == sd->spans.size() - 1) {
for (int j = span.end - 1; j > span.start; j--) {
last_non_zero_w = j;
uint32_t idx = (int32_t)sd->text[j - sd->start];
if (!is_control(idx) && !(idx >= 0x200B && idx <= 0x200D)) {
break;
}
}
}
RID prev_font;
for (int j = span.start; j < span.end; j++) {
Glyph gl;
@ -4819,7 +4830,8 @@ bool TextServerFallback::_shaped_text_shape(const RID &p_shaped) {
gl.count = 1;
gl.font_size = span.font_size;
gl.index = (int32_t)sd->text[j - sd->start]; // Use codepoint.
if (gl.index == 0x0009 || gl.index == 0x000b) {
bool zw = (gl.index >= 0x200b && gl.index <= 0x200d);
if (gl.index == 0x0009 || gl.index == 0x000b || zw) {
gl.index = 0x0020;
}
if (!sd->preserve_control && is_control(gl.index)) {
@ -4865,8 +4877,11 @@ bool TextServerFallback::_shaped_text_shape(const RID &p_shaped) {
sd->descent = MAX(sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
}
}
if (j < sd->end - 1) {
// Do not add extra spacing to the last glyph of the string.
if (zw) {
gl.advance = 0.0;
}
if ((j < last_non_zero_w) && !Math::is_zero_approx(gl.advance)) {
// Do not add extra spacing to the last glyph of the string and zero width glyphs.
if (is_whitespace(sd->text[j - sd->start])) {
gl.advance += sd->extra_spacing[SPACING_SPACE] + _font_get_spacing(gl.font_rid, SPACING_SPACE);
} else {