Files
vectorcraft/crates/engine/tests/eraser_open_fill.rs
T

259 lines
12 KiB
Rust

//! Fill-only open-path Eraser regressions through the public engine and CPU renderer.
// Integration tests may unwrap and panic on failure (AGENTS.md).
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use serde_json::{Value, json};
use vectorcraft_doc::{NodeId, NodeKind};
use vectorcraft_engine::Session;
use vectorcraft_geom::{FillRule, PathData, Point};
fn session() -> Session {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 300, "height": 300})).unwrap();
s
}
fn path(s: &Session, id: NodeId) -> &PathData {
match &s.doc().unwrap().doc.node(id).unwrap().kind {
NodeKind::Path { path, .. } => path,
_ => panic!("the fixture is a path"),
}
}
fn paths(s: &Session) -> Vec<PathData> {
let mut paths = vec![];
s.doc().unwrap().doc.walk(|node| {
if let NodeKind::Path { path, .. } = &node.kind {
paths.push(path.clone());
}
});
paths
}
// The varied setting necessarily selects erase_varied, while a circular nib keeps the
// same deterministic swept area as the fixed tip. No random input or device is involved.
fn erase(s: &mut Session, mut params: Value, varied: bool) -> Value {
if varied {
params["angleMode"] = json!("pressure");
params["angleVariation"] = json!(30);
params["roundness"] = json!(100);
}
s.execute("path.eraseRegion", &params).unwrap()
}
fn eraser_preserves_the_fill_of_an_open_rectangle_case(varied: bool) {
let samples = [(110.0, 125.0), (190.0, 125.0), (110.0, 175.0), (190.0, 175.0)];
let render = |s: &Session| {
vectorcraft_render::Renderer::new().render(&s.doc().unwrap().doc, 300, 300, vectorcraft_geom::Affine::IDENTITY, &Default::default())
};
for closed in [true, false] {
let mut s = session();
let id = NodeId(
s.execute(
"path.create",
&json!({"closed": closed, "anchors": [
{"x": 100, "y": 100}, {"x": 200, "y": 100}, {"x": 200, "y": 200}, {"x": 100, "y": 200}
]}),
)
.unwrap()["id"]
.as_u64()
.unwrap(),
);
s.execute("paint.setFill", &json!({"ids": [id.0], "color": "#000000"})).unwrap();
s.execute("paint.setStroke", &json!({"ids": [id.0], "none": true})).unwrap();
s.execute("select.none", &json!({})).unwrap();
assert_eq!(path(&s, id).is_closed(), closed);
let before = render(&s);
for (x, y) in samples.into_iter().chain([(150.0, 150.0)]) {
assert_eq!(before.pixel(x as u32, y as u32), [0, 0, 0, 255], "closed={closed}: original fill at ({x}, {y})");
}
// The explicit command fixes the tip at 10 pt; its round ends are outside the rectangle.
erase(&mut s, json!({"points": [[50, 150], [250, 150]], "size": 10}), varied);
let remaining: Vec<PathData> = paths(&s);
let filled =
|x, y| remaining.iter().any(|path| vectorcraft_geom::hit::fill_contains(&path.to_bezpath(), FillRule::NonZero, Point::new(x, y)));
let after = render(&s);
for (x, y) in samples {
assert!(filled(x, y), "closed={closed}: retained fill missing at ({x}, {y})");
assert_eq!(after.pixel(x as u32, y as u32), [0, 0, 0, 255], "closed={closed}: retained pixel at ({x}, {y})");
}
assert!(!filled(150.0, 150.0), "closed={closed}: eraser band still filled");
assert_eq!(after.pixel(150, 150)[3], 0, "closed={closed}: erased pixel");
let area: f64 = remaining.iter().map(|path| vectorcraft_pathops::area(path, FillRule::NonZero)).sum();
assert!((area - 9000.0).abs() < 1.0, "closed={closed}: retained area {area}");
}
}
#[test]
fn eraser_preserves_the_fill_of_an_open_rectangle() {
eraser_preserves_the_fill_of_an_open_rectangle_case(false);
}
#[test]
fn eraser_preserves_the_fill_of_an_open_rectangle_varied_tip() {
eraser_preserves_the_fill_of_an_open_rectangle_case(true);
}
fn eraser_keeps_open_compound_holes_with_both_fill_rules_case(varied: bool) {
for rule in ["nonZero", "evenOdd"] {
let mut s = session();
let outer = NodeId(
s.execute(
"path.create",
&json!({"anchors": [
{"x": 100, "y": 100}, {"x": 300, "y": 100}, {"x": 300, "y": 300}, {"x": 100, "y": 300}
]}),
)
.unwrap()["id"]
.as_u64()
.unwrap(),
);
let inner = NodeId(
s.execute(
"path.create",
&json!({"anchors": [
{"x": 150, "y": 150}, {"x": 150, "y": 250}, {"x": 250, "y": 250}, {"x": 250, "y": 150}
]}),
)
.unwrap()["id"]
.as_u64()
.unwrap(),
);
for id in [outer, inner] {
s.execute("paint.setFill", &json!({"ids": [id.0], "color": "#000000"})).unwrap();
s.execute("paint.setStroke", &json!({"ids": [id.0], "none": true})).unwrap();
}
s.execute("select.set", &json!({"ids": [outer.0, inner.0]})).unwrap();
let compound = NodeId(s.execute("object.compoundPath.make", &json!({})).unwrap()["id"].as_u64().unwrap());
s.execute("path.setFillRule", &json!({"rule": rule})).unwrap();
s.execute("select.none", &json!({})).unwrap();
let members = match &s.doc().unwrap().doc.node(compound).unwrap().kind {
NodeKind::Compound { children, .. } => children,
other => panic!("expected compound path, got {other:?}"),
};
assert!(members.iter().all(|member| !member.path_data().unwrap().is_closed()), "{rule}: both source contours remain open");
let before =
vectorcraft_render::Renderer::new().render(&s.doc().unwrap().doc, 300, 300, vectorcraft_geom::Affine::IDENTITY, &Default::default());
assert!(before.pixel(120, 140)[3] > 240, "{rule}: ring fills before Eraser");
assert_eq!(before.pixel(200, 200)[3], 0, "{rule}: the compound hole is empty before Eraser");
let before_miss = serde_json::to_value(&s.doc().unwrap().doc).unwrap();
let miss_history = s.doc().unwrap().history.undo.len();
let miss = erase(&mut s, json!({"points": [[200, 200]], "size": 10}), varied);
assert_eq!(miss["ids"], json!([]), "{rule}: a stroke wholly inside the hole misses");
assert_eq!(serde_json::to_value(&s.doc().unwrap().doc).unwrap(), before_miss);
assert_eq!(s.doc().unwrap().history.undo.len(), miss_history, "{rule}: the hole miss adds no history");
erase(&mut s, json!({"points": [[80, 120], [320, 120]], "size": 10}), varied);
let after =
vectorcraft_render::Renderer::new().render(&s.doc().unwrap().doc, 300, 300, vectorcraft_geom::Affine::IDENTITY, &Default::default());
assert_eq!(after.pixel(120, 120)[3], 0, "{rule}: swept band is empty");
assert!(after.pixel(120, 140)[3] > 240, "{rule}: retained ring is filled");
assert_eq!(after.pixel(200, 200)[3], 0, "{rule}: compound hole remains empty");
}
}
#[test]
fn eraser_keeps_open_compound_holes_with_both_fill_rules() {
eraser_keeps_open_compound_holes_with_both_fill_rules_case(false);
}
#[test]
fn eraser_keeps_open_compound_holes_with_both_fill_rules_varied_tip() {
eraser_keeps_open_compound_holes_with_both_fill_rules_case(true);
}
fn eraser_removes_only_the_swept_strip_from_a_zero_signed_area_open_bowtie_case(varied: bool) {
let mut s = session();
let id = NodeId(
s.execute(
"path.create",
&json!({"anchors": [
{"x": 100, "y": 100}, {"x": 200, "y": 200}, {"x": 100, "y": 200}, {"x": 200, "y": 100}
]}),
)
.unwrap()["id"]
.as_u64()
.unwrap(),
);
s.execute("paint.setFill", &json!({"ids": [id.0], "color": "#000000"})).unwrap();
s.execute("paint.setStroke", &json!({"ids": [id.0], "none": true})).unwrap();
s.execute("select.none", &json!({})).unwrap();
assert!(!path(&s, id).is_closed(), "the bowtie is open");
assert!(path(&s, id).subpaths.first().unwrap().signed_area().abs() < 1e-9, "the bowtie has zero signed area");
let render = |s: &Session| {
vectorcraft_render::Renderer::new().render(&s.doc().unwrap().doc, 300, 300, vectorcraft_geom::Affine::IDENTITY, &Default::default())
};
let before = render(&s);
let before_area: f64 = before.pixels.as_chunks::<4>().0.iter().map(|rgba| f64::from(rgba[3]) / 255.0).sum();
assert!((before_area - 5000.0).abs() < 2.0, "the open bowtie fills both lobes: {before_area}");
for y in [110, 140, 175] {
assert!(before.pixel(150, y)[3] > 240, "original lobe pixel y={y}");
}
erase(&mut s, json!({"points": [[50, 125], [250, 125]], "size": 10}), varied);
let after = render(&s);
let after_area: f64 = after.pixels.as_chunks::<4>().0.iter().map(|rgba| f64::from(rgba[3]) / 255.0).sum();
assert!((after_area - 4500.0).abs() < 2.0, "only the 500 px² sweep strip is removed: {after_area}");
assert!(after.pixel(150, 110)[3] > 240);
assert!(after.pixel(150, 140)[3] > 240);
assert!(after.pixel(150, 175)[3] > 240);
assert_eq!(after.pixel(150, 125)[3], 0, "the swept strip is empty");
let area: f64 = paths(&s).iter().map(|path| vectorcraft_pathops::area(path, FillRule::NonZero)).sum();
assert!((area - 4500.0).abs() < 1.0, "retained bowtie area {area}");
}
#[test]
fn eraser_removes_only_the_swept_strip_from_a_zero_signed_area_open_bowtie() {
eraser_removes_only_the_swept_strip_from_a_zero_signed_area_open_bowtie_case(false);
}
#[test]
fn eraser_removes_only_the_swept_strip_from_a_zero_signed_area_open_bowtie_varied_tip() {
eraser_removes_only_the_swept_strip_from_a_zero_signed_area_open_bowtie_case(true);
}
#[test]
fn eraser_keeps_painted_stroke_open_paths_on_the_outline_route() {
for varied in [false, true] {
let mut s = session();
let id = NodeId(
s.execute(
"path.create",
&json!({"anchors": [
{"x": 100, "y": 100}, {"x": 200, "y": 100},
{"x": 200, "y": 200}, {"x": 100, "y": 200}
]}),
)
.unwrap()["id"]
.as_u64()
.unwrap(),
);
s.execute("paint.setFill", &json!({"ids": [id.0], "color": "#000000"})).unwrap();
s.execute("paint.setStroke", &json!({"ids": [id.0], "color": "#000000"})).unwrap();
s.execute("stroke.set", &json!({"ids": [id.0], "weight": 4})).unwrap();
s.execute("select.none", &json!({})).unwrap();
assert!(!path(&s, id).is_closed());
let appearance = &s.doc().unwrap().doc.node(id).unwrap().appearance;
assert_eq!(appearance.stroke_width(), 4.0);
assert!(!appearance.stroke_paint().is_none());
let before = serde_json::to_value(&s.doc().unwrap().doc).unwrap();
let history = s.doc().unwrap().history.undo.len();
let selection = s.doc().unwrap().selection.clone();
let before_image =
vectorcraft_render::Renderer::new().render(&s.doc().unwrap().doc, 300, 300, vectorcraft_geom::Affine::IDENTITY, &Default::default());
assert_eq!(before_image.pixel(150, 150), [0, 0, 0, 255], "the interior really has visible fill");
assert!(before_image.pixel(150, 100)[3] > 240, "the open path has painted visible stroke");
// This sweep lies inside the fill, at least 30 pt from any painted outline.
// Fill-only scope must not extend to this painted-stroke path or wrap it.
let miss = erase(&mut s, json!({"points": [[130, 150], [170, 150]], "size": 10}), varied);
assert_eq!(miss["ids"], json!([]), "varied={varied}: the existing outline route misses");
assert_eq!(serde_json::to_value(&s.doc().unwrap().doc).unwrap(), before, "varied={varied}: no replacement or wrapper");
assert_eq!(s.doc().unwrap().history.undo.len(), history);
assert_eq!(s.doc().unwrap().selection, selection);
let after_image =
vectorcraft_render::Renderer::new().render(&s.doc().unwrap().doc, 300, 300, vectorcraft_geom::Affine::IDENTITY, &Default::default());
assert_eq!(after_image.pixels, before_image.pixels);
}
}