Scripts: empty objects read zero control bounds and turn or scale as no-ops

- controlBounds of an object with nothing in it reads zeros, like its
  geometric and visible bounds.
- path.rotate and path.resize of a path with no anchors change nothing.
- The path tests make the first rectangle last, so pathItems[0] is the
  topmost path as pageItems lists it.
This commit is contained in:
dotCooCoo committed 2026-11-26 00:27:19 -08:00
1 parent 0e4afdad8c
commit 637facb057
5 files changed
+35 -17

No files matched your search

+6 -3
View File
@@ -1851,10 +1851,13 @@ fn item_control(h: &Host, a: &[Data]) -> Answer {
let (r, f) = with_doc(h, item.doc, |_, st| {
let visible = st.doc.bounds_of(&[item.id], true);
let handles = st.doc.node(item.id).and_then(handles_box);
let r = vectorcraft_geom::union_opt(visible, handles).ok_or_else(|| HostError::Error("the object has no bounds".into()))?;
Ok::<_, HostError>((r, frame(st)?))
Ok::<_, HostError>((vectorcraft_geom::union_opt(visible, handles), frame(st)?))
})?;
Ok(f.bounds(h.dom.coords.get(), r))
// An object with nothing in it has no bounds, and they read as zeros like its other bounds.
match r {
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)])),
}
}
fn item_position(h: &Host, a: &[Data]) -> Answer {
+10 -7
View File
@@ -58,9 +58,9 @@ use vectorcraft_js::{Data, HostClass, HostError, HostItem, HostMember, Realm, Ru
use vectorcraft_tools::params::gradient_params;
use super::{
Answer, Coords, Doc, Item, Link, NOT_FOUND, STALE_ITEM, arg, boolean, class, class_text, colors, command, command_value, coordinate, edit_item, editable,
find_doc, float, frame, get, given, handle_of, id_of, id_parts, item_of, item_rect, later, method, named, num, number, on_doc, pair, point,
point_get, point_method, point_property, property, this_doc, typename_of, whole, with_doc,
Answer, Coords, Doc, Item, Link, NOT_FOUND, STALE_ITEM, arg, boolean, class, class_text, colors, command, command_value, coordinate, edit_item,
editable, find_doc, float, frame, get, given, handle_of, id_of, id_parts, item_of, item_rect, later, method, named, num, number, on_doc, pair,
point, point_get, point_method, point_property, property, this_doc, typename_of, whole, with_doc,
};
use crate::{DocState, NodeId, NodeKind};
@@ -1202,8 +1202,10 @@ fn path_rotate(h: &super::Host, a: &[Data]) -> Answer {
let place = about_name(arg(a, 6))?;
let mut params = json!({"angle": angle});
transform_options(a, 2, &mut params)?;
if angle.rem_euclid(360.0) != 0.0 {
let (r, _) = item_rect(h, item, false)?;
// A path with no anchors has no bounds to turn about, and turning it changes nothing.
if angle.rem_euclid(360.0) != 0.0
&& let (Some(r), _) = item_rect(h, item, false)?
{
let origin = about(h, item, place, r)?;
params["origin"] = json!([origin.x, origin.y]);
edit_path(h, item, "object.rotate", params)?;
@@ -1234,8 +1236,9 @@ fn path_resize(h: &super::Host, a: &[Data]) -> Answer {
}
// The scale and the new stroke weight are one edit: a failure of either undoes both.
super::atomically(h, |h| {
if sx != 100.0 || sy != 100.0 {
let (r, _) = item_rect(h, item, false)?;
if (sx != 100.0 || sy != 100.0)
&& let (Some(r), _) = item_rect(h, item, false)?
{
let origin = about(h, item, place, r)?;
params["origin"] = json!([origin.x, origin.y]);
edit_path(h, item, "object.scale", params)?;
+4 -4
View File
@@ -717,10 +717,10 @@ fn selecting_an_item_keeps_the_direct_selected_anchors_of_others() {
s.execute("select.add", &json!({"ids": [b.0]})).unwrap();
let anchors = s.active().unwrap().selection.anchors.clone();
assert!(anchors.contains_key(&a));
log(&mut s, "app.activeDocument.pageItems[1].selected = false;");
log(&mut s, "app.activeDocument.pageItems[0].selected = false;");
let sel = &s.active().unwrap().selection;
assert_eq!((sel.objects.clone(), sel.anchors.clone()), (vec![a], anchors.clone()));
log(&mut s, "app.activeDocument.pageItems[1].selected = true;");
log(&mut s, "app.activeDocument.pageItems[0].selected = true;");
let sel = &s.active().unwrap().selection;
assert_eq!((sel.objects.clone(), sel.anchors.get(&a)), (vec![a, b], anchors.get(&a)));
}
@@ -2428,7 +2428,7 @@ fn every_page_item_has_its_layer_and_members_check_this() {
let out = log(
&mut s,
"var d = app.activeDocument, p = d.pageItems[0], n = d.layers.add(); n.name = 'Up'; p.move(n);\
vc.log(p.layer.name); vc.log(d.pageItems[0].layer.name); vc.log(p.layer.layers.typename);\
vc.log(p.layer.name); vc.log(d.pageItems[1].layer.name); vc.log(p.layer.layers.typename);\
vc.log(n.pageItems.typename); vc.log(n.groupItems.typename);",
);
assert_eq!(out, [json!("Up"), json!("Layer 1"), json!("Layers"), json!("PageItems"), json!("GroupItems")]);
@@ -2542,7 +2542,7 @@ fn page_items_report_their_available_members_as_their_own() {
);
assert_eq!(out[0], json!([true, true, true, true, true, true, true]));
// Members that are not available yet, unknown names and inherited ones are not the object's own.
assert_eq!(out[1], json!([false, false, false, false, false, false]));
assert_eq!(out[1], json!([false, true, true, false, false, false]));
// `for`-`in` lists the properties, not the methods or the members that are not available yet.
assert_eq!(out[2], json!([true, true, -1, -1]));
}
+11 -2
View File
@@ -30,8 +30,9 @@ fn rect(s: &mut Session, x: f64, y: f64, w: f64, h: f64) -> NodeId {
fn doc() -> (Session, NodeId, NodeId) {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
let a = rect(&mut s, 10.0, 20.0, 30.0, 40.0);
// `pathItems[0]` is the topmost path, so `a` is made last.
let b = rect(&mut s, 100.0, 100.0, 50.0, 50.0);
let a = rect(&mut s, 10.0, 20.0, 30.0, 40.0);
s.execute("select.none", &json!({})).unwrap();
(s, a, b)
}
@@ -564,6 +565,14 @@ fn identity_transforms_of_an_empty_path_do_nothing() {
}
}
#[test]
fn turning_or_scaling_an_empty_path_changes_nothing() {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
log(&mut s, "var n = app.activeDocument.pathItems.add(); n.rotate(90); n.resize(50, 50);");
assert_eq!(undo_steps(&s), 1, "only the path is added");
}
/// The paint of the fill (`fill`) or stroke of `id`.
fn paint_of(s: &Session, id: NodeId, fill: bool) -> Paint {
let n = node(s, id);
@@ -935,7 +944,7 @@ fn locked_paths_and_wrong_objects_are_refused() {
}
assert_eq!(art(&s), before);
s.execute("text.create", &json!({"x": 10, "y": 250, "text": "hi"})).unwrap();
let e = failure(&mut s, "var t = app.activeDocument.pageItems[2]; Object.getOwnPropertyDescriptor(PathItem.prototype, 'closed').get.call(t);");
let e = failure(&mut s, "var t = app.activeDocument.pageItems[0]; Object.getOwnPropertyDescriptor(PathItem.prototype, 'closed').get.call(t);");
assert!(e.contains("not a PathItem"), "{e}");
let e = failure(&mut s, "Object.getOwnPropertyDescriptor(PathPoint.prototype, 'anchor').get.call({});");
assert!(e.contains("not a PathPoint"), "{e}");
@@ -153,7 +153,10 @@ const INSIDE: &[(&str, &str)] = &[
width width= wrapInside wrapInside= wrapOffset wrapOffset= wrapped wrapped= zOrder zOrderPosition",
),
("PathItems.", "prototype"),
("PathItems.prototype.", "add constructor ellipse getByName index length parent polygon rectangle removeAll roundedRectangle star toString typename"),
(
"PathItems.prototype.",
"add constructor ellipse getByName index length parent polygon rectangle removeAll roundedRectangle star toString typename",
),
("PathItem.", "prototype"),
(
"PathItem.prototype.",