Make label sizing algorithm more robust
This commit is contained in:
parent
8c7b98d452
commit
4f7f1ef60b
2 changed files with 137 additions and 130 deletions
|
|
@ -86,9 +86,7 @@ int Label::get_line_height(int p_line) const {
|
|||
}
|
||||
}
|
||||
|
||||
void Label::_shape() {
|
||||
bool font_was_dirty = font_dirty;
|
||||
|
||||
bool Label::_shape() {
|
||||
Ref<StyleBox> style = theme_cache.normal_style;
|
||||
int width = (get_size().width - style->get_minimum_size().width);
|
||||
|
||||
|
|
@ -103,7 +101,7 @@ void Label::_shape() {
|
|||
}
|
||||
const Ref<Font> &font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
|
||||
int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
|
||||
ERR_FAIL_COND(font.is_null());
|
||||
ERR_FAIL_COND_V(font.is_null(), true);
|
||||
String txt = (uppercase) ? TS->string_to_upper(xl_text, language) : xl_text;
|
||||
if (visible_chars >= 0 && visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
|
||||
txt = txt.substr(0, visible_chars);
|
||||
|
|
@ -123,6 +121,7 @@ void Label::_shape() {
|
|||
dirty = false;
|
||||
font_dirty = false;
|
||||
lines_dirty = true;
|
||||
// Note for future maintainers: forgetting stable width here (e.g., setting it to -1) may fix still undiscovered bugs.
|
||||
}
|
||||
|
||||
if (lines_dirty) {
|
||||
|
|
@ -135,19 +134,20 @@ void Label::_shape() {
|
|||
Size2i prev_minsize = minsize;
|
||||
minsize = Size2();
|
||||
|
||||
bool can_process_lines = false;
|
||||
if (xl_text.length() == 0) {
|
||||
can_process_lines = true;
|
||||
lines_dirty = false;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// With autowrap on, we won't compute the minimum size until width is stable (two shape requests in a
|
||||
// row with the same width.) This avoids situations in which the initial width is very narrow and the label
|
||||
// would break text into many very short lines, causing a very tall label that can leave a deformed container.
|
||||
|
||||
// Don't compute minimum size until width is stable (two shape requests in a row with the same width.)
|
||||
// This avoids situations in which the initial width is very narrow and the label would break text
|
||||
// into many very short lines, causing a very tall label that can leave a deformed container.
|
||||
bool width_stabilized = get_size().width == stable_width;
|
||||
can_process_lines = get_size().width == stable_width || autowrap_mode == TextServer::AUTOWRAP_OFF;
|
||||
stable_width = get_size().width;
|
||||
|
||||
// With autowrap off, there's still a point in shaping before width is stable: to contribute a min width.
|
||||
if (lines_dirty && (width_stabilized || autowrap_mode == TextServer::AUTOWRAP_OFF)) {
|
||||
if (can_process_lines) {
|
||||
if (lines_dirty) {
|
||||
BitField<TextServer::LineBreakFlag> autowrap_flags = TextServer::BREAK_MANDATORY;
|
||||
switch (autowrap_mode) {
|
||||
case TextServer::AUTOWRAP_WORD_SMART:
|
||||
|
|
@ -180,10 +180,9 @@ void Label::_shape() {
|
|||
|
||||
// With autowrap off, by now we already know the width the label will take.
|
||||
width = (minsize.width - style->get_minimum_size().width);
|
||||
width_stabilized = true;
|
||||
}
|
||||
|
||||
if (lines_dirty && width_stabilized) {
|
||||
if (lines_dirty) {
|
||||
BitField<TextServer::TextOverrunFlag> overrun_flags = TextServer::OVERRUN_NO_TRIM;
|
||||
switch (overrun_behavior) {
|
||||
case TextServer::OVERRUN_TRIM_WORD_ELLIPSIS:
|
||||
|
|
@ -250,14 +249,21 @@ void Label::_shape() {
|
|||
|
||||
lines_dirty = false;
|
||||
}
|
||||
} else {
|
||||
callable_mp(this, &Label::_shape).call_deferred();
|
||||
}
|
||||
}
|
||||
|
||||
if (minsize != prev_minsize || font_was_dirty) {
|
||||
if (draw_pending) {
|
||||
queue_redraw();
|
||||
draw_pending = false;
|
||||
}
|
||||
|
||||
if (minsize != prev_minsize) {
|
||||
update_minimum_size();
|
||||
}
|
||||
|
||||
if (!width_stabilized) {
|
||||
callable_mp(this, &Label::_shape).call_deferred();
|
||||
}
|
||||
return can_process_lines;
|
||||
}
|
||||
|
||||
inline void draw_glyph(const Glyph &p_gl, const RID &p_canvas, const Color &p_font_color, const Vector2 &p_ofs) {
|
||||
|
|
@ -367,10 +373,9 @@ void Label::_notification(int p_what) {
|
|||
}
|
||||
|
||||
if (dirty || font_dirty || lines_dirty) {
|
||||
_shape();
|
||||
if (lines_dirty) {
|
||||
if (!_shape()) {
|
||||
// There will be another pass.
|
||||
queue_redraw();
|
||||
draw_pending = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -396,7 +401,7 @@ void Label::_notification(int p_what) {
|
|||
style->draw(ci, Rect2(Point2(0, 0), get_size()));
|
||||
|
||||
float total_h = 0.0;
|
||||
int visible_lines = 0;
|
||||
int lines_visible = 0;
|
||||
|
||||
// Get number of lines to fit to the height.
|
||||
for (int64_t i = lines_skipped; i < lines_rid.size(); i++) {
|
||||
|
|
@ -404,14 +409,14 @@ void Label::_notification(int p_what) {
|
|||
if (total_h > (get_size().height - style->get_minimum_size().height + line_spacing)) {
|
||||
break;
|
||||
}
|
||||
visible_lines++;
|
||||
lines_visible++;
|
||||
}
|
||||
|
||||
if (max_lines_visible >= 0 && visible_lines > max_lines_visible) {
|
||||
visible_lines = max_lines_visible;
|
||||
if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
|
||||
lines_visible = max_lines_visible;
|
||||
}
|
||||
|
||||
int last_line = MIN(lines_rid.size(), visible_lines + lines_skipped);
|
||||
int last_line = MIN(lines_rid.size(), lines_visible + lines_skipped);
|
||||
bool trim_chars = (visible_chars >= 0) && (visible_chars_behavior == TextServer::VC_CHARS_AFTER_SHAPING);
|
||||
bool trim_glyphs_ltr = (visible_chars >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_LTR) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && !rtl_layout));
|
||||
bool trim_glyphs_rtl = (visible_chars >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_RTL) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && rtl_layout));
|
||||
|
|
@ -428,7 +433,7 @@ void Label::_notification(int p_what) {
|
|||
total_h += style->get_margin(SIDE_TOP) + style->get_margin(SIDE_BOTTOM);
|
||||
|
||||
int vbegin = 0, vsep = 0;
|
||||
if (visible_lines > 0) {
|
||||
if (lines_visible > 0) {
|
||||
switch (vertical_alignment) {
|
||||
case VERTICAL_ALIGNMENT_TOP: {
|
||||
// Nothing.
|
||||
|
|
@ -445,8 +450,8 @@ void Label::_notification(int p_what) {
|
|||
} break;
|
||||
case VERTICAL_ALIGNMENT_FILL: {
|
||||
vbegin = 0;
|
||||
if (visible_lines > 1) {
|
||||
vsep = (size.y - (total_h - line_spacing)) / (visible_lines - 1);
|
||||
if (lines_visible > 1) {
|
||||
vsep = (size.y - (total_h - line_spacing)) / (lines_visible - 1);
|
||||
} else {
|
||||
vsep = 0;
|
||||
}
|
||||
|
|
@ -615,6 +620,8 @@ void Label::_notification(int p_what) {
|
|||
}
|
||||
ofs.y += TS->shaped_text_get_descent(lines_rid[i]) + vsep + line_spacing;
|
||||
}
|
||||
|
||||
draw_pending = false;
|
||||
} break;
|
||||
|
||||
case NOTIFICATION_THEME_CHANGED: {
|
||||
|
|
@ -666,25 +673,25 @@ int Label::get_line_count() const {
|
|||
int Label::get_visible_line_count() const {
|
||||
Ref<StyleBox> style = theme_cache.normal_style;
|
||||
int line_spacing = settings.is_valid() ? settings->get_line_spacing() : theme_cache.line_spacing;
|
||||
int visible_lines = 0;
|
||||
int lines_visible = 0;
|
||||
float total_h = 0.0;
|
||||
for (int64_t i = lines_skipped; i < lines_rid.size(); i++) {
|
||||
total_h += TS->shaped_text_get_size(lines_rid[i]).y + line_spacing;
|
||||
if (total_h > (get_size().height - style->get_minimum_size().height + line_spacing)) {
|
||||
break;
|
||||
}
|
||||
visible_lines++;
|
||||
lines_visible++;
|
||||
}
|
||||
|
||||
if (visible_lines > lines_rid.size()) {
|
||||
visible_lines = lines_rid.size();
|
||||
if (lines_visible > lines_rid.size()) {
|
||||
lines_visible = lines_rid.size();
|
||||
}
|
||||
|
||||
if (max_lines_visible >= 0 && visible_lines > max_lines_visible) {
|
||||
visible_lines = max_lines_visible;
|
||||
if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
|
||||
lines_visible = max_lines_visible;
|
||||
}
|
||||
|
||||
return visible_lines;
|
||||
return lines_visible;
|
||||
}
|
||||
|
||||
void Label::set_horizontal_alignment(HorizontalAlignment p_alignment) {
|
||||
|
|
@ -969,7 +976,7 @@ void Label::_bind_methods() {
|
|||
ClassDB::bind_method(D_METHOD("get_visible_ratio"), &Label::get_visible_ratio);
|
||||
ClassDB::bind_method(D_METHOD("set_lines_skipped", "lines_skipped"), &Label::set_lines_skipped);
|
||||
ClassDB::bind_method(D_METHOD("get_lines_skipped"), &Label::get_lines_skipped);
|
||||
ClassDB::bind_method(D_METHOD("set_max_lines_visible", "visible_lines"), &Label::set_max_lines_visible);
|
||||
ClassDB::bind_method(D_METHOD("set_max_lines_visible", "lines_visible"), &Label::set_max_lines_visible);
|
||||
ClassDB::bind_method(D_METHOD("get_max_lines_visible"), &Label::get_max_lines_visible);
|
||||
ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &Label::set_structured_text_bidi_override);
|
||||
ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &Label::get_structured_text_bidi_override);
|
||||
|
|
|
|||
|
|
@ -50,7 +50,6 @@ private:
|
|||
bool uppercase = false;
|
||||
|
||||
bool lines_dirty = true;
|
||||
|
||||
bool dirty = true;
|
||||
bool font_dirty = true;
|
||||
RID text_rid;
|
||||
|
|
@ -66,6 +65,7 @@ private:
|
|||
float visible_ratio = 1.0;
|
||||
int lines_skipped = 0;
|
||||
int max_lines_visible = -1;
|
||||
bool draw_pending = false;
|
||||
|
||||
Ref<LabelSettings> settings;
|
||||
|
||||
|
|
@ -83,7 +83,7 @@ private:
|
|||
int font_shadow_outline_size;
|
||||
} theme_cache;
|
||||
|
||||
void _shape();
|
||||
bool _shape();
|
||||
void _invalidate();
|
||||
|
||||
protected:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue