mirror of
https://github.com/storytold/effectcraft.git
synced 2026-10-28 12:07:48 -06:00
Merge #1901: Resolve SVG font-size percentages against the parent font size (@Silvertree2010)
# Conflicts: # crates/svg/src/tests.rs
This commit is contained in:
@@ -719,7 +719,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)) {
|
||||
|
||||
@@ -391,3 +391,18 @@ fn matching_work_past_the_budget_errors_without_doing_it() {
|
||||
let ok = styled(&format!("{}{{{decls}}}", vec!["circle"; 10_000].join(",")), &[""]);
|
||||
assert!(parse(&ok).is_ok());
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user