From ae11172f5ad05a501fc7458a7f9aabd45fb979dc Mon Sep 17 00:00:00 2001 From: Shark Date: Wed, 27 Mar 2024 05:23:33 +0100 Subject: [PATCH] fix percent paring and text pre-rendering, --- crates/gosub_rendering/src/style/parse.rs | 4 +++ crates/gosub_styling/src/prerender_text.rs | 2 -- crates/gosub_styling/src/render_tree.rs | 34 +++++++++++++++------- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/crates/gosub_rendering/src/style/parse.rs b/crates/gosub_rendering/src/style/parse.rs index 04c4b6943..a53f63891 100644 --- a/crates/gosub_rendering/src/style/parse.rs +++ b/crates/gosub_rendering/src/style/parse.rs @@ -63,6 +63,10 @@ pub(crate) fn parse_dimension(node: &mut RenderTreeNode, name: &str) -> Dimensio match &property.actual { CssValue::String(value) => match value.as_str() { "auto" => auto, + s if s.ends_with("%") => { + let value = s.trim_end_matches("%").parse::().unwrap_or(0.0); + Dimension::Percent(value) + } _ => Dimension::Length(property.actual.unit_to_px()), //HACK }, CssValue::Percentage(value) => Dimension::Percent(*value), diff --git a/crates/gosub_styling/src/prerender_text.rs b/crates/gosub_styling/src/prerender_text.rs index ecf41730a..6aa68d646 100644 --- a/crates/gosub_styling/src/prerender_text.rs +++ b/crates/gosub_styling/src/prerender_text.rs @@ -276,8 +276,6 @@ impl PrerenderText { .chars() .filter_map(|c| { if c == '\n' { - width = width.max(pen_x); - pen_x = 0.0; return None; } diff --git a/crates/gosub_styling/src/render_tree.rs b/crates/gosub_styling/src/render_tree.rs index ab9ff1f8c..fbf431591 100644 --- a/crates/gosub_styling/src/render_tree.rs +++ b/crates/gosub_styling/src/render_tree.rs @@ -194,13 +194,24 @@ impl RenderTree { let binding = document.get(); let current_node = binding.get_node_by_id(current_node_id).unwrap(); - let Ok(data) = - RenderNodeData::from_node_data(current_node.data.clone(), &mut css_map_entry) - else { - eprintln!( - "Failed to create render node data for node {:?}", - current_node_id - ); + let mut data = || { + if let Some(parent_id) = current_node.parent { + if let Some(parent) = self.nodes.get_mut(&parent_id) { + let parent_props = Some(&mut parent.properties); + + return RenderNodeData::from_node_data( + current_node.data.clone(), + parent_props, + ) + .ok(); + }; + }; + + RenderNodeData::from_node_data(current_node.data.clone(), None).ok() + }; + + let Some(data) = data() else { + eprintln!("Failed to create node data for node: {:?}", current_node_id); continue; }; @@ -261,11 +272,12 @@ pub enum RenderNodeData { } impl RenderNodeData { - pub fn from_node_data(node: NodeData, props: &mut CssProperties) -> Result { + pub fn from_node_data(node: NodeData, props: Option<&mut CssProperties>) -> Result { Ok(match node { NodeData::Document(data) => RenderNodeData::Document(data), NodeData::Element(data) => RenderNodeData::Element(data), NodeData::Text(data) => { + let props = props.ok_or(anyhow::anyhow!("No properties found"))?; let ff; if let Some(prop) = props.get("font-family") { prop.compute_value(); @@ -293,13 +305,13 @@ impl RenderNodeData { if fs.ends_with("px") { fs.trim_end_matches("px").parse::().unwrap_or(12.0) } else { - 12.0 + 12.01 } } else { - 12.0 + 12.02 }; } else { - fs = 12.0 + fs = 12.03 }; let text = PrerenderText::new(data.value.clone(), fs, ff)?;