mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 00:47:11 +00:00
Merge pull request #8339 from dotCooCoo/fix/bounds-reference-number
Fix #8334: setBounds reads whole-number float references correctly
This commit is contained in:
@@ -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`]).
|
||||
|
||||
@@ -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
|
||||
),
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user