Skip to content
Snippets Groups Projects
Commit 3bba71a3 authored by Gino Valente's avatar Gino Valente
Browse files

Cached TextLayout in RenderPrimitive

Avoids having to run the calculation every frame when extracting.
parent b2f1c98d
No related branches found
No related tags found
No related merge requests found
...@@ -21,51 +21,32 @@ pub fn extract_texts( ...@@ -21,51 +21,32 @@ pub fn extract_texts(
_dpi: f32, _dpi: f32,
) -> Vec<ExtractQuadBundle> { ) -> Vec<ExtractQuadBundle> {
let mut extracted_texts = Vec::new(); let mut extracted_texts = Vec::new();
let (background_color, layout, font_size, content, font, parent_size, line_height) = let (background_color, text_layout, layout, font, properties) =
match render_primitive { match render_primitive {
RenderPrimitive::Text { RenderPrimitive::Text {
color, color,
text_layout,
layout, layout,
size,
content,
font, font,
parent_size, properties,
line_height, ..
} => ( } => (
color, color,
text_layout,
layout, layout,
*size,
content,
font, font,
parent_size, *properties,
line_height,
), ),
_ => panic!(""), _ => panic!(""),
}; };
let font_handle = font_mapping.get_handle(font.clone()).unwrap(); let font_handle = font_mapping.get_handle(font.clone()).unwrap();
let font = fonts.get(font_handle.clone()); let font = match fonts.get(font_handle.clone()) {
Some(font) => font,
if font.is_none() { None => return Vec::new()
return vec![];
}
let font = font.unwrap();
let properties = TextProperties {
alignment: Alignment::Start,
font_size,
line_height: *line_height,
max_size: (parent_size.0, parent_size.1),
..Default::default()
}; };
let text_layout = font.measure( let base_position = Vec2::new(layout.posx, layout.posy + properties.font_size);
content,
properties,
);
let base_position = Vec2::new(layout.posx, layout.posy + font_size);
for glyph_rect in text_layout.glyphs() { for glyph_rect in text_layout.glyphs() {
let mut position = Vec2::from(glyph_rect.position); let mut position = Vec2::from(glyph_rect.position);
......
use morphorm::layout;
use kayak_font::{TextLayout, TextProperties};
use crate::{ use crate::{
color::Color, color::Color,
layout_cache::Rect, layout_cache::Rect,
...@@ -22,10 +24,9 @@ pub enum RenderPrimitive { ...@@ -22,10 +24,9 @@ pub enum RenderPrimitive {
color: Color, color: Color,
content: String, content: String,
font: String, font: String,
text_layout: TextLayout,
layout: Rect, layout: Rect,
line_height: f32, properties: TextProperties,
parent_size: (f32, f32),
size: f32,
}, },
Image { Image {
border_radius: Corner<f32>, border_radius: Corner<f32>,
...@@ -85,10 +86,13 @@ impl From<&Style> for RenderPrimitive { ...@@ -85,10 +86,13 @@ impl From<&Style> for RenderPrimitive {
color: style.color.resolve(), color: style.color.resolve(),
content, content,
font, font,
text_layout: TextLayout::default(),
layout: Rect::default(), layout: Rect::default(),
line_height, properties: TextProperties {
parent_size: (0.0, 0.0), font_size,
size: font_size, line_height,
..Default::default()
},
}, },
RenderCommand::Image { handle } => Self::Image { RenderCommand::Image { handle } => Self::Image {
border_radius: style.border_radius.resolve(), border_radius: style.border_radius.resolve(),
......
...@@ -293,11 +293,10 @@ impl WidgetManager { ...@@ -293,11 +293,10 @@ impl WidgetManager {
match &mut render_primitive { match &mut render_primitive {
RenderPrimitive::Text { RenderPrimitive::Text {
parent_size,
content, content,
font, font,
size, properties,
line_height, text_layout,
.. ..
} => { } => {
// --- Bind to Font Asset --- // // --- Bind to Font Asset --- //
...@@ -307,18 +306,11 @@ impl WidgetManager { ...@@ -307,18 +306,11 @@ impl WidgetManager {
if let Some(font) = asset.get() { if let Some(font) = asset.get() {
if let Some(parent_id) = self.get_valid_parent(id) { if let Some(parent_id) = self.get_valid_parent(id) {
if let Some(parent_layout) = self.get_layout(&parent_id) { if let Some(parent_layout) = self.get_layout(&parent_id) {
*parent_size = (parent_layout.width, parent_layout.height); properties.max_size = (parent_layout.width, parent_layout.height);
// --- Calculate Text Layout --- // // --- Calculate Text Layout --- //
let properties = TextProperties { *text_layout = font.measure(&content, *properties);
font_size: *size, let measurement = text_layout.size();
max_size: *parent_size,
alignment: Alignment::Start,
line_height: *line_height,
..Default::default()
};
let layout = font.measure(&content, properties);
let measurement = layout.size();
// --- Apply Layout --- // // --- Apply Layout --- //
if matches!(styles.width, StyleProp::Default) { if matches!(styles.width, StyleProp::Default) {
......
...@@ -82,7 +82,7 @@ impl PartialOrd for Line { ...@@ -82,7 +82,7 @@ impl PartialOrd for Line {
/// Calculated text layout. /// Calculated text layout.
/// ///
/// This can be retrieved using [`measure`](crate::KayakFont::measure). /// This can be retrieved using [`measure`](crate::KayakFont::measure).
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, Default, PartialEq)]
pub struct TextLayout { pub struct TextLayout {
glyphs: Vec<GlyphRect>, glyphs: Vec<GlyphRect>,
lines: Vec<Line>, lines: Vec<Line>,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment