From d6bb6429f4abf4a48df27d505e33741d05846db2 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 27 Nov 2026 23:11:19 -0800 Subject: [PATCH] Scripts: controlBounds, placedItems and rasterItems follow main's empty-object and topmost-first rules On main, a page item's bounds are an Option, and an object with nothing in it reads its bounds as zeros. controlBounds now does the same: it joins the visible bounds with the handle bounds when either exists, and reads zeros when neither does. Main lists pageItems topmost first. placedItems and rasterItems take their order from pageItems, so they list the images topmost first too; docs/mcp.md says so, and the tests that read the newest object or the bottom one by index read it at its new position. The plug-in test main added that builds a ScriptGate by hand now sets the held field this branch added to it. --- crates/engine/src/script/dom.rs | 8 ++++++-- crates/engine/src/script/dom/images.rs | 3 ++- crates/engine/src/tests/plugins.rs | 1 + crates/engine/src/tests/script_dom.rs | 2 +- crates/engine/src/tests/script_dom_images.rs | 9 +++++---- docs/mcp.md | 2 +- 6 files changed, 16 insertions(+), 9 deletions(-) diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index 9fae8ab13e..1a0ecbc44b 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -1811,12 +1811,16 @@ fn item_visible(h: &Host, a: &[Data]) -> Answer { bounds(h, a, true) } -/// `controlBounds`: the visible bounds and the Bézier handles of the item's paths. +/// `controlBounds`: the visible bounds and the Bézier handles of the item's paths, zeros for an +/// object with nothing in it. fn item_control(h: &Host, a: &[Data]) -> Answer { let item = this_item(h, a)?; let (r, f) = item_rect(h, item, true)?; let handles = with_doc(h, item.doc, |_, st| st.doc.node(item.id).and_then(handle_bounds)); - Ok(f.bounds(h.dom.coords.get(), handles.map_or(r, |b| r.union(b)))) + match r.into_iter().chain(handles).reduce(|a, b| a.union(b)) { + Some(r) => Ok(f.bounds(h.dom.coords.get(), r)), + None => Ok(Data::Array(vec![num(0.0), num(0.0), num(0.0), num(0.0)])), + } } /// The bounds of the anchors and handles of `n`'s paths: a path's own, and those of the shown diff --git a/crates/engine/src/script/dom/images.rs b/crates/engine/src/script/dom/images.rs index 921cbef27f..24c40006e2 100644 --- a/crates/engine/src/script/dom/images.rs +++ b/crates/engine/src/script/dom/images.rs @@ -127,7 +127,8 @@ fn pending_handle(d: &Data) -> Option<[f64; 3]> { } } -/// The ids of a document's linked (`placed`) or embedded images in paint order. +/// The ids of a document's linked (`placed`) or embedded images, topmost first as [`page_items`] +/// lists them. fn images(h: &Host, st: &DocState, placed: bool) -> Result>, HostError> { let pick = |l: &Lists| if placed { l.2.clone() } else { l.3.clone() }; if let Some(l) = &*h.dom.images.lists.borrow() diff --git a/crates/engine/src/tests/plugins.rs b/crates/engine/src/tests/plugins.rs index 1ae16ec6a8..c6da92ac55 100644 --- a/crates/engine/src/tests/plugins.rs +++ b/crates/engine/src/tests/plugins.rs @@ -2202,6 +2202,7 @@ fn a_script_loads_no_plugin_from_outside_its_folder_through_a_name_that_is_not_t read: ScriptScope::of_folder(&inside), write: ScriptScope::default(), documents: vec![], + held: vec![], linked: Default::default(), also: None, }; diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index 91598e766a..6eca338c60 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -140,7 +140,7 @@ fn page_item_control_bounds_take_in_the_handles() { let (mut s, _, _) = doc(); let anchors = json!([{"x": 0, "y": 100, "out": [0, 0]}, {"x": 100, "y": 100, "in": [100, 0]}]); s.execute("path.create", &json!({"anchors": anchors})).unwrap(); - let out = log(&mut s, "var p = app.activeDocument.pageItems, i = p[p.length - 1]; vc.log(i.visibleBounds); vc.log(i.controlBounds);"); + let out = log(&mut s, "var i = app.activeDocument.pageItems[0]; vc.log(i.visibleBounds); vc.log(i.controlBounds);"); let b = |v: &Value| -> Vec { v.as_array().unwrap().iter().map(|n| n.as_f64().unwrap()).collect() }; let (visible, control) = (b(&out[0]), b(&out[1])); assert!(visible[1] < 290.0, "the curve stays well under its handles: {visible:?}"); diff --git a/crates/engine/src/tests/script_dom_images.rs b/crates/engine/src/tests/script_dom_images.rs index 8101e17ab0..639020ab18 100644 --- a/crates/engine/src/tests/script_dom_images.rs +++ b/crates/engine/src/tests/script_dom_images.rs @@ -326,9 +326,10 @@ fn raster_metadata_uses_encoded_header_not_rgb_fallback() { &base, "var r = app.activeDocument.rasterItems, t = [];\ for (var i = 0; i < r.length; i++) t.push([r[i].imageColorSpace, r[i].channels, r[i].bitsPerChannel, r[i].transparent, r[i].colorizedGrayscale]);\ - vc.log(t); vc.log(r[0].boundingBox); vc.log(r[1].imageColorSpace === ImageColorSpace.RGB); var m = r[1].matrix; vc.log([m.typename, m.mValueA, m.mValueD]);", + vc.log(t); vc.log(r[1].boundingBox); vc.log(r[0].imageColorSpace === ImageColorSpace.RGB); var m = r[0].matrix; vc.log([m.typename, m.mValueA, m.mValueD]);", ); - assert_eq!(out[0], json!([["ImageColorSpace.Grayscale", 1, 8, false, false], ["ImageColorSpace.RGB", 4, 8, true, false]])); + // The collection lists the images topmost first, as `pageItems` does: the color image placed last comes first. + assert_eq!(out[0], json!([["ImageColorSpace.RGB", 4, 8, true, false], ["ImageColorSpace.Grayscale", 1, 8, false, false]])); assert_eq!(out[1], json!([0, 4, 6, 0])); assert_eq!(out[2], json!(true)); let m = out[3].as_array().unwrap(); @@ -577,9 +578,9 @@ fn a_stale_placed_item_is_refused_as_the_relative_object_of_move() { "var d = app.activeDocument, p = d.placedItems[0], x = null, m = '';\ for (var i = 0; i < d.pageItems.length; i++) { if (d.pageItems[i].typename == 'PathItem') x = d.pageItems[i]; }\ p.embed(); try { x.move(p, ElementPlacement.PLACEAFTER); } catch (e) { m = e.message; } vc.log(m);\ - x.move(d.rasterItems[0], ElementPlacement.PLACEAFTER); vc.log(d.pageItems[0].typename);", + x.move(d.rasterItems[0], ElementPlacement.PLACEAFTER); vc.log([d.pageItems[0].typename, d.pageItems[1].typename]);", ); - assert_eq!(out, vec![json!("Stale page item"), json!("PathItem")]); + assert_eq!(out, vec![json!("Stale page item"), json!(["RasterItem", "PathItem"])]); assert!(s.active().unwrap().doc.node(NodeId(rect)).is_some()); let _ = std::fs::remove_dir_all(base); } diff --git a/docs/mcp.md b/docs/mcp.md index 59bc92d6dd..1b22e2896a 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -2938,7 +2938,7 @@ other files through `$`: keeps the preset's value. A bad value throws an error before anything is written. - `Document.placedItems` (https://ai-scripting.docsforadobe.dev/jsobjref/PlacedItems/) lists the document's linked images and `Document.rasterItems` (https://ai-scripting.docsforadobe.dev/jsobjref/RasterItems/) its embedded ones, - in paint order, each with `length`, `[i]`, `index(key)`, `getByName(name)` and `removeAll()`. `placedItems.add()` + topmost first as `pageItems` lists them, each with `length`, `[i]`, `index(key)`, `getByName(name)` and `removeAll()`. `placedItems.add()` gives a `PlacedItem` with no file; assigning a `File` to its `file` runs `file.place` with `link: true`, and every other member of it throws `PlacedItem.file is required` until then. A `PlacedItem` (https://ai-scripting.docsforadobe.dev/jsobjref/PlacedItem/) has `file`, `relink(file)` (`links.relink`), `embed()`