Skip to content

Commit 47d6ce2

Browse files
committed
Merge pull request godotengine#110408 from bruvzg/el_trim_w
[TextServer] Use a separate flag to disable min. string length for adding ellipsis.
2 parents 884bf2f + 5fdcb80 commit 47d6ce2

File tree

5 files changed

+31
-24
lines changed

5 files changed

+31
-24
lines changed

doc/classes/TextServer.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,6 +2143,9 @@
21432143
<constant name="OVERRUN_JUSTIFICATION_AWARE" value="16" enum="TextOverrunFlag" is_bitfield="true">
21442144
Accounts for the text being justified before attempting to trim it (see [enum JustificationFlag]).
21452145
</constant>
2146+
<constant name="OVERRUN_SHORT_STRING_ELLIPSIS" value="32" enum="TextOverrunFlag" is_bitfield="true">
2147+
Determines whether the ellipsis should be added regardless of the string length, otherwise it is added only if the string is 6 characters or longer.
2148+
</constant>
21462149
<constant name="GRAPHEME_IS_VALID" value="1" enum="GraphemeFlag" is_bitfield="true">
21472150
Grapheme is supported by the font, and can be drawn.
21482151
</constant>

modules/text_server_adv/text_server_adv.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5882,6 +5882,7 @@ void TextServerAdvanced::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
58825882
bool add_ellipsis = p_trim_flags.has_flag(OVERRUN_ADD_ELLIPSIS);
58835883
bool cut_per_word = p_trim_flags.has_flag(OVERRUN_TRIM_WORD_ONLY);
58845884
bool enforce_ellipsis = p_trim_flags.has_flag(OVERRUN_ENFORCE_ELLIPSIS);
5885+
bool short_string_ellipsis = p_trim_flags.has_flag(OVERRUN_SHORT_STRING_ELLIPSIS);
58855886
bool justification_aware = p_trim_flags.has_flag(OVERRUN_JUSTIFICATION_AWARE);
58865887

58875888
Glyph *sd_glyphs = sd->glyphs.ptr();
@@ -5914,7 +5915,7 @@ void TextServerAdvanced::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
59145915

59155916
// Find usable fonts, if fonts from the last glyph do not have required chars.
59165917
RID dot_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
5917-
if (add_ellipsis || enforce_ellipsis) {
5918+
if (add_ellipsis || enforce_ellipsis || short_string_ellipsis) {
59185919
if (!_font_has_char(dot_gl_font_rid, sd->el_char)) {
59195920
const Array &fonts = spans[span_size - 1].fonts;
59205921
for (int i = 0; i < fonts.size(); i++) {
@@ -5967,8 +5968,8 @@ void TextServerAdvanced::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
59675968
}
59685969
}
59695970

5970-
int32_t dot_gl_idx = ((add_ellipsis || enforce_ellipsis) && dot_gl_font_rid.is_valid()) ? _font_get_glyph_index(dot_gl_font_rid, last_gl_font_size, (found_el_char ? sd->el_char : '.'), 0) : -1;
5971-
Vector2 dot_adv = ((add_ellipsis || enforce_ellipsis) && dot_gl_font_rid.is_valid()) ? _font_get_glyph_advance(dot_gl_font_rid, last_gl_font_size, dot_gl_idx) : Vector2();
5971+
int32_t dot_gl_idx = ((add_ellipsis || enforce_ellipsis || short_string_ellipsis) && dot_gl_font_rid.is_valid()) ? _font_get_glyph_index(dot_gl_font_rid, last_gl_font_size, (found_el_char ? sd->el_char : '.'), 0) : -1;
5972+
Vector2 dot_adv = ((add_ellipsis || enforce_ellipsis || short_string_ellipsis) && dot_gl_font_rid.is_valid()) ? _font_get_glyph_advance(dot_gl_font_rid, last_gl_font_size, dot_gl_idx) : Vector2();
59725973
int32_t whitespace_gl_idx = whitespace_gl_font_rid.is_valid() ? _font_get_glyph_index(whitespace_gl_font_rid, last_gl_font_size, ' ', 0) : -1;
59735974
Vector2 whitespace_adv = whitespace_gl_font_rid.is_valid() ? _font_get_glyph_advance(whitespace_gl_font_rid, last_gl_font_size, whitespace_gl_idx) : Vector2();
59745975

@@ -5984,7 +5985,7 @@ void TextServerAdvanced::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
59845985
bool is_rtl = sd->para_direction == DIRECTION_RTL;
59855986

59865987
int trim_pos = (is_rtl) ? sd_size : 0;
5987-
int ellipsis_pos = (enforce_ellipsis) ? 0 : -1;
5988+
int ellipsis_pos = (enforce_ellipsis || short_string_ellipsis) ? 0 : -1;
59885989

59895990
int last_valid_cut = -1;
59905991
int last_valid_cut_witout_el = -1;
@@ -5993,7 +5994,7 @@ void TextServerAdvanced::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
59935994
int glyphs_to = (is_rtl) ? sd_size - 1 : -1;
59945995
int glyphs_delta = (is_rtl) ? +1 : -1;
59955996

5996-
if (enforce_ellipsis && (width + ellipsis_width <= p_width)) {
5997+
if ((enforce_ellipsis || short_string_ellipsis) && (width + ellipsis_width <= p_width)) {
59975998
trim_pos = -1;
59985999
ellipsis_pos = (is_rtl) ? 0 : sd_size;
59996000
} else {
@@ -6009,7 +6010,7 @@ void TextServerAdvanced::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
60096010
width = width_without_el;
60106011
break;
60116012
}
6012-
if (!enforce_ellipsis && width <= p_width && last_valid_cut_witout_el == -1) {
6013+
if (!(enforce_ellipsis || short_string_ellipsis) && width <= p_width && last_valid_cut_witout_el == -1) {
60136014
if (cut_per_word && above_min_char_threshold) {
60146015
if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
60156016
last_valid_cut_witout_el = i;
@@ -6020,7 +6021,7 @@ void TextServerAdvanced::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
60206021
width_without_el = width;
60216022
}
60226023
}
6023-
if (width + (((above_min_char_threshold && add_ellipsis) || enforce_ellipsis) ? ellipsis_width : 0) <= p_width) {
6024+
if (width + (((above_min_char_threshold && add_ellipsis) || enforce_ellipsis || short_string_ellipsis) ? ellipsis_width : 0) <= p_width) {
60246025
if (cut_per_word && above_min_char_threshold) {
60256026
if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
60266027
last_valid_cut = i;
@@ -6031,7 +6032,7 @@ void TextServerAdvanced::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
60316032
if (last_valid_cut != -1) {
60326033
trim_pos = last_valid_cut;
60336034

6034-
if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis) && width - ellipsis_width <= p_width) {
6035+
if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis || short_string_ellipsis) && width - ellipsis_width <= p_width) {
60356036
ellipsis_pos = trim_pos;
60366037
}
60376038
break;
@@ -6046,12 +6047,12 @@ void TextServerAdvanced::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
60466047

60476048
sd->overrun_trim_data.trim_pos = trim_pos;
60486049
sd->overrun_trim_data.ellipsis_pos = ellipsis_pos;
6049-
if (trim_pos == 0 && enforce_ellipsis && add_ellipsis) {
6050+
if (trim_pos == 0 && (enforce_ellipsis || short_string_ellipsis) && add_ellipsis) {
60506051
sd->overrun_trim_data.ellipsis_pos = 0;
60516052
}
60526053

6053-
if ((trim_pos >= 0 && sd->width > p_width) || enforce_ellipsis) {
6054-
if (add_ellipsis && (ellipsis_pos > 0 || enforce_ellipsis)) {
6054+
if ((trim_pos >= 0 && sd->width > p_width) || enforce_ellipsis || short_string_ellipsis) {
6055+
if (add_ellipsis && (ellipsis_pos > 0 || enforce_ellipsis || short_string_ellipsis)) {
60556056
// Insert an additional space when cutting word bound for aesthetics.
60566057
if (cut_per_word && (ellipsis_pos > 0)) {
60576058
Glyph gl;

modules/text_server_fb/text_server_fb.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4586,6 +4586,7 @@ void TextServerFallback::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
45864586
bool add_ellipsis = p_trim_flags.has_flag(OVERRUN_ADD_ELLIPSIS);
45874587
bool cut_per_word = p_trim_flags.has_flag(OVERRUN_TRIM_WORD_ONLY);
45884588
bool enforce_ellipsis = p_trim_flags.has_flag(OVERRUN_ENFORCE_ELLIPSIS);
4589+
bool short_string_ellipsis = p_trim_flags.has_flag(OVERRUN_SHORT_STRING_ELLIPSIS);
45894590
bool justification_aware = p_trim_flags.has_flag(OVERRUN_JUSTIFICATION_AWARE);
45904591

45914592
Glyph *sd_glyphs = sd->glyphs.ptrw();
@@ -4618,7 +4619,7 @@ void TextServerFallback::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
46184619

46194620
// Find usable fonts, if fonts from the last glyph do not have required chars.
46204621
RID dot_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
4621-
if (add_ellipsis || enforce_ellipsis) {
4622+
if (add_ellipsis || enforce_ellipsis || short_string_ellipsis) {
46224623
if (!_font_has_char(dot_gl_font_rid, sd->el_char)) {
46234624
const Array &fonts = spans[span_size - 1].fonts;
46244625
for (int i = 0; i < fonts.size(); i++) {
@@ -4671,8 +4672,8 @@ void TextServerFallback::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
46714672
}
46724673
}
46734674

4674-
int32_t dot_gl_idx = ((add_ellipsis || enforce_ellipsis) && dot_gl_font_rid.is_valid()) ? _font_get_glyph_index(dot_gl_font_rid, last_gl_font_size, (found_el_char ? sd->el_char : '.'), 0) : -1;
4675-
Vector2 dot_adv = ((add_ellipsis || enforce_ellipsis) && dot_gl_font_rid.is_valid()) ? _font_get_glyph_advance(dot_gl_font_rid, last_gl_font_size, dot_gl_idx) : Vector2();
4675+
int32_t dot_gl_idx = ((add_ellipsis || enforce_ellipsis || short_string_ellipsis) && dot_gl_font_rid.is_valid()) ? _font_get_glyph_index(dot_gl_font_rid, last_gl_font_size, (found_el_char ? sd->el_char : '.'), 0) : -1;
4676+
Vector2 dot_adv = ((add_ellipsis || enforce_ellipsis || short_string_ellipsis) && dot_gl_font_rid.is_valid()) ? _font_get_glyph_advance(dot_gl_font_rid, last_gl_font_size, dot_gl_idx) : Vector2();
46764677
int32_t whitespace_gl_idx = whitespace_gl_font_rid.is_valid() ? _font_get_glyph_index(whitespace_gl_font_rid, last_gl_font_size, ' ', 0) : -1;
46774678
Vector2 whitespace_adv = whitespace_gl_font_rid.is_valid() ? _font_get_glyph_advance(whitespace_gl_font_rid, last_gl_font_size, whitespace_gl_idx) : Vector2();
46784679

@@ -4686,12 +4687,12 @@ void TextServerFallback::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
46864687
double width_without_el = width;
46874688

46884689
int trim_pos = 0;
4689-
int ellipsis_pos = (enforce_ellipsis) ? 0 : -1;
4690+
int ellipsis_pos = (enforce_ellipsis || short_string_ellipsis) ? 0 : -1;
46904691

46914692
int last_valid_cut = -1;
46924693
int last_valid_cut_witout_el = -1;
46934694

4694-
if (enforce_ellipsis && (width + ellipsis_width <= p_width)) {
4695+
if ((enforce_ellipsis || short_string_ellipsis) && (width + ellipsis_width <= p_width)) {
46954696
trim_pos = -1;
46964697
ellipsis_pos = sd_size;
46974698
} else {
@@ -4706,7 +4707,7 @@ void TextServerFallback::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
47064707
width = width_without_el;
47074708
break;
47084709
}
4709-
if (!enforce_ellipsis && width <= p_width && last_valid_cut_witout_el == -1) {
4710+
if (!(enforce_ellipsis || short_string_ellipsis) && width <= p_width && last_valid_cut_witout_el == -1) {
47104711
if (cut_per_word && above_min_char_threshold) {
47114712
if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
47124713
last_valid_cut_witout_el = i;
@@ -4717,7 +4718,7 @@ void TextServerFallback::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
47174718
width_without_el = width;
47184719
}
47194720
}
4720-
if (width + (((above_min_char_threshold && add_ellipsis) || enforce_ellipsis) ? ellipsis_width : 0) <= p_width) {
4721+
if (width + (((above_min_char_threshold && add_ellipsis) || enforce_ellipsis || short_string_ellipsis) ? ellipsis_width : 0) <= p_width) {
47214722
if (cut_per_word && above_min_char_threshold) {
47224723
if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
47234724
last_valid_cut = i;
@@ -4728,7 +4729,7 @@ void TextServerFallback::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
47284729
if (last_valid_cut != -1) {
47294730
trim_pos = last_valid_cut;
47304731

4731-
if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis) && width - ellipsis_width <= p_width) {
4732+
if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis || short_string_ellipsis) && width - ellipsis_width <= p_width) {
47324733
ellipsis_pos = trim_pos;
47334734
}
47344735
break;
@@ -4740,12 +4741,12 @@ void TextServerFallback::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
47404741

47414742
sd->overrun_trim_data.trim_pos = trim_pos;
47424743
sd->overrun_trim_data.ellipsis_pos = ellipsis_pos;
4743-
if (trim_pos == 0 && enforce_ellipsis && add_ellipsis) {
4744+
if (trim_pos == 0 && (enforce_ellipsis || short_string_ellipsis) && add_ellipsis) {
47444745
sd->overrun_trim_data.ellipsis_pos = 0;
47454746
}
47464747

4747-
if ((trim_pos >= 0 && sd->width > p_width) || enforce_ellipsis) {
4748-
if (add_ellipsis && (ellipsis_pos > 0 || enforce_ellipsis)) {
4748+
if ((trim_pos >= 0 && sd->width > p_width) || enforce_ellipsis || short_string_ellipsis) {
4749+
if (add_ellipsis && (ellipsis_pos > 0 || enforce_ellipsis || short_string_ellipsis)) {
47494750
// Insert an additional space when cutting word bound for aesthetics.
47504751
if (cut_per_word && (ellipsis_pos > 0)) {
47514752
Glyph gl;

servers/text/text_server.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ void TextServer::_bind_methods() {
608608
BIND_BITFIELD_FLAG(OVERRUN_ADD_ELLIPSIS);
609609
BIND_BITFIELD_FLAG(OVERRUN_ENFORCE_ELLIPSIS);
610610
BIND_BITFIELD_FLAG(OVERRUN_JUSTIFICATION_AWARE);
611+
BIND_BITFIELD_FLAG(OVERRUN_SHORT_STRING_ELLIPSIS);
611612

612613
/* GraphemeFlag */
613614
BIND_BITFIELD_FLAG(GRAPHEME_IS_VALID);
@@ -2398,12 +2399,12 @@ BitField<TextServer::TextOverrunFlag> TextServer::get_overrun_flags_from_behavio
23982399
overrun_flags.set_flag(OVERRUN_TRIM);
23992400
overrun_flags.set_flag(OVERRUN_TRIM_WORD_ONLY);
24002401
overrun_flags.set_flag(OVERRUN_ADD_ELLIPSIS);
2401-
overrun_flags.set_flag(OVERRUN_ENFORCE_ELLIPSIS);
2402+
overrun_flags.set_flag(OVERRUN_SHORT_STRING_ELLIPSIS);
24022403
} break;
24032404
case OVERRUN_TRIM_ELLIPSIS_FORCE: {
24042405
overrun_flags.set_flag(OVERRUN_TRIM);
24052406
overrun_flags.set_flag(OVERRUN_ADD_ELLIPSIS);
2406-
overrun_flags.set_flag(OVERRUN_ENFORCE_ELLIPSIS);
2407+
overrun_flags.set_flag(OVERRUN_SHORT_STRING_ELLIPSIS);
24072408
} break;
24082409
case OVERRUN_TRIM_WORD_ELLIPSIS:
24092410
overrun_flags.set_flag(OVERRUN_TRIM);

servers/text/text_server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class TextServer : public RefCounted {
137137
OVERRUN_ADD_ELLIPSIS = 1 << 2,
138138
OVERRUN_ENFORCE_ELLIPSIS = 1 << 3,
139139
OVERRUN_JUSTIFICATION_AWARE = 1 << 4,
140+
OVERRUN_SHORT_STRING_ELLIPSIS = 1 << 5,
140141
};
141142

142143
enum GraphemeFlag {

0 commit comments

Comments
 (0)