Path Precision Tools: the bounding shapes' menu items open a dialog with Offset and One per Object

Add Bounding Rectangle… and Add Bounding Circle… open the command dialog
with Offset (0 to 4,000,000 pt), One per Object (`each`) and Preview, so
the menu sets what the commands take. Cancel removes the preview without
a step of history, and an Offset that takes the shape past the canvas
edge leaves the dialog open with the error.

- The canvas check reuses `sanity::rect_in_range`; the params text and
  docs/mcp.md say the limit holds along each axis.
- Tests: the dialog (defaults, preview, Cancel, OK with and without an
  Offset and One per Object, the canvas error, the catalog rows); a circle
  just inside the limit; one edge past the limit is refused.
- Catalogs: the two menu labels with "…" and "One per Object" in all 28;
  the four messages in bg, bs and id as well.
This commit is contained in:
dotCooCoo
2026-11-18 08:17:22 -08:00
parent 8e09fe6e53
commit 91cacaa6ed
39 changed files with 249 additions and 54 deletions
@@ -8,7 +8,8 @@ use vectorcraft_geom::{Affine, CubicBez, FillRule, ParamCurve, PathSeg, Point, R
use super::super::super::edit::selected_roots;
use super::super::super::{CommandSpec, bad, cmd, has_selection, number_or, switch_or};
use crate::{MAX_COORD, Result, Session};
use crate::sanity::rect_in_range;
use crate::{Result, Session};
#[cfg(test)]
mod tests;
@@ -29,7 +30,7 @@ pub(super) fn specs() -> Vec<CommandSpec> {
"Add Bounding Rectangle",
["Object", "Plug-ins", "Path Precision Tools"],
None,
"{each?: bool (default false: one rectangle around the whole selection; true: one around each selected object), offset?: pt (0 or more; default 0: added on every side)} draw a live rectangle on the geometric bounding box of the selected objects (strokes and effects left out), with the current paint, on top of the current layer or of the isolated group, as other new art; the rectangles are selected; one undo step. An object whose box has no width or no height (a straight horizontal or vertical line) gets no rectangle unless `offset` is more than 0, nor does an object with no bounds (an empty group); with `each` the result's `skipped` counts them. It is an error when no rectangle is left (\"the shape would have no size\" when a rectangle with no size was left out, else \"nothing to enclose\") and when a rectangle would reach more than 4,000,000 pt from the origin → {id: the first rectangle, ids, skipped?}",
"{each?: bool (default false: one rectangle around the whole selection; true: one around each selected object), offset?: pt (0 or more; default 0: added on every side)} draw a live rectangle on the geometric bounding box of the selected objects (strokes and effects left out), with the current paint, on top of the current layer or of the isolated group, as other new art; the rectangles are selected; one undo step. An object whose box has no width or no height (a straight horizontal or vertical line) gets no rectangle unless `offset` is more than 0, nor does an object with no bounds (an empty group); with `each` the result's `skipped` counts them. It is an error when no rectangle is left (\"the shape would have no size\" when a rectangle with no size was left out, else \"nothing to enclose\") and when an edge of a rectangle would lie more than 4,000,000 pt from the origin along either axis (\"the shape would reach past the edge of the canvas\"). The menu item Add Bounding Rectangle… opens a dialog with Offset and One per Object (`each`) and a Preview → {id: the first rectangle, ids, skipped?}",
has_selection,
bounding_rectangle
),
@@ -38,7 +39,7 @@ pub(super) fn specs() -> Vec<CommandSpec> {
"Add Bounding Circle",
["Object", "Plug-ins", "Path Precision Tools"],
None,
"{each?: bool (default false: one circle around the whole selection; true: one around each selected object), offset?: pt (0 or more; default 0: added to the radius)} draw the smallest live circle that holds the outlines of the selected objects (paths and compound paths by their curves, clip groups and sublayers that clip their contents by their clipping paths, other sublayers by their shown objects, images by their corners, type and other objects by the corners of their bounding box; strokes and effects left out), with the radius 0.01 pt larger so it holds the curves themselves, with the current paint, on top of the current layer or of the isolated group, as other new art; the circles are selected; one undo step. An object at a single point gets no circle unless `offset` is more than 0, nor does an object with no outline (an empty group); with `each` the result's `skipped` counts them. It is an error when no circle is left (\"the shape would have no size\" when a circle with no size was left out, else \"nothing to enclose\") and when a circle would reach more than 4,000,000 pt from the origin → {id: the first circle, ids, skipped?}",
"{each?: bool (default false: one circle around the whole selection; true: one around each selected object), offset?: pt (0 or more; default 0: added to the radius)} draw the smallest live circle that holds the outlines of the selected objects (paths and compound paths by their curves, clip groups and sublayers that clip their contents by their clipping paths, other sublayers by their shown objects, images by their corners, type and other objects by the corners of their bounding box; strokes and effects left out), with the radius 0.01 pt larger so it holds the curves themselves, with the current paint, on top of the current layer or of the isolated group, as other new art; the circles are selected; one undo step. An object at a single point gets no circle unless `offset` is more than 0, nor does an object with no outline (an empty group); with `each` the result's `skipped` counts them. It is an error when no circle is left (\"the shape would have no size\" when a circle with no size was left out, else \"nothing to enclose\") and when an edge of a circle's bounding box would lie more than 4,000,000 pt from the origin along either axis (\"the shape would reach past the edge of the canvas\"). The menu item Add Bounding Circle… opens a dialog with Offset and One per Object (`each`) and a Preview → {id: the first circle, ids, skipped?}",
has_selection,
bounding_circle
),
@@ -121,22 +122,18 @@ enum Around {
NoOutline,
/// The rectangle would have no width or no height, or the circle no radius.
NoSize,
/// The shape would reach past [`MAX_COORD`] from the origin.
/// An edge of the shape would lie more than [`MAX_COORD`](crate::MAX_COORD) from the origin
/// along either axis.
PastCanvas,
}
/// Is every edge of `r` finite and within [`MAX_COORD`] of the origin?
fn on_canvas(r: Rect) -> bool {
[r.x0, r.y0, r.x1, r.y1].iter().all(|v| v.abs() <= MAX_COORD)
}
/// The live rectangle or circle around `ids`, grown by `offset`.
fn shape_around(d: &Document, symbols: &SymbolBoxes, ids: &[NodeId], kind: Kind, offset: f64) -> Around {
match kind {
Kind::Rectangle => {
let Some(r) = d.bounds_of_with(ids, false, symbols) else { return Around::NoOutline };
let r = r.inflate(offset, offset);
if !on_canvas(r) {
if !rect_in_range(r) {
return Around::PastCanvas;
}
if r.width() <= 0.0 || r.height() <= 0.0 {
@@ -163,7 +160,7 @@ fn shape_around(d: &Document, symbols: &SymbolBoxes, ids: &[NodeId], kind: Kind,
}
let r = radius + FLATNESS + offset;
let b = Rect::new(center.x - r, center.y - r, center.x + r, center.y + r);
if !on_canvas(b) {
if !rect_in_range(b) {
return Around::PastCanvas;
}
Around::Shape(LiveShape::Ellipse { w: b.width(), h: b.height(), pie: (0.0, 360.0), xf: Affine::translate(b.origin().to_vec2()) })
@@ -345,8 +345,9 @@ fn bad_params_and_an_empty_selection_draw_nothing() {
}
}
/// An offset that would take the shape more than 4,000,000 pt from the origin is an error that
/// names the canvas edge, and nothing is drawn; the largest offset that stays inside is drawn.
/// An offset that would put an edge of the shape more than 4,000,000 pt from the origin along
/// either axis is an error that names the canvas edge, and nothing is drawn; the largest offsets
/// that stay inside are drawn, for the rectangle and for the circle.
#[test]
fn an_offset_past_the_canvas_edge_draws_nothing() {
let mut s = session();
@@ -360,6 +361,25 @@ fn an_offset_past_the_canvas_edge_draws_nothing() {
}
let r = id_of(&s.execute(RECTANGLE, &json!({"offset": 3.99e6})).unwrap());
assert!(rect_near(bounds(&s, r), Rect::new(-3.99e6, -3.99e6, 3.99e6 + 10.0, 3.99e6 + 10.0), 1e-3));
s.execute("select.set", &json!({"ids": [a.0]})).unwrap();
let c = id_of(&s.execute(CIRCLE, &json!({"offset": 3.99e6})).unwrap());
// The circle around the square from (0, 0) to (10, 10), 0.01 pt larger, then the offset.
let radius = 50.0f64.sqrt() + 0.01 + 3.99e6;
assert!(rect_near(bounds(&s, c), Rect::new(5.0 - radius, 5.0 - radius, 5.0 + radius, 5.0 + radius), 1e-3));
}
/// The limit holds along each axis on its own: a shape whose right edge would pass 4,000,000 pt is
/// refused although its other edges, and its distance along the other axis, are small.
#[test]
fn one_edge_past_the_canvas_edge_is_enough_to_refuse() {
let mut s = session();
let a = rect(&mut s, 3_999_980.0, 0.0, 10.0, 10.0);
s.execute("select.set", &json!({"ids": [a.0]})).unwrap();
for command in [RECTANGLE, CIRCLE] {
let e = refused(&mut s, command, json!({"offset": 20}));
assert!(e.contains("the shape would reach past the edge of the canvas"), "{command}: {e}");
}
assert!(s.execute(RECTANGLE, &json!({"offset": 5})).is_ok(), "the right edge at 3,999,995 pt is inside");
}
/// Many points keep only their hull's corners, and the circle around them is the one around all
+2 -1
View File
@@ -49,7 +49,8 @@ pub(crate) fn doc_sane(d: &Document, sel: &Selection) -> bool {
&& objects_in_range(d, &sel.objects)
}
fn rect_in_range(r: vectorcraft_geom::Rect) -> bool {
/// Is every edge of `r` finite and within [`MAX_COORD`] of the origin?
pub(crate) fn rect_in_range(r: vectorcraft_geom::Rect) -> bool {
[r.x0, r.y0, r.x1, r.y1].iter().all(|v| v.is_finite() && v.abs() <= MAX_COORD)
}