From 12b3d0e08dbb10318186cd37b833c40dbb98749f Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Fri, 3 Oct 2025 20:52:50 -0700 Subject: [PATCH] raylib: cache wrap text (#36258) * cache html height * clean up * todo --- system/ui/lib/wrap_text.py | 8 ++++++++ system/ui/widgets/html_render.py | 1 + 2 files changed, 9 insertions(+) diff --git a/system/ui/lib/wrap_text.py b/system/ui/lib/wrap_text.py index 35dc5ac401..f6caa3c5f0 100644 --- a/system/ui/lib/wrap_text.py +++ b/system/ui/lib/wrap_text.py @@ -36,7 +36,14 @@ def _break_long_word(font: rl.Font, word: str, font_size: int, max_width: int) - return parts +_cache: dict[int, list[str]] = {} + + def wrap_text(font: rl.Font, text: str, font_size: int, max_width: int) -> list[str]: + key = hash((font.texture.id, text, font_size, max_width)) + if key in _cache: + return _cache[key] + if not text or max_width <= 0: return [] @@ -100,4 +107,5 @@ def wrap_text(font: rl.Font, text: str, font_size: int, max_width: int) -> list[ # Add all lines from this paragraph all_lines.extend(lines) + _cache[key] = all_lines return all_lines diff --git a/system/ui/widgets/html_render.py b/system/ui/widgets/html_render.py index b032df4d9f..f7dc9e9344 100644 --- a/system/ui/widgets/html_render.py +++ b/system/ui/widgets/html_render.py @@ -155,6 +155,7 @@ class HtmlRenderer(Widget): self.elements.append(element) def _render(self, rect: rl.Rectangle): + # TODO: speed up by removing duplicate calculations across renders current_y = rect.y padding = 20 content_width = rect.width - (padding * 2)