From 4bbfb2d4aff9a1614856560221982cbeb138684f Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 3 Dec 2026 02:01:46 -0800 Subject: [PATCH] Scripts: pathItems.removeAll counts only type that draws glyphs as a clip outline The document's pathItems.removeAll() counted any visible text object as geometry that keeps a clipping mask clipping. Blank type and area type too short for its first line lay out no glyphs, so the clip renderer clips by nothing for them; deleting the mask's last path then hid the clipped art. - Text counts only when it is visible and its layout gives glyph outlines. - A clipping group nested in the mask counts only its first member. - Hidden objects and members of hidden groups do not count. - Tests cover blank, overset, hidden and nested-clipped type, and type with glyphs that still allows the delete. - docs/mcp.md states the rule. --- crates/engine/src/script/dom/paths.rs | 20 +++- crates/engine/src/tests/script_dom.rs | 140 ++++++++++++++++++++++---- docs/mcp.md | 4 +- 3 files changed, 141 insertions(+), 23 deletions(-) diff --git a/crates/engine/src/script/dom/paths.rs b/crates/engine/src/script/dom/paths.rs index 40db503d05..4ae9d65dc0 100644 --- a/crates/engine/src/script/dom/paths.rs +++ b/crates/engine/src/script/dom/paths.rs @@ -540,7 +540,7 @@ fn items_index(h: &super::Host, a: &[Data]) -> Answer { /// path, one in a hidden or locked layer or group, or one on a locked artboard stops it before /// anything is deleted, as `PageItems.removeAll()` does. So does the clipping path of a group or /// layer that keeps other objects, or a compound path or group that clips one and would be left -/// with no visible path or text. +/// with no visible path or visible type that lays out glyphs to clip by. fn items_remove_all(h: &super::Host, a: &[Data]) -> Answer { if super::layers::is_scoped(a, "PathItems") { return super::layers::scoped_remove_all(h, a); @@ -579,9 +579,21 @@ fn items_remove_all(h: &super::Host, a: &[Data]) -> Answer { if kids.iter().any(|k| affected.contains(&k.id)) { affected.insert(n.id); } - if (matches!(n.kind, NodeKind::Path { .. } | NodeKind::Text(_)) && n.visible && !going.contains(&n.id)) - || kids.iter().any(|k| k.visible && geometry.contains(&k.id)) - { + // A clip outline takes in visible paths and the glyphs of visible type, as + // `push_clip_shapes` does; type that lays out no glyphs (blank, or overset) gives no outline. + let shape = n.visible + && match &n.kind { + NodeKind::Path { .. } => true, + NodeKind::Text(t) => { + t.runs.iter().any(|r| !r.text.trim().is_empty()) + && !vectorcraft_text::layout(vectorcraft_text::FontDb::global(), t).to_bezpath().elements().is_empty() + } + _ => false, + }; + // A clipping group's outline is its first member's alone. + let inner = + if n.clips() { kids.first().is_some_and(|k| geometry.contains(&k.id)) } else { kids.iter().any(|k| geometry.contains(&k.id)) }; + if n.visible && ((shape && !going.contains(&n.id)) || inner) { geometry.insert(n.id); } if n.clips() diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index ab0772e2ea..222c40cf21 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -3421,16 +3421,16 @@ fn document_path_items_remove_all_refuses_to_empty_a_clipping_group_of_paths() { assert_eq!(undo_steps(&s), steps); } -/// A clipping group of a path and text keeps the text's glyph outlines when the document's -/// `pathItems.removeAll()` deletes the path, so the paths are deleted. +/// Type clips by its glyph outlines, so a clipping group that keeps type after its paths go still +/// clips, and the document's `pathItems.removeAll()` deletes every path. #[test] -fn document_path_items_remove_all_deletes_a_path_from_a_clipping_group_that_keeps_text() { - let (mut s, a, _) = doc(); +fn document_path_items_remove_all_keeps_a_clipping_group_that_holds_type() { + let (mut s, a, b) = doc(); let t = NodeId(s.execute("text.create", &json!({"x": 10, "y": 250, "text": "hi"})).unwrap()["id"].as_u64().unwrap()); - let kept = NodeId(s.execute("text.create", &json!({"x": 10, "y": 300, "text": "art"})).unwrap()["id"].as_u64().unwrap()); + let u = NodeId(s.execute("text.create", &json!({"x": 10, "y": 300, "text": "yo"})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [a.0, t.0]})).unwrap(); let mask = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); - s.execute("select.set", &json!({"ids": [mask.0, kept.0]})).unwrap(); + s.execute("select.set", &json!({"ids": [mask.0, u.0]})).unwrap(); let outer = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.edit("Clip", |d, _| { if let Some(NodeKind::Group { children, clip }) = d.node_mut(outer).map(|n| &mut n.kind) { @@ -3445,33 +3445,33 @@ fn document_path_items_remove_all_deletes_a_path_from_a_clipping_group_that_keep assert_eq!(d.node(outer).and_then(|g| g.children()).map(|k| k[0].id), Some(mask)); let steps = undo_steps(&s); let out = log(&mut s, CAUGHT_PATHS_REMOVE_ALL); - assert_eq!(out, [json!(""), json!(0)]); + assert_eq!(out[0], json!(""), "{out:?}"); + assert_eq!(out[1], json!(0)); let d = &s.active().unwrap().doc; - assert!(d.node(a).is_none()); - assert!(d.node(t).is_some()); - assert!(d.node(kept).is_some()); + assert!(d.node(a).is_none() && d.node(b).is_none()); + assert!(d.node(t).is_some() && d.node(u).is_some()); assert_eq!(undo_steps(&s), steps + 1); } -/// Hidden text in a clipping group does not clip, so the document's `pathItems.removeAll()` -/// refuses to delete the group's only visible clipping path. +/// Type with no characters to draw gives no clip outline, so a clipping group whose paths would +/// leave only such type makes the document's `pathItems.removeAll()` throw and delete nothing. #[test] -fn document_path_items_remove_all_refuses_when_only_hidden_text_would_clip() { +fn document_path_items_remove_all_refuses_when_only_blank_type_would_clip() { let (mut s, a, _) = doc(); let t = NodeId(s.execute("text.create", &json!({"x": 10, "y": 250, "text": "hi"})).unwrap()["id"].as_u64().unwrap()); - let kept = NodeId(s.execute("text.create", &json!({"x": 10, "y": 300, "text": "art"})).unwrap()["id"].as_u64().unwrap()); + let u = NodeId(s.execute("text.create", &json!({"x": 10, "y": 300, "text": "yo"})).unwrap()["id"].as_u64().unwrap()); s.execute("select.set", &json!({"ids": [a.0, t.0]})).unwrap(); let mask = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); - s.execute("select.set", &json!({"ids": [mask.0, kept.0]})).unwrap(); + s.execute("select.set", &json!({"ids": [mask.0, u.0]})).unwrap(); let outer = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.edit("Clip", |d, _| { + if let Some(NodeKind::Text(tx)) = d.node_mut(t).map(|n| &mut n.kind) { + tx.runs.iter_mut().for_each(|r| r.text = " ".into()); + } if let Some(NodeKind::Group { children, clip }) = d.node_mut(outer).map(|n| &mut n.kind) { children.sort_by_key(|c| c.id != mask); *clip = true; } - if let Some(n) = d.node_mut(t) { - n.visible = false; - } Ok(()) }) .unwrap(); @@ -3480,6 +3480,110 @@ fn document_path_items_remove_all_refuses_when_only_hidden_text_would_clip() { let out = log(&mut s, CAUGHT_PATHS_REMOVE_ALL); let e = out[0].as_str().unwrap_or_default(); assert!(e.contains("cannot delete a clipping path"), "{out:?}"); + assert!(e.contains(&mask.0.to_string()), "{out:?}"); + assert_eq!(out[1], json!(2)); + assert_eq!(s.active().unwrap().doc, before); + assert_eq!(undo_steps(&s), steps); +} + +/// A clipping group nested in a clipping mask clips by its first member only, so type it clips does +/// not keep the outer mask's outline, and the document's `pathItems.removeAll()` names the outer +/// mask too. +#[test] +fn document_path_items_remove_all_ignores_type_clipped_inside_a_nested_mask() { + let (mut s, a, b) = doc(); + let t = NodeId(s.execute("text.create", &json!({"x": 10, "y": 250, "text": "hi"})).unwrap()["id"].as_u64().unwrap()); + let u = NodeId(s.execute("text.create", &json!({"x": 10, "y": 300, "text": "yo"})).unwrap()["id"].as_u64().unwrap()); + s.execute("select.set", &json!({"ids": [b.0, t.0]})).unwrap(); + let nested = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); + s.execute("select.set", &json!({"ids": [a.0, nested.0]})).unwrap(); + let mask = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); + s.execute("select.set", &json!({"ids": [mask.0, u.0]})).unwrap(); + let outer = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); + s.edit("Clip", |d, _| { + for (g, first) in [(nested, b), (outer, mask)] { + if let Some(NodeKind::Group { children, clip }) = d.node_mut(g).map(|n| &mut n.kind) { + children.sort_by_key(|c| c.id != first); + *clip = true; + } + } + Ok(()) + }) + .unwrap(); + let before = s.active().unwrap().doc.clone(); + let out = log(&mut s, CAUGHT_PATHS_REMOVE_ALL); + let e = out[0].as_str().unwrap_or_default(); + assert!(e.contains("cannot delete a clipping path"), "{out:?}"); + assert!(e.split(": ").last().unwrap_or_default().split(", ").any(|id| id == mask.0.to_string()), "{out:?}"); + assert_eq!(out[1], json!(2)); + assert_eq!(s.active().unwrap().doc, before); +} + +/// Area type too short for its first line lays out no glyphs and gives no clip outline, so a +/// clipping group whose paths would leave only such type makes the document's +/// `pathItems.removeAll()` throw and delete nothing. +#[test] +fn document_path_items_remove_all_refuses_when_only_overset_type_would_clip() { + let (mut s, a, _) = doc(); + let t = NodeId( + s.execute("text.create", &json!({"x": 10, "y": 250, "size": 12, "area": {"width": 100, "height": 1}, "text": "hi"})).unwrap()["id"] + .as_u64() + .unwrap(), + ); + let u = NodeId(s.execute("text.create", &json!({"x": 10, "y": 300, "text": "yo"})).unwrap()["id"].as_u64().unwrap()); + if let Some(NodeKind::Text(tx)) = s.active().unwrap().doc.node(t).map(|n| &n.kind) { + assert!(vectorcraft_text::layout(vectorcraft_text::FontDb::global(), tx).to_bezpath().elements().is_empty()); + } + s.execute("select.set", &json!({"ids": [a.0, t.0]})).unwrap(); + let mask = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); + s.execute("select.set", &json!({"ids": [mask.0, u.0]})).unwrap(); + let outer = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); + s.edit("Clip", |d, _| { + if let Some(NodeKind::Group { children, clip }) = d.node_mut(outer).map(|n| &mut n.kind) { + children.sort_by_key(|c| c.id != mask); + *clip = true; + } + Ok(()) + }) + .unwrap(); + let before = s.active().unwrap().doc.clone(); + let out = log(&mut s, CAUGHT_PATHS_REMOVE_ALL); + let e = out[0].as_str().unwrap_or_default(); + assert!(e.contains("cannot delete a clipping path"), "{out:?}"); + assert!(e.contains(&mask.0.to_string()), "{out:?}"); + assert_eq!(out[1], json!(2)); + assert_eq!(s.active().unwrap().doc, before); +} + +/// Hidden type does not clip, so a clipping group whose paths would leave only hidden type makes +/// the document's `pathItems.removeAll()` throw and delete nothing. +#[test] +fn document_path_items_remove_all_refuses_when_only_hidden_type_would_clip() { + let (mut s, a, _) = doc(); + let t = NodeId(s.execute("text.create", &json!({"x": 10, "y": 250, "text": "hi"})).unwrap()["id"].as_u64().unwrap()); + let u = NodeId(s.execute("text.create", &json!({"x": 10, "y": 300, "text": "yo"})).unwrap()["id"].as_u64().unwrap()); + s.execute("select.set", &json!({"ids": [a.0, t.0]})).unwrap(); + let mask = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); + s.execute("select.set", &json!({"ids": [mask.0, u.0]})).unwrap(); + let outer = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); + s.edit("Clip", |d, _| { + if let Some(n) = d.node_mut(t) { + n.visible = false; + } + if let Some(NodeKind::Group { children, clip }) = d.node_mut(outer).map(|n| &mut n.kind) { + children.sort_by_key(|c| c.id != mask); + *clip = true; + } + Ok(()) + }) + .unwrap(); + let before = s.active().unwrap().doc.clone(); + let steps = undo_steps(&s); + let out = log(&mut s, CAUGHT_PATHS_REMOVE_ALL); + let e = out[0].as_str().unwrap_or_default(); + assert!(e.contains("cannot delete a clipping path"), "{out:?}"); + assert!(e.contains(&mask.0.to_string()), "{out:?}"); + assert_eq!(out[1], json!(2)); assert_eq!(s.active().unwrap().doc, before); assert_eq!(undo_steps(&s), steps); } diff --git a/docs/mcp.md b/docs/mcp.md index 8eb344964e..d6fd6b06fa 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3236,7 +3236,9 @@ through `$`: `removeAll()` on a layer's or group's `pageItems`, `groupItems` or `compoundPathItems`, or on the document's `pathItems`, `groupItems` or `compoundPathItems`, throws and deletes nothing when it would delete the clipping path of a group or layer that keeps other objects. The document's `pathItems.removeAll()` still deletes the paths of a group that - clips when a text object in that group stays, because the text then clips. `remove()` on an object that + clips when a visible text object that lays out glyphs stays in it, because the text then clips. Hidden text, text + in a hidden group, blank text, area text too short for its first line, and text that a clipping group nested in + the mask clips do not keep it clipping, so `removeAll()` then throws and deletes nothing. `remove()` on an object that is hidden or locked, is inside a hidden or locked layer or group, or is a group that holds one, and `removeAll()` when any object is, throw `cannot delete hidden or locked objects` and delete nothing. `remove()` on an object on a locked artboard, and `removeAll()` when any object is on one, throw `cannot delete objects on a locked artboard`