diff --git a/crates/engine/src/script/dom/images.rs b/crates/engine/src/script/dom/images.rs index f9e07bc2d3..60f322e79f 100644 --- a/crates/engine/src/script/dom/images.rs +++ b/crates/engine/src/script/dom/images.rs @@ -481,7 +481,8 @@ fn area_of(m: Affine) -> bool { /// `matrix = Matrix`: `object.transform` of the image by what takes its pixels to the new matrix. /// A matrix with no area, one that puts a corner of the image outside the canvas, or a distorted -/// image is refused before anything changes; the matrix the image has already changes nothing. +/// image is refused before anything changes, and so is a locked or hidden image or one on a locked +/// artboard, as the transform methods refuse them; the matrix the image has already changes nothing. fn set_matrix(h: &Host, a: &[Data], cls: &str) -> Answer { let item = this_of(h, a, cls)?; let m = matrix_value(arg(a, 1))?; @@ -492,6 +493,22 @@ fn set_matrix(h: &Host, a: &[Data], cls: &str) -> Answer { if n.is_distorted_image() { return Err(HostError::Error(DISTORTED.into())); } + // The transform methods' refusals: a locked or hidden image, one inside a locked or hidden + // layer or group, and one on a locked artboard, whether or not the matrix changes. + let d = &st.doc; + let line = d.ancestry(item.id); + let any = |f: fn(&vectorcraft_doc::Node) -> bool| { + line.as_ref().is_none_or(|l| l.iter().chain(std::iter::once(&item.id)).any(|i| d.node(*i).is_none_or(f))) + }; + if any(|n| n.locked) { + return Err(HostError::Error(format!("{}: {}", super::transform::LOCKED, item.id.0))); + } + if any(|n| !n.visible) { + return Err(HostError::Error(format!("{}: {}", super::transform::HIDDEN, item.id.0))); + } + if super::on_locked_artboard(h, st, item.id) { + return Err(HostError::Error(format!("{}: {}", super::transform::ON_LOCKED_ARTBOARD, item.id.0))); + } let f = frame(st)?; let target = Affine::new(match h.dom.coords.get() { Coords::Document => [m[0], -m[1], m[2], -m[3], m[4] + f.left0, f.bottom0 - m[5]], diff --git a/crates/engine/src/script/dom/transform.rs b/crates/engine/src/script/dom/transform.rs index 1c6bbfc280..3bf88a577c 100644 --- a/crates/engine/src/script/dom/transform.rs +++ b/crates/engine/src/script/dom/transform.rs @@ -41,16 +41,16 @@ use super::matrix::{self, Matrix}; use super::{Answer, Coords, Host, Item, arg, atomically, coordinate, edit_item, frame, number, paths, this_item, with_doc}; /// The error for an object on a locked artboard. -const ON_LOCKED_ARTBOARD: &str = "cannot transform objects on a locked artboard"; +pub(super) const ON_LOCKED_ARTBOARD: &str = "cannot transform objects on a locked artboard"; /// The error for a locked object. -const LOCKED: &str = "cannot transform locked objects"; +pub(super) const LOCKED: &str = "cannot transform locked objects"; /// The error for a guide while guides are locked. const GUIDES_LOCKED: &str = "cannot transform a guide while guides are locked"; /// The error for a hidden object. -const HIDDEN: &str = "cannot transform hidden objects"; +pub(super) const HIDDEN: &str = "cannot transform hidden objects"; /// The identity matrix. const IDENTITY: Matrix = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]; diff --git a/crates/engine/src/tests/script_dom_images.rs b/crates/engine/src/tests/script_dom_images.rs index 8f50cd97cc..8fc2876981 100644 --- a/crates/engine/src/tests/script_dom_images.rs +++ b/crates/engine/src/tests/script_dom_images.rs @@ -536,6 +536,41 @@ fn a_stale_placed_item_is_not_a_relative_object() { let _ = std::fs::remove_dir_all(base); } +#[test] +fn image_matrix_refuses_locked_hidden_and_locked_artboard_images() { + let base = folder("matrix-locks"); + let g = base.join("main/g.png"); + std::fs::write(&g, gray_png(10, 20)).unwrap(); + let mut s = session(); + let id = place(&mut s, &g, true); + // A moved matrix and the matrix the image already has are refused alike. + let sets = [ + "var p = app.activeDocument.placedItems[0], m = p.matrix; m.mValueTX = m.mValueTX + 5; p.matrix = m;", + "var p = app.activeDocument.placedItems[0]; p.matrix = p.matrix;", + ]; + let refused = |s: &mut Session, message: &str| { + for set in sets { + let (was, before) = (s.active().unwrap().doc.clone(), undo_steps(s)); + let e = failure(s, &base, set); + assert!(e.contains(message), "{set}: {e}"); + assert_eq!(*s.active().unwrap().doc, *was, "{set}"); + assert_eq!(undo_steps(s), before, "{set}"); + } + }; + for (field, set_to, message) in [("locked", true, "cannot transform locked objects"), ("visible", false, "cannot transform hidden objects")] { + s.execute("object.setProps", &json!({"ids": [id.0], field: set_to})).unwrap(); + refused(&mut s, message); + s.execute("object.setProps", &json!({"ids": [id.0], field: !set_to})).unwrap(); + } + s.edit("Lock Artboard", |d, _| { + d.artboards[0].locked = true; + Ok(()) + }) + .unwrap(); + refused(&mut s, "cannot transform objects on a locked artboard"); + let _ = std::fs::remove_dir_all(base); +} + #[test] fn image_matrix_refuses_bad_values_without_changing_the_image() { let base = folder("matrix-bad"); diff --git a/docs/mcp.md b/docs/mcp.md index 752bdfba22..244db90ff5 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3536,7 +3536,9 @@ through `$`: that maps the image's pixels, row 0 at the top, to script coordinates, and a distorted image has none. Assigning `matrix` runs `object.transform`, except that a matrix equal to the one `matrix` reads runs no command; a value that is not finite, a matrix with no area, one that puts a corner of the image outside the canvas and one that a - nearly singular image cannot reach exactly throw an error with nothing changed. `boundingBox` is the pixel rectangle at one point + nearly singular image cannot reach exactly throw an error with nothing changed. Assigning `matrix` to a locked or + hidden image, one inside a locked or hidden layer or group, or one on a locked artboard throws the transform methods' + error with nothing changed, even when the matrix is unchanged. `boundingBox` is the pixel rectangle at one point a pixel. `removeAll()` refuses images as `pageItems.removeAll()` refuses objects, and so throws `cannot delete objects on a locked artboard` and deletes nothing when an image is on one. Assigning `file` or `matrix` checks the item before the value, so a stale item throws `Stale page item` whatever the value. Assigning `matrix` to an `add()`