diff --git a/crates/engine/src/script/dom/tracing.rs b/crates/engine/src/script/dom/tracing.rs index a37a062849..04fac60a66 100644 --- a/crates/engine/src/script/dom/tracing.rs +++ b/crates/engine/src/script/dom/tracing.rs @@ -199,15 +199,24 @@ 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); + // The source image is kept whole, so the art of its own opacity mask is not looked at. + let source = group.children().and_then(|c| c.first()).map(|n| n.id); + let mut todo: Vec<&Node> = members(group).collect(); + while let Some(n) = todo.pop() { + ids.insert(n.id); + if Some(n.id) != source { + todo.extend(members(n)); } - }); + } ids }); } +/// The objects `n` holds: its children and the art of its opacity mask. +fn members(n: &Node) -> impl Iterator { + n.children().into_iter().flatten().map(|c| &**c).chain(n.extras().mask.as_deref().map(|m| &*m.art)) +} + pub(super) fn same_source(h: &Host, uid: u64, original: NodeId, group: &Node) -> bool { let source = h.dom.tracing.sources.borrow().get(&(uid, original.0)).copied(); source.is_some_and(|source| group.children().and_then(|c| c.first()).is_some_and(|n| n.id == source)) @@ -321,7 +330,8 @@ 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, +/// Throws `refusal` when the Image Trace object of `item` holds an object its trace did not make +/// (the art of an opacity mask on it or on one of its objects other than the source image included), /// 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> { @@ -338,7 +348,8 @@ 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> = group.children().map(|c| c.iter().collect()).unwrap_or_default(); + // 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) { @@ -353,7 +364,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)))))) })?; diff --git a/crates/engine/src/tests/script_dom_tracing.rs b/crates/engine/src/tests/script_dom_tracing.rs index 24223785dd..e6e3f4dfa1 100644 --- a/crates/engine/src/tests/script_dom_tracing.rs +++ b/crates/engine/src/tests/script_dom_tracing.rs @@ -1253,6 +1253,69 @@ 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); +} + +/// 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] diff --git a/docs/mcp.md b/docs/mcp.md index e716a1a08e..4f76bea4f6 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3365,7 +3365,8 @@ 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 (the art of an opacity mask on the tracing or on an + object the trace made counts; 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 `, 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