From 316b50ba8d21995fcc7e58494398d5e72b8dfa21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pa=CC=84vels=20Nadtoc=CC=8Cajevs?= <7645683+bruvzg@users.noreply.github.com> Date: Sun, 9 Feb 2025 22:33:46 +0200 Subject: [PATCH] [TextServer] Use all available space when string is too short for ellipsis. --- modules/text_server_adv/text_server_adv.cpp | 27 ++++++++++++++++----- modules/text_server_fb/text_server_fb.cpp | 27 ++++++++++++++++----- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/modules/text_server_adv/text_server_adv.cpp b/modules/text_server_adv/text_server_adv.cpp index 527d2dce41f..52a61cd9244 100644 --- a/modules/text_server_adv/text_server_adv.cpp +++ b/modules/text_server_adv/text_server_adv.cpp @@ -5415,14 +5415,15 @@ void TextServerAdvanced::_shaped_text_overrun_trim_to_width(const RID &p_shaped_ int ell_min_characters = 6; double width = sd->width; + double width_without_el = width; bool is_rtl = sd->para_direction == DIRECTION_RTL; int trim_pos = (is_rtl) ? sd_size : 0; int ellipsis_pos = (enforce_ellipsis) ? 0 : -1; - int last_valid_cut = 0; - bool found = false; + int last_valid_cut = -1; + int last_valid_cut_witout_el = -1; int glyphs_from = (is_rtl) ? 0 : sd_size - 1; int glyphs_to = (is_rtl) ? sd_size - 1 : -1; @@ -5438,18 +5439,32 @@ void TextServerAdvanced::_shaped_text_overrun_trim_to_width(const RID &p_shaped_ } if (sd_glyphs[i].count > 0) { bool above_min_char_threshold = ((is_rtl) ? sd_size - 1 - i : i) >= ell_min_characters; - + if (!above_min_char_threshold && last_valid_cut_witout_el != -1) { + trim_pos = last_valid_cut_witout_el; + ellipsis_pos = -1; + width = width_without_el; + break; + } + if (!enforce_ellipsis && width <= p_width && last_valid_cut_witout_el == -1) { + if (cut_per_word && above_min_char_threshold) { + if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) { + last_valid_cut_witout_el = i; + width_without_el = width; + } + } else { + last_valid_cut_witout_el = i; + width_without_el = width; + } + } if (width + (((above_min_char_threshold && add_ellipsis) || enforce_ellipsis) ? ellipsis_width : 0) <= p_width) { if (cut_per_word && above_min_char_threshold) { if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) { last_valid_cut = i; - found = true; } } else { last_valid_cut = i; - found = true; } - if (found) { + if (last_valid_cut != -1) { trim_pos = last_valid_cut; if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis) && width - ellipsis_width <= p_width) { diff --git a/modules/text_server_fb/text_server_fb.cpp b/modules/text_server_fb/text_server_fb.cpp index 63ff8177ea4..8ca81368436 100644 --- a/modules/text_server_fb/text_server_fb.cpp +++ b/modules/text_server_fb/text_server_fb.cpp @@ -4210,12 +4210,13 @@ void TextServerFallback::_shaped_text_overrun_trim_to_width(const RID &p_shaped_ int ell_min_characters = 6; double width = sd->width; + double width_without_el = width; int trim_pos = 0; int ellipsis_pos = (enforce_ellipsis) ? 0 : -1; - int last_valid_cut = 0; - bool found = false; + int last_valid_cut = -1; + int last_valid_cut_witout_el = -1; if (enforce_ellipsis && (width + ellipsis_width <= p_width)) { trim_pos = -1; @@ -4226,18 +4227,32 @@ void TextServerFallback::_shaped_text_overrun_trim_to_width(const RID &p_shaped_ if (sd_glyphs[i].count > 0) { bool above_min_char_threshold = (i >= ell_min_characters); - + if (!above_min_char_threshold && last_valid_cut_witout_el != -1) { + trim_pos = last_valid_cut_witout_el; + ellipsis_pos = -1; + width = width_without_el; + break; + } + if (!enforce_ellipsis && width <= p_width && last_valid_cut_witout_el == -1) { + if (cut_per_word && above_min_char_threshold) { + if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) { + last_valid_cut_witout_el = i; + width_without_el = width; + } + } else { + last_valid_cut_witout_el = i; + width_without_el = width; + } + } if (width + (((above_min_char_threshold && add_ellipsis) || enforce_ellipsis) ? ellipsis_width : 0) <= p_width) { if (cut_per_word && above_min_char_threshold) { if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) { last_valid_cut = i; - found = true; } } else { last_valid_cut = i; - found = true; } - if (found) { + if (last_valid_cut != -1) { trim_pos = last_valid_cut; if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis) && width - ellipsis_width <= p_width) {