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,
|
subpixel,
|
||||||
coords: coords_hash,
|
coords: coords_hash,
|
||||||
};
|
};
|
||||||
let entry = self.glyph_entry(
|
let Some(entry) = self.glyph_entry(
|
||||||
GlyphRaster {
|
GlyphRaster {
|
||||||
key,
|
key,
|
||||||
font: font_ref,
|
font: font_ref,
|
||||||
@@ -195,8 +195,9 @@ impl TextData {
|
|||||||
glyph_id: glyph.id,
|
glyph_id: glyph.id,
|
||||||
},
|
},
|
||||||
textures,
|
textures,
|
||||||
);
|
) else {
|
||||||
let Some(entry) = entry else { continue };
|
continue;
|
||||||
|
};
|
||||||
placed.push(PlacedGlyph {
|
placed.push(PlacedGlyph {
|
||||||
entry,
|
entry,
|
||||||
offset: Vec2::new(
|
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
|
(font_size * GLYPH_SIZE_STEPS_PER_PIXEL).round() as u32
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct RenderedText {
|
pub struct RenderedText {
|
||||||
pub glyphs: std::sync::Arc<Vec<PlacedGlyph>>,
|
pub glyphs: Vec<PlacedGlyph>,
|
||||||
pub size: Vec2,
|
pub size: Vec2,
|
||||||
pub color: UiColor,
|
pub color: UiColor,
|
||||||
}
|
}
|
||||||
@@ -283,7 +283,7 @@ impl TextData {
|
|||||||
buffer.shape(self, attrs, width);
|
buffer.shape(self, attrs, width);
|
||||||
let glyphs = self.place(buffer, textures);
|
let glyphs = self.place(buffer, textures);
|
||||||
RenderedText {
|
RenderedText {
|
||||||
glyphs: std::sync::Arc::new(glyphs),
|
glyphs,
|
||||||
size: buffer.size(),
|
size: buffer.size(),
|
||||||
color: attrs.color,
|
color: attrs.color,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
GlyphPrimitive, PatchRect, TextureHandle, Textures,
|
PatchRect, TextureHandle, Textures,
|
||||||
util::{HashMap, Vec2},
|
util::{HashMap, Vec2},
|
||||||
};
|
};
|
||||||
use image::RgbaImage;
|
use image::RgbaImage;
|
||||||
@@ -40,12 +40,10 @@ pub struct GlyphEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl GlyphEntry {
|
impl GlyphEntry {
|
||||||
|
const IS_COLORED: u32 = 1;
|
||||||
|
|
||||||
pub(crate) fn flags(&self) -> u32 {
|
pub(crate) fn flags(&self) -> u32 {
|
||||||
if self.is_colored {
|
if self.is_colored { Self::IS_COLORED } else { 0 }
|
||||||
GlyphPrimitive::IS_COLORED
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -243,10 +243,6 @@ pub struct GlyphPrimitive {
|
|||||||
pub flags: u32,
|
pub flags: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GlyphPrimitive {
|
|
||||||
pub const IS_COLORED: u32 = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct PrimitiveVec<T> {
|
pub struct PrimitiveVec<T> {
|
||||||
vec: Vec<T>,
|
vec: Vec<T>,
|
||||||
free: Vec<usize>,
|
free: Vec<usize>,
|
||||||
|
|||||||
+19
-22
@@ -52,25 +52,19 @@ impl TextView {
|
|||||||
.align(self.align)
|
.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 {
|
let width = if self.attrs.wrap {
|
||||||
Some(ctx.px_size().x)
|
Some(ctx.px_size().x)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
if width == self.width
|
if width != self.width || self.tex.is_none() || self.attrs.changed || self.buf.changed {
|
||||||
&& let Some(tex) = &self.tex
|
self.width = width;
|
||||||
&& !self.attrs.changed
|
self.tex = Some(ctx.draw_text(&mut self.buf, &self.attrs, width));
|
||||||
&& !self.buf.changed
|
self.attrs.changed = false;
|
||||||
{
|
self.buf.changed = false;
|
||||||
return tex.clone();
|
|
||||||
}
|
}
|
||||||
self.width = width;
|
self.tex.as_ref().unwrap()
|
||||||
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
|
|
||||||
}
|
}
|
||||||
pub fn tex(&self) -> Option<&RenderedText> {
|
pub fn tex(&self) -> Option<&RenderedText> {
|
||||||
self.tex.as_ref()
|
self.tex.as_ref()
|
||||||
@@ -94,16 +88,19 @@ impl TextView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn draw(&mut self, painter: &mut Painter) -> UiRegion {
|
pub fn draw(&mut self, painter: &mut Painter) -> UiRegion {
|
||||||
let tex = self.render(&mut painter.size_ctx());
|
let align = self.align;
|
||||||
let region = tex.size.align(self.align);
|
if self.is_empty() && self.hint.is_some() {
|
||||||
if self.is_empty()
|
let region = self.render(&mut painter.size_ctx()).size.align(align);
|
||||||
&& let Some(hint) = &self.hint
|
if let Some(hint) = &self.hint {
|
||||||
{
|
painter.widget(hint);
|
||||||
painter.widget(hint);
|
}
|
||||||
} else {
|
return region;
|
||||||
let within = region.within(&painter.region());
|
|
||||||
painter.glyphs(&tex, within);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
region
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user