Merge pull request #65914 from bruvzg/fix_trim_wo_space

Fix line trim/ellipsis when line do not have space or newline at the end.
This commit is contained in:
Rémi Verschelde 2022-09-21 18:55:44 +02:00
commit a0c85c11c6
2 changed files with 53 additions and 39 deletions

View File

@ -3065,8 +3065,10 @@ int64_t TextServerAdvanced::font_get_glyph_index(const RID &p_font_rid, int64_t
bool TextServerAdvanced::font_has_char(const RID &p_font_rid, int64_t p_char) const { bool TextServerAdvanced::font_has_char(const RID &p_font_rid, int64_t p_char) const {
FontAdvanced *fd = font_owner.get_or_null(p_font_rid); FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
ERR_FAIL_COND_V(!fd, false);
ERR_FAIL_COND_V_MSG((p_char >= 0xd800 && p_char <= 0xdfff) || (p_char > 0x10ffff), false, "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_char, 16) + "."); ERR_FAIL_COND_V_MSG((p_char >= 0xd800 && p_char <= 0xdfff) || (p_char > 0x10ffff), false, "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_char, 16) + ".");
if (!fd) {
return false;
}
MutexLock lock(fd->mutex); MutexLock lock(fd->mutex);
if (fd->cache.is_empty()) { if (fd->cache.is_empty()) {
@ -4495,35 +4497,40 @@ void TextServerAdvanced::shaped_text_overrun_trim_to_width(const RID &p_shaped_l
int glyphs_to = (is_rtl) ? sd_size - 1 : -1; int glyphs_to = (is_rtl) ? sd_size - 1 : -1;
int glyphs_delta = (is_rtl) ? +1 : -1; int glyphs_delta = (is_rtl) ? +1 : -1;
for (int i = glyphs_from; i != glyphs_to; i += glyphs_delta) { if (enforce_ellipsis && (width + ellipsis_width <= p_width)) {
if (!is_rtl) { trim_pos = -1;
width -= sd_glyphs[i].advance * sd_glyphs[i].repeat; ellipsis_pos = (is_rtl) ? 0 : sd_size;
} } else {
if (sd_glyphs[i].count > 0) { for (int i = glyphs_from; i != glyphs_to; i += glyphs_delta) {
bool above_min_char_threshold = ((is_rtl) ? sd_size - 1 - i : i) >= ell_min_characters; if (!is_rtl) {
width -= sd_glyphs[i].advance * sd_glyphs[i].repeat;
}
if (sd_glyphs[i].count > 0) {
bool above_min_char_threshold = ((is_rtl) ? sd_size - 1 - i : i) >= ell_min_characters;
if (width + (((above_min_char_threshold && add_ellipsis) || enforce_ellipsis) ? ellipsis_width : 0) <= p_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 (cut_per_word && above_min_char_threshold) {
if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) { if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
last_valid_cut = i;
found = true;
}
} else {
last_valid_cut = i; last_valid_cut = i;
found = true; found = true;
} }
} else { if (found) {
last_valid_cut = i; trim_pos = last_valid_cut;
found = true;
}
if (found) {
trim_pos = last_valid_cut;
if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis) && width - ellipsis_width <= p_width) { if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis) && width - ellipsis_width <= p_width) {
ellipsis_pos = trim_pos; ellipsis_pos = trim_pos;
}
break;
} }
break;
} }
} }
} if (is_rtl) {
if (is_rtl) { width -= sd_glyphs[i].advance * sd_glyphs[i].repeat;
width -= sd_glyphs[i].advance * sd_glyphs[i].repeat; }
} }
} }

View File

@ -2127,8 +2127,10 @@ int64_t TextServerFallback::font_get_glyph_index(const RID &p_font_rid, int64_t
bool TextServerFallback::font_has_char(const RID &p_font_rid, int64_t p_char) const { bool TextServerFallback::font_has_char(const RID &p_font_rid, int64_t p_char) const {
FontFallback *fd = font_owner.get_or_null(p_font_rid); FontFallback *fd = font_owner.get_or_null(p_font_rid);
ERR_FAIL_COND_V(!fd, false);
ERR_FAIL_COND_V_MSG((p_char >= 0xd800 && p_char <= 0xdfff) || (p_char > 0x10ffff), false, "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_char, 16) + "."); ERR_FAIL_COND_V_MSG((p_char >= 0xd800 && p_char <= 0xdfff) || (p_char > 0x10ffff), false, "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_char, 16) + ".");
if (!fd) {
return false;
}
MutexLock lock(fd->mutex); MutexLock lock(fd->mutex);
if (fd->cache.is_empty()) { if (fd->cache.is_empty()) {
@ -3453,29 +3455,34 @@ void TextServerFallback::shaped_text_overrun_trim_to_width(const RID &p_shaped_l
int last_valid_cut = 0; int last_valid_cut = 0;
bool found = false; bool found = false;
for (int i = sd_size - 1; i != -1; i--) { if (enforce_ellipsis && (width + ellipsis_width <= p_width)) {
width -= sd_glyphs[i].advance * sd_glyphs[i].repeat; trim_pos = -1;
ellipsis_pos = sd_size;
} else {
for (int i = sd_size - 1; i != -1; i--) {
width -= sd_glyphs[i].advance * sd_glyphs[i].repeat;
if (sd_glyphs[i].count > 0) { if (sd_glyphs[i].count > 0) {
bool above_min_char_threshold = (i >= ell_min_characters); bool above_min_char_threshold = (i >= ell_min_characters);
if (width + (((above_min_char_threshold && add_ellipsis) || enforce_ellipsis) ? ellipsis_width : 0) <= p_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 (cut_per_word && above_min_char_threshold) {
if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) { if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
last_valid_cut = i;
found = true;
}
} else {
last_valid_cut = i; last_valid_cut = i;
found = true; found = true;
} }
} else { if (found) {
last_valid_cut = i; trim_pos = last_valid_cut;
found = true;
}
if (found) {
trim_pos = last_valid_cut;
if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis) && width - ellipsis_width <= p_width) { if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis) && width - ellipsis_width <= p_width) {
ellipsis_pos = trim_pos; ellipsis_pos = trim_pos;
}
break;
} }
break;
} }
} }
} }