From 15a156595bc9ff66d9745db9b82108aab6e4e617 Mon Sep 17 00:00:00 2001 From: iris <2+iris@noreply.localhost> Date: Sun, 13 Sep 2026 03:11:51 -0400 Subject: [PATCH] Borrow cached rendered text instead of cloning it --- core/src/primitive/text.rs | 12 +++++------ core/src/render/atlas.rs | 10 ++++----- core/src/render/primitive.rs | 4 ---- src/widget/text/mod.rs | 41 +++++++++++++++++------------------- 4 files changed, 29 insertions(+), 38 deletions(-) diff --git a/core/src/primitive/text.rs b/core/src/primitive/text.rs index 0770121..a077ada 100644 --- a/core/src/primitive/text.rs +++ b/core/src/primitive/text.rs @@ -185,7 +185,7 @@ impl TextData { subpixel, coords: coords_hash, }; - let entry = self.glyph_entry( + let Some(entry) = self.glyph_entry( GlyphRaster { key, font: font_ref, @@ -195,8 +195,9 @@ impl TextData { glyph_id: glyph.id, }, textures, - ); - let Some(entry) = entry else { continue }; + ) else { + continue; + }; placed.push(PlacedGlyph { entry, offset: Vec2::new( @@ -265,9 +266,8 @@ fn glyph_size_key(font_size: f32) -> u32 { (font_size * GLYPH_SIZE_STEPS_PER_PIXEL).round() as u32 } -#[derive(Clone)] pub struct RenderedText { - pub glyphs: std::sync::Arc>, + pub glyphs: Vec, pub size: Vec2, pub color: UiColor, } @@ -283,7 +283,7 @@ impl TextData { buffer.shape(self, attrs, width); let glyphs = self.place(buffer, textures); RenderedText { - glyphs: std::sync::Arc::new(glyphs), + glyphs, size: buffer.size(), color: attrs.color, } diff --git a/core/src/render/atlas.rs b/core/src/render/atlas.rs index 3315c56..a193427 100644 --- a/core/src/render/atlas.rs +++ b/core/src/render/atlas.rs @@ -1,5 +1,5 @@ use crate::{ - GlyphPrimitive, PatchRect, TextureHandle, Textures, + PatchRect, TextureHandle, Textures, util::{HashMap, Vec2}, }; use image::RgbaImage; @@ -40,12 +40,10 @@ pub struct GlyphEntry { } impl GlyphEntry { + const IS_COLORED: u32 = 1; + pub(crate) fn flags(&self) -> u32 { - if self.is_colored { - GlyphPrimitive::IS_COLORED - } else { - 0 - } + if self.is_colored { Self::IS_COLORED } else { 0 } } } diff --git a/core/src/render/primitive.rs b/core/src/render/primitive.rs index 4d77a79..849cbe0 100644 --- a/core/src/render/primitive.rs +++ b/core/src/render/primitive.rs @@ -243,10 +243,6 @@ pub struct GlyphPrimitive { pub flags: u32, } -impl GlyphPrimitive { - pub const IS_COLORED: u32 = 1; -} - pub struct PrimitiveVec { vec: Vec, free: Vec, diff --git a/src/widget/text/mod.rs b/src/widget/text/mod.rs index 15bc8ce..1aec47e 100644 --- a/src/widget/text/mod.rs +++ b/src/widget/text/mod.rs @@ -52,25 +52,19 @@ impl TextView { .align(self.align) } - fn render(&mut self, ctx: &mut SizeCtx) -> RenderedText { + fn render(&mut self, ctx: &mut SizeCtx) -> &RenderedText { let width = if self.attrs.wrap { Some(ctx.px_size().x) } else { None }; - if width == self.width - && let Some(tex) = &self.tex - && !self.attrs.changed - && !self.buf.changed - { - return tex.clone(); + if width != self.width || self.tex.is_none() || self.attrs.changed || self.buf.changed { + self.width = width; + self.tex = Some(ctx.draw_text(&mut self.buf, &self.attrs, width)); + self.attrs.changed = false; + self.buf.changed = false; } - self.width = width; - let tex = ctx.draw_text(&mut self.buf, &self.attrs, width); - self.tex = Some(tex.clone()); - self.attrs.changed = false; - self.buf.changed = false; - tex + self.tex.as_ref().unwrap() } pub fn tex(&self) -> Option<&RenderedText> { self.tex.as_ref() @@ -94,16 +88,19 @@ impl TextView { } } pub fn draw(&mut self, painter: &mut Painter) -> UiRegion { - let tex = self.render(&mut painter.size_ctx()); - let region = tex.size.align(self.align); - if self.is_empty() - && let Some(hint) = &self.hint - { - painter.widget(hint); - } else { - let within = region.within(&painter.region()); - painter.glyphs(&tex, within); + let align = self.align; + if self.is_empty() && self.hint.is_some() { + let region = self.render(&mut painter.size_ctx()).size.align(align); + if let Some(hint) = &self.hint { + painter.widget(hint); + } + return region; } + + let tex = self.render(&mut painter.size_ctx()); + let region = tex.size.align(align); + let within = region.within(&painter.region()); + painter.glyphs(tex, within); region }