From 525b6e48e99a00f95cf5fe12fce71c9434219583 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Sun, 2 Nov 2025 04:32:29 -0800 Subject: [PATCH] raylib: fix word wrap (#36545) * fix word wrap underestimating width * and that --- system/ui/lib/wrap_text.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/system/ui/lib/wrap_text.py b/system/ui/lib/wrap_text.py index d51975087a..745d37b468 100644 --- a/system/ui/lib/wrap_text.py +++ b/system/ui/lib/wrap_text.py @@ -67,8 +67,6 @@ def wrap_text(font: rl.Font, text: str, font_size: int, max_width: int) -> list[ lines: list[str] = [] current_line: list[str] = [] - current_width = 0 - space_width = int(measure_text_cached(font, " ", font_size).x) for word in words: word_width = int(measure_text_cached(font, word, font_size).x) @@ -79,28 +77,23 @@ def wrap_text(font: rl.Font, text: str, font_size: int, max_width: int) -> list[ if current_line: lines.append(" ".join(current_line)) current_line = [] - current_width = 0 # Break the long word into parts lines.extend(_break_long_word(font, word, font_size, max_width)) continue - # Calculate width if we add this word - needed_width = current_width - if current_line: # Need space before word - needed_width += space_width - needed_width += word_width + # Measure the actual joined string to get accurate width (accounts for kerning, etc.) + test_line = " ".join(current_line + [word]) if current_line else word + test_width = int(measure_text_cached(font, test_line, font_size).x) # Check if word fits on current line - if needed_width <= max_width: + if test_width <= max_width: current_line.append(word) - current_width = needed_width else: # Start new line with this word if current_line: lines.append(" ".join(current_line)) current_line = [word] - current_width = word_width # Add remaining words if current_line: