diff --git a/crates/engine/src/script/dom/paths.rs b/crates/engine/src/script/dom/paths.rs index 3e1f5e1266..50d8666389 100644 --- a/crates/engine/src/script/dom/paths.rs +++ b/crates/engine/src/script/dom/paths.rs @@ -625,9 +625,7 @@ fn items_remove_all(h: &super::Host, a: &[Data]) -> Answer { && match &n.kind { NodeKind::Path { .. } => true, // A frame collapsed to a line or a point encloses nothing to clip by. - NodeKind::Image(_) | NodeKind::PlacedDocument(_) => { - n.clip_shapes(None).iter().any(|(frame, _)| kurbo::Shape::area(frame).abs() > 0.0) - } + NodeKind::Image(_) | NodeKind::PlacedDocument(_) => n.clip_shapes(None).iter().any(|(frame, _)| encloses(frame)), NodeKind::Text(t) => { in_mask.contains(&n.id) && !vectorcraft_text::layout(vectorcraft_text::FontDb::global(), t).to_bezpath().elements().is_empty() } @@ -639,10 +637,16 @@ fn items_remove_all(h: &super::Host, a: &[Data]) -> Answer { if n.visible && ((shape && !going.contains(&n.id)) || inner) { geometry.insert(n.id); } + // The live geometry effects of what stays in the clipping path reshape the region it + // clips by, as the renderer draws it; deleting paths that lets an effect shrink the + // region to nothing leaves no outline. A region that was empty before is left as it is. + let reshaped_empty = |m: &vectorcraft_doc::Node| { + vectorcraft_render::effects::reshaped_clip_outline(m).is_some_and(|o| !o.is_some_and(|(bp, _)| encloses(&bp))) + }; if n.clips() && let Some(mask) = kids.first() && affected.contains(&mask.id) - && !geometry.contains(&mask.id) + && (!geometry.contains(&mask.id) || (reshaped_empty(&without(mask, &going, &affected)) && !reshaped_empty(mask))) && kids.iter().skip(1).any(|k| !going.contains(&k.id) && !matches!(k.kind, NodeKind::RulerGuide { .. })) && masks.len() < 20 { @@ -671,6 +675,44 @@ fn items_remove_all(h: &super::Host, a: &[Data]) -> Answer { Ok(Data::Undefined) } +/// Whether a subpath of `outline` encloses an area. Each subpath's area is measured from its first +/// point, so its position does not change the result, and an area within roundoff of its own +/// bounding box's is counted as none. +fn encloses(outline: &kurbo::BezPath) -> bool { + let mut subpaths: Vec = Vec::new(); + for el in outline.elements() { + if matches!(el, kurbo::PathEl::MoveTo(_)) || subpaths.is_empty() { + subpaths.push(kurbo::BezPath::new()); + } + if let Some(last) = subpaths.last_mut() { + last.push(*el); + } + } + subpaths.into_iter().any(|mut sub| { + let Some(start) = sub.elements().iter().find_map(|e| e.end_point()) else { return false }; + sub.apply_affine(kurbo::Affine::translate(-start.to_vec2())); + let area = kurbo::Shape::area(&sub).abs(); + area > 0.0 && area > 64.0 * f64::EPSILON * kurbo::Shape::bounding_box(&sub).area() + }) +} + +/// `n` without the members in `going`, at every depth. Only the members in `affected` (those that +/// hold a member in `going`) are copied; the others are shared. +fn without( + n: &vectorcraft_doc::Node, + going: &std::collections::HashSet, + affected: &std::collections::HashSet, +) -> vectorcraft_doc::Node { + let mut m = n.clone(); + if let Some(kids) = m.children_mut() { + kids.retain(|k| !going.contains(&k.id)); + for k in kids.iter_mut().filter(|k| affected.contains(&k.id)) { + *k = Arc::new(without(k, going, affected)); + } + } + m +} + /// The id in the JSON result of a command that made an object, read exactly, as the text creators /// read it. fn made_id(r: &(Data, Value)) -> std::result::Result { diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index 30250055d5..07e7ec9dfb 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -4018,14 +4018,28 @@ fn document_path_items_remove_all_refuses_to_empty_a_clipping_group_with_an_empt /// an image still has an outline after its paths are deleted. #[test] fn document_path_items_remove_all_keeps_a_clipping_group_whose_clipping_path_keeps_an_image() { - for flat in [false, true] { - remove_all_beside_an_image(flat); + for frame in [Frame::Square, Frame::Line, Frame::Far, Frame::Inset] { + remove_all_beside_an_image(frame); } } -/// A clipping group whose clipping path is a group of one path and an image, the image's frame -/// collapsed to a line when `flat`, then the document's `pathItems.removeAll()`. -fn remove_all_beside_an_image(flat: bool) { +/// The image frame of [`remove_all_beside_an_image`]. +#[derive(Clone, Copy, PartialEq)] +enum Frame { + /// 40 pt square. + Square, + /// Collapsed to a line. + Line, + /// 1/64 pt square 3,500,000 pt from the origin. + Far, + /// 40 pt square with a live Offset Path of -30 pt, which leaves nothing. + Inset, +} + +/// A clipping group whose clipping path is a group of one path and an image with `frame`, then the +/// document's `pathItems.removeAll()`. +fn remove_all_beside_an_image(frame: Frame) { + let flat = matches!(frame, Frame::Line | Frame::Inset); 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()); s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); @@ -4037,7 +4051,11 @@ fn remove_all_beside_an_image(flat: bool) { key: "pic".into(), width: 4, height: 4, - xf: kurbo::Affine::scale_non_uniform(10.0, if flat { 0.0 } else { 10.0 }), + xf: match frame { + Frame::Square | Frame::Inset => kurbo::Affine::scale(10.0), + Frame::Line => kurbo::Affine::scale_non_uniform(10.0, 0.0), + Frame::Far => kurbo::Affine::translate((3_500_000.0, 3_500_000.0)) * kurbo::Affine::scale(1.0 / 256.0), + }, link: None, placement: Default::default(), pixels: None, @@ -4046,6 +4064,9 @@ fn remove_all_beside_an_image(flat: bool) { Ok(id) }) .unwrap(); + if frame == Frame::Inset { + s.execute("effect.apply", &json!({"effect": "path.offsetPath", "params": {"offset": -30}, "ids": [image.0], "item": null})).unwrap(); + } s.execute("select.set", &json!({"ids": [mask.0, t.0]})).unwrap(); let outer = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); s.edit("Clip", |d, _| { diff --git a/docs/mcp.md b/docs/mcp.md index a7b3263f44..52509fe45d 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3249,7 +3249,7 @@ through `$`: `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 is the clipping path when a visible text object that lays out glyphs stays inside that group, because the text then - clips. A visible image or placed document inside that group keeps it clipping by its frame too, when the frame encloses an area. Text beside the clipping path in the clipping group does not count. Hidden text, text + clips. A visible image or placed document inside that group keeps it clipping by its frame too, when the frame encloses an area. When a live geometry effect, such as an Offset Path inset, leaves no clipping region once the paths are deleted, `removeAll()` throws and deletes nothing. Text beside the clipping path in the clipping group does not count. Hidden text, text in a hidden group, blank text without a list marker, 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()` diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index d13d090c99..1634645d88 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1119,7 +1119,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - Scripts: a page item's `translate`, `rotate`, `resize` and `transform` move, turn, scale and transform the object by offsets, an angle, percentages or a `Matrix` record, about its center or a `Transformation` point, with positions, patterns, gradients and line widths following their arguments (https://ai-scripting.docsforadobe.dev/jsobjref/PageItem/). Each call that moves or reshapes the object runs `object.transform` inside the run's transaction, a call that changes only a path's stroke weight runs `stroke.set`, and a call that changes nothing runs no command. `resize` and `transform` on a member of a compound path with `changeLineWidths` at their own scale set the compound's stroke weight; locked art, art on a locked artboard and bad arguments throw an error with nothing changed (`docs/mcp.md`). `crates/engine/src/tests/script_dom_transform.rs`. - Scripts: `Document.placedItems` and `Document.rasterItems` list a document's linked and embedded images; `placedItems.add()` with a `File` assigned to `file` places an image linked, and `PlacedItem.relink(file)` and `embed()` relink and embed it, as the help pages https://ai-scripting.docsforadobe.dev/jsobjref/PlacedItems/, https://ai-scripting.docsforadobe.dev/jsobjref/PlacedItem/, https://ai-scripting.docsforadobe.dev/jsobjref/RasterItems/ and https://ai-scripting.docsforadobe.dev/jsobjref/RasterItem/ describe them. A raster item's bits per channel, channels, color space and transparency come from its encoded pixels' header. Only files the script may read, of at most 256 MiB and holding an image, are placed or relinked. `crates/engine/src/tests/script_dom_images.rs`. - Scripts: `app.preferences` reads, stores and removes values by key with the type they were set as, keeps them across runs and restarts, and turns the warning before a picked script on with the key `ShowExternalJSXWarning`, and off only after the person answers Yes to a confirm (https://ai-scripting.docsforadobe.dev/jsobjref/Preferences/; the confirm is VectorCraft's own rule); `app` makes, joins, inverts and compares `Matrix` records (https://ai-scripting.docsforadobe.dev/jsobjref/Matrix/), rounding each joined value once from its exact sum of products and deciding whether an inverted value overflows from its exact quotient, and runs Copy, Cut and Paste on the active document inside the run's transaction (https://ai-scripting.docsforadobe.dev/jsobjref/Application/). `crates/engine/src/tests/script_dom_prefs.rs`. - - Scripts: `Document.pathItems` lists the paths and makes rectangles, rounded rectangles, ellipses, polygons, stars and empty paths with the defaults the help page https://ai-scripting.docsforadobe.dev/jsobjref/PathItems/ gives; each path's points, closed state, fill rule, area, length, guide state, selected points, fill and stroke colors, stroke options and transforms read and change as https://ai-scripting.docsforadobe.dev/jsobjref/PathItem/, https://ai-scripting.docsforadobe.dev/jsobjref/PathPoints/ and https://ai-scripting.docsforadobe.dev/jsobjref/PathPoint/ describe them, and `RGBColor`, `CMYKColor`, `GrayColor`, `LabColor` and `NoColor` make colors (`docs/mcp.md`). `pathItems["name"]` reads a path by name as `pageItems` does, and `removeAll()` deletes nothing and throws when any path is hidden or locked, is on a locked artboard or is a guide while guides are locked, or when it would delete or empty the clipping path of a group or layer that keeps other objects. It still deletes the paths of a group that is the clipping path when a visible text object that lays out glyphs stays inside that group, because the text then clips, and the same holds for a visible image or placed document whose frame encloses an area; text beside the clipping path does not count, and a clipping group nested in the mask counts only when its own clipping path keeps such geometry. A path takes the page item transforms, and `resize` also sets its stroke weight to a `changeLineWidths` percent from 0 to 100000 (at most 1000 points; a member of a compound sets the compound's stroke); gradients move with positions, so a `changeFillGradients` different from `changePositions` throws an error, as do `changeFillPatterns` and `changeStrokePattern` given as different values (`docs/mcp.md` lists these limits). Gradient stops are not there yet. A `PathPoint` of a path in an Objects on Path group goes stale when an edit of another path in the group moves it. A `PathPoint` of another Global Edit member goes stale when Global Edit changes its path's points. `crates/engine/src/tests/script_dom_paths.rs`. + - Scripts: `Document.pathItems` lists the paths and makes rectangles, rounded rectangles, ellipses, polygons, stars and empty paths with the defaults the help page https://ai-scripting.docsforadobe.dev/jsobjref/PathItems/ gives; each path's points, closed state, fill rule, area, length, guide state, selected points, fill and stroke colors, stroke options and transforms read and change as https://ai-scripting.docsforadobe.dev/jsobjref/PathItem/, https://ai-scripting.docsforadobe.dev/jsobjref/PathPoints/ and https://ai-scripting.docsforadobe.dev/jsobjref/PathPoint/ describe them, and `RGBColor`, `CMYKColor`, `GrayColor`, `LabColor` and `NoColor` make colors (`docs/mcp.md`). `pathItems["name"]` reads a path by name as `pageItems` does, and `removeAll()` deletes nothing and throws when any path is hidden or locked, is on a locked artboard or is a guide while guides are locked, or when it would delete or empty the clipping path of a group or layer that keeps other objects. It still deletes the paths of a group that is the clipping path when a visible text object that lays out glyphs stays inside that group, because the text then clips, and the same holds for a visible image or placed document whose frame encloses an area, unless a live geometry effect then leaves no clipping region; text beside the clipping path does not count, and a clipping group nested in the mask counts only when its own clipping path keeps such geometry. A path takes the page item transforms, and `resize` also sets its stroke weight to a `changeLineWidths` percent from 0 to 100000 (at most 1000 points; a member of a compound sets the compound's stroke); gradients move with positions, so a `changeFillGradients` different from `changePositions` throws an error, as do `changeFillPatterns` and `changeStrokePattern` given as different values (`docs/mcp.md` lists these limits). Gradient stops are not there yet. A `PathPoint` of a path in an Objects on Path group goes stale when an edit of another path in the group moves it. A `PathPoint` of another Global Edit member goes stale when Global Edit changes its path's points. `crates/engine/src/tests/script_dom_paths.rs`. - Scripts: `PlacedItem.trace()` and `RasterItem.trace()` make an Image Trace object and return it as a `PluginItem` whose `tracing` gives its path, anchor and color counts, the source image's resolution, `expandTracing()`, `releaseTracing()` and its `TracingOptions`; assigning an option or loading a preset traces it again, except that the view options (`viewRaster` and `viewVector`) only change the view, as the help pages https://ai-scripting.docsforadobe.dev/jsobjref/PluginItem/, https://ai-scripting.docsforadobe.dev/jsobjref/TracingObject/ and https://ai-scripting.docsforadobe.dev/jsobjref/TracingOptions/ describe them. Option values are checked for type and range before any trace, a script cannot save a preset, and the options without a VectorCraft setting throw. The source image of a traced item is left out of `pageItems`, `placedItems` and `rasterItems`; a script reaches it through `PluginItem.tracing.sourceArt`. Renaming an ordinary group that holds an image first to `Image Trace`, or moving or duplicating an image to the bottom of an ordinary group with that name, throws an error. `crates/engine/src/tests/script_dom_tracing.rs`. - Scripts: a document's `default*` members (https://ai-scripting.docsforadobe.dev/jsobjref/Document/) read and set the fill, stroke and stroke options of the next object drawn through `appearance.setNewArt`, leaving the selection and its art as they were, and `scaleFactor` reads 1 (`docs/mcp.md`). `crates/engine/src/tests/script_dom_defaults.rs`. - Scripts: `app.undo()` and `app.redo()` undo and redo the edits the run made, and each document open before and after the run that it changed gets one undo step. `crates/engine/src/tests/script_dom.rs`.