diff --git a/crates/engine/src/cmd/draw2/cut.rs b/crates/engine/src/cmd/draw2/cut.rs index 747f5f710f..8a9c5d039e 100644 --- a/crates/engine/src/cmd/draw2/cut.rs +++ b/crates/engine/src/cmd/draw2/cut.rs @@ -343,6 +343,7 @@ pub(super) fn erase_region(s: &mut Session, p: &Value) -> Result { let mut plan: Vec<(NodeId, Vec)> = vec![]; for (id, pd, rule) in fill_targets(&st.doc, &st.selection, poly_bounds(&pts, reach), true)? { if erase_as_area(&st.doc, id, &pd) { + let pd = eraser_fill_operand(pd); if po::area(&po::boolean(&pd, rule, ®ion, FillRule::NonZero, BoolOp::Intersect), FillRule::NonZero) <= 1e-6 { continue; } @@ -364,6 +365,23 @@ fn erase_as_area(doc: &Document, id: NodeId, pd: &PathData) -> bool { pd.is_closed() || filled_open } +/// Close only Eraser's temporary fill operand, preserving an open path's straight closing edge. +fn eraser_fill_operand(mut pd: PathData) -> PathData { + for sp in &mut pd.subpaths { + if !sp.closed { + // These endpoint handles are unused by an open path. Closing it must not activate them. + if let Some(first) = sp.anchors.first_mut() { + first.h_in = first.p; + } + if let Some(last) = sp.anchors.last_mut() { + last.h_out = last.p; + } + sp.closed = true; + } + } + pd +} + /// `path.eraseRegion`'s `rect: [x, y, width, height]`: four finite numbers, with a width and a /// height above 0. fn marquee_param(v: &Value, cmd: &str) -> Result { @@ -405,6 +423,7 @@ fn erase_rect(s: &mut Session, r: Rect) -> Result { let mut plan: Vec<(NodeId, Vec)> = vec![]; for (id, pd, rule) in fill_targets(&st.doc, &st.selection, r, true)? { if erase_as_area(&st.doc, id, &pd) { + let pd = eraser_fill_operand(pd); if po::area(&po::boolean(&pd, rule, ®ion, FillRule::NonZero, BoolOp::Intersect), FillRule::NonZero) <= 1e-6 { continue; } @@ -505,6 +524,7 @@ fn erase_varied(s: &mut Session, samples: &[Sample], tip: [f64; 3], vary: [(Vari let mut plan: Vec<(NodeId, Vec)> = vec![]; for (id, pd, rule) in fill_targets(&st.doc, &st.selection, poly_bounds(&pts, reach), true)? { if erase_as_area(&st.doc, id, &pd) { + let pd = eraser_fill_operand(pd); if po::area(&po::boolean(&pd, rule, ®ion, FillRule::NonZero, BoolOp::Intersect), FillRule::NonZero) <= 1e-6 { continue; } diff --git a/crates/engine/tests/eraser_open_fill.rs b/crates/engine/tests/eraser_open_fill.rs index b00a9ce208..f36208e8ad 100644 --- a/crates/engine/tests/eraser_open_fill.rs +++ b/crates/engine/tests/eraser_open_fill.rs @@ -1346,3 +1346,90 @@ fn eraser_mixed_gradient_rows_keep_visible_fill_placement() { "hidden rows must not suppress the visible fill's placement: Along {fitted_along:?}; Rasterize {fitted_rasterize:?}" ); } + +fn eraser_endpoint_region(s: &mut Session, mode: &str, miss: bool) -> Value { + if mode == "rect" { + let rect = if miss { json!([55, 145, 10, 10]) } else { json!([150, 140, 20, 20]) }; + s.execute("path.eraseRegion", &json!({"rect": rect})).unwrap() + } else { + let points = if miss { json!([[60, 150]]) } else { json!([[160, 150]]) }; + erase(s, json!({"points": points, "size": 20}), mode == "varied") + } +} + +fn eraser_keeps_unused_endpoint_handles_out_of_fill_case(mode: &str) { + for (closed, stroked) in [(false, false), (true, false), (false, true)] { + let mut s = session(); + let id = NodeId( + s.execute( + "path.create", + &json!({"closed": closed, "anchors": [ + {"x": 100, "y": 100, "in": [0, 100]}, {"x": 200, "y": 100}, + {"x": 200, "y": 200}, {"x": 100, "y": 200, "out": [0, 200]} + ]}), + ) + .unwrap()["id"] + .as_u64() + .unwrap(), + ); + if stroked { + s.execute("paint.setFill", &json!({"ids": [id.0], "none": true})).unwrap(); + s.execute("paint.setStroke", &json!({"ids": [id.0], "color": "#000000"})).unwrap(); + s.execute("stroke.set", &json!({"ids": [id.0], "weight": 4})).unwrap(); + } else { + 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": [id.0]})).unwrap(); + let sp = &path(&s, id).subpaths[0]; + assert_eq!(sp.closed, closed); + assert_eq!(sp.anchors.first().unwrap().h_in, Point::new(0.0, 100.0)); + assert_eq!(sp.anchors.last().unwrap().h_out, Point::new(0.0, 200.0)); + let before = serde_json::to_value(&s.doc().unwrap().doc).unwrap(); + let selection = s.doc().unwrap().selection.clone(); + let history = s.doc().unwrap().history.undo.len(); + let before_image = eraser_gradient_image(&s); + assert_eq!(before_image.pixel(60, 150)[3], if closed { 255 } else { 0 }, "{mode}: fixture closure"); + assert_eq!(before_image.pixel(160, 150)[3], if stroked { 0 } else { 255 }, "{mode}: interior witness"); + + let result = eraser_endpoint_region(&mut s, mode, false); + let after_image = eraser_gradient_image(&s); + assert_eq!(after_image.pixel(160, 150)[3], 0, "{mode}: erased interior"); + assert_eq!(after_image.pixel(60, 150), before_image.pixel(60, 150), "{mode}: no new bulge or lost closed curve"); + if stroked { + assert_eq!(result["ids"], json!([]), "{mode}: the open outline misses an interior dab"); + assert_eq!(serde_json::to_value(&s.doc().unwrap().doc).unwrap(), before); + assert_eq!(s.doc().unwrap().selection, selection); + assert_eq!(s.doc().unwrap().history.undo.len(), history); + continue; + } + assert_eq!(after_image.pixel(180, 180), [0, 0, 0, 255], "{mode}: retained fill"); + assert_eq!(s.doc().unwrap().history.undo.len(), history + 1); + s.execute("edit.undo", &json!({})).unwrap(); + assert_eq!(serde_json::to_value(&s.doc().unwrap().doc).unwrap(), before, "{mode}: original geometry and endpoint handles restored"); + assert_eq!(s.doc().unwrap().selection, selection); + assert_eq!(s.doc().unwrap().history.undo.len(), history); + if !closed { + let miss = eraser_endpoint_region(&mut s, mode, true); + assert_eq!(miss["ids"], json!([]), "{mode}: the phantom bulge cannot count as an intersection"); + assert_eq!(serde_json::to_value(&s.doc().unwrap().doc).unwrap(), before); + assert_eq!(s.doc().unwrap().selection, selection); + assert_eq!(s.doc().unwrap().history.undo.len(), history); + } + } +} + +#[test] +fn eraser_keeps_unused_endpoint_handles_out_of_fill_marquee() { + eraser_keeps_unused_endpoint_handles_out_of_fill_case("rect"); +} + +#[test] +fn eraser_keeps_unused_endpoint_handles_out_of_fill_fixed_tip() { + eraser_keeps_unused_endpoint_handles_out_of_fill_case("fixed"); +} + +#[test] +fn eraser_keeps_unused_endpoint_handles_out_of_fill_varied_tip() { + eraser_keeps_unused_endpoint_handles_out_of_fill_case("varied"); +}