mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-28 09:21:55 +01:00
Merge pull request #9793 from dotCooCoo/fix/cleanup-keeps-clipping-paths
Clean Up: keep the clipping paths of groups and layers
This commit is contained in:
2 files changed
+105
-10
No files matched your search
@@ -190,7 +190,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Clean Up…",
|
||||
["Object", "Path"],
|
||||
None,
|
||||
"{strayPoints?: true, unpaintedObjects?: true, emptyTextPaths?: true} → {removed}",
|
||||
"{strayPoints?: true, unpaintedObjects?: true, emptyTextPaths?: true} remove them, except the clipping paths of groups and layers and what a group or compound path used as one holds → {removed}",
|
||||
has_doc,
|
||||
clean_up
|
||||
),
|
||||
@@ -1725,9 +1725,14 @@ fn clean_up(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let mut v = vec![];
|
||||
fn visit(n: &Node, parent_compound: bool, parent_clip: bool, first: bool, v: &mut Vec<NodeId>, opts: (bool, bool, bool)) {
|
||||
let (stray, unpainted, empty_text) = opts;
|
||||
// The clipping path of a group or layer stays as it is, whatever kind of object it is,
|
||||
// and so does everything a group or compound path used as one holds.
|
||||
if parent_clip && first {
|
||||
return;
|
||||
}
|
||||
match &n.kind {
|
||||
NodeKind::Path { path, clipping, guide, .. } => {
|
||||
if *guide || *clipping || (parent_clip && first) {
|
||||
if *guide || *clipping {
|
||||
return;
|
||||
}
|
||||
if (stray && path.anchor_count() <= 1) || (unpainted && !parent_compound && !is_painted(&n.appearance)) {
|
||||
@@ -1735,7 +1740,7 @@ fn clean_up(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
}
|
||||
NodeKind::Compound { children, .. } => {
|
||||
if unpainted && !(parent_clip && first) && !is_painted(&n.appearance) {
|
||||
if unpainted && !is_painted(&n.appearance) {
|
||||
v.push(n.id);
|
||||
return;
|
||||
}
|
||||
@@ -1744,14 +1749,10 @@ fn clean_up(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
}
|
||||
NodeKind::Text(t) if empty_text && t.plain_text().trim().is_empty() => v.push(n.id),
|
||||
NodeKind::Layer { children, .. } => {
|
||||
for c in children {
|
||||
visit(c, false, false, false, v, opts);
|
||||
}
|
||||
}
|
||||
NodeKind::Group { children, clip } => {
|
||||
NodeKind::Layer { children, .. } | NodeKind::Group { children, .. } => {
|
||||
let clips = n.clips();
|
||||
for (i, c) in children.iter().enumerate() {
|
||||
visit(c, false, *clip, i == 0, v, opts);
|
||||
visit(c, false, clips, i == 0, v, opts);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
|
||||
@@ -1066,6 +1066,100 @@ fn clean_up_removes_junk() {
|
||||
assert!(d.node(unpainted).is_none() && d.node(stray).is_none() && d.node(empty).is_none());
|
||||
}
|
||||
|
||||
/// A page-sized square under a ring (a compound path with a square hole), nothing selected.
|
||||
fn art_and_ring(s: &mut Session) -> (NodeId, NodeId) {
|
||||
let art = rect(s, 0.0, 0.0, 100.0, 100.0);
|
||||
let (outer, inner) = (rect(s, 10.0, 10.0, 80.0, 80.0), rect(s, 35.0, 35.0, 30.0, 30.0));
|
||||
s.execute("select.set", &json!({"ids": [inner.0]})).unwrap();
|
||||
s.execute("path.reverse", &json!({})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [outer.0, inner.0]})).unwrap();
|
||||
s.execute("object.compoundPath.make", &json!({})).unwrap();
|
||||
let ring = s.doc().unwrap().selection.objects[0];
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
(art, ring)
|
||||
}
|
||||
|
||||
fn one_anchor_path(s: &mut Session) -> NodeId {
|
||||
NodeId(s.execute("path.create", &json!({"anchors": [{"x": 5, "y": 5}]})).unwrap()["id"].as_u64().unwrap())
|
||||
}
|
||||
|
||||
fn clip_group(s: &mut Session, ids: &[NodeId]) -> NodeId {
|
||||
s.execute("select.set", &json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>()})).unwrap();
|
||||
NodeId(s.execute("object.clippingMask.make", &json!({})).unwrap()["id"].as_u64().unwrap())
|
||||
}
|
||||
|
||||
fn clip_path_of(s: &Session, id: NodeId) -> vectorcraft_doc::Node {
|
||||
let n = node(s, id);
|
||||
assert!(n.clips(), "{id:?} still clips");
|
||||
(*n.children().unwrap()[0]).clone()
|
||||
}
|
||||
|
||||
/// Unpainted Objects leaves a clipping layer's clipping path, which has no paint of its own.
|
||||
#[test]
|
||||
fn clean_up_keeps_a_layers_clipping_path() {
|
||||
let mut s = session();
|
||||
let (art, ring) = art_and_ring(&mut s);
|
||||
let layer = s.doc().unwrap().current_layer().unwrap();
|
||||
assert!(s.execute("layer.clippingMask.toggle", &json!({"id": layer.0})).unwrap()["clip"].as_bool().unwrap());
|
||||
let r = s.execute("object.path.cleanUp", &json!({})).unwrap();
|
||||
assert_eq!(r["removed"], 0);
|
||||
assert_eq!(clip_path_of(&s, layer).id, ring);
|
||||
assert!(s.doc().unwrap().doc.node(art).is_some());
|
||||
}
|
||||
|
||||
/// Stray Points leaves the one-anchor members of a compound path used as a clipping path.
|
||||
#[test]
|
||||
fn clean_up_keeps_the_members_of_a_compound_clipping_path() {
|
||||
let mut s = session();
|
||||
let (art, ring) = art_and_ring(&mut s);
|
||||
let stray = one_anchor_path(&mut s);
|
||||
let g = clip_group(&mut s, &[art, ring]);
|
||||
s.edit("t", |d, _| {
|
||||
let n = d.remove(stray)?;
|
||||
d.children_mut(Some(ring))?.push(n);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let r = s.execute("object.path.cleanUp", &json!({})).unwrap();
|
||||
assert_eq!(r["removed"], 0);
|
||||
let clip = clip_path_of(&s, g);
|
||||
assert_eq!(clip.id, ring);
|
||||
assert!(clip.children().unwrap().iter().any(|c| c.id == stray));
|
||||
}
|
||||
|
||||
/// No option removes a member of a group used as a clipping path, or empty type used as one.
|
||||
#[test]
|
||||
fn clean_up_keeps_a_group_or_type_used_as_a_clipping_path() {
|
||||
let mut s = session();
|
||||
let art = rect(&mut s, 0.0, 0.0, 100.0, 100.0);
|
||||
let top = rect(&mut s, 10.0, 10.0, 50.0, 50.0);
|
||||
let g = clip_group(&mut s, &[art, top]);
|
||||
let unpainted = rect(&mut s, 20.0, 20.0, 10.0, 10.0);
|
||||
let stray = one_anchor_path(&mut s);
|
||||
let empty = NodeId(s.execute("text.create", &json!({"x": 10, "y": 10, "text": ""})).unwrap()["id"].as_u64().unwrap());
|
||||
let inner = s
|
||||
.edit("t", |d, _| {
|
||||
d.node_mut(unpainted).unwrap().appearance = Appearance::basic(Paint::None, Paint::None, 1.0).into();
|
||||
let members = [top, unpainted, stray, empty].map(|id| d.remove(id)).into_iter().collect::<std::result::Result<Vec<_>, _>>()?;
|
||||
let inner = d.alloc_id();
|
||||
d.children_mut(Some(g))?.insert(0, std::sync::Arc::new(vectorcraft_doc::Node::group(inner, members)));
|
||||
Ok(inner)
|
||||
})
|
||||
.unwrap();
|
||||
let r = s.execute("object.path.cleanUp", &json!({})).unwrap();
|
||||
assert_eq!(r["removed"], 0);
|
||||
let clip = clip_path_of(&s, g);
|
||||
assert_eq!(clip.id, inner);
|
||||
assert_eq!(clip.children().unwrap().iter().map(|c| c.id).collect::<Vec<_>>(), [top, unpainted, stray, empty]);
|
||||
// Type of spaces on top of a clip group clips by nothing, and still stays its clipping path.
|
||||
let art = rect(&mut s, 200.0, 0.0, 100.0, 100.0);
|
||||
let blank = NodeId(s.execute("text.create", &json!({"x": 210, "y": 10, "text": " "})).unwrap()["id"].as_u64().unwrap());
|
||||
let g = clip_group(&mut s, &[art, blank]);
|
||||
let r = s.execute("object.path.cleanUp", &json!({})).unwrap();
|
||||
assert_eq!(r["removed"], 0);
|
||||
assert_eq!(clip_path_of(&s, g).id, blank);
|
||||
}
|
||||
|
||||
/// Offset Path, Simplify, Split Into Grid and Clean Up refuse a param of the wrong type with an
|
||||
/// error naming the param, and leave the document, the selection and the undo history as they were.
|
||||
/// Null keeps a param's default, and a distance still takes a string with a unit.
|
||||
|
||||
Reference in new issue
Block a user