diff --git a/crates/io/src/svg.rs b/crates/io/src/svg.rs index 4d92f2e67e..94a841b7ff 100644 --- a/crates/io/src/svg.rs +++ b/crates/io/src/svg.rs @@ -669,11 +669,14 @@ fn filter_cost(root: &usvg::Group, base: tiny_skia::Transform, rec: usize, visit let world = base.pre_concat(node.abs_transform()); let mut sub = |root: &usvg::Group, ts: tiny_skia::Transform| cost = cost.saturating_add(filter_cost(root, ts, rec + 1, visits)); match node { - // resvg draws a pattern's tile at the path's scale times the pattern's own. + // resvg draws a pattern's tile at the path's scale times the pattern's own, and + // only that scale (`get_scale`, no skew or rotation): a skewed patternTransform + // whose full-affine bounds are slim still renders a large tile. usvg::Node::Path(p) => { for paint in [p.fill().map(|f| f.paint()), p.stroke().map(|s| s.paint())].into_iter().flatten() { if let usvg::Paint::Pattern(pt) = paint { - sub(pt.root(), world.pre_concat(pt.transform())); + let (sx, sy) = world.pre_concat(pt.transform()).get_scale(); + sub(pt.root(), tiny_skia::Transform::from_scale(sx, sy)); } } } diff --git a/crates/io/src/svg/tests.rs b/crates/io/src/svg/tests.rs index 9d438c9f37..98c8ac7124 100644 --- a/crates/io/src/svg/tests.rs +++ b/crates/io/src/svg/tests.rs @@ -26,6 +26,26 @@ fn ordinary_filters_cost_nothing_on_any_canvas() { assert!(check_filter_cost(&tree, 1.0).is_ok()); } +/// resvg draws a pattern's tile under the scale part of its transform only (`get_scale`): a +/// skewed patternTransform (determinant 1) whose full-affine bounds of a slim filter region are +/// 180 × 181 px still draws that region at 2400 × 180 px, with a morphology window as large, +/// about 2 × 10^11 reads. Never rendered here: only the estimate is checked. +#[test] +fn a_skewed_pattern_filter_counts_at_resvgs_tile_scale() { + let svg = "\ + \ + \ + \ + "; + let tree = parse(svg.as_bytes()).unwrap().tree; + // The tile resvg would allocate: 20 × 20 units at get_scale ≈ (100, 100). + let (sx, sy) = tiny_skia::Transform::from_row(0.0, -0.01, 100.0, 100.0, 0.0, 0.0).get_scale(); + assert!((sx * 20.0).round() >= 2000.0 && (sy * 20.0).round() >= 1999.0, "{sx} {sy}"); + let cost = filter_cost(tree.root(), tiny_skia::Transform::identity(), 0, &mut { MAX_FILTER_VISITS }); + assert!(cost > 50 * MAX_FILTER_COST, "charged only {cost} reads"); + assert!(check_filter_cost(&tree, 1.0).is_err()); +} + #[test] fn quadratics_become_cubics_and_closing_merges_the_start_knot() { let mut pb = tiny_skia::PathBuilder::new();