From cca97099a7cabeaf9fe4635613fd03da77073648 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 18:02:52 -0800 Subject: [PATCH] Scripts: image matrix assignment refuses locked and hidden images Assigning `matrix` to a placed or raster image ran `object.transform` on a locked or hidden image, an image inside a locked or hidden layer or group, and an image on a locked artboard. The transform methods (`translate`, `rotate`, `resize`, `transform`) refuse all of these with nothing changed. The setter now refuses them with the same errors, before the unchanged matrix check, so assigning the current matrix to such an image throws too. The locked artboard check uses the cached lookup that `editable` uses. The setter came in with #9062. Docs: docs/mcp.md. --- crates/engine/src/script/dom/images.rs | 19 ++++++++++- crates/engine/src/script/dom/transform.rs | 6 ++-- crates/engine/src/tests/script_dom_images.rs | 35 ++++++++++++++++++++ docs/mcp.md | 4 ++- 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/script/dom/images.rs b/crates/engine/src/script/dom/images.rs index ec2c7997cf..41711ad12e 100644 --- a/crates/engine/src/script/dom/images.rs +++ b/crates/engine/src/script/dom/images.rs @@ -473,7 +473,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))?; @@ -484,6 +485,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 6c07a070e1..e4834de923 100644 --- a/crates/engine/src/script/dom/transform.rs +++ b/crates/engine/src/script/dom/transform.rs @@ -36,13 +36,13 @@ 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 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 ac1e9ca782..646d4f2e2a 100644 --- a/crates/engine/src/tests/script_dom_images.rs +++ b/crates/engine/src/tests/script_dom_images.rs @@ -497,6 +497,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 90aefa32ce..b560031dc0 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3507,7 +3507,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()`