mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-20 17:31:55 +01:00
svg: add SVG render and scene-mapping parity regression tests (#3984)
* svg: add SVG render and scene-mapping parity regression tests * svg: fix expected CSS/inherited-opacity/pixel-center assertions in parity tests * svg: fix inline style override and square-cap dash gap in parity tests
This commit is contained in:
1 parent
02b9ac978b
commit
ff1cd02409
3 files changed
+364
No files matched your search
@@ -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##"<svg xmlns="http://www.w3.org/2000/svg" width="80" height="40" viewBox="10 10 40 20">
|
||||
<style>
|
||||
.box { fill: #ff0000; }
|
||||
#hole { fill: #00ff00; fill-rule: evenodd; }
|
||||
</style>
|
||||
<defs>
|
||||
<linearGradient id="g" x1="0%" y1="0%" x2="100%" y2="0%">
|
||||
<stop offset="0" stop-color="#ff0000"/>
|
||||
<stop offset="1" stop-color="#0000ff"/>
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<g id="layer" opacity="0.5" transform="translate(10, 10)" font-size="10">
|
||||
<rect id="rect-a" class="box" x="0" y="0" width="1em" height="1em"/>
|
||||
<path id="hole" d="M12 0 H20 V8 H12 Z M14 2 V6 H18 V2 Z"/>
|
||||
<rect id="grad" x="22" y="0" width="8" height="8" fill="url(#g)"/>
|
||||
<rect id="fallback" x="32" y="0" width="4" height="4" fill="url(#missing) #0000ff"/>
|
||||
</g>
|
||||
</svg>"##;
|
||||
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]));
|
||||
}
|
||||
}
|
||||
@@ -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##"<svg xmlns="http://www.w3.org/2000/svg" width="120" height="80" viewBox="10 10 60 40" preserveAspectRatio="xMidYMid meet">
|
||||
<style>
|
||||
.card { fill: #204080; }
|
||||
#accent { fill: #ff6020; }
|
||||
</style>
|
||||
<defs>
|
||||
<linearGradient id="sky" x1="0%" y1="0%" x2="100%" y2="100%">
|
||||
<stop offset="0" stop-color="#102040"/>
|
||||
<stop offset="1" stop-color="#4080c0"/>
|
||||
</linearGradient>
|
||||
<radialGradient id="glow" cx="50%" cy="50%" r="50%">
|
||||
<stop offset="0" stop-color="#ffffff"/>
|
||||
<stop offset="1" stop-color="#ff0000"/>
|
||||
</radialGradient>
|
||||
<symbol id="badge" viewBox="0 0 20 20">
|
||||
<circle cx="10" cy="10" r="8" fill="currentColor"/>
|
||||
</symbol>
|
||||
</defs>
|
||||
<rect x="10" y="10" width="60" height="40" fill="url(#sky)"/>
|
||||
<g font-size="10" color="#ffff00" opacity="0.85">
|
||||
<rect class="card" x="14" y="14" width="2em" height="1.5em" rx="2" ry="2" stroke="#ffffff" stroke-width="1"/>
|
||||
<ellipse id="accent" cx="50" cy="24" rx="8" ry="6" transform="rotate(20 50 24)" fill="url(#glow)"/>
|
||||
<path d="M14 34 H34 V46 H14 Z M18 37 V43 H30 V37 Z" fill="#00d080" fill-rule="evenodd"/>
|
||||
<use href="#badge" x="42" y="34" width="12" height="12"/>
|
||||
<rect x="58" y="36" width="8" height="8" fill="url(#missing) #ff00ff"/>
|
||||
</g>
|
||||
</svg>"##;
|
||||
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}");
|
||||
}
|
||||
@@ -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##"<svg xmlns="http://www.w3.org/2000/svg" width="60" height="20">
|
||||
<style>
|
||||
rect { fill: #ff0000; }
|
||||
.cls { fill: #00ff00; }
|
||||
.cls.hi { fill: #00ffff; }
|
||||
#exact { fill: #0000ff; }
|
||||
.gone { display: none; }
|
||||
</style>
|
||||
<rect id="elem" x="0" y="0" width="10" height="10" fill="#808080"/>
|
||||
<rect id="cls" class="cls" x="10" y="0" width="10" height="10" fill="#808080"/>
|
||||
<rect id="multi" class="cls hi" x="20" y="0" width="10" height="10"/>
|
||||
<rect id="exact" class="cls hi" x="30" y="0" width="10" height="10"/>
|
||||
<rect id="inline" class="cls hi" x="40" y="0" width="10" height="10" style="fill: #ffff00"/>
|
||||
<g class="gone"><rect x="50" y="0" width="10" height="10" fill="#ff00ff"/></g>
|
||||
<g visibility="hidden">
|
||||
<rect x="0" y="10" width="10" height="10" fill="#ff0000"/>
|
||||
<rect x="10" y="10" width="10" height="10" style="fill: #00ff00" visibility="visible"/>
|
||||
</g>
|
||||
<g opacity="0.5">
|
||||
<rect x="20" y="10" width="10" height="10" style="fill: #ff0000"/>
|
||||
<rect x="20" y="10" width="10" height="10" style="fill: #0000ff"/>
|
||||
</g>
|
||||
</svg>"##;
|
||||
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##"<svg xmlns="http://www.w3.org/2000/svg" width="50" height="20">
|
||||
<defs>
|
||||
<rect id="tpl" width="8" height="8"/>
|
||||
<rect id="own" width="8" height="8" fill="#00ff00"/>
|
||||
</defs>
|
||||
<g color="#ff0000" fill="currentColor" fill-opacity="0.5" stroke="#0000ff" stroke-width="2"
|
||||
stroke-linecap="round" stroke-linejoin="bevel" stroke-miterlimit="6"
|
||||
stroke-dasharray="4 2" stroke-dashoffset="1" fill-rule="evenodd">
|
||||
<rect id="inherited" x="2" y="2" width="6" height="6"/>
|
||||
<rect id="override" x="12" y="2" width="6" height="6" color="#00ff00" fill-opacity="1" stroke="none"/>
|
||||
<use id="u-tpl" href="#tpl" x="21" y="1" fill="#ffff00" stroke="none"/>
|
||||
<use id="u-own" href="#own" x="31" y="1" fill="#ffff00" stroke="none"/>
|
||||
</g>
|
||||
</svg>"##;
|
||||
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());
|
||||
|
||||
// <use> 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 <use> elements inherit fill-opacity="0.5" from the enclosing <g>.
|
||||
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#"<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" viewBox="0 0 200 100">
|
||||
<g font-size="20">
|
||||
<rect id="em" x="1em" y="2ex" width="2em" height="2ex" stroke="black" stroke-width="0.25em"/>
|
||||
<rect id="abs" x="1in" y="1pc" width="72pt" height="2.54cm"/>
|
||||
<rect id="mm" x="25.4mm" y="0" width="10%" height="20%"/>
|
||||
</g>
|
||||
</svg>"#;
|
||||
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#"<svg xmlns="http://www.w3.org/2000/svg" width="80" height="40" viewBox="10 20 20 20" preserveAspectRatio="{par}">
|
||||
<rect x="10" y="20" width="20" height="20" fill="red"/>
|
||||
</svg>"#
|
||||
);
|
||||
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#"<svg xmlns="http://www.w3.org/2000/svg" width="80" height="40" viewBox="10 20 20 20" preserveAspectRatio="xMinYMin meet"><rect x="10" y="20" width="20" height="20" fill="red"/></svg>"#).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#"<svg xmlns="http://www.w3.org/2000/svg" width="80" height="40" viewBox="10 20 20 20" preserveAspectRatio="xMaxYMax meet"><rect x="10" y="20" width="20" height="20" fill="red"/></svg>"#).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#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 50"/>"#).unwrap();
|
||||
assert_eq!((implicit.width, implicit.height), (100.0, 50.0));
|
||||
let only_w = parse(br#"<svg xmlns="http://www.w3.org/2000/svg" width="200" viewBox="0 0 100 50"/>"#).unwrap();
|
||||
assert_eq!((only_w.width, only_w.height), (200.0, 100.0));
|
||||
let only_h = parse(br#"<svg xmlns="http://www.w3.org/2000/svg" height="25" viewBox="0 0 100 50"/>"#).unwrap();
|
||||
assert_eq!((only_h.width, only_h.height), (50.0, 25.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transform_functions_chaining_and_raster_parity() {
|
||||
let svg = br#"<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40">
|
||||
<g transform="translate(20, 20) rotate(90)">
|
||||
<rect id="rot" x="-5" y="-5" width="10" height="10" fill="red"/>
|
||||
</g>
|
||||
<rect id="pivot" x="0" y="0" width="10" height="10" transform="rotate(180 5 5)" fill="blue"/>
|
||||
<rect id="skew" x="0" y="0" width="4" height="4" transform="translate(2, 24) skewX(45)" fill="green"/>
|
||||
<rect id="mat" x="0" y="0" width="4" height="4" transform="matrix(2 0 0 2 24 24)" fill="yellow"/>
|
||||
</svg>"#;
|
||||
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##"<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="80" height="40">
|
||||
<defs>
|
||||
<linearGradient id="base-stops">
|
||||
<stop offset="0%" stop-color="#ff0000"/>
|
||||
<stop offset="100%" stop-color="#0000ff"/>
|
||||
</linearGradient>
|
||||
<linearGradient id="inherited-grad" xlink:href="#base-stops" x1="0" y1="0" x2="10" y2="0" gradientUnits="userSpaceOnUse" spreadMethod="reflect"/>
|
||||
<radialGradient id="rad" cx="50.5" cy="10.5" r="10" fx="50.5" fy="10.5" gradientUnits="userSpaceOnUse">
|
||||
<stop offset="0" stop-color="#ffffff"/>
|
||||
<stop offset="1" stop-color="#00ff00"/>
|
||||
</radialGradient>
|
||||
</defs>
|
||||
<rect id="grad" x="0" y="0" width="20" height="10" fill="url(#inherited-grad)"/>
|
||||
<rect id="rad-rect" x="40" y="0" width="20" height="20" fill="url(#rad)"/>
|
||||
<rect id="fallback" x="0" y="20" width="10" height="10" fill="url(#missing) #ff00ff" stroke="url(#missing) #00ffff" stroke-width="2"/>
|
||||
<path id="evenodd" d="M20 20 H36 V36 H20 Z M24 24 V32 H32 V24 Z" fill="#00ff00" fill-rule="evenodd"/>
|
||||
<path id="dashed" d="M40 30 H76" fill="none" stroke="#ffffff" stroke-width="4" stroke-linecap="square" stroke-dasharray="6 8" stroke-dashoffset="0"/>
|
||||
</svg>"##;
|
||||
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);
|
||||
}
|
||||
Reference in new issue
Block a user