Skip to content

Commit 5131b81

Browse files
committed
Enhance label sizing algorithm
1 parent 1ed549e commit 5131b81

File tree

2 files changed

+59
-56
lines changed

2 files changed

+59
-56
lines changed

scene/gui/label.cpp

+58-54
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ int Label::get_line_height(int p_line) const {
8787
}
8888

8989
void Label::_shape() {
90+
bool font_was_dirty = font_dirty;
91+
9092
Ref<StyleBox> style = theme_cache.normal_style;
9193
int width = (get_size().width - style->get_minimum_size().width);
9294

@@ -128,7 +130,24 @@ void Label::_shape() {
128130
TS->free_rid(lines_rid[i]);
129131
}
130132
lines_rid.clear();
133+
}
134+
135+
Size2i prev_minsize = minsize;
136+
minsize = Size2();
137+
138+
if (xl_text.length() == 0) {
139+
lines_dirty = false;
140+
return;
141+
}
131142

143+
// Don't compute minimum size until width is stable (two shape requests in a row with the same width.)
144+
// This avoids situations in which the initial width is very narrow and the label would break text
145+
// into many very short lines, causing a very tall label that can leave a deformed container.
146+
bool width_stabilized = get_size().width == stable_width;
147+
stable_width = get_size().width;
148+
149+
// With autowrap off, there's still a point in shaping before width is stable: to contribute a min width.
150+
if (lines_dirty && (width_stabilized || autowrap_mode == TextServer::AUTOWRAP_OFF)) {
132151
BitField<TextServer::LineBreakFlag> autowrap_flags = TextServer::BREAK_MANDATORY;
133152
switch (autowrap_mode) {
134153
case TextServer::AUTOWRAP_WORD_SMART:
@@ -152,21 +171,15 @@ void Label::_shape() {
152171
}
153172
}
154173

155-
if (xl_text.length() == 0) {
156-
minsize = Size2(1, get_line_height());
157-
return;
158-
}
159-
160174
if (autowrap_mode == TextServer::AUTOWRAP_OFF) {
161-
minsize.width = 0.0f;
162175
for (int i = 0; i < lines_rid.size(); i++) {
163176
if (minsize.width < TS->shaped_text_get_size(lines_rid[i]).x) {
164177
minsize.width = TS->shaped_text_get_size(lines_rid[i]).x;
165178
}
166179
}
167180
}
168181

169-
if (lines_dirty) {
182+
if (lines_dirty && width_stabilized) {
170183
BitField<TextServer::TextOverrunFlag> overrun_flags = TextServer::OVERRUN_NO_TRIM;
171184
switch (overrun_behavior) {
172185
case TextServer::OVERRUN_TRIM_WORD_ELLIPSIS:
@@ -191,8 +204,11 @@ void Label::_shape() {
191204

192205
// Fill after min_size calculation.
193206

207+
int visible_lines = lines_rid.size();
208+
if (max_lines_visible >= 0 && visible_lines > max_lines_visible) {
209+
visible_lines = max_lines_visible;
210+
}
194211
if (autowrap_mode != TextServer::AUTOWRAP_OFF) {
195-
int visible_lines = get_visible_line_count();
196212
bool lines_hidden = visible_lines > 0 && visible_lines < lines_rid.size();
197213
if (lines_hidden) {
198214
overrun_flags.set_flag(TextServer::OVERRUN_ENFORCE_ELLIPSIS);
@@ -221,33 +237,22 @@ void Label::_shape() {
221237
}
222238
}
223239
}
224-
lines_dirty = false;
225-
lines_shaped_last_width = get_size().width;
226-
}
227240

228-
_update_visible();
241+
int last_line = MIN(lines_rid.size(), visible_lines + lines_skipped);
242+
int line_spacing = settings.is_valid() ? settings->get_line_spacing() : theme_cache.line_spacing;
243+
for (int64_t i = lines_skipped; i < last_line; i++) {
244+
minsize.height += TS->shaped_text_get_size(lines_rid[i]).y + line_spacing;
245+
}
229246

230-
if (autowrap_mode == TextServer::AUTOWRAP_OFF || !clip || overrun_behavior == TextServer::OVERRUN_NO_TRIMMING) {
231-
update_minimum_size();
247+
lines_dirty = false;
232248
}
233-
}
234-
235-
void Label::_update_visible() {
236-
int line_spacing = settings.is_valid() ? settings->get_line_spacing() : theme_cache.line_spacing;
237-
Ref<StyleBox> style = theme_cache.normal_style;
238-
int lines_visible = lines_rid.size();
239249

240-
if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
241-
lines_visible = max_lines_visible;
250+
if (minsize != prev_minsize || font_was_dirty) {
251+
update_minimum_size();
242252
}
243253

244-
minsize.height = 0;
245-
int last_line = MIN(lines_rid.size(), lines_visible + lines_skipped);
246-
for (int64_t i = lines_skipped; i < last_line; i++) {
247-
minsize.height += TS->shaped_text_get_size(lines_rid[i]).y + line_spacing;
248-
if (minsize.height > (get_size().height - style->get_minimum_size().height + line_spacing)) {
249-
break;
250-
}
254+
if (!width_stabilized) {
255+
callable_mp(this, &Label::_shape).call_deferred();
251256
}
252257
}
253258

@@ -347,6 +352,11 @@ void Label::_notification(int p_what) {
347352

348353
if (dirty || font_dirty || lines_dirty) {
349354
_shape();
355+
if (lines_dirty) {
356+
// There will be another pass.
357+
queue_redraw();
358+
break;
359+
}
350360
}
351361

352362
RID ci = get_canvas_item();
@@ -370,22 +380,22 @@ void Label::_notification(int p_what) {
370380
style->draw(ci, Rect2(Point2(0, 0), get_size()));
371381

372382
float total_h = 0.0;
373-
int lines_visible = 0;
383+
int visible_lines = 0;
374384

375385
// Get number of lines to fit to the height.
376386
for (int64_t i = lines_skipped; i < lines_rid.size(); i++) {
377387
total_h += TS->shaped_text_get_size(lines_rid[i]).y + line_spacing;
378388
if (total_h > (get_size().height - style->get_minimum_size().height + line_spacing)) {
379389
break;
380390
}
381-
lines_visible++;
391+
visible_lines++;
382392
}
383393

384-
if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
385-
lines_visible = max_lines_visible;
394+
if (max_lines_visible >= 0 && visible_lines > max_lines_visible) {
395+
visible_lines = max_lines_visible;
386396
}
387397

388-
int last_line = MIN(lines_rid.size(), lines_visible + lines_skipped);
398+
int last_line = MIN(lines_rid.size(), visible_lines + lines_skipped);
389399
bool trim_chars = (visible_chars >= 0) && (visible_chars_behavior == TextServer::VC_CHARS_AFTER_SHAPING);
390400
bool trim_glyphs_ltr = (visible_chars >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_LTR) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && !rtl_layout));
391401
bool trim_glyphs_rtl = (visible_chars >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_RTL) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && rtl_layout));
@@ -402,7 +412,7 @@ void Label::_notification(int p_what) {
402412
total_h += style->get_margin(SIDE_TOP) + style->get_margin(SIDE_BOTTOM);
403413

404414
int vbegin = 0, vsep = 0;
405-
if (lines_visible > 0) {
415+
if (visible_lines > 0) {
406416
switch (vertical_alignment) {
407417
case VERTICAL_ALIGNMENT_TOP: {
408418
// Nothing.
@@ -419,8 +429,8 @@ void Label::_notification(int p_what) {
419429
} break;
420430
case VERTICAL_ALIGNMENT_FILL: {
421431
vbegin = 0;
422-
if (lines_visible > 1) {
423-
vsep = (size.y - (total_h - line_spacing)) / (lines_visible - 1);
432+
if (visible_lines > 1) {
433+
vsep = (size.y - (total_h - line_spacing)) / (visible_lines - 1);
424434
} else {
425435
vsep = 0;
426436
}
@@ -597,13 +607,7 @@ void Label::_notification(int p_what) {
597607
} break;
598608

599609
case NOTIFICATION_RESIZED: {
600-
// It may happen that the reshaping due to this size change triggers a cascade of re-layout
601-
// across the hierarchy where this label belongs to in a way that its size changes multiple
602-
// times, but ending up with the original size it was already shaped for.
603-
// This check prevents the catastrophic, freezing infinite cascade of re-layout.
604-
if (lines_shaped_last_width != get_size().width) {
605-
lines_dirty = true;
606-
}
610+
lines_dirty = true;
607611
} break;
608612
}
609613
}
@@ -646,25 +650,25 @@ int Label::get_line_count() const {
646650
int Label::get_visible_line_count() const {
647651
Ref<StyleBox> style = theme_cache.normal_style;
648652
int line_spacing = settings.is_valid() ? settings->get_line_spacing() : theme_cache.line_spacing;
649-
int lines_visible = 0;
653+
int visible_lines = 0;
650654
float total_h = 0.0;
651655
for (int64_t i = lines_skipped; i < lines_rid.size(); i++) {
652656
total_h += TS->shaped_text_get_size(lines_rid[i]).y + line_spacing;
653657
if (total_h > (get_size().height - style->get_minimum_size().height + line_spacing)) {
654658
break;
655659
}
656-
lines_visible++;
660+
visible_lines++;
657661
}
658662

659-
if (lines_visible > lines_rid.size()) {
660-
lines_visible = lines_rid.size();
663+
if (visible_lines > lines_rid.size()) {
664+
visible_lines = lines_rid.size();
661665
}
662666

663-
if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
664-
lines_visible = max_lines_visible;
667+
if (max_lines_visible >= 0 && visible_lines > max_lines_visible) {
668+
visible_lines = max_lines_visible;
665669
}
666670

667-
return lines_visible;
671+
return visible_lines;
668672
}
669673

670674
void Label::set_horizontal_alignment(HorizontalAlignment p_alignment) {
@@ -886,7 +890,7 @@ void Label::set_lines_skipped(int p_lines) {
886890
}
887891

888892
lines_skipped = p_lines;
889-
_update_visible();
893+
lines_dirty = true;
890894
queue_redraw();
891895
}
892896

@@ -900,7 +904,7 @@ void Label::set_max_lines_visible(int p_lines) {
900904
}
901905

902906
max_lines_visible = p_lines;
903-
_update_visible();
907+
lines_dirty = true;
904908
queue_redraw();
905909
}
906910

@@ -949,7 +953,7 @@ void Label::_bind_methods() {
949953
ClassDB::bind_method(D_METHOD("get_visible_ratio"), &Label::get_visible_ratio);
950954
ClassDB::bind_method(D_METHOD("set_lines_skipped", "lines_skipped"), &Label::set_lines_skipped);
951955
ClassDB::bind_method(D_METHOD("get_lines_skipped"), &Label::get_lines_skipped);
952-
ClassDB::bind_method(D_METHOD("set_max_lines_visible", "lines_visible"), &Label::set_max_lines_visible);
956+
ClassDB::bind_method(D_METHOD("set_max_lines_visible", "visible_lines"), &Label::set_max_lines_visible);
953957
ClassDB::bind_method(D_METHOD("get_max_lines_visible"), &Label::get_max_lines_visible);
954958
ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &Label::set_structured_text_bidi_override);
955959
ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &Label::get_structured_text_bidi_override);

scene/gui/label.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ class Label : public Control {
4646
bool clip = false;
4747
TextServer::OverrunBehavior overrun_behavior = TextServer::OVERRUN_NO_TRIMMING;
4848
Size2 minsize;
49+
real_t stable_width = -1;
4950
bool uppercase = false;
5051

5152
bool lines_dirty = true;
52-
int lines_shaped_last_width = -1;
5353

5454
bool dirty = true;
5555
bool font_dirty = true;
@@ -83,7 +83,6 @@ class Label : public Control {
8383
int font_shadow_outline_size;
8484
} theme_cache;
8585

86-
void _update_visible();
8786
void _shape();
8887
void _invalidate();
8988

0 commit comments

Comments
 (0)