raylib: fix word wrap (#36545)

* fix word wrap underestimating width

* and that
pull/36547/head
Shane Smiskol 3 days ago committed by GitHub
parent c7b115b68e
commit 525b6e48e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 15
      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] = [] lines: list[str] = []
current_line: list[str] = [] current_line: list[str] = []
current_width = 0
space_width = int(measure_text_cached(font, " ", font_size).x)
for word in words: for word in words:
word_width = int(measure_text_cached(font, word, font_size).x) 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: if current_line:
lines.append(" ".join(current_line)) lines.append(" ".join(current_line))
current_line = [] current_line = []
current_width = 0
# Break the long word into parts # Break the long word into parts
lines.extend(_break_long_word(font, word, font_size, max_width)) lines.extend(_break_long_word(font, word, font_size, max_width))
continue continue
# Calculate width if we add this word # Measure the actual joined string to get accurate width (accounts for kerning, etc.)
needed_width = current_width test_line = " ".join(current_line + [word]) if current_line else word
if current_line: # Need space before word test_width = int(measure_text_cached(font, test_line, font_size).x)
needed_width += space_width
needed_width += word_width
# Check if word fits on current line # Check if word fits on current line
if needed_width <= max_width: if test_width <= max_width:
current_line.append(word) current_line.append(word)
current_width = needed_width
else: else:
# Start new line with this word # Start new line with this word
if current_line: if current_line:
lines.append(" ".join(current_line)) lines.append(" ".join(current_line))
current_line = [word] current_line = [word]
current_width = word_width
# Add remaining words # Add remaining words
if current_line: if current_line:

Loading…
Cancel
Save