Borrow cached rendered text instead of cloning it
This commit is contained in:
1 parent
dc008441e4
commit
15a156595b
4 files changed
+29
-38
No files matched your search
@@ -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<Vec<PlacedGlyph>>,
|
||||
pub glyphs: Vec<PlacedGlyph>,
|
||||
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,
|
||||
}
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -243,10 +243,6 @@ pub struct GlyphPrimitive {
|
||||
pub flags: u32,
|
||||
}
|
||||
|
||||
impl GlyphPrimitive {
|
||||
pub const IS_COLORED: u32 = 1;
|
||||
}
|
||||
|
||||
pub struct PrimitiveVec<T> {
|
||||
vec: Vec<T>,
|
||||
free: Vec<usize>,
|
||||
|
||||
+19
-22
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user