EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust
Resolve SVG font-size percentages against the parent font size
NetterSilver81548edff996
diff --git a/crates/svg/src/lib.rs b/crates/svg/src/lib.rsindex 24cc584e0..82ab5943f 100644--- a/crates/svg/src/lib.rs+++ b/crates/svg/src/lib.rs@@ -621,7 +621,11 @@ impl<'a, 'i> Parser<'a, 'i> {if let Some(c) = d.get("color").and_then(|v| parse_color(v)) {st.color = c;}- if let Some(v) = d.get("font-size").and_then(|v| self.length(v, 'o', st.font_size)) {+ let font_size = d.get("font-size").and_then(|v| match v.trim().strip_suffix('%') {+ Some(p) => p.trim().parse::<f64>().ok().map(|p| p / 100.0 * st.font_size),+ None => self.length(v, 'o', st.font_size),+ });+ if let Some(v) = font_size.filter(|v| v.is_finite()) {st.font_size = v;}if let Some(p) = d.get("fill").and_then(|v| self.paint_ref(v)) {diff --git a/crates/svg/src/tests.rs b/crates/svg/src/tests.rsindex 93db715c1..5c5838ebd 100644--- a/crates/svg/src/tests.rs+++ b/crates/svg/src/tests.rs@@ -277,3 +277,18 @@ fn a_symbol_used_a_thousand_times_still_imports() {let doc = parse(svg.as_bytes()).unwrap();assert_eq!(depth_and_shapes(&doc.root).1, 3000);}++#[test]+fn font_size_percentage_resolves_against_the_parent_font_size() {+ for (body, expected) in [+ (r#"<g font-size="200%"><rect width="1em" height="1em"/></g>"#, 32.0),+ (r#"<g font-size="200%"><rect width="2ex" height="1em"/></g>"#, 32.0),+ (r#"<g font-size="200%"><g font-size="200%"><rect width="1em" height="1em"/></g></g>"#, 64.0),+ ] {+ let svg = format!(r#"<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">{body}</svg>"#);+ let d = parse(svg.as_bytes()).unwrap();+ let shapes = d.flatten();+ assert_eq!(shapes.len(), 1);+ assert!(matches!(shapes[0].2.geom, Geom::Rect { w, .. } if w == expected), "{body}: {:?}", shapes[0].2.geom);+ }+}