Skip to content

Commit c2d81b0

Browse files
committed
Merge pull request #96265 from bruvzg/rtl_dc4
Account for dropcap when calculating paragraph size, fix duplicate last char when using dropcap with autowrap off.
2 parents 5abb206 + fc2fae2 commit c2d81b0

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

scene/resources/text_paragraph.cpp

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ void TextParagraph::_shape_lines() {
173173
v_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
174174
}
175175

176+
Size2i range = TS->shaped_text_get_range(rid);
176177
if (h_offset > 0) {
177178
// Dropcap, flow around.
178179
PackedInt32Array line_breaks = TS->shaped_text_get_line_breaks(rid, width - h_offset, 0, brk_flags);
@@ -182,7 +183,7 @@ void TextParagraph::_shape_lines() {
182183
if (!tab_stops.is_empty()) {
183184
TS->shaped_text_tab_align(line, tab_stops);
184185
}
185-
start = line_breaks[i + 1];
186+
start = (i < line_breaks.size() - 2) ? line_breaks[i + 2] : range.y;
186187
lines_rid.push_back(line);
187188
if (v_offset < h) {
188189
break;
@@ -192,13 +193,15 @@ void TextParagraph::_shape_lines() {
192193
}
193194
}
194195
// Use fixed for the rest of lines.
195-
PackedInt32Array line_breaks = TS->shaped_text_get_line_breaks(rid, width, start, brk_flags);
196-
for (int i = 0; i < line_breaks.size(); i = i + 2) {
197-
RID line = TS->shaped_text_substr(rid, line_breaks[i], line_breaks[i + 1] - line_breaks[i]);
198-
if (!tab_stops.is_empty()) {
199-
TS->shaped_text_tab_align(line, tab_stops);
196+
if (start == 0 || start < range.y) {
197+
PackedInt32Array line_breaks = TS->shaped_text_get_line_breaks(rid, width, start, brk_flags);
198+
for (int i = 0; i < line_breaks.size(); i = i + 2) {
199+
RID line = TS->shaped_text_substr(rid, line_breaks[i], line_breaks[i + 1] - line_breaks[i]);
200+
if (!tab_stops.is_empty()) {
201+
TS->shaped_text_tab_align(line, tab_stops);
202+
}
203+
lines_rid.push_back(line);
200204
}
201-
lines_rid.push_back(line);
202205
}
203206

204207
BitField<TextServer::TextOverrunFlag> overrun_flags = TextServer::OVERRUN_NO_TRIM;
@@ -550,18 +553,42 @@ Size2 TextParagraph::get_size() const {
550553
_THREAD_SAFE_METHOD_
551554

552555
const_cast<TextParagraph *>(this)->_shape_lines();
556+
557+
float h_offset = 0.f;
558+
float v_offset = 0.f;
559+
if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
560+
h_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
561+
v_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
562+
} else {
563+
h_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
564+
v_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
565+
}
566+
553567
Size2 size;
554568
int visible_lines = (max_lines_visible >= 0) ? MIN(max_lines_visible, (int)lines_rid.size()) : (int)lines_rid.size();
555569
for (int i = 0; i < visible_lines; i++) {
556570
Size2 lsize = TS->shaped_text_get_size(lines_rid[i]);
557571
if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
572+
if (h_offset > 0 && i <= dropcap_lines) {
573+
lsize.x += h_offset;
574+
}
558575
size.x = MAX(size.x, lsize.x);
559576
size.y += lsize.y;
560577
} else {
578+
if (h_offset > 0 && i <= dropcap_lines) {
579+
lsize.y += h_offset;
580+
}
561581
size.x += lsize.x;
562582
size.y = MAX(size.y, lsize.y);
563583
}
564584
}
585+
if (h_offset > 0) {
586+
if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
587+
size.y = MAX(size.y, v_offset);
588+
} else {
589+
size.x = MAX(size.x, v_offset);
590+
}
591+
}
565592
return size;
566593
}
567594

@@ -624,7 +651,7 @@ Rect2 TextParagraph::get_line_object_rect(int p_line, Variant p_key) const {
624651
ofs.x += TS->shaped_text_get_ascent(lines_rid[i]);
625652
if (i <= dropcap_lines) {
626653
if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_LTR) {
627-
ofs.x -= h_offset;
654+
ofs.y -= h_offset;
628655
}
629656
l_width -= h_offset;
630657
}
@@ -793,7 +820,7 @@ void TextParagraph::draw(RID p_canvas, const Vector2 &p_pos, const Color &p_colo
793820
ofs.x += TS->shaped_text_get_ascent(lines_rid[i]);
794821
if (i <= dropcap_lines) {
795822
if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_LTR) {
796-
ofs.x -= h_offset;
823+
ofs.y -= h_offset;
797824
}
798825
l_width -= h_offset;
799826
}
@@ -895,7 +922,7 @@ void TextParagraph::draw_outline(RID p_canvas, const Vector2 &p_pos, int p_outli
895922
ofs.x += TS->shaped_text_get_ascent(lines_rid[i]);
896923
if (i <= dropcap_lines) {
897924
if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_LTR) {
898-
ofs.x -= h_offset;
925+
ofs.y -= h_offset;
899926
}
900927
l_width -= h_offset;
901928
}

0 commit comments

Comments
 (0)