Skip to content
Snippets Groups Projects
Commit ead47ea5 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 d526b285
No related branches found
No related tags found
No related merge requests found
......@@ -21,51 +21,32 @@ pub fn extract_texts(
_dpi: f32,
) -> Vec<ExtractQuadBundle> {
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 {
RenderPrimitive::Text {
color,
text_layout,
layout,
size,
content,
font,
parent_size,
line_height,
properties,
..
} => (
color,
text_layout,
layout,
*size,
content,
font,
parent_size,
line_height,
*properties,
),
_ => panic!(""),
};
let font_handle = font_mapping.get_handle(font.clone()).unwrap();
let font = fonts.get(font_handle.clone());
if font.is_none() {
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 font = match fonts.get(font_handle.clone()) {
Some(font) => font,
None => return Vec::new()
};
let text_layout = font.measure(
content,
properties,
);
let base_position = Vec2::new(layout.posx, layout.posy + font_size);
let base_position = Vec2::new(layout.posx, layout.posy + properties.font_size);
for glyph_rect in text_layout.glyphs() {
let mut position = Vec2::from(glyph_rect.position);
......
use morphorm::layout;
use kayak_font::{TextLayout, TextProperties};
use crate::{
color::Color,
layout_cache::Rect,
......@@ -22,10 +24,9 @@ pub enum RenderPrimitive {
color: Color,
content: String,
font: String,
text_layout: TextLayout,
layout: Rect,
line_height: f32,
parent_size: (f32, f32),
size: f32,
properties: TextProperties,
},
Image {
border_radius: Corner<f32>,
......@@ -85,10 +86,13 @@ impl From<&Style> for RenderPrimitive {
color: style.color.resolve(),
content,
font,
text_layout: TextLayout::default(),
layout: Rect::default(),
line_height,
parent_size: (0.0, 0.0),
size: font_size,
properties: TextProperties {
font_size,
line_height,
..Default::default()
},
},
RenderCommand::Image { handle } => Self::Image {
border_radius: style.border_radius.resolve(),
......
......@@ -293,11 +293,10 @@ impl WidgetManager {
match &mut render_primitive {
RenderPrimitive::Text {
parent_size,
content,
font,
size,
line_height,
properties,
text_layout,
..
} => {
// --- Bind to Font Asset --- //
......@@ -307,18 +306,11 @@ impl WidgetManager {
if let Some(font) = asset.get() {
if let Some(parent_id) = self.get_valid_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 --- //
let properties = TextProperties {
font_size: *size,
max_size: *parent_size,
alignment: Alignment::Start,
line_height: *line_height,
..Default::default()
};
let layout = font.measure(&content, properties);
let measurement = layout.size();
*text_layout = font.measure(&content, *properties);
let measurement = text_layout.size();
// --- Apply Layout --- //
if matches!(styles.width, StyleProp::Default) {
......
......@@ -82,7 +82,7 @@ impl PartialOrd for Line {
/// Calculated text layout.
///
/// This can be retrieved using [`measure`](crate::KayakFont::measure).
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, Default, PartialEq)]
pub struct TextLayout {
glyphs: Vec<GlyphRect>,
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