Skip to content

Commit 6f01413

Browse files
committed
Merge pull request godotengine#110317 from bruvzg/ts_zero_w
[TextServer] Do not add extra spacing to zero-width glyphs.
2 parents 9712d6f + 2914a87 commit 6f01413

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

modules/text_server_adv/text_server_adv.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6816,6 +6816,22 @@ void TextServerAdvanced::_shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_star
68166816
unsigned int last_cluster_index = 0;
68176817
bool last_cluster_valid = true;
68186818

6819+
unsigned int last_non_zero_w = glyph_count - 1;
6820+
if (last_run) {
6821+
for (unsigned int i = glyph_count - 1; i > 0; i--) {
6822+
last_non_zero_w = i;
6823+
if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
6824+
if (glyph_pos[i].x_advance != 0) {
6825+
break;
6826+
}
6827+
} else {
6828+
if (glyph_pos[i].y_advance != 0) {
6829+
break;
6830+
}
6831+
}
6832+
}
6833+
}
6834+
68196835
double adv_rem = 0.0;
68206836
for (unsigned int i = 0; i < glyph_count; i++) {
68216837
if ((i > 0) && (last_cluster_id != glyph_info[i].cluster)) {
@@ -6907,8 +6923,8 @@ void TextServerAdvanced::_shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_star
69076923
gl.x_off += _font_get_baseline_offset(gl.font_rid) * (double)(_font_get_ascent(gl.font_rid, gl.font_size) + _font_get_descent(gl.font_rid, gl.font_size));
69086924
}
69096925
}
6910-
if (!last_run || i < glyph_count - 1) {
6911-
// Do not add extra spacing to the last glyph of the string.
6926+
if ((!last_run || i < last_non_zero_w) && !Math::is_zero_approx(gl.advance)) {
6927+
// Do not add extra spacing to the last glyph of the string and zero width glyphs.
69126928
if (sp_sp && is_whitespace(p_sd->text[glyph_info[i].cluster])) {
69136929
gl.advance += sp_sp;
69146930
} else {

modules/text_server_fb/text_server_fb.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4810,6 +4810,17 @@ bool TextServerFallback::_shaped_text_shape(const RID &p_shaped) {
48104810
sd->glyphs.push_back(gl);
48114811
} else {
48124812
// Text span.
4813+
int last_non_zero_w = sd->end - 1;
4814+
if (i == sd->spans.size() - 1) {
4815+
for (int j = span.end - 1; j > span.start; j--) {
4816+
last_non_zero_w = j;
4817+
uint32_t idx = (int32_t)sd->text[j - sd->start];
4818+
if (!is_control(idx) && !(idx >= 0x200B && idx <= 0x200D)) {
4819+
break;
4820+
}
4821+
}
4822+
}
4823+
48134824
RID prev_font;
48144825
for (int j = span.start; j < span.end; j++) {
48154826
Glyph gl;
@@ -4819,7 +4830,8 @@ bool TextServerFallback::_shaped_text_shape(const RID &p_shaped) {
48194830
gl.count = 1;
48204831
gl.font_size = span.font_size;
48214832
gl.index = (int32_t)sd->text[j - sd->start]; // Use codepoint.
4822-
if (gl.index == 0x0009 || gl.index == 0x000b) {
4833+
bool zw = (gl.index >= 0x200b && gl.index <= 0x200d);
4834+
if (gl.index == 0x0009 || gl.index == 0x000b || zw) {
48234835
gl.index = 0x0020;
48244836
}
48254837
if (!sd->preserve_control && is_control(gl.index)) {
@@ -4865,8 +4877,11 @@ bool TextServerFallback::_shaped_text_shape(const RID &p_shaped) {
48654877
sd->descent = MAX(sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
48664878
}
48674879
}
4868-
if (j < sd->end - 1) {
4869-
// Do not add extra spacing to the last glyph of the string.
4880+
if (zw) {
4881+
gl.advance = 0.0;
4882+
}
4883+
if ((j < last_non_zero_w) && !Math::is_zero_approx(gl.advance)) {
4884+
// Do not add extra spacing to the last glyph of the string and zero width glyphs.
48704885
if (is_whitespace(sd->text[j - sd->start])) {
48714886
gl.advance += sd->extra_spacing[SPACING_SPACE] + _font_get_spacing(gl.font_rid, SPACING_SPACE);
48724887
} else {

0 commit comments

Comments
 (0)