Repository: godotengine/godot (language: cpp)

Issue:
In a RichTextLabel, you can't see highlighted text when a background color wraps lines
### Tested versions

- Reproducible in v4.4.dev5.official [9e6098432] 
- Reproducible in 4.3-stable 


### System information

Godot v4.4.dev5 - Windows 10.0.26100 - Multi-window, 1 monitor - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 4090 (NVIDIA; 32.0.15.6614) - 13th Gen Intel(R) Core(TM) i9-13900K (32 threads)

### Issue description

If you have a `RichTextLabel` which uses BBCode and has a `[bgcolor]` tag, you cannot always see your text selection.

In particular, if the `[bgcolor]` spans over a line break, you cannot see your selection at all.

For example with this control:

![Image](https://github.com/user-attachments/assets/4c41249b-2e40-4c78-a79d-79cbe9bb19d9)

If I highlight from before the end of the line the red text is not highlighted. (The additional red on the second like does get highlighted, though.)

![Image](https://github.com/user-attachments/assets/7555d8bf-1579-4a99-813d-11ad08a0d383)

Here is an example where the `[bgcolor]` is mid sentence and it works fine:

![Image](https://github.com/user-attachments/assets/45d56f26-dab6-44f8-9e0f-d740c88347b6)


### Steps to reproduce

I have included a MRP that demonstrates the issue. Simply start it and try to select text in the second line that goes beyond the wrap.



### Minimal reproduction project (MRP)

[highlight-bg-mrp.zip](https://github.com/user-attachments/files/18030106/highlight-bg-mrp.zip)



Discussion / hints:

Fix patch:
diff --git a/scene/gui/rich_text_label.cpp b/scene/gui/rich_text_label.cpp
index ea0b8cd80864..be54ef7c94ed 100644
--- a/scene/gui/rich_text_label.cpp
+++ b/scene/gui/rich_text_label.cpp
@@ -1370,6 +1370,13 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
 				}
 			}
 			// Finish lines and boxes.
+			if (step == DRAW_STEP_BACKGROUND || step == DRAW_STEP_FOREGROUND) {
+				if (last_color.a > 0.0) {
+					Vector2 rect_off = p_ofs + Vector2(box_start - theme_cache.text_highlight_h_padding, off_step.y - l_ascent - theme_cache.text_highlight_v_padding);
+					Vector2 rect_size = Vector2(off_step.x - box_start + 2 * theme_cache.text_highlight_h_padding, l_size.y + 2 * theme_cache.text_highlight_v_padding);
+					RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(rect_off, rect_size), last_color);
+				}
+			}
 			if (step == DRAW_STEP_BACKGROUND) {
 				if (sel_start != -1) {
 					Color selection_bg = theme_cache.selection_color;
@@ -1380,13 +1387,6 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
 					}
 				}
 			}
-			if (step == DRAW_STEP_BACKGROUND || step == DRAW_STEP_FOREGROUND) {
-				if (last_color.a > 0.0) {
-					Vector2 rect_off = p_ofs + Vector2(box_start - theme_cache.text_highlight_h_padding, off_step.y - l_ascent - theme_cache.text_highlight_v_padding);
-					Vector2 rect_size = Vector2(off_step.x - box_start + 2 * theme_cache.text_highlight_h_padding, l_size.y + 2 * theme_cache.text_highlight_v_padding);
-					RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(rect_off, rect_size), last_color);
-				}
-			}
 			if (step == DRAW_STEP_TEXT) {
 				if (ul_started) {
 					ul_started = false;


Test patch: