mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 13:32:45 -05:00
Merge pull request #10632 from dotCooCoo/fix/tracing-mask-art
Scripts: retracing and releasing refuse a tracing with added opacity mask art
This commit is contained in:
4 files changed
+168
-15
No files matched your search
@@ -203,15 +203,22 @@ pub(super) fn remember_source(h: &Host, uid: u64, group: &Node) {
|
||||
}
|
||||
h.dom.tracing.descendants.borrow_mut().entry((uid, group.id.0)).or_insert_with(|| {
|
||||
let mut ids = HashSet::new();
|
||||
group.walk(&mut |n| {
|
||||
if n.id != group.id {
|
||||
ids.insert(n.id);
|
||||
}
|
||||
});
|
||||
// Only children are recorded: an opacity mask is refused whatever its art, so the art of
|
||||
// one held now must not become an object the trace made if the mask is later undone.
|
||||
let mut todo: Vec<&Node> = group.children().into_iter().flatten().map(|c| &**c).collect();
|
||||
while let Some(n) = todo.pop() {
|
||||
ids.insert(n.id);
|
||||
todo.extend(n.children().into_iter().flatten().map(|c| &**c));
|
||||
}
|
||||
ids
|
||||
});
|
||||
}
|
||||
|
||||
/// The objects `n` holds: its children and the art of its opacity mask.
|
||||
fn members(n: &Node) -> impl Iterator<Item = &Node> {
|
||||
n.children().into_iter().flatten().map(|c| &**c).chain(n.extras().mask.as_deref().map(|m| &*m.art))
|
||||
}
|
||||
|
||||
/// Before each command a script runs: notes the active document ([`note_document`]).
|
||||
pub(super) fn before_command(h: &Host) {
|
||||
let s = h.session.borrow();
|
||||
@@ -357,6 +364,7 @@ fn replace(h: &Host, item: Item, id: &str, params: &Value, key: &str) -> Result<
|
||||
}
|
||||
|
||||
/// Throws `refusal` when the Image Trace object of `item` holds an object its trace did not make,
|
||||
/// or has an opacity mask on it or on one of its objects other than the source image, whatever its art,
|
||||
/// and "Cannot edit page item" when it holds a locked object other than its source image, which
|
||||
/// the replacement would discard. The objects looked at are charged to the run either way.
|
||||
fn refuse_added(h: &Host, item: Item, refusal: &str) -> Result<(), HostError> {
|
||||
@@ -380,14 +388,22 @@ fn refuse_added(h: &Host, item: Item, refusal: &str) -> Result<(), HostError> {
|
||||
let source = group.children().and_then(|c| c.first()).map(|n| n.id);
|
||||
let mut visited = 0u64;
|
||||
let mut locked = None;
|
||||
let mut todo: Vec<&Arc<Node>> = group.children().map(|c| c.iter().collect()).unwrap_or_default();
|
||||
// An opacity mask on the tracing or on an object other than the source image is added
|
||||
// artwork whatever the ids of its art: a mask made from traced objects keeps their ids.
|
||||
if group.extras().mask.is_some() {
|
||||
return Ok((visited, Err(HostError::Error(refusal.into()))));
|
||||
}
|
||||
// The tracing's own opacity mask goes with it too; the source image is kept whole.
|
||||
let mut todo: Vec<&Node> = members(group).collect();
|
||||
while let Some(n) = todo.pop() {
|
||||
visited += 1;
|
||||
let added = match (&made, ids) {
|
||||
(Some(m), _) => !m.contains(n.id),
|
||||
(None, Some(ids)) => !ids.contains(&n.id),
|
||||
(None, None) => false,
|
||||
} || newer.is_some_and(|m| n.id.0 >= m);
|
||||
let added = (Some(n.id) != source && n.extras().mask.is_some())
|
||||
|| match (&made, ids) {
|
||||
(Some(m), _) => !m.contains(n.id),
|
||||
(None, Some(ids)) => !ids.contains(&n.id),
|
||||
(None, None) => false,
|
||||
}
|
||||
|| newer.is_some_and(|m| n.id.0 >= m);
|
||||
// Added artwork is reported before a locked object, whichever comes first.
|
||||
if added {
|
||||
return Ok((visited, Err(HostError::Error(refusal.into()))));
|
||||
@@ -395,7 +411,9 @@ fn refuse_added(h: &Host, item: Item, refusal: &str) -> Result<(), HostError> {
|
||||
if n.locked && Some(n.id) != source && locked.is_none() {
|
||||
locked = Some(n.id);
|
||||
}
|
||||
todo.extend(n.children().into_iter().flatten());
|
||||
if Some(n.id) != source {
|
||||
todo.extend(members(n));
|
||||
}
|
||||
}
|
||||
Ok((visited, locked.map_or(Ok(()), |id| Err(HostError::Error(format!("Cannot edit page item {}", id.0))))))
|
||||
})?;
|
||||
|
||||
@@ -1461,6 +1461,138 @@ fn retrace_and_release_refuse_locked_traced_artwork() {
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// The art of an opacity mask given to the tracing or to an object the trace made is added artwork
|
||||
/// too: retracing and releasing discard it, so both refuse, before the document changes.
|
||||
#[test]
|
||||
fn retrace_and_release_refuse_added_opacity_mask_art() {
|
||||
let base = folder("mask-art");
|
||||
for on_group in [false, true] {
|
||||
for (action, refusal) in [
|
||||
("p.tracing.tracingOptions.threshold = 90;", "Cannot retrace a tracing containing added artwork"),
|
||||
("p.tracing.releaseTracing();", "Cannot release a tracing containing added artwork"),
|
||||
] {
|
||||
let (mut s, image) = session(&base, false);
|
||||
let r = s.execute("imageTrace.make", &json!({"id": image.0})).unwrap();
|
||||
let group = NodeId(r["id"].as_u64().unwrap());
|
||||
let made = s.active().unwrap().doc.node(group).unwrap().children().unwrap()[1].id;
|
||||
let masked = if on_group { group } else { made };
|
||||
s.edit("Mask traced artwork", |d, _| {
|
||||
let art = crate::doc::Node::group(d.alloc_id(), vec![]);
|
||||
d.node_mut(masked).unwrap().extras_mut().mask = Some(Box::new(vectorcraft_doc::OpacityMask::new(art, true)));
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let before = vectorcraft_format::save(&s.active().unwrap().doc, false);
|
||||
let steps = undo_steps(&s);
|
||||
let out = log(
|
||||
&mut s,
|
||||
&base,
|
||||
&format!("var p = app.activeDocument.pageItems[0], m = ''; try {{ {action} }} catch (e) {{ m = e.message; }} vc.log(m);"),
|
||||
);
|
||||
assert_eq!(out, [json!(refusal)], "{on_group} {action}");
|
||||
assert_eq!(vectorcraft_format::save(&s.active().unwrap().doc, false), before, "{on_group} {action}");
|
||||
assert_eq!(undo_steps(&s), steps, "{on_group} {action}");
|
||||
}
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// An opacity mask made from two objects the trace made keeps the mask art's id, which the tracing
|
||||
/// records as made, but retracing and releasing still discard it, so both refuse.
|
||||
#[test]
|
||||
fn retrace_and_release_refuse_an_opacity_mask_made_from_traced_paths() {
|
||||
let base = folder("traced-mask");
|
||||
for (action, refusal) in [
|
||||
("p.tracing.tracingOptions.threshold = 90;", "Cannot retrace a tracing containing added artwork"),
|
||||
("p.tracing.releaseTracing();", "Cannot release a tracing containing added artwork"),
|
||||
] {
|
||||
let (mut s, image) = session(&base, false);
|
||||
let r = s.execute("imageTrace.make", &json!({"id": image.0})).unwrap();
|
||||
let group = NodeId(r["id"].as_u64().unwrap());
|
||||
let kids: Vec<NodeId> = s.active().unwrap().doc.node(group).unwrap().children().unwrap().iter().map(|n| n.id).collect();
|
||||
assert!(kids.len() > 2, "{kids:?}");
|
||||
s.execute("select.set", &json!({"ids": [kids[1].0, kids[2].0]})).unwrap();
|
||||
s.execute("transparency.makeOpacityMask", &json!({})).unwrap();
|
||||
let g = s.active().unwrap().doc.node(group).unwrap();
|
||||
assert!(g.children().unwrap().iter().any(|n| n.extras().mask.is_some()), "a traced path holds the mask");
|
||||
let before = vectorcraft_format::save(&s.active().unwrap().doc, false);
|
||||
let steps = undo_steps(&s);
|
||||
let out = log(
|
||||
&mut s,
|
||||
&base,
|
||||
&format!("var p = app.activeDocument.pageItems[0], m = ''; try {{ {action} }} catch (e) {{ m = e.message; }} vc.log(m);"),
|
||||
);
|
||||
assert_eq!(out, [json!(refusal)], "{action}");
|
||||
assert_eq!(vectorcraft_format::save(&s.active().unwrap().doc, false), before, "{action}");
|
||||
assert_eq!(undo_steps(&s), steps, "{action}");
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// A tracing saved without the record of what its trace made is checked against what it held when
|
||||
/// the run first saw it; an opacity mask it already held then still counts as added artwork.
|
||||
#[test]
|
||||
fn retrace_and_release_refuse_a_mask_on_a_tracing_without_its_record() {
|
||||
let base = folder("unrecorded-mask");
|
||||
for (action, refusal) in [
|
||||
("p.tracing.tracingOptions.threshold = 90;", "Cannot retrace a tracing containing added artwork"),
|
||||
("p.tracing.releaseTracing();", "Cannot release a tracing containing added artwork"),
|
||||
] {
|
||||
let (mut s, image) = session(&base, false);
|
||||
let r = s.execute("imageTrace.make", &json!({"id": image.0})).unwrap();
|
||||
let group = NodeId(r["id"].as_u64().unwrap());
|
||||
let made = s.active().unwrap().doc.node(group).unwrap().children().unwrap()[1].id;
|
||||
s.edit("Mask traced artwork of an older tracing", |d, _| {
|
||||
if let Some(t) = d.node_mut(group).unwrap().extras_mut().trace.as_deref_mut().and_then(Value::as_object_mut) {
|
||||
t.remove(vectorcraft_doc::trace::TRACE_MADE);
|
||||
}
|
||||
let art = crate::doc::Node::group(d.alloc_id(), vec![]);
|
||||
d.node_mut(made).unwrap().extras_mut().mask = Some(Box::new(vectorcraft_doc::OpacityMask::new(art, true)));
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
assert!(s.active().unwrap().doc.node(group).unwrap().trace_made().is_none());
|
||||
let before = vectorcraft_format::save(&s.active().unwrap().doc, false);
|
||||
let steps = undo_steps(&s);
|
||||
let out = log(
|
||||
&mut s,
|
||||
&base,
|
||||
&format!("var p = app.activeDocument.pageItems[0], m = ''; try {{ {action} }} catch (e) {{ m = e.message; }} vc.log(m);"),
|
||||
);
|
||||
assert_eq!(out, [json!(refusal)], "{action}");
|
||||
assert_eq!(vectorcraft_format::save(&s.active().unwrap().doc, false), before, "{action}");
|
||||
assert_eq!(undo_steps(&s), steps, "{action}");
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// The source image keeps its opacity mask through retracing and releasing, so its mask art is
|
||||
/// not added artwork.
|
||||
#[test]
|
||||
fn retrace_and_release_take_a_source_image_with_an_opacity_mask() {
|
||||
let base = folder("source-mask");
|
||||
for action in ["p.tracing.tracingOptions.threshold = 90;", "p.tracing.releaseTracing();"] {
|
||||
let (mut s, image) = session(&base, false);
|
||||
s.edit("Mask the image", |d, _| {
|
||||
let art = crate::doc::Node::group(d.alloc_id(), vec![]);
|
||||
d.node_mut(image).unwrap().extras_mut().mask = Some(Box::new(vectorcraft_doc::OpacityMask::new(art, true)));
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let mask = s.active().unwrap().doc.node(image).unwrap().extras().mask.clone();
|
||||
s.execute("imageTrace.make", &json!({"id": image.0})).unwrap();
|
||||
assert_eq!(s.active().unwrap().doc.node(image).unwrap().extras().mask, mask, "imageTrace.make: {action}");
|
||||
let out = log(
|
||||
&mut s,
|
||||
&base,
|
||||
&format!("var p = app.activeDocument.pageItems[0], m = ''; try {{ {action} }} catch (e) {{ m = e.message; }} vc.log(m);"),
|
||||
);
|
||||
assert_eq!(out, [json!("")], "{action}");
|
||||
assert_eq!(s.active().unwrap().doc.node(image).unwrap().extras().mask, mask, "{action}");
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// The `TracingOptions` and `TracingObject` members that are not implemented are left out of
|
||||
/// `hasOwnProperty` and `reflect`, and still throw when read.
|
||||
#[test]
|
||||
|
||||
+5
-2
@@ -3702,11 +3702,14 @@ through `$`:
|
||||
`viewVector` does not trace again: it runs `imageTrace.setView`. The `PluginItem`, `TracingObject`
|
||||
and `TracingOptions` follow the new object, and a run traces again at most 10,000 times. Tracing again throws
|
||||
`Cannot retrace a tracing containing added artwork`, with nothing changed, when the tracing holds an object its trace
|
||||
did not make, whether this run, an earlier run or a command added it, and `releaseTracing()` throws
|
||||
did not make, whether this run, an earlier run or a command added it (any opacity mask on the tracing or on an object
|
||||
the trace made counts, whether or not the trace made its art; the source image's own mask does not, since both keep
|
||||
the image whole), and `releaseTracing()` throws
|
||||
`Cannot release a tracing containing added artwork` in the same case. When no object was added, both throw
|
||||
`Cannot edit page item <id>`, with nothing changed, when an object the trace made is locked. `imageTrace.make` records the objects it made in
|
||||
the tracing's `trace` settings as `made`, ranges of ids `[[first, last], ...]`, and a copy of the tracing records
|
||||
their copies. A tracing saved without `made` is checked only for objects added in the same run. `threshold` takes a whole
|
||||
their copies. A tracing saved without `made` is checked only for objects added in the same run, but an opacity
|
||||
mask counts with or without `made`, even one it held before the run. `threshold` takes a whole
|
||||
number from 0 to 255, `maxColors` from 2 to 256 and `minArea` (the Noise setting, in pixels) from 0 to the largest
|
||||
image the tracer reads; `fills` and `strokes` cannot both be `false`; `palette` is `""` (automatic), `"Limited"`,
|
||||
`"Full Tone"` or `"Document Library"`; `viewRaster` and `viewVector` take only the pairs that match an Image Trace
|
||||
|
||||
@@ -1126,7 +1126,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
|
||||
- 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`). For ordinary layer placement, the creators use the topmost shown, unlocked layer when the active layer is locked or hidden. The call also throws and makes no path when the document has layers and every one is locked or hidden. `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; 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. Loading the built-in preset the tracing already has, by its name with or without brackets (`[Default]`), with unchanged options does not trace again. 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`. `remove()` also refuses a shown source image. 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, and so does a `groupItems.removeAll()` or `compoundPathItems.removeAll()` that would leave an image at the bottom of such a group. `crates/engine/src/tests/script_dom_tracing.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. Loading the built-in preset the tracing already has, by its name with or without brackets (`[Default]`), with unchanged options does not trace again. 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. Tracing again and `releaseTracing()` throw, with nothing changed, when the tracing holds added artwork, an opacity mask on the tracing or on an object the trace made included; the source image keeps its own mask. A tracing saved without `made` is checked only for artwork added during the same run, so tracing it again or releasing it discards artwork that was added before the run without an error, and the opacity mask check applies with or without `made`. The source image of a traced item is left out of `pageItems`, `placedItems` and `rasterItems`; a script reaches it through `PluginItem.tracing.sourceArt`. `remove()` also refuses a shown source image. 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, and so does a `groupItems.removeAll()` or `compoundPathItems.removeAll()` that would leave an image at the bottom of such a group. `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`.
|
||||
- Scripts: `document.textFrames` makes point type, area type and type on a path, and each text frame gives its kind, its contents, its text as text ranges with their characters, words and paragraphs (which can add text), their character attributes (size, leading, tracking, baseline shift, scales, rotation, underline, strikethrough, font, fill and stroke colors, stroke weight) and paragraph attributes (alignment, indents, space before and after), outlines and point and area conversion; `app.textFonts` lists the fonts, as the help pages https://ai-scripting.docsforadobe.dev/jsobjref/TextFrameItems/, https://ai-scripting.docsforadobe.dev/jsobjref/TextFrameItem/, https://ai-scripting.docsforadobe.dev/jsobjref/TextRange/, https://ai-scripting.docsforadobe.dev/jsobjref/CharacterAttributes/ and https://ai-scripting.docsforadobe.dev/jsobjref/TextFonts/ describe them (`docs/mcp.md`). Offsets count UTF-16 units, VectorCraft's own rule. Lines, tab stops are not there yet. `crates/engine/src/tests/script_text.rs`.
|
||||
|
||||
Reference in new issue
Block a user