Merge pull request #8339 from dotCooCoo/fix/bounds-reference-number

Fix #8334: setBounds reads whole-number float references correctly
This commit is contained in:
BFlat
2026-11-18 03:20:23 +07:00
committed by GitHub
5 changed files with 86 additions and 4 deletions
+10 -2
View File
@@ -56,6 +56,7 @@ pub(crate) fn object_bounds_transform(s: &Session, p: &Value, id: NodeId) -> Res
/// direct-selected anchors are what changes ([`Bounds::Anchors`]).
fn bounds_transform(s: &Session, p: &Value, ids: Vec<NodeId>, least: f64, anchors: bool) -> Result<Bounds> {
check_numbers(p, &["x", "y", "width", "height", "reference"], "object.setBounds")?;
let refi = reference_index(p)?;
let st = s.doc()?;
check_switches(p, &TRANSFORM_SWITCHES, "object.setBounds")?;
check_switches(p, &["proportional"], "object.setBounds")?;
@@ -90,7 +91,6 @@ fn bounds_transform(s: &Session, p: &Value, ids: Vec<NodeId>, least: f64, anchor
let margins = |b: Rect| [g.x0 - b.x0, g.y0 - b.y0, b.x1 - g.x1, b.y1 - g.y1];
let (m, all) = (margins(vs), margins(v));
let fixed = [all[0] - m[0], all[1] - m[1], all[2] - m[2], all[3] - m[3]];
let refi = p.get("reference").and_then(Value::as_u64).unwrap_or(4).min(8) as usize;
let rp = vectorcraft_geom::reference_point(v, refi);
let (w, h) = wanted_size(p, v);
// The geometric scale that gives the visual size: margins grow by the strokes' scale `f`. It is
@@ -262,7 +262,7 @@ fn wanted_size(p: &Value, b: Rect) -> (f64, f64) {
/// `b`, the box around the anchors (and the objects selected whole) square to the page, and only
/// the anchors, their handles with them, and those objects move or scale.
fn set_anchor_bounds(s: &mut Session, p: &Value, ids: Vec<NodeId>, b: Rect) -> Result<Value> {
let refi = p.get("reference").and_then(Value::as_u64).unwrap_or(4).min(8) as usize;
let refi = reference_index(p)?;
let rp = vectorcraft_geom::reference_point(b, refi);
let (w, h) = wanted_size(p, b);
// A single anchor, or anchors in a line, have no size to scale on that side.
@@ -272,6 +272,14 @@ fn set_anchor_bounds(s: &mut Session, p: &Value, ids: Vec<NodeId>, b: Rect) -> R
apply_transform(s, "Transform", ids, Affine::translate(to - rp) * scale, p, true)
}
fn reference_index(p: &Value) -> Result<usize> {
let reference = crate::cmd::number_or(p, "reference", 4.0, "object.setBounds")?;
if !reference.is_finite() || reference < 0.0 || reference.fract() != 0.0 {
return Err(crate::cmd::bad("object.setBounds", "`reference` must be a whole number"));
}
Ok(reference.min(8.0) as usize)
}
/// The visual box of `ids` square to `frame`, as [`Document::oriented_bounds`] measures it, with
/// symbol instances measured without the strokes in their art
/// ([`vectorcraft_doc::SymbolBoxes::without_art_strokes`]).
+1 -1
View File
@@ -287,7 +287,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Set Bounds",
[],
None,
"{x?, y?, width?, height?, reference?: 0..8 (9-point grid; a number past 8 is read as 8), proportional?, strokes?, corners?, copy?: bool, patterns?: bool (Transform Patterns), typeAreas?: bool (area type: the frame is reshaped and the text reflows at its size), objects?: bool (Transform Objects, as object.move)} (Transform panel; width/height and the reference point follow the bounding box, rotated with rotated objects; x, y are page coordinates; with Use Preview Bounds the values measure the visual bounds; with the typeGlyphBounds preference, each point type object selected as an object (not inside a selected group) is measured by its glyphs' ink (underline and strikethrough included), which the scale gives the size asked for; strokes/corners as object.scale; with anchor points selected (Direct Selection) and no copy, the values are the box around those points, square to the page, and only they move or scale, as with object.move (#1824))",
"{x?, y?, width?, height?, reference?: whole number >= 0 (9-point grid 0..8; 0.0 equals 0; a whole number past 8 is read as 8; omitted/null: center, 4), proportional?, strokes?, corners?, copy?: bool, patterns?: bool (Transform Patterns), typeAreas?: bool (area type: the frame is reshaped and the text reflows at its size), objects?: bool (Transform Objects, as object.move)} (Transform panel; width/height and the reference point follow the bounding box, rotated with rotated objects; x, y are page coordinates; with Use Preview Bounds the values measure the visual bounds; with the typeGlyphBounds preference, each point type object selected as an object (not inside a selected group) is measured by its glyphs' ink (underline and strikethrough included), which the scale gives the size asked for; strokes/corners as object.scale; with anchor points selected (Direct Selection) and no copy, the values are the box around those points, square to the page, and only they move or scale, as with object.move (#1824))",
has_selection,
set_bounds
),
+69
View File
@@ -428,6 +428,75 @@ fn transform_panel_sizes_follow_the_turned_box() {
assert!(near(angle(&s, a), 45.0));
}
#[test]
fn set_bounds_whole_number_references_match_integer_references() {
for direct in [false, true] {
for index in 0..=9 {
let run = |reference: Value| {
let mut s = session();
let a = rect(&mut s, 10.0, 20.0, 100.0, 50.0);
sel(&mut s, &[a]);
if direct {
s.execute("select.anchors", &json!({"id": a.0, "anchors": [[0, 0], [0, 2]], "mode": "set"})).unwrap();
}
let before = anchors(&s, a);
let selection = s.doc().unwrap().selection.clone();
let n = undo_len(&s);
s.execute("object.setBounds", &json!({"x": 200, "y": 300, "reference": reference})).unwrap();
assert_eq!(undo_len(&s), n + 1);
assert_eq!(s.doc().unwrap().selection, selection);
if !direct {
let b = selection_box(&s);
assert!(b.reference_point(index.min(8)).distance(Point::new(200.0, 300.0)) < 1e-6, "{b:?}");
}
let after = anchors(&s, a);
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(anchors(&s, a), before);
s.execute("edit.redo", &json!({})).unwrap();
assert_eq!(anchors(&s, a), after);
after
};
assert_eq!(run(json!(index as f64)), run(json!(index)), "reference {index}, direct {direct}");
if index == 8 {
assert_eq!(run(json!(u64::MAX)), run(json!(8)), "large reference, direct {direct}");
assert_eq!(run(json!(1e100)), run(json!(8)), "large float reference, direct {direct}");
}
}
}
for reference in [None, Some(Value::Null)] {
let mut s = session();
let a = rect(&mut s, 10.0, 20.0, 100.0, 50.0);
sel(&mut s, &[a]);
let mut p = json!({"x": 200, "y": 300});
if let Some(reference) = reference {
p["reference"] = reference;
}
s.execute("object.setBounds", &p).unwrap();
assert!(selection_box(&s).center().distance(Point::new(200.0, 300.0)) < 1e-6);
}
}
#[test]
fn set_bounds_rejects_invalid_references_without_changes() {
for direct in [false, true] {
for reference in [json!(-1), json!(-1.0), json!(0.5), json!(8.5), json!("0"), json!(true), json!([]), json!({})] {
let mut s = session();
let a = rect(&mut s, 10.0, 20.0, 100.0, 50.0);
sel(&mut s, &[a]);
if direct {
s.execute("select.anchors", &json!({"id": a.0, "anchors": [[0, 0], [0, 2]], "mode": "set"})).unwrap();
}
let before = anchors(&s, a);
let selection = s.doc().unwrap().selection.clone();
let n = undo_len(&s);
assert!(s.execute("object.setBounds", &json!({"x": 200, "y": 300, "reference": reference})).is_err(), "{reference}, direct {direct}");
assert_eq!(anchors(&s, a), before);
assert_eq!(s.doc().unwrap().selection, selection);
assert_eq!(undo_len(&s), n);
}
}
}
#[test]
fn reset_bounding_box_squares_the_box_in_one_step() {
let mut s = session();
+4 -1
View File
@@ -2088,7 +2088,10 @@ commands fail and change nothing, and `appearance.removeItem` removes nothing. A
`artboard.reorder`, `graphicStyle.move`, `appearance.moveItem`, `appearance.duplicateItem` and `effect.move`) puts
the item last. A text `start` or `end` is past the end of the text. `object.slice.divide` refuses such a `rows` or
`columns`, `recolor.reduce` keeps each color in its own row for such a `colors`, and `layer.setProps` refuses such a
`color`. `object.setBounds` reads a `reference` past 8 as 8, as `object.transformEach` does.
`color`. `object.setBounds` reads a whole-number `reference` past 8 as 8, as `object.transformEach` does.
For `object.setBounds`, `reference` accepts whole numbers of 0 or more, including floating-point representations
such as `0.0`. Negative and fractional values are errors. An omitted or null `reference` uses the center point, 4.
These rules apply to both whole objects and directly selected anchors.
An index or place given as a value that is not a whole number of 0 or more, such as `"3"`, `2.5`, `-1` or `true`, is
refused with `` `anchor` must be a whole number ``, naming the key: `anchor` of `path.setHandle`, `path.moveAnchor` and
`text.setPathHandle`, `segment` of `path.insertAnchor`, `to` of `artboard.reorder`, `library.moveGroup` and
+2
View File
@@ -588,6 +588,8 @@ since 2026-10-01.
## Evidence: what ships today, by area
- `object.setBounds` uses the same reference point for whole-number integer and floating-point values, including `0` and `0.0`. It rejects negative and fractional indices without changing geometry, selection, or undo history. An omitted or null reference uses the center, and whole-number references above 8 use point 8. Object and direct-anchor selections follow these rules (`tests::bboxrotate::set_bounds_whole_number_references_match_integer_references`, `set_bounds_rejects_invalid_references_without_changes`).
- **Recolor picker follow-up (#8031/#4028):** native Lab numeric entry, document/loaded-library swatches in the Color Picker, and Assign-row Pick from Swatches / Limit to Library menus. #4090 provided the double-click picker and #5113 the Assign sliders. Regression tests `native_lab_entry_in_color_picker` and `row_context_menu_pick_from_swatches_and_limit_to_library` exercise the fields and menu callbacks rather than writing their result state. Area score and hours unchanged.
- **Save refusal (#6146):** ordinary File > Save to its own location offers Save As after a terminal permission/read-only refusal, suggesting the original name and format, preserving options for the unchanged format and opening the chosen format’s Save options on a change. Cancel keeps the failed-save state. Busy pickers, recovery-copy errors, other failures, explicit destinations and Close/Quit keep the existing dialog. Engine, UI and MCP regression cases are in `cmd/fileio/tests_readonly.rs`, `io/tests_read_only_save.rs` and MCP `tests/fileio.rs`; focused UI regressions passed; engine regression execution and native panel acceptance pending. Scores and hours unchanged.