Repository: godotengine/godot (language: cpp)

Issue:
In Right-to-Left, the SceneTree Item text is displayed incorrectly.
### Tested versions

 Reproducible in 4.5dev.
Not  reproducible in 4.4stable.
The bug is trggered after #103424

### System information

Windows 11 Intel(R) Core(TM) i7-11800H NVIDIA RTX3050

### Issue description

In Right-to-Left, if you narrow the SceneTree, the SceneTree Item text is displayed incorrectly.
Left-to-Right - correctly:

![Image](https://github.com/user-attachments/assets/4a6b6541-80c9-49c5-a3f9-851b3ee68cb3)
Right-to-Left - incorrectly:

![Image](https://github.com/user-attachments/assets/93f6f106-6261-466f-9399-6cceb148481b)

### Steps to reproduce

1. Create a new project.
2. Add multi-level child items to the SceneTree.
3. Narrow the SceneTree.

### Minimal reproduction project (MRP)

N/A


Discussion / hints:

Fix patch:
diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp
index 45984dc89a20..80bc10e733da 100644
--- a/scene/gui/tree.cpp
+++ b/scene/gui/tree.cpp
@@ -1945,54 +1945,59 @@ void Tree::draw_item_rect(TreeItem::Cell &p_cell, const Rect2i &p_rect, const Co
 	Size2 ts = p_cell.text_buf->get_size();
 	bool rtl = is_layout_rtl();
 
-	int w = 0;
-	Size2i bmsize;
+	Size2i icon_size;
 	if (p_cell.icon.is_valid()) {
-		bmsize = _get_cell_icon_size(p_cell);
-		w += bmsize.width + theme_cache.h_separation;
-		if (rect.size.width > 0 && (w + ts.width) > rect.size.width) {
-			ts.width = rect.size.width - w;
-		}
+		icon_size = _get_cell_icon_size(p_cell);
 	}
-	w += ts.width;
+
+	int displayed_width = 0;
+	if (p_cell.icon.is_valid()) {
+		displayed_width += icon_size.width + theme_cache.h_separation;
+	}
+	if (displayed_width + ts.width > rect.size.width) {
+		ts.width = rect.size.width - displayed_width;
+	}
+	displayed_width += ts.width;
+
+	int empty_width = rect.size.width - displayed_width;
 
 	switch (p_cell.text_alignment) {
 		case HORIZONTAL_ALIGNMENT_FILL:
 		case HORIZONTAL_ALIGNMENT_LEFT: {
 			if (rtl) {
-				rect.position.x += MAX(0, (rect.size.width - w));
+				rect.position.x += empty_width;
 			}
 		} break;
 		case HORIZONTAL_ALIGNMENT_CENTER:
-			rect.position.x += MAX(0, (rect.size.width - w) / 2);
+			rect.position.x += empty_width / 2;
 			break;
 		case HORIZONTAL_ALIGNMENT_RIGHT:
 			if (!rtl) {
-				rect.position.x += MAX(0, (rect.size.width - w));
+				rect.position.x += empty_width;
 			}
 			break;
 	}
 
 	RID ci = get_canvas_item();
 
-	if (rtl && rect.size.width > 0) {
-		Point2 draw_pos = rect.position;
-		draw_pos.y += Math::floor((rect.size.y - p_cell.text_buf->get_size().y) * 0.5);
-		if (p_ol_size > 0 && p_ol_color.a > 0) {
-			p_cell.text_buf->draw_outline(ci, draw_pos, p_ol_size, p_ol_color);
+	if (rtl) {
+		if (ts.width > 0) {
+			Point2 draw_pos = rect.position;
+			draw_pos.y += Math::floor((rect.size.y - p_cell.text_buf->get_size().y) * 0.5);
+			if (p_ol_size > 0 && p_ol_color.a > 0) {
+				p_cell.text_buf->draw_outline(ci, draw_pos, p_ol_size, p_ol_color);
+			}
+			p_cell.text_buf->draw(ci, draw_pos, p_color);
 		}
-		p_cell.text_buf->draw(ci, draw_pos, p_color);
 		rect.position.x += ts.width + theme_cache.h_separation;
-		rect.size.x -= ts.width + theme_cache.h_separation;
 	}
 
 	if (p_cell.icon.is_valid()) {
-		p_cell.draw_icon(ci, rect.position + Size2i(0, Math::floor((real_t)(rect.size.y - bmsize.y) / 2)), bmsize, p_icon_color);
-		rect.position.x += bmsize.x + theme_cache.h_separation;
-		rect.size.x -= bmsize.x + theme_cache.h_separation;
+		p_cell.draw_icon(ci, rect.position + Size2i(0, Math::floor((real_t)(rect.size.y - icon_size.y) / 2)), icon_size, p_icon_color);
+		rect.position.x += icon_size.x + theme_cache.h_separation;
 	}
 
-	if (!rtl && rect.size.width > 0) {
+	if (!rtl && ts.width > 0) {
 		Point2 draw_pos = rect.position;
 		draw_pos.y += Math::floor((rect.size.y - p_cell.text_buf->get_size().y) * 0.5);
 		if (p_ol_size > 0 && p_ol_color.a > 0) {


Test patch: