Scissors: paths a cut makes have every stroke aligned Center

path.split (Scissors) and path.cutAtAnchors (Cut Path at Selected
Anchor Points) keep the source path's fill and stroke settings on the
paths they make, and now set every stroke's alignment to Center in the
same undo step, as the help page describes:
https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/modify-paths/split-paths.html

Every stroke row is reset, hidden ones included. A path of several
subpaths has one appearance, so its other subpaths' strokes are
centered too, and a cut member of a compound path centers the compound
path's strokes. An appearance whose strokes are already centered is
left shared. Knife, the erasers and Divide are unchanged.
This commit is contained in:
dotCooCoo
2026-11-16 03:46:53 -08:00
parent ed6f22a84c
commit a10c403df6
8 changed files with 169 additions and 6 deletions
+1 -1
View File
@@ -31,6 +31,6 @@ mod freehand;
mod smooth;
pub use commands::specs;
pub(super) use common::{ids_json, replace_with_pieces, usize_opt};
pub(super) use common::{center_strokes, ids_json, replace_with_pieces, usize_opt};
pub(super) use cut::{components, erase_outline};
pub(super) use freehand::freehand_brushed;
+1 -1
View File
@@ -74,7 +74,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Split Path",
[],
None,
"{id, subpath?, segment, t: 0–1 (0.5)} or {id, subpath?, anchor}: closed paths open there, open paths become two; one of the two new end points is left selected → {ids}. A t outside 0–1 is refused with an error, and nothing changes",
"{id, subpath?, segment, t: 0–1 (0.5)} or {id, subpath?, anchor}: closed paths open there, open paths become two; one of the two new end points is left selected; the paths keep the source's fill and stroke settings except that every stroke's alignment becomes Center (in a path of several subpaths, the other subpaths' strokes too) → {ids}. A t outside 0–1 is refused with an error, and nothing changes",
has_doc,
split
),
+20
View File
@@ -406,6 +406,26 @@ pub(super) fn sibling_with(d: &mut Document, id: NodeId, pd: PathData) -> Result
Ok(n)
}
/// Set every stroke of node `id`, and every stroke of the compound path it is a member of (whose
/// strokes its members show), to Center alignment. Every other stroke and fill setting stays as it
/// is, and an appearance whose strokes are all centered stays shared with its other copies.
pub(in super::super) fn center_strokes(d: &mut Document, id: NodeId) {
use vectorcraft_doc::{AppearanceItem, StrokeAlign};
let parent = d.parent_of(id).filter(|p| matches!(d.node(*p).map(|n| &n.kind), Some(NodeKind::Compound { .. })));
for n in [Some(id), parent].into_iter().flatten() {
let Some(n) = d.node_mut(n) else { continue };
let off = |it: &AppearanceItem| matches!(it, AppearanceItem::Stroke(st) if st.align != StrokeAlign::Center);
if !n.appearance.items.iter().any(off) {
continue;
}
for it in &mut n.appearance.items {
if let AppearanceItem::Stroke(st) = it {
st.align = StrokeAlign::Center;
}
}
}
}
/// Replace path or compound path `id` with `pieces` (first keeps the id, the rest go right above
/// it; none removes it). `cut` removes the path or puts the other pieces above it. Returns ids.
pub(in super::super) fn replace_with_pieces(d: &mut Document, cut: &mut Beside, id: NodeId, pieces: Vec<PathData>) -> Result<Vec<NodeId>> {
+6 -2
View File
@@ -1,6 +1,6 @@
use super::common::{
PenRecord, SIZE_VARIATIONS, SIZES, Sample, dist_to_polyline, fill_targets, ids_json, path_mut, path_of, points_param, poly_bounds,
pressure_region, ranged, replace_with_pieces, samples_param, sibling_with, size_pressure, target_paths, usize_opt,
PenRecord, SIZE_VARIATIONS, SIZES, Sample, center_strokes, dist_to_polyline, fill_targets, ids_json, path_mut, path_of, points_param,
poly_bounds, pressure_region, ranged, replace_with_pieces, samples_param, sibling_with, size_pressure, target_paths, usize_opt,
};
use super::{
Anchor, AnchorKind, Beside, BezPath, BoolOp, Cap, EngineError, FillRule, Join, NodeId, Paint, ParamCurve, PathData, Point, Rect, Result, Session,
@@ -225,14 +225,17 @@ pub(super) fn split(s: &mut Session, p: &Value) -> Result<Value> {
sel.add(id);
sel.anchors.entry(id).or_default().insert((si, 0));
};
// The paths a cut makes keep the source's appearance with every stroke centered.
if sp.closed {
path.subpaths[si] = pieces.pop().ok_or_else(|| EngineError::Other("no such anchor".into()))?;
center_strokes(d, id);
pick(sel, id, si);
return Ok(vec![id]);
}
let [left, right] = <[SubPath; 2]>::try_from(pieces).map_err(|_| EngineError::Other("cannot split an open path at its end point".into()))?;
if path.subpaths.len() == 1 {
path.subpaths[0] = left;
center_strokes(d, id);
let node = sibling_with(d, id, PathData::single(right))?;
let (parent, idx, _) = d.position(id).ok_or(EngineError::NoNode(id))?;
let nid = d.insert(parent, idx + 1, node)?;
@@ -241,6 +244,7 @@ pub(super) fn split(s: &mut Session, p: &Value) -> Result<Value> {
} else {
path.subpaths[si] = left;
path.subpaths.insert(si + 1, right);
center_strokes(d, id);
pick(sel, id, si + 1);
Ok(vec![id])
}
+3 -1
View File
@@ -149,7 +149,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Cut Path at Selected Anchor Points",
[],
None,
"{} cut the paths at their direct-selected anchors (one undo step): a closed path opens there, an open one becomes one path per piece; one of each cut's two coincident anchors stays selected → {ids}",
"{} cut the paths at their direct-selected anchors (one undo step): a closed path opens there, an open one becomes one path per piece; one of each cut's two coincident anchors stays selected; the paths keep the source's fill and stroke settings except that every stroke's alignment becomes Center (in a path of several subpaths, the other subpaths' strokes too) → {ids}",
has_anchors,
cut_at_anchors
),
@@ -1858,6 +1858,8 @@ fn cut_at_anchors(s: &mut Session, _: &Value) -> Result<Value> {
let (mut all, mut picked) = (vec![], vec![]);
let mut cut = Beside::replacing(d, &cuts.iter().map(|(id, ..)| *id).collect::<Vec<_>>());
for (id, subs, starts) in cuts {
// The paths a cut makes keep the source's appearance with every stroke centered.
super::draw2::center_strokes(d, id);
// A path of one subpath becomes one path per piece (the first keeps the id); the
// pieces of a compound shape stay subpaths of it.
if d.node(id).and_then(|n| n.path_data()).is_some_and(|pd| pd.subpaths.len() == 1) {
+130
View File
@@ -3219,6 +3219,88 @@ fn split_refuses_a_subpath_anchor_or_segment_that_is_not_a_whole_number() {
}
}
/// Give `id` a 4 pt round-capped Inside stroke and, above it, a hidden red copy aligned Outside.
/// Returns the appearance it now has.
fn side_strokes(s: &mut Session, id: NodeId) -> vectorcraft_doc::Appearance {
use vectorcraft_doc::{AppearanceItem, LineCap, StrokeAlign};
s.edit("Side Strokes", |d, _| {
let ap = &mut d.node_mut(id).unwrap().appearance;
let st = ap.stroke_mut().unwrap();
(st.width, st.cap, st.align) = (4.0, LineCap::Round, StrokeAlign::Inside);
let mut top = st.clone();
(top.align, top.visible, top.paint) =
(StrokeAlign::Outside, false, vectorcraft_color::Paint::solid(vectorcraft_color::Color::rgb(1.0, 0.0, 0.0)));
ap.items.push(AppearanceItem::Stroke(top));
Ok(())
})
.unwrap();
appearance(s, id)
}
fn appearance(s: &Session, id: NodeId) -> vectorcraft_doc::Appearance {
(*s.doc().unwrap().doc.node(id).unwrap().appearance).clone()
}
/// `ap` with every stroke aligned Center and nothing else changed.
fn centered(ap: &vectorcraft_doc::Appearance) -> vectorcraft_doc::Appearance {
let mut ap = ap.clone();
for it in &mut ap.items {
if let vectorcraft_doc::AppearanceItem::Stroke(st) = it {
st.align = vectorcraft_doc::StrokeAlign::Center;
}
}
ap
}
#[test]
fn scissors_cut_paths_have_their_strokes_centered() {
// The paths a Scissors cut makes keep the source's fill and strokes, every stroke centered,
// in the cut's one undo step; redo centers them again.
let mut s = session();
let r = rect(&mut s, 100.0, 100.0, 100.0, 100.0);
let before = side_strokes(&mut s, r);
assert_ne!(before, centered(&before));
let undo = undo_steps(&s);
s.execute("path.split", &json!({"id": r.0, "anchor": 2})).unwrap();
assert_eq!(undo_steps(&s), undo + 1);
assert!(!path(&s, r).subpaths[0].closed);
assert_eq!(appearance(&s, r), centered(&before), "closed path");
s.execute("edit.undo", &json!({})).unwrap();
assert!(path(&s, r).subpaths[0].closed);
assert_eq!(appearance(&s, r), before, "undo");
s.execute("edit.redo", &json!({})).unwrap();
assert_eq!(appearance(&s, r), centered(&before), "redo");
// An open path becomes two, at an anchor or inside a segment, both centered.
let anchors = json!([{"x": 100, "y": 300}, {"x": 200, "y": 300}, {"x": 300, "y": 300}]);
for at in [json!({"anchor": 1}), json!({"segment": 0, "t": 0.5})] {
let id = NodeId(s.execute("path.create", &json!({"anchors": anchors})).unwrap()["id"].as_u64().unwrap());
let before = side_strokes(&mut s, id);
let made = s.execute("path.split", &with(&at, json!({"id": id.0}))).unwrap();
let ids: Vec<NodeId> = made["ids"].as_array().unwrap().iter().map(|v| NodeId(v.as_u64().unwrap())).collect();
assert_eq!(ids.len(), 2, "{at}");
for i in ids {
assert_eq!(appearance(&s, i), centered(&before), "{at}: {i:?}");
}
}
// A path of several subpaths keeps one node: its other subpath's strokes are centered too.
let id = two_subpaths(&mut s);
let before = side_strokes(&mut s, id);
s.execute("path.split", &json!({"id": id.0, "anchor": 1})).unwrap();
assert_eq!(path(&s, id).subpaths.len(), 3);
assert_eq!(appearance(&s, id), centered(&before), "several subpaths");
// A cut that fails, at an open path's end point, changes neither the strokes nor the history.
let id = NodeId(s.execute("path.create", &json!({"anchors": anchors})).unwrap()["id"].as_u64().unwrap());
let before = side_strokes(&mut s, id);
let undo = undo_steps(&s);
assert!(s.execute("path.split", &json!({"id": id.0, "anchor": 0})).is_err());
assert_eq!((undo_steps(&s), appearance(&s, id)), (undo, before));
// Strokes already centered stay shared with the undo history's copy.
let id = NodeId(s.execute("path.create", &json!({"anchors": anchors})).unwrap()["id"].as_u64().unwrap());
let shared = s.doc().unwrap().doc.node(id).unwrap().appearance.clone();
s.execute("path.split", &json!({"id": id.0, "anchor": 1})).unwrap();
assert!(vectorcraft_doc::Shared::ptr_eq(&s.doc().unwrap().doc.node(id).unwrap().appearance, &shared));
}
/// A subpath, anchor or segment of 2^32 + k fails, where k names one that exists, and the document
/// and the undo history stay as they were. A 32-bit build read the number as k.
#[test]
@@ -3938,6 +4020,54 @@ fn cut_at_anchors_splits_paths_and_leaves_one_end_selected() {
assert_eq!(undo_steps(&s), undo);
}
#[test]
fn cut_at_anchors_paths_have_their_strokes_centered() {
// Every path the cut makes keeps the source's fill and strokes, every stroke centered, in one
// undo step; a path whose anchors were not selected keeps its strokes.
let mut s = session();
let anchors = json!([{"x": 100, "y": 100}, {"x": 200, "y": 100}, {"x": 300, "y": 100}, {"x": 400, "y": 100}]);
let id = NodeId(s.execute("path.create", &json!({"anchors": anchors})).unwrap()["id"].as_u64().unwrap());
let open = side_strokes(&mut s, id);
let r = rect(&mut s, 500.0, 100.0, 100.0, 100.0);
let closed = side_strokes(&mut s, r);
let other = rect(&mut s, 100.0, 300.0, 100.0, 100.0);
let untouched = side_strokes(&mut s, other);
s.execute("select.anchors", &json!({"id": id.0, "anchors": [[0, 1], [0, 2]]})).unwrap();
s.execute("select.anchors", &json!({"id": r.0, "anchors": [[0, 2]], "mode": "add"})).unwrap();
assert_eq!(s.doc().unwrap().selection.anchors.len(), 2);
let undo = undo_steps(&s);
let made = s.execute("path.cutAtAnchors", &json!({})).unwrap();
let ids: Vec<NodeId> = made["ids"].as_array().unwrap().iter().map(|v| NodeId(v.as_u64().unwrap())).collect();
assert_eq!(undo_steps(&s), undo + 1);
assert_eq!(ids.len(), 4, "three pieces and the opened rectangle");
for i in &ids {
let want = if *i == r { centered(&closed) } else { centered(&open) };
assert_eq!(appearance(&s, *i), want, "{i:?}");
}
assert_eq!(appearance(&s, other), untouched);
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!((appearance(&s, id), appearance(&s, r)), (open.clone(), closed.clone()), "undo");
s.execute("edit.redo", &json!({})).unwrap();
assert_eq!((appearance(&s, id), appearance(&s, r)), (centered(&open), centered(&closed)), "redo");
// A cut member of a compound path: the compound path's strokes, which it shows, are centered.
let a = rect(&mut s, 100.0, 450.0, 100.0, 100.0);
let b = rect(&mut s, 125.0, 475.0, 50.0, 50.0);
s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap();
s.execute("object.compoundPath.make", &json!({})).unwrap();
let compound = s.doc().unwrap().selection.objects[0];
let before = side_strokes(&mut s, compound);
let members: Vec<NodeId> = match &s.doc().unwrap().doc.node(compound).unwrap().kind {
NodeKind::Compound { children, .. } => children.iter().map(|c| c.id).collect(),
k => panic!("not a compound path: {k:?}"),
};
let held = path(&s, members[1]);
s.execute("select.anchors", &json!({"id": members[0].0, "anchors": [[0, 2]]})).unwrap();
s.execute("path.cutAtAnchors", &json!({})).unwrap();
assert!(!path(&s, members[0]).subpaths[0].closed);
assert_eq!(path(&s, members[1]), held, "the other member keeps its shape");
assert_eq!(appearance(&s, compound), centered(&before), "compound path");
}
#[test]
fn direct_segment_delete_keeps_the_other_rectangle_edges_and_one_undo() {
for initially_selected in [false, true] {