diff --git a/crates/engine/src/interchange/vector.rs b/crates/engine/src/interchange/vector.rs index 64ef00253..9f8ffba25 100644 --- a/crates/engine/src/interchange/vector.rs +++ b/crates/engine/src/interchange/vector.rs @@ -764,4 +764,60 @@ mod tests { assert_eq!(got, want); assert!(segs.iter().all(|d| d.root.name == "root" && d.width == 10.0)); } + + #[test] + fn svg_contents_preserves_viewbox_cascade_units_transforms_and_gradients() { + let svg = br##" + + + + + + + + + + + + + + "##; + let doc = effectcraft_svg::parse(svg).unwrap(); + let mut next_id = 1u64; + let mut ids = Ids(&mut next_id); + let items = svg_contents(&mut ids, &doc); + assert_eq!(items.len(), 1); + let layer = &items[0]; + assert_eq!(layer.name, "layer"); + assert!((layer.prop("transform/opacity").unwrap().value.as_f64() - 50.0).abs() < 1e-9); + + // Paint order is reversed (top of paint order first). + let children: Vec<_> = layer.sub("contents").unwrap().groups().collect(); + let names: Vec<_> = children.iter().map(|g| g.name.as_str()).collect(); + assert_eq!(names, vec!["fallback", "grad", "hole", "rect-a"]); + + // rect-a: viewBox (10, 10, 40, 20) -> (80, 40) scales by 2x; translate(10, 10) lands at (0, 0) in comp space, + // and 1em = 10 -> 20x20 rect centered at (10, 10). + let rect_a: Vec<_> = children[3].sub("contents").unwrap().groups().collect(); + assert_eq!(rect_a[0].get("size").unwrap().value.as_vec2(), [20.0, 20.0]); + assert_eq!(rect_a[0].get("position").unwrap().value.as_vec2(), [10.0, 10.0]); + assert_eq!(rect_a[1].get("color").unwrap().value, Value::Color([1.0, 0.0, 0.0, 1.0])); + + // hole: EvenOdd fill rule preserved. + let hole: Vec<_> = children[2].sub("contents").unwrap().groups().collect(); + let hole_fill = hole.iter().find(|g| g.match_id == "fill").unwrap(); + assert_eq!(hole_fill.get("rule").unwrap().value, Value::Enum(1)); + + // grad: gradient fill mapped with both stops. + let grad: Vec<_> = children[1].sub("contents").unwrap().groups().collect(); + assert!(grad.iter().any(|g| g.match_id == "gfill")); + + // fallback: missing paint server keeps fallback solid color. + let fb: Vec<_> = children[0].sub("contents").unwrap().groups().collect(); + let fb_fill = fb.iter().find(|g| g.match_id == "fill").unwrap(); + assert_eq!(fb_fill.get("color").unwrap().value, Value::Color([0.0, 0.0, 1.0, 1.0])); + } } diff --git a/crates/host/tests/it/vector_import.rs b/crates/host/tests/it/vector_import.rs index 81b0d8e59..451a30db8 100644 --- a/crates/host/tests/it/vector_import.rs +++ b/crates/host/tests/it/vector_import.rs @@ -1097,3 +1097,67 @@ fn a_vectorcraft_document_without_a_pdf_reports_the_option_to_turn_on() { assert_eq!(r["items"], json!([]), "{import_as}: {r}"); } } + +#[test] +fn svg_scene_mapping_render_parity() { + let svg = br##" + + + + + + + + + + + + + + + + + + + + + + + "##; + let path = tmp("scene-parity.svg"); + std::fs::write(&path, svg).unwrap(); + let doc = effectcraft_svg::parse(svg).unwrap(); + let (w, h) = doc.pixel_size(); + assert_eq!((w, h), (120, 80)); + + let mut s = effectcraft_host::session(); + let r = s.execute_checked("file.import", json!({"paths": [path]})).unwrap(); + assert_eq!(r["errors"], json!([]), "{r}"); + let item = r["items"][0].as_u64().unwrap(); + s.execute("comp.new", json!({"name": "Parity", "width": w, "height": h, "frameRate": 30, "duration": 1})).unwrap(); + let lid = s.execute_checked("layer.addItem", json!({"item": item})).unwrap()["layer"].as_u64().unwrap(); + let cid = s.active_comp_id().unwrap(); + let t = effectcraft_time::Tick::ZERO; + + let footage = s.render(cid, t, RenderOpts::default()); + let direct = effectcraft_svg::rasterize(&doc, w, h, 1.0); + assert!(mean_diff(&footage, &direct) < 1.0 / 255.0, "footage = rasterised SVG"); + + s.execute("layer.select", json!({"layers": [lid]})).unwrap(); + let r = s.execute_checked("layer.create", json!({"op": "shapesFromVector"})).unwrap(); + let sid = r["layers"][0].as_u64().unwrap(); + let shapes = s.render(cid, t, RenderOpts::default()); + let d1 = mean_diff(&shapes, &direct); + assert!(d1 < 0.02, "shapes vs SVG mean diff {d1}"); + + s.execute("prop.set", json!({"layer": sid, "path": "transform/scale", "value": [300, 300]})).unwrap(); + s.execute("prop.set", json!({"layer": sid, "path": "transform/position", "value": [0, 0]})).unwrap(); + s.execute("prop.set", json!({"layer": sid, "path": "transform/anchor", "value": [0, 0]})).unwrap(); + let big = s.render(cid, t, RenderOpts::default()); + let direct3 = effectcraft_svg::rasterize(&doc, w, h, 3.0); + let d3 = mean_diff(&big, &direct3); + assert!(d3 < 0.03, "shapes x3 vs SVG x3 mean diff {d3}"); +} diff --git a/crates/svg/src/tests.rs b/crates/svg/src/tests.rs index 672ef5910..ccbf58dea 100644 --- a/crates/svg/src/tests.rs +++ b/crates/svg/src/tests.rs @@ -1343,3 +1343,247 @@ fn axis_rect_recognises_only_axis_aligned_rectangles() { folded.close_path(); assert_eq!(axis_rect(&folded, Affine::IDENTITY), None); } + +#[test] +fn css_cascade_specificity_display_visibility_and_group_opacity_parity() { + let svg = br##" + + + + + + + + + + + + + + + + "##; + let doc = parse(svg).unwrap(); + let shapes = doc.flatten(); + // 5 top-row rects + 1 visible child in hidden group + 2 rects in opacity group = 8. + assert_eq!(shapes.len(), 8); + assert_eq!(shapes[0].2.fill, Some(Paint::Color([1.0, 0.0, 0.0]))); + assert_eq!(shapes[1].2.fill, Some(Paint::Color([0.0, 1.0, 0.0]))); + assert_eq!(shapes[2].2.fill, Some(Paint::Color([0.0, 1.0, 1.0]))); + assert_eq!(shapes[3].2.fill, Some(Paint::Color([0.0, 0.0, 1.0]))); + assert_eq!(shapes[4].2.fill, Some(Paint::Color([1.0, 1.0, 0.0]))); + // Group opacity multiplies in flatten() rather than mutating child fill_opacity. + assert!((shapes[6].1 - 0.5).abs() < 1e-12); + assert!((shapes[6].2.fill_opacity - 1.0).abs() < 1e-12); + + let img = rasterize(&doc, 60, 20, 1.0); + assert!(near(px(&img, 5, 5), [1.0, 0.0, 0.0, 1.0], 1e-3)); + assert!(near(px(&img, 15, 5), [0.0, 1.0, 0.0, 1.0], 1e-3)); + assert!(near(px(&img, 25, 5), [0.0, 1.0, 1.0, 1.0], 1e-3)); + assert!(near(px(&img, 35, 5), [0.0, 0.0, 1.0, 1.0], 1e-3)); + assert!(near(px(&img, 45, 5), [1.0, 1.0, 0.0, 1.0], 1e-3)); + // display:none suppresses the entire subtree. + assert_eq!(px(&img, 55, 5)[3], 0.0); + // visibility:hidden suppresses the first child, visibility:visible restores the second. + assert_eq!(px(&img, 5, 15)[3], 0.0); + assert!(near(px(&img, 15, 15), [0.0, 1.0, 0.0, 1.0], 1e-3)); + // Isolated group opacity: blue over red inside opacity=0.5 yields premultiplied [0, 0, 0.5, 0.5], + // without red bleeding through the opaque blue rect. + assert!(near(px(&img, 25, 15), [0.0, 0.0, 0.5, 0.5], 2e-3)); +} + +#[test] +fn inherited_attributes_current_color_and_use_inheritance_parity() { + let svg = br##" + + + + + + + + + + + "##; + let doc = parse(svg).unwrap(); + let shapes = doc.flatten(); + assert_eq!(shapes.len(), 4); + + let inh = shapes[0].2; + assert_eq!(inh.fill, Some(Paint::Color([1.0, 0.0, 0.0]))); + assert!((inh.fill_opacity - 0.5).abs() < 1e-12); + assert_eq!(inh.fill_rule, FillRule::EvenOdd); + let st = inh.stroke.as_ref().expect("inherited stroke"); + assert_eq!(st.paint, Paint::Color([0.0, 0.0, 1.0])); + assert_eq!(st.width, 2.0); + assert_eq!(st.cap, Cap::Round); + assert_eq!(st.join, Join::Bevel); + assert_eq!(st.miter, 6.0); + assert_eq!(st.dash, Some((vec![4.0, 2.0], 1.0))); + + let ovr = shapes[1].2; + assert_eq!(ovr.fill, Some(Paint::Color([0.0, 1.0, 0.0]))); + assert!((ovr.fill_opacity - 1.0).abs() < 1e-12); + assert!(ovr.stroke.is_none()); + + // passes its inherited fill to #tpl (which has no fill), while #own keeps its own fill. + assert_eq!(shapes[2].2.fill, Some(Paint::Color([1.0, 1.0, 0.0]))); + assert_eq!(shapes[3].2.fill, Some(Paint::Color([0.0, 1.0, 0.0]))); + + let img = rasterize(&doc, 50, 20, 1.0); + assert!(near(px(&img, 15, 5), [0.0, 1.0, 0.0, 1.0], 1e-3)); + // Both elements inherit fill-opacity="0.5" from the enclosing . + assert!(near(px(&img, 25, 5), [0.5, 0.5, 0.0, 0.5], 1e-3)); + assert!(near(px(&img, 35, 5), [0.0, 0.5, 0.0, 0.5], 1e-3)); +} + +#[test] +fn length_units_and_font_size_inheritance_parity() { + let svg = br#" + + + + + + "#; + let doc = parse(svg).unwrap(); + let shapes = doc.flatten(); + assert_eq!(shapes.len(), 3); + + let (em, abs, mm) = (shapes[0].2, shapes[1].2, shapes[2].2); + assert!(matches!(em.geom, Geom::Rect { x, y, w, h, .. } + if (x - 20.0).abs() < 1e-9 && (y - 20.0).abs() < 1e-9 && (w - 40.0).abs() < 1e-9 && (h - 20.0).abs() < 1e-9)); + assert!((em.stroke.as_ref().unwrap().width - 5.0).abs() < 1e-9); + assert!(matches!(abs.geom, Geom::Rect { x, y, w, h, .. } + if (x - 96.0).abs() < 1e-9 && (y - 16.0).abs() < 1e-9 && (w - 96.0).abs() < 1e-9 && (h - 96.0).abs() < 1e-9)); + assert!(matches!(mm.geom, Geom::Rect { x, w, h, .. } + if (x - 96.0).abs() < 1e-9 && (w - 20.0).abs() < 1e-9 && (h - 20.0).abs() < 1e-9)); +} + +#[test] +fn viewbox_preserve_aspect_ratio_and_implicit_dimensions_parity() { + // Non-zero min-x/min-y and preserveAspectRatio modes into an 80x40 viewport. + for (par, pt_expect) in [ + ("none", (40.0, 20.0)), + ("xMidYMid meet", (40.0, 20.0)), + ("xMinYMin meet", (20.0, 20.0)), + ("xMaxYMax meet", (60.0, 20.0)), + ("xMidYMid slice", (40.0, 20.0)), + ("xMinYMin slice", (40.0, 40.0)), + ] { + let svg = format!( + r#" + + "# + ); + let doc = parse(svg.as_bytes()).unwrap(); + let shapes = doc.flatten(); + assert_eq!(shapes.len(), 1); + // Center of viewBox (20, 30) in root pixel space. + let center = shapes[0].0 * kurbo::Point::new(20.0, 30.0); + assert!((center.x - pt_expect.0).abs() < 1e-9 && (center.y - pt_expect.1).abs() < 1e-9, "{par}: {center:?}"); + } + + // Pixel check for xMinYMin meet vs xMaxYMax meet: 20x20 viewBox in 80x40 -> 40x40 square. + let min_doc = parse(br#""#).unwrap(); + let min_img = rasterize(&min_doc, 80, 40, 1.0); + assert!(near(px(&min_img, 10, 20), [1.0, 0.0, 0.0, 1.0], 1e-3)); + assert_eq!(px(&min_img, 60, 20)[3], 0.0); + + let max_doc = parse(br#""#).unwrap(); + let max_img = rasterize(&max_doc, 80, 40, 1.0); + assert_eq!(px(&max_img, 10, 20)[3], 0.0); + assert!(near(px(&max_img, 60, 20), [1.0, 0.0, 0.0, 1.0], 1e-3)); + + // Omitted width/height derive from viewBox aspect ratio. + let implicit = parse(br#""#).unwrap(); + assert_eq!((implicit.width, implicit.height), (100.0, 50.0)); + let only_w = parse(br#""#).unwrap(); + assert_eq!((only_w.width, only_w.height), (200.0, 100.0)); + let only_h = parse(br#""#).unwrap(); + assert_eq!((only_h.width, only_h.height), (50.0, 25.0)); +} + +#[test] +fn transform_functions_chaining_and_raster_parity() { + let svg = br#" + + + + + + + "#; + let doc = parse(svg).unwrap(); + let shapes = doc.flatten(); + assert_eq!(shapes.len(), 4); + + // rotate(180 5 5) maps (0, 0) -> (10, 10) and keeps the [0,10]^2 square covered. + let p = shapes[1].0 * kurbo::Point::new(0.0, 0.0); + assert!((p.x - 10.0).abs() < 1e-9 && (p.y - 10.0).abs() < 1e-9); + // skewX(45) at y=2 shifts x by +2 -> (0, 2) lands at (2+2, 26) = (4, 26). + let s = shapes[2].0 * kurbo::Point::new(0.0, 2.0); + assert!((s.x - 4.0).abs() < 1e-9 && (s.y - 26.0).abs() < 1e-9); + + let img = rasterize(&doc, 40, 40, 1.0); + assert!(near(px(&img, 20, 20), [1.0, 0.0, 0.0, 1.0], 1e-3)); + assert!(near(px(&img, 5, 5), [0.0, 0.0, 1.0, 1.0], 1e-3)); + assert!(near(px(&img, 6, 26), [0.0, 128.0 / 255.0, 0.0, 1.0], 1e-3)); + assert!(near(px(&img, 28, 28), [1.0, 1.0, 0.0, 1.0], 1e-3)); +} + +#[test] +fn paint_references_gradients_fallbacks_and_stroke_styles_parity() { + let svg = br##" + + + + + + + + + + + + + + + + + "##; + let doc = parse(svg).unwrap(); + let shapes = doc.flatten(); + assert_eq!(shapes.len(), 5); + + let Some(Paint::Gradient(lg)) = &shapes[0].2.fill else { panic!("linearGradient expected") }; + assert!(!lg.bbox_units); + assert_eq!(lg.spread, Spread::Reflect); + assert_eq!(lg.stops.len(), 2); + + assert_eq!(shapes[2].2.fill, Some(Paint::Color([1.0, 0.0, 1.0]))); + assert_eq!(shapes[2].2.stroke.as_ref().map(|s| &s.paint), Some(&Paint::Color([0.0, 1.0, 1.0]))); + + let img = rasterize(&doc, 80, 40, 1.0); + // Reflected gradient: red at x=0, blue at x=10, back toward red at x=19. + assert!(px(&img, 0, 5)[0] > 0.8 && px(&img, 10, 5)[2] > 0.8 && px(&img, 19, 5)[0] > 0.8); + // Radial gradient white center at (50, 10). + assert!(near(px(&img, 50, 10), [1.0, 1.0, 1.0, 1.0], 0.05)); + // Fallback solid fill. + assert!(near(px(&img, 5, 25), [1.0, 0.0, 1.0, 1.0], 1e-3)); + // Even-odd hole at (28, 28) is transparent, outer ring at (22, 28) is green. + assert_eq!(px(&img, 28, 28)[3], 0.0); + assert!(near(px(&img, 22, 28), [0.0, 1.0, 0.0, 1.0], 1e-3)); + // Dashed stroke (6 on, 8 off, square cap extends each end by 2): on at x=43, gap at x=50. + assert!(near(px(&img, 43, 30), [1.0, 1.0, 1.0, 1.0], 1e-3)); + assert_eq!(px(&img, 50, 30)[3], 0.0); +}