Skip to content

Commit

Permalink
fix percent paring and text pre-rendering,
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharktheone committed Mar 27, 2024
1 parent 47fc058 commit ae11172
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
4 changes: 4 additions & 0 deletions crates/gosub_rendering/src/style/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<f32>().unwrap_or(0.0);
Dimension::Percent(value)
}
_ => Dimension::Length(property.actual.unit_to_px()), //HACK
},
CssValue::Percentage(value) => Dimension::Percent(*value),
Expand Down
2 changes: 0 additions & 2 deletions crates/gosub_styling/src/prerender_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,6 @@ impl PrerenderText {
.chars()
.filter_map(|c| {
if c == '\n' {
width = width.max(pen_x);
pen_x = 0.0;
return None;
}

Expand Down
34 changes: 23 additions & 11 deletions crates/gosub_styling/src/render_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -261,11 +272,12 @@ pub enum RenderNodeData {
}

impl RenderNodeData {
pub fn from_node_data(node: NodeData, props: &mut CssProperties) -> Result<Self> {
pub fn from_node_data(node: NodeData, props: Option<&mut CssProperties>) -> Result<Self> {
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();
Expand Down Expand Up @@ -293,13 +305,13 @@ impl RenderNodeData {
if fs.ends_with("px") {
fs.trim_end_matches("px").parse::<f32>().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)?;
Expand Down

0 comments on commit ae11172

Please sign in to comment.