Skip to content

VectorCraft: An open-source, vector art tool written by scratch in pure Rust

Paste: Paste in Back keeps a clip group's clipping path at the bottom

dotCooCoo637229931f54
diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs
index 3427e23f1..62a17bda3 100644
--- a/crates/engine/src/cmd/edit.rs
+++ b/crates/engine/src/cmd/edit.rs
@@ -862,17 +862,20 @@ fn paste_clip(s: &mut Session, p: &Value, mode: PasteMode, clip: &Clipboard, cho
Some(Some(name)) if remember => Some(*layers.entry(name).or_insert_with(|| layer_named(d, name))),
_ => None,
};
+ // In a clip group or a clipping layer the bottom child is the clipping path: art pasted
+ // in back goes above it, not below it.
+ let floor = |d: &Document, par: Option<NodeId>| usize::from(par.and_then(|p| d.node(p)).is_some_and(Node::clips));
let (par, idx) = match (into, anchor, layer) {
// Inside the object Draw Inside draws into, on top of what is in it already.
(Some(group), ..) => (Some(group), usize::MAX),
// Next to the anchor, unless the object goes back to another layer.
(None, Some((par, i, _)), l) if l.is_none() || l == par => {
let k = placed.get(&par).copied().unwrap_or(0);
- (par, if front { i + 1 + k } else { i + k })
+ (par, if front { i + 1 + k } else { i.max(floor(d, par)) + k })
}
(None, _, l) => {
let par = l.or(parent);
- (par, if back { placed.get(&par).copied().unwrap_or(0) } else { usize::MAX })
+ (par, if back { floor(d, par) + placed.get(&par).copied().unwrap_or(0) } else { usize::MAX })
}
};
*placed.entry(par).or_default() += 1;
diff --git a/crates/engine/src/tests/clipboard.rs b/crates/engine/src/tests/clipboard.rs
index 3336d384e..e7ddbc7c0 100644
--- a/crates/engine/src/tests/clipboard.rs
+++ b/crates/engine/src/tests/clipboard.rs
@@ -599,6 +599,39 @@ fn front_and_back_beside_a_compound_paths_member_go_beside_the_compound_path() {
assert_eq!(members(&s), [before[0], path[0], before[1]]);
}
+#[test]
+fn paste_in_back_of_a_clipping_path_keeps_it_the_clipping_path() {
+ let mut s = session();
+ let children = |s: &Session, g: NodeId| doc(s).node(g).unwrap().children().unwrap().iter().map(|n| n.id).collect::<Vec<_>>();
+ let loose = rect(&mut s, 200.0, 0.0, 10.0, 10.0);
+ let other = rect(&mut s, 220.0, 0.0, 10.0, 10.0);
+ sel(&mut s, &[other]);
+ let group = id_of(&s.execute("object.group", &json!({})).unwrap());
+ // A path as the clipping path, and a compound path as the clipping path (a group pasted in back
+ // of a member goes beside the compound path).
+ let art = rect(&mut s, 0.0, 0.0, 100.0, 100.0);
+ let mask = rect(&mut s, 10.0, 10.0, 50.0, 50.0);
+ sel(&mut s, &[art, mask]);
+ let g = id_of(&s.execute("object.clippingMask.make", &json!({})).unwrap());
+ let art2 = rect(&mut s, 0.0, 120.0, 100.0, 100.0);
+ let (a, b) = (rect(&mut s, 10.0, 130.0, 20.0, 20.0), rect(&mut s, 40.0, 130.0, 20.0, 20.0));
+ sel(&mut s, &[a, b]);
+ let c = id_of(&s.execute("object.compoundPath.make", &json!({})).unwrap());
+ sel(&mut s, &[art2, c]);
+ let g2 = id_of(&s.execute("object.clippingMask.make", &json!({})).unwrap());
+ for (g, clipping, target, copies) in [(g, mask, mask, vec![loose, group]), (g2, c, a, vec![group])] {
+ assert_eq!(children(&s, g)[0], clipping);
+ for copied in copies {
+ copy(&mut s, &[copied]);
+ sel(&mut s, &[target]);
+ let pasted = ids_of(&s.execute("edit.pasteInBack", &json!({})).unwrap());
+ let ch = children(&s, g);
+ assert_eq!(ch[0], clipping, "the clipping path stays the bottom child");
+ assert_eq!(&ch[1..2], &pasted[..], "right above the clipping path");
+ }
+ }
+}
+
#[test]
fn paste_on_all_artboards_keeps_the_offset_to_the_source_artboard() {
let mut s = Session::new();