Merge pull request #9855 from dotCooCoo/fix/dimension-preview-bounds-group-edges

Dimension: Linear points snap onto preview bounds edges, groups and stroked type
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-27 21:36:07 -08:00
commit 8529caaba7
3 files changed
+353 -93

No files matched your search

+334 -80
View File
@@ -85,13 +85,13 @@ pub struct DimensionTool {
/// Where the next Linear pick lands, as last worked out for the pointer (for the overlays).
snapped: Option<(Point, Option<PointRef>)>,
/// Whether the last Linear pick worked out landed on the end of a line of a dimension already
/// drawn, or on a corner of a path's preview bounds (shown as an anchor).
/// drawn, or on a corner of an object's preview bounds (shown as an anchor).
on_end: bool,
/// The path that holds the last Linear pick worked out, when it landed on a corner of that
/// path's preview bounds.
/// The path that holds the last Linear pick worked out, when it landed on a corner or an edge
/// of an object's preview bounds (the type object, when type holds it).
corner: Option<NodeId>,
/// The path that holds the first Linear point, when it was picked on a corner of that path's
/// preview bounds.
/// The path that holds the first Linear point, when it was picked on a corner or an edge of an
/// object's preview bounds (the type object, when type holds it).
first_corner: Option<NodeId>,
/// The link the first Linear point took at its click when it was not on an anchor: held
/// against the path whose preview-bounds corner it was picked on, or on the segment under it.
@@ -298,21 +298,30 @@ fn anchor_under_where(cx: &ToolContext, p: Point, keep: impl Fn(NodeId) -> bool)
best.map(|(_, at, r)| (at, r))
}
/// The geometric bounds of the paths [`preview_corner`] looked at, and the preview bounds of
/// The geometric bounds of the objects [`preview_snap`] looked at, and the preview bounds of
/// those it measured, kept while the document is unchanged, so that the pointer moving over the
/// art measures each path once.
/// art measures each object once.
#[derive(Default)]
struct PreviewBoxes {
revision: Option<(u64, u64)>,
geometric: HashMap<NodeId, Option<Rect>>,
preview: HashMap<NodeId, Option<Rect>>,
/// The editable path holding each corner of a measured object's preview bounds, found when
/// the object is measured, so that a pointer move reads no member.
/// The editable path or type object holding each corner of a measured object's preview
/// bounds, found when the object is measured, so that a pointer move reads no member.
holders: HashMap<NodeId, [Option<NodeId>; 4]>,
/// Whether a point at each corner (by object and corner number) can be held against its path.
holdable: HashMap<(NodeId, usize), bool>,
}
/// Where [`preview_snap`] puts a Linear pick on the preview bounds: the nearest corner within
/// reach, and the nearest point of an edge within reach with the path or type object it is held
/// against.
#[derive(Clone, Copy, Debug, Default)]
struct PreviewSnap {
corner: Option<PreviewCorner>,
edge: Option<(Point, NodeId)>,
}
/// How many member paths one pointer move may read to measure objects not yet measured, apart
/// from the objects it visits: an object with more members than are left gives no corner on
/// that move.
@@ -322,21 +331,24 @@ const PREVIEW_MEMBER_WORK: usize = 20_000;
#[derive(Clone, Copy, Debug)]
struct PreviewCorner {
at: Point,
/// The path the point is held against.
/// The path the point is held against, or the type object it lies on (type holds no point).
holder: NodeId,
/// The same corner of the object's geometric bounds: an anchor of `holder` there is the
/// anchor the corner stands in for.
stands_for: Point,
}
/// With Use Preview Bounds on, the corner of a path's or compound path's preview bounds (its
/// strokes and effects included) nearest `p` within reach, on any editable one whose preview
/// bounds differ from its geometric bounds. In a compound path, the member whose own box reaches
/// nearest to the corner holds it. The work is bounded, and dimension artwork is skipped, as in
/// [`pick`]: the objects visited count against one budget, and the member paths read to measure
/// them against [`PREVIEW_MEMBER_WORK`]. `cache` keeps the bounds and holders measured for the
/// document's revision.
fn preview_corner(cx: &ToolContext, p: Point, cache: &mut PreviewBoxes) -> Option<PreviewCorner> {
/// With Use Preview Bounds on, where a Linear pick at `p` lands on the preview bounds (strokes and
/// effects included) of a path, a compound path, type, or a group or layer with fills, strokes or
/// effects of its own, on any editable one whose preview bounds differ from its geometric bounds:
/// the corner nearest `p` within reach, and the point of an edge nearest `p` within the same
/// reach. In a compound path, group or layer, the member path or type object whose own box
/// reaches nearest to a corner holds it, and a point on an edge is held by the holder of the
/// edge's nearer corner. The work is bounded, and dimension artwork is skipped, as in [`pick`]:
/// the objects visited count against one budget, and the members read to measure them against
/// [`PREVIEW_MEMBER_WORK`]. `cache` keeps the bounds and holders measured for the document's
/// revision.
fn preview_snap(cx: &ToolContext, p: Point, cache: &mut PreviewBoxes) -> PreviewSnap {
struct Walk<'a, 'c> {
symbols: Option<vectorcraft_doc::SymbolBoxes<'a>>,
doc: &'a vectorcraft_doc::Document,
@@ -344,9 +356,11 @@ fn preview_corner(cx: &ToolContext, p: Point, cache: &mut PreviewBoxes) -> Optio
p: Point,
reach: f64,
budget: usize,
/// Member paths left to read for objects not yet measured.
/// Members left to read for objects not yet measured.
work: usize,
best: Option<(f64, PreviewCorner)>,
/// The nearest point of an edge so far: its distance, where it is and what holds it.
edge: Option<(f64, Point, NodeId)>,
}
/// Takes `count` from the member work left, or nothing when less is left.
fn charge(work: &mut usize, count: usize) -> bool {
@@ -359,14 +373,65 @@ fn preview_corner(cx: &ToolContext, p: Point, cache: &mut PreviewBoxes) -> Optio
fn corners(r: Rect) -> [Point; 4] {
[Point::new(r.x0, r.y0), Point::new(r.x1, r.y0), Point::new(r.x1, r.y1), Point::new(r.x0, r.y1)]
}
/// For each corner of `preview`, the path whose own box reaches nearest to it: in a compound
/// path, the member that puts that corner where it is. A hidden or locked member, or one made
/// into a guide, gives no corner. One pass reads each member once for all four corners.
fn holders<'n>(members: impl Iterator<Item = &'n Node>, preview: Rect) -> [Option<NodeId>; 4] {
/// Whether a point at `q` can be held against `holder`: a path holds it by its anchors, except
/// a path that cannot (a stroked dot, whose anchors all coincide). Type has no anchors to hold
/// a point against: a point on it stays where it was picked.
fn holds(doc: &vectorcraft_doc::Document, holder: NodeId, q: Point) -> bool {
doc.node(holder).is_some_and(|h| h.path_data().is_none() || FreePoint::on(doc, holder, q).is_some())
}
/// The objects that can hold a corner of `n`'s preview bounds, each with whether it can be
/// edited, read within the member work left in `work` and taken from it. A path or type object
/// is its own member, a compound path's members are its paths, and a group's or layer's are
/// the paths and type objects in it at any depth. A group or layer holding dimension artwork
/// has none: its bounds take in the dimensions. A hidden or locked member, one made into a
/// guide, and anything in a hidden or locked group cannot be edited. `None` when `n` holds more
/// than is left: a path or compound path then takes nothing from `work`, and a group or layer
/// takes as many objects as it read.
fn members<'n>(n: &'n Node, work: &mut usize) -> Option<Vec<(&'n Node, bool)>> {
/// Reads the members of `n` into `out` until it meets dimension artwork, which sets
/// `dimensions`. False when the work ran out first.
fn gather<'n>(n: &'n Node, editable: bool, work: &mut usize, dimensions: &mut bool, out: &mut Vec<(&'n Node, bool)>) -> bool {
for c in n.children().into_iter().flatten() {
if *dimensions {
return true;
}
if !charge(work, 1) {
return false;
}
if c.extras().dimension.is_some() {
*dimensions = true;
return true;
}
let editable = editable && c.visible && !c.locked && !c.is_guide();
match &c.kind {
NodeKind::Path { .. } | NodeKind::Text(_) => out.push((c.as_ref(), editable)),
_ => {
if !gather(c, editable, work, dimensions, out) {
return false;
}
}
}
}
true
}
let editable = |m: &Node| m.visible && !m.locked && !m.is_guide();
match &n.kind {
NodeKind::Compound { children, .. } => charge(work, children.len())
.then(|| children.iter().map(|c| c.as_ref()).filter(|m| m.path_data().is_some()).map(|m| (m, editable(m))).collect()),
NodeKind::Group { .. } | NodeKind::Layer { .. } => {
let (mut out, mut dimensions) = (vec![], false);
gather(n, true, work, &mut dimensions, &mut out).then(|| if dimensions { vec![] } else { out })
}
_ => charge(work, 1).then(|| vec![(n, editable(n))]),
}
}
/// For each corner of `preview`, the member whose own box reaches nearest to it: in a compound
/// path, group or layer, the member that puts that corner where it is. A member that cannot be
/// edited gives no corner. One pass reads each member once for all four corners.
fn holders(members: &[(&Node, bool)], preview: Rect) -> [Option<NodeId>; 4] {
let mut best: [Option<(f64, NodeId, bool)>; 4] = [None; 4];
for m in members {
for &(m, editable) in members {
let Some(b) = m.geometric_bounds() else { continue };
let editable = m.visible && !m.locked && !m.is_guide();
for ((holder, c), q) in best.iter_mut().zip(corners(b)).zip(corners(preview)) {
let gap = c.distance(q);
if holder.is_none_or(|(near, _, was)| gap < near || (gap == near && editable && !was)) {
@@ -384,75 +449,105 @@ fn preview_corner(cx: &ToolContext, p: Point, cache: &mut PreviewBoxes) -> Optio
// Only effects, on the object or on one of its fills or strokes, paint further out than
// its strokes reach.
let effects = !n.appearance.effects.is_empty() || n.appearance.items.iter().any(|i| !i.effects().is_empty());
let reach = n.appearance.outset() + w.reach;
// A path is its own member; a compound path's members are its paths.
let compound = match &n.kind {
NodeKind::Compound { children, .. } => Some(children.as_slice()),
_ => None,
// Type's character strokes paint around its glyphs as far as its own strokes do.
let outset = match &n.kind {
NodeKind::Text(t) => n.appearance.outset().max(t.char_stroke_outset()),
_ => n.appearance.outset(),
};
let reach = outset + w.reach;
// A group or layer is measured when it has fills, strokes or effects of its own. The
// strokes and effects of its members paint past its own reach, so it is measured wherever
// the pointer is.
let container = matches!(n.kind, NodeKind::Group { .. } | NodeKind::Layer { .. });
let measures = match &n.kind {
NodeKind::Path { .. } | NodeKind::Compound { .. } | NodeKind::Text(_) => true,
_ => container && vectorcraft_effects::has_container_appearance(n),
};
let members =
|| compound.into_iter().flatten().map(|c| c.as_ref()).chain(compound.is_none().then_some(n)).filter(|m| m.path_data().is_some());
// Measuring the object, its bounds or its holders, reads every member once.
let count = compound.map_or(1, <[_]>::len);
// A path made into a guide draws as a guide, without its strokes or effects. The pointer
// test includes the right and bottom edges, as the distance test below does.
let within = |r: Rect| (r.x0..=r.x1).contains(&w.p.x) && (r.y0..=r.y1).contains(&w.p.y);
// Set once this move has paid for reading the members: measuring the preview bounds and
// holders on the same move reads them again without a second charge.
let mut paid = false;
// The members this move read to measure the object's bounds: measuring the preview bounds
// and holders on the same move uses them without reading them again.
let mut read = None;
let full = w.work == PREVIEW_MEMBER_WORK;
let geometric = match w.cache.geometric.get(&n.id) {
_ if n.is_guide() || members().next().is_none() => None,
_ if !measures || n.is_guide() => None,
Some(g) => *g,
None if charge(&mut w.work, count) => {
paid = true;
let g = n.geometric_bounds();
w.cache.geometric.insert(n.id, g);
g
}
None => None,
None => match members(n, &mut w.work) {
Some(m) => {
let g = if m.is_empty() { None } else { n.geometric_bounds() };
w.cache.geometric.insert(n.id, g);
read = Some(m);
g
}
// A group or layer holding more than a whole move may read is not read again for
// this revision.
None if container && full => {
w.cache.geometric.insert(n.id, None);
None
}
None => None,
},
};
if let Some(geometric) = geometric
&& (effects || within(geometric.inflate(reach, reach)))
&& (effects || container || within(geometric.inflate(reach, reach)))
{
let doc = w.doc;
let measured = match w.cache.preview.get(&n.id) {
Some(r) => Some(*r),
None if paid || charge(&mut w.work, count) => {
None => read.take().or_else(|| members(n, &mut w.work)).map(|m| {
let symbols = w.symbols.get_or_insert_with(|| doc.symbol_boxes());
let r = vectorcraft_effects::preview::visual_bounds(symbols, n);
w.cache.preview.insert(n.id, r);
if let Some(r) = r
&& r != geometric
{
w.cache.holders.insert(n.id, holders(members(), r));
w.cache.holders.insert(n.id, holders(&m, r));
}
Some(r)
}
None => None,
r
}),
};
if let Some(Some(r)) = measured
&& r != geometric
{
for (k, q) in corners(r).into_iter().enumerate() {
let held = w.cache.holders.get(&n.id).copied().unwrap_or_default();
let at = corners(r);
// The holder of corner `k`, when it can hold a point there, worked out once per
// revision. A path that cannot hold a point at the corner gives no corner.
let holder = |k: usize, cache: &mut PreviewBoxes| {
let (id, q) = (held.get(k).copied().flatten()?, *at.get(k)?);
cache.holdable.entry((n.id, k)).or_insert_with(|| holds(doc, id, q)).then_some(id)
};
for (k, q) in at.into_iter().enumerate() {
let d = q.distance(w.p);
if d > w.reach || w.best.is_some_and(|(nearest, ..)| d >= nearest) {
continue;
}
let holder = w.cache.holders.get(&n.id).and_then(|h| h.get(k).copied().flatten());
// A path that cannot hold a point at the corner (a stroked dot, whose anchors
// all coincide) gives no corner.
let holdable = |id: NodeId, cache: &mut PreviewBoxes| {
*cache.holdable.entry((n.id, k)).or_insert_with(|| FreePoint::on(doc, id, q).is_some())
};
if let Some((holder, stands_for)) = holder.filter(|id| holdable(*id, w.cache)).zip(corners(geometric).get(k).copied()) {
if let Some((holder, stands_for)) = holder(k, w.cache).zip(corners(geometric).get(k).copied()) {
w.best = Some((d, PreviewCorner { at: q, holder, stands_for }));
}
}
// Edge `k` runs from corner `k` to the next one. A point on it is held by the
// holder of its nearer end that can hold a point at that end, so a pointer move
// reads no anchor.
for (k, (&a, &b)) in at.iter().zip(at.iter().cycle().skip(1)).enumerate() {
let along = b - a;
let t = if along.hypot2() > 0.0 { ((w.p - a).dot(along) / along.hypot2()).clamp(0.0, 1.0) } else { 0.0 };
let q = a + along * t;
let d = q.distance(w.p);
if !d.is_finite() || d > w.reach || w.edge.is_some_and(|(nearest, ..)| d >= nearest) {
continue;
}
let ends = if t <= 0.5 { [k, (k + 1) % 4] } else { [(k + 1) % 4, k] };
if let Some(holder) = ends.into_iter().find_map(|e| holder(e, w.cache)) {
w.edge = Some((d, q, holder));
}
}
}
}
// A compound path draws its members with its own appearance: they give no corners of
// their own.
if compound.is_none()
if !matches!(n.kind, NodeKind::Compound { .. })
&& let Some(ch) = n.children()
{
for c in ch.iter().rev() {
@@ -462,16 +557,17 @@ fn preview_corner(cx: &ToolContext, p: Point, cache: &mut PreviewBoxes) -> Optio
}
// Anchors take a pick whatever the snapping options, and so do the corners standing in for them.
if !cx.preview_bounds {
return None;
return PreviewSnap::default();
}
if cache.revision != Some(cx.revision) {
*cache = PreviewBoxes { revision: Some(cx.revision), ..Default::default() };
}
let mut w = Walk { symbols: None, doc: cx.doc, cache, p, reach: cx.tol(ANCHOR_REACH), budget: 20_000, work: PREVIEW_MEMBER_WORK, best: None };
let mut w =
Walk { symbols: None, doc: cx.doc, cache, p, reach: cx.tol(ANCHOR_REACH), budget: 20_000, work: PREVIEW_MEMBER_WORK, best: None, edge: None };
for n in cx.doc.layers.iter().rev() {
visit(n, &mut w);
}
w.best.map(|(_, corner)| corner)
PreviewSnap { corner: w.best.map(|(_, corner)| corner), edge: w.edge.map(|(_, q, holder)| (q, holder)) }
}
/// The anchor a pick at `p` lands on instead of `corner`, given `anchor`, the nearest anchor
@@ -797,9 +893,10 @@ fn nearest_on(cx: &ToolContext, edge: &SegmentRef, p: Point) -> Option<Point> {
/// A point on a segment keeps its place along the segment. Any other point is held
/// against the anchors of the path under it, or, with no path under it, the path of the other
/// point, so that it moves with that path. None when neither point has a path to link to.
/// `held_a` and `held_b` name the path holding a point picked on a corner of that path's
/// preview bounds: such a point is held against that path, however far its strokes or effects
/// put the corner from the path and whatever other segment lies there. `link_a` is the link the
/// `held_a` and `held_b` name the path holding a point picked on a corner or an edge of an
/// object's preview bounds: such a point is held against that path, however far its strokes or
/// effects put the corner from the path and whatever other segment lies there. A point that type
/// holds that way has no link. `link_a` is the link the
/// first point took at its own click ([`own_link`]): while it still resolves, the point keeps it,
/// whatever other art lies there now.
fn points_source(
@@ -837,9 +934,9 @@ fn points_source(
}
/// The link a point picked at `p` off any anchor takes from its own pick, as [`points_source`]
/// links it: held against `corner`'s path when it was picked on a corner of that path's preview
/// bounds, else on the segment under it, or held against that segment's path. None when nothing
/// under it holds it.
/// links it: held against `corner`'s path when it was picked on a corner or an edge of an object's
/// preview bounds (none when type holds it), else on the segment under it, or held against that
/// segment's path. None when nothing under it holds it.
fn own_link(cx: &ToolContext, p: Point, corner: Option<NodeId>) -> Option<FreePoint> {
match corner.filter(|node| cx.doc.node(*node).is_some()) {
Some(node) => FreePoint::on(cx.doc, node, p),
@@ -848,8 +945,8 @@ fn own_link(cx: &ToolContext, p: Point, corner: Option<NodeId>) -> Option<FreePo
}
impl DimensionTool {
/// Where a Linear pick at `p` lands: with Use Preview Bounds on, on the nearest corner of a
/// path's preview bounds within reach, where they differ from its anchors, unless another
/// Where a Linear pick at `p` lands: with Use Preview Bounds on, on the nearest corner of an
/// object's preview bounds within reach, where they differ from its anchors, unless another
/// anchor is nearer (held against the path, labeled "anchor"; [`anchor_over_corner`]); else on the
/// nearest anchor of any path, within reach (kept as
/// a link to its path), labeled "anchor"; else on the nearest end of a line of a dimension
@@ -857,7 +954,9 @@ impl DimensionTool {
/// line with one, or for the second point square to a segment or grazing a curve from the
/// first with Snap to Perpendicular or Snap to Tangent on, or on a construction guide through
/// a first point picked on an anchor), its guides showing; else on the closest point of the
/// line under the pointer, of the art or of a dimension, labeled "path"; else `p` itself. A
/// line under the pointer, of the art or of a dimension, or with Use Preview Bounds on of an
/// edge of an object's preview bounds (held against the path holding the edge), labeled
/// "path"; else `p` itself. A
/// first point not on an anchor has no construction guides through it.
/// A point the guides leave where the pointer is (right on a guide line) stays there, and with
/// Snap to Pixel or Snap to Grid on a point stays where they place it.
@@ -871,7 +970,8 @@ impl DimensionTool {
self.on_end = false;
self.corner = None;
let mut anchor = anchor_under(cx, p);
if let Some(corner) = preview_corner(cx, p, &mut self.preview_boxes) {
let bounds = preview_snap(cx, p, &mut self.preview_boxes);
if let Some(corner) = bounds.corner {
anchor = anchor_over_corner(cx, p, &corner, anchor);
if anchor.is_none() {
self.on_end = true;
@@ -894,11 +994,14 @@ impl DimensionTool {
return (guided, None);
}
// Not on an anchor or a guide: snap to the closest point of the line (#2432), of the art
// or of a dimension already drawn.
// or of a dimension already drawn, or with Use Preview Bounds on of an edge of the preview
// bounds, held against the path that holds the edge. The art and the lines win a tie.
let art = pick(cx, p).and_then(|e| nearest_on(cx, &e, p));
let line = drawn.map(|(q, _)| q);
match art.into_iter().chain(line).min_by(|a, b| a.distance(p).total_cmp(&b.distance(p))) {
Some(q) => {
let near = art.into_iter().chain(line).map(|q| (q, None)).chain(bounds.edge.map(|(q, holder)| (q, Some(holder))));
match near.min_by(|(a, _), (b, _)| a.distance(p).total_cmp(&b.distance(p))) {
Some((q, holder)) => {
self.corner = holder;
self.snap.label(q, "path");
(q, None)
}
@@ -1319,7 +1422,7 @@ impl Tool for DimensionTool {
// pointer.
self.press_on_anchor = self.kind == Kind::Linear
&& (anchor_under(cx, ev.pos).is_some()
|| preview_corner(cx, ev.pos, &mut self.preview_boxes).is_some()
|| preview_snap(cx, ev.pos, &mut self.preview_boxes).corner.is_some()
|| on_dimensions(cx, ev.pos).is_some_and(|(_, end)| end));
let target = self.target(cx, ev.pos, ev.mods.alt);
// A press on a dimension drawn already, nearer to its lines and label than to any
@@ -2152,13 +2255,13 @@ mod tests {
let (s, p) = (Selection::default(), paint());
let c = ToolContext { preview_bounds: true, ..cx(&d, &s, &p) };
let mut cache = PreviewBoxes::default();
let corner = preview_corner(&c, Point::new(99.5, 99.5), &mut cache).expect("the corner of the stroke");
let corner = preview_snap(&c, Point::new(99.5, 99.5), &mut cache).corner.expect("the corner of the stroke");
assert_eq!(corner.holder, part, "the member that reaches the corner holds it");
assert_eq!(cache.holders.get(&whole), Some(&[Some(part); 4]));
// A move over the same revision reads the holder from the cache: one changed there is the
// one it gives.
cache.holders.insert(whole, [Some(hole); 4]);
let corner = preview_corner(&c, Point::new(200.5, 200.5), &mut cache).expect("the corner of the stroke");
let corner = preview_snap(&c, Point::new(200.5, 200.5), &mut cache).corner.expect("the corner of the stroke");
assert_eq!(corner.holder, hole, "the cached holder is used");
}
@@ -2173,7 +2276,7 @@ mod tests {
let (d, [whole, ..]) = stroked_compound(extra);
let c = ToolContext { preview_bounds: true, ..cx(&d, &s, &p) };
let mut cache = PreviewBoxes::default();
let corner = preview_corner(&c, Point::new(99.5, 99.5), &mut cache);
let corner = preview_snap(&c, Point::new(99.5, 99.5), &mut cache).corner;
assert_eq!(corner.is_some(), within, "{} members", extra + 2);
assert_eq!(cache.preview.contains_key(&whole), within, "{} members", extra + 2);
assert_eq!(cache.geometric.contains_key(&whole), within, "{} members", extra + 2);
@@ -2406,6 +2509,157 @@ mod tests {
}
}
/// Clicks of a Linear dimension at `at`, with the tool's pending dimension.
fn linear_picks(c: &ToolContext, at: [(f64, f64); 2]) -> Dimension {
let mut t = DimensionTool::default();
for (x, y) in at {
for kind in [PointerKind::Move, PointerKind::Down, PointerKind::Up] {
t.pointer(c, &PointerEvent::new(kind, x, y));
}
}
t.pending.expect("both points were picked")
}
/// A group of a small square and the unpainted square from 100 to 200, with a 10 point stroke
/// of its own with round joins, its preview bounds from 95 to 205, and the ids of the group and
/// the square.
fn group_stroked_on_its_own() -> (vectorcraft_doc::Document, [NodeId; 2]) {
let mut d = vectorcraft_doc::Document::new(500.0, 500.0);
let (l, small, square, group) = (d.layers[0].id, d.alloc_id(), d.alloc_id(), d.alloc_id());
let path =
|id, a: f64, b: f64| std::sync::Arc::new(Node::path(id, vectorcraft_geom::shapes::rectangle(Rect::new(a, a, b, b)), Default::default()));
let mut g = Node::group(group, vec![path(small, 140.0, 160.0), path(square, 100.0, 200.0)]);
let mut stroked = vectorcraft_doc::Appearance::default_art();
let stroke = stroked.stroke_mut().expect("a stroke");
(stroke.width, stroke.join) = (10.0, vectorcraft_doc::LineJoin::Round);
g.appearance = stroked.into();
d.insert(Some(l), 0, g).expect("the group");
(d, [group, square])
}
/// With Use Preview Bounds on, a group's own stroke and stroked type give the corners of their
/// preview bounds. A group's corner is held against the member path that reaches it, and a
/// point on a corner of type has no link.
#[test]
fn a_groups_own_stroke_and_stroked_type_give_preview_corners() {
use vectorcraft_doc::dimension::FreePoint;
let (s, p) = (Selection::default(), paint());
let near = |a: Point, b: Point| a.distance(b) < 1e-9;
let (d, [_, square]) = group_stroked_on_its_own();
let c = ToolContext { preview_bounds: true, smart_guides: false, snap_to_point: false, ..cx(&d, &s, &p) };
let pending = linear_picks(&c, [(97.0, 97.0), (203.0, 203.0)]);
assert!(near(pending.a, Point::new(95.0, 95.0)) && near(pending.b, Point::new(205.0, 205.0)), "{:?} {:?}", pending.a, pending.b);
let Some(DimensionSource::Points { a: None, b: None, free_a: Some(free_a), free_b: Some(free_b), .. }) = &pending.source else {
panic!("{:?}", pending.source)
};
for (free, at) in [(free_a, pending.a), (free_b, pending.b)] {
assert!(matches!(free, FreePoint::Anchors { node, .. } if *node == square), "{free:?}");
assert!(free.position(&d).is_some_and(|q| near(q, at)), "{free:?}");
}
// The same group holding a dimension drawn below the square: its bounds take in the
// dimension, so it gives no corner, and the click picks the square's anchor.
let mut held = d.clone();
let (group, id, line) = (held.layers[0].children().and_then(|c| c.first()).map(|g| g.id), held.alloc_id(), held.alloc_id());
let rule =
vectorcraft_geom::PathData::single(vectorcraft_geom::SubPath::polyline(&[Point::new(100.0, 300.0), Point::new(200.0, 300.0)], false));
let mut drawn =
Node::new(id, NodeKind::Group { children: vec![std::sync::Arc::new(Node::path(line, rule, Default::default()))], clip: false });
let recipe = DimensionTool::default().recipe(DimensionMode::Linear, Point::new(100.0, 300.0), Point::new(200.0, 300.0), Point::ZERO, None);
drawn.extras_mut().dimension = Some(Box::new(recipe));
held.insert(group, 0, drawn).expect("the dimension");
let c = ToolContext { preview_bounds: true, smart_guides: false, snap_to_point: false, ..cx(&held, &s, &p) };
let pending = linear_picks(&c, [(97.0, 97.0), (203.0, 203.0)]);
assert!(near(pending.a, Point::new(100.0, 100.0)), "{:?}", pending.a);
// Type stroked 20 points: its corners lie 10 points outside its frame, at (290, 290) and
// (430, 350), further from it than a click reaches. The second point lands on the stroked
// rectangle's corner.
let (mut d, rect) = doc_with_rect();
let (l, text) = (d.layers[0].id, d.alloc_id());
let style = vectorcraft_doc::CharStyle {
stroke: vectorcraft_color::Paint::solid(vectorcraft_color::Color::BLACK),
stroke_width: 20.0,
..Default::default()
};
let mut t = vectorcraft_doc::TextObject::point(Point::new(300.0, 300.0), "Some words", style);
t.kind = vectorcraft_doc::TextKind::Area { frame: vectorcraft_geom::shapes::rectangle(Rect::new(0.0, 0.0, 120.0, 40.0)) };
d.insert(Some(l), 1, Node::new(text, NodeKind::Text(Box::new(t)))).expect("the type");
let c = ToolContext { preview_bounds: true, smart_guides: false, snap_to_point: false, ..cx(&d, &s, &p) };
let pending = linear_picks(&c, [(289.0, 289.0), (199.0, 199.0)]);
assert!(near(pending.a, Point::new(290.0, 290.0)) && near(pending.b, Point::new(200.5, 200.5)), "{:?} {:?}", pending.a, pending.b);
let Some(DimensionSource::Points { a: None, free_a: None, free_b: Some(free_b), .. }) = &pending.source else {
panic!("{:?}", pending.source)
};
assert!(matches!(free_b, FreePoint::Anchors { node, .. } if *node == rect), "{free_b:?}");
let pending = linear_picks(&c, [(431.0, 351.0), (199.0, 199.0)]);
assert!(near(pending.a, Point::new(430.0, 350.0)), "{:?}", pending.a);
// With the preference off, the same clicks find no corner.
let off = ToolContext { preview_bounds: false, ..c };
let pending = linear_picks(&off, [(289.0, 289.0), (199.0, 199.0)]);
assert!(near(pending.a, Point::new(289.0, 289.0)), "{:?}", pending.a);
}
/// A group with more members than one pointer move may read gives no corner, and is not read
/// again for the revision. A group within the limit still gives its corner.
#[test]
fn a_group_past_the_member_work_gives_no_corner() {
let (s, p) = (Selection::default(), paint());
// Two members plus `extra`: exactly the limit, then one more.
for (extra, within) in [(PREVIEW_MEMBER_WORK - 2, true), (PREVIEW_MEMBER_WORK - 1, false)] {
let (mut d, [group, _]) = group_stroked_on_its_own();
for _ in 0..extra {
let (id, at) = (d.alloc_id(), d.node(group).map_or(0, |g| g.children().map_or(0, Vec::len)));
let dot = Node::path(id, vectorcraft_geom::shapes::rectangle(Rect::new(150.0, 150.0, 151.0, 151.0)), Default::default());
d.insert(Some(group), at, dot).expect("a member");
}
let c = ToolContext { preview_bounds: true, ..cx(&d, &s, &p) };
let mut cache = PreviewBoxes::default();
let corner = preview_snap(&c, Point::new(96.0, 96.0), &mut cache).corner;
assert_eq!(corner.is_some(), within, "{} members", extra + 2);
assert_eq!(cache.preview.contains_key(&group), within, "{} members", extra + 2);
assert_eq!(cache.geometric.get(&group).copied().flatten().is_some(), within, "{} members", extra + 2);
assert!(cache.geometric.contains_key(&group), "{} members", extra + 2);
}
}
/// With Use Preview Bounds on, a Linear pick off any anchor or segment lands on the nearest
/// point of an edge of the preview bounds within 8 pixels, held against the path, so a dimension across a stroked rectangle measures the outside of the stroke. A
/// segment nearer the pointer than the edge takes the pick, and with the preference off the
/// segment takes it.
#[test]
fn with_preview_bounds_points_land_on_the_edges_of_the_stroke() {
use vectorcraft_doc::dimension::FreePoint;
let (s, p) = (Selection::default(), paint());
let near = |a: Point, b: Point| a.distance(b) < 1e-9;
let (mut d, id) = doc_with_rect();
let mut wide = vectorcraft_doc::Appearance::default_art();
wide.stroke_mut().expect("a stroke").width = 10.0;
d.node_mut(id).expect("the rectangle").appearance = wide.into();
let c = ToolContext { preview_bounds: true, smart_guides: false, snap_to_point: false, ..cx(&d, &s, &p) };
let pending = linear_picks(&c, [(150.0, 97.0), (150.0, 203.0)]);
assert!(near(pending.a, Point::new(150.0, 95.0)) && near(pending.b, Point::new(150.0, 205.0)), "{:?} {:?}", pending.a, pending.b);
assert!((pending.length - 110.0).abs() < 1e-9, "{}", pending.length);
let Some(DimensionSource::Points { a: None, b: None, free_a: Some(free_a), free_b: Some(free_b), .. }) = &pending.source else {
panic!("{:?}", pending.source)
};
for (free, at) in [(free_a, pending.a), (free_b, pending.b)] {
assert!(matches!(free, FreePoint::Anchors { node, .. } if *node == id), "{free:?}");
assert!(free.position(&d).is_some_and(|q| near(q, at)), "{free:?}");
}
// Nearer the segment than the edge: the segment takes it.
let pending = linear_picks(&c, [(150.0, 101.0), (150.0, 203.0)]);
assert!(near(pending.a, Point::new(150.0, 100.0)), "{:?}", pending.a);
let off = ToolContext { preview_bounds: false, ..c };
let pending = linear_picks(&off, [(150.0, 97.0), (150.0, 203.0)]);
assert!(near(pending.a, Point::new(150.0, 100.0)) && near(pending.b, Point::new(150.0, 200.0)), "{:?} {:?}", pending.a, pending.b);
// A group's own stroke: its edges take the pick too, held against its member.
let (d, [_, square]) = group_stroked_on_its_own();
let c = ToolContext { preview_bounds: true, smart_guides: false, snap_to_point: false, ..cx(&d, &s, &p) };
let pending = linear_picks(&c, [(97.0, 150.0), (203.0, 150.0)]);
assert!(near(pending.a, Point::new(95.0, 150.0)) && near(pending.b, Point::new(205.0, 150.0)), "{:?} {:?}", pending.a, pending.b);
let Some(DimensionSource::Points { free_a: Some(free_a), .. }) = &pending.source else { panic!("{:?}", pending.source) };
assert!(matches!(free_a, FreePoint::Anchors { node, .. } if *node == square), "{free_a:?}");
}
/// A first point keeps its link when the art moves before the second click: a point picked on
/// a corner of the preview bounds, on a segment or on an anchor is measured, shown and
/// constrained by Shift from where the art puts it now.
+18 -12
View File
@@ -36,19 +36,25 @@ The **Dimension Tool** is in the Modify section of the toolbar. Its strip offers
before the second click: the rubber band, Shift and the dimension start from where the art puts
the point then. When that art is deleted before the second click, the point goes back to where
it was picked.
With Use Preview Bounds on (Preferences › General), a Linear point can land on a corner of the
preview bounds of a path or compound path whose strokes or effects reach past its anchors. The
With Use Preview Bounds on (Preferences › General), a Linear point can land on the preview
bounds of a path, a compound path, stroked type, or a group or layer with fills, strokes or
effects of its own, wherever those bounds reach past the object's geometric bounds. The
[linear dimensions help](https://helpx.adobe.com/illustrator/desktop/measure-and-align/plot-and-measure/plot-linear-dimensions-of-objects.html)
describes measuring a stroked segment by snapping onto its preview bounds. VectorCraft snaps to
the four corners of those bounds only, not to their edges, by its own rules: a click within 8
pixels of a corner lands on it instead of on the anchor at the same corner of the object's
geometric bounds, and any other anchor, of that object or of other art, that is nearer the
pointer than the corner takes the point instead. A corner that is also an anchor, as on a path
without a stroke, takes the anchor, and so does another path's anchor right on the corner. The
corners of a group's own strokes or effects, of type, of paths made into guides and of the
members of a compound path on their own are not snapped to. The point is linked to the path (in a compound path, to the member
whose own bounds reach nearest to the corner), is held against its anchors and shows the
"anchor" label. It does not start construction guides for the second point. For an object that
describes measuring a stroked segment by snapping onto its preview bounds. VectorCraft's own
rules decide where the point lands on them: a click within 8 pixels of a corner lands on it
instead of on the anchor at the same corner of the object's geometric bounds, and any other
anchor, of that object or of other art, that is nearer the pointer than the corner takes the
point instead. A corner that is also an anchor, as on a path without a stroke, takes the anchor,
and so does another path's anchor right on the corner. A click that no anchor, corner, line end,
guide, grid or pixel snapping places lands on the nearest point of an edge of the preview bounds
within 8 pixels when that edge is nearer the pointer than any segment or dimension line, and
shows the "path" label. The corners and edges of paths made into guides and of the members of a
compound path on their own are not snapped to. A point on a corner or an edge is linked to the
path (in a compound path, group or layer, to the member path whose own bounds reach nearest to
the corner, and for an edge to the member holding its nearer corner) and is held against its
anchors. A point on the preview bounds of type, or held by type in a group, has no link and
stays where it was picked. A point on a corner shows the "anchor" label. Neither kind of point
starts construction guides for the second point. For an object that
is not rotated, a horizontal or vertical dimension between two such corners measures, when it
is drawn, the outside of the stroke, the width or height that W and H show in the Transform and
Properties panels. The point then moves with the path's anchors, so a later change to the
+1 -1
View File
@@ -741,7 +741,7 @@ since 2026-10-01.
**Drawing tools, Dimension tool points on a Smart Guide stay on the guide:** By VectorCraft's own rule, a clicked point that lies on a Smart Guide already, such as one in line with an object's center, stays where the pointer is when no anchor is within 8 pixels and no segment is within the Smart Guides snapping tolerance, instead of moving onto a segment farther than that tolerance and within 8 pixels; with Snap to Grid or Snap to Pixel on, a point stays on the grid or the whole pixel those options give. `a_point_on_a_guide_stays_on_the_guide` covers it.
**Drawing tools, Dimension tool second point without construction guides through a first point off the anchors:** By VectorCraft's own rule, with Smart Guides on, the second Linear point does not snap to the construction guides through a first point that is not on an anchor, so a click beside a segment straight below such a first point lands on the segment; after a first point picked on an anchor, the construction guides through it still apply. The second point still snaps to anchors, centers and alignment lines, and with Snap to Perpendicular or Snap to Tangent on, it lands where the line from the first point meets a segment square or grazes a curve. `a_point_on_a_guide_stays_on_the_guide`, `the_second_point_after_an_anchor_lands_on_construction_guides`, `the_second_point_lands_square_to_a_segment_or_grazing_a_curve` and `linear_picks_show_and_snap_to_smart_guides` cover it.
**Drawing tools, Dimension tool points land on dimensions already drawn (#8063):** By VectorCraft's own rule, with Smart Guides or Snap to Point on, a Linear point that no anchor of the art within 8 pixels takes lands on the end of a visible, unlocked dimension's dimension line, or on the far end of its extension line, within 8 pixels, and a point that nothing else places lands on the closest point of such a line within 8 pixels, so at any zoom a dimension can start where another one ends. The end of an extension line next to the point it measures does not pull points away from that anchor. With Smart Guides and Anchor/Path Labels on, a point on an anchor or line end shows the "anchor" label and a point on a segment or line shows the "path" label. `linear_picks_land_on_anchors_paths_and_drawn_dimensions` and `an_anchor_beside_a_drawn_dimension_takes_the_point` cover it.
**Drawing tools, Dimension tool points on preview bounds corners (#8537):** With Use Preview Bounds on, a Linear point can land on a corner of the preview bounds of a path or compound path with a stroke or effects; the [linear dimensions help](https://helpx.adobe.com/illustrator/desktop/measure-and-align/plot-and-measure/plot-linear-dimensions-of-objects.html) describes measuring a stroked segment by snapping onto its preview bounds. By VectorCraft's own rule, only the four corners are snapped to: a click within 8 pixels of a corner lands on it instead of on the anchor at the same corner of the object's geometric bounds, and any other anchor nearer the pointer takes the point; for an object that is not rotated, a horizontal or vertical dimension between two such corners measures, when it is drawn, the size that W and H show. The edges of the preview bounds, a group's own strokes or effects, type and paths made into guides are not snapped to. `with_preview_bounds_points_land_on_the_corners_of_the_stroke`, `preview_bounds_corners_take_picks_as_anchors_do`, `a_corner_takes_only_the_anchor_it_stands_for`, `a_point_is_held_for_a_corner_only_when_picked_on_it` and `hovering_a_corner_of_the_preview_bounds_shows_the_anchor_ring` cover it.
**Drawing tools, Dimension tool points on preview bounds corners (#8537):** With Use Preview Bounds on, a Linear point can land on a corner of the preview bounds of a path or compound path with a stroke or effects; the [linear dimensions help](https://helpx.adobe.com/illustrator/desktop/measure-and-align/plot-and-measure/plot-linear-dimensions-of-objects.html) describes measuring a stroked segment by snapping onto its preview bounds. By VectorCraft's own rules, a click within 8 pixels of a corner lands on it instead of on the anchor at the same corner of the object's geometric bounds, and any other anchor nearer the pointer takes the point; for an object that is not rotated, a horizontal or vertical dimension between two such corners measures, when it is drawn, the size that W and H show. The corners of stroked type and of a group or layer with fills, strokes or effects of its own are snapped to as well (a group's corner held against the member path that reaches it, a point on type without a link), and a click off any anchor or segment lands on the nearest point of an edge of the preview bounds within 8 pixels when no segment is nearer. Paths made into guides are not snapped to. `with_preview_bounds_points_land_on_the_corners_of_the_stroke`, `preview_bounds_corners_take_picks_as_anchors_do`, `a_corner_takes_only_the_anchor_it_stands_for`, `a_point_is_held_for_a_corner_only_when_picked_on_it`, `hovering_a_corner_of_the_preview_bounds_shows_the_anchor_ring`, `a_groups_own_stroke_and_stroked_type_give_preview_corners`, `a_group_past_the_member_work_gives_no_corner` and `with_preview_bounds_points_land_on_the_edges_of_the_stroke` cover it.
**Drawing tools, Dimension tool lines line up with dimensions already drawn (#8063, #5590):** By VectorCraft's own rule, with Smart Guides on, a linear dimension placed with its line within the Smart Guides snapping tolerance of the level of a parallel line of a visible, unlocked linear dimension already drawn has its line on that level, and with Alignment Guides on a guide runs along the level across both lines while it is placed. `a_dimension_line_placed_beside_another_lands_on_its_level`, `a_dimension_line_on_another_level_shows_one_guide_across_both`, `a_dimension_of_the_same_points_lines_up_and_the_line_follows_the_pointer`, `a_dimension_dragged_out_beside_another_lands_on_its_level` and `a_line_the_pointer_lies_across_wins_on_one_level` cover it.
**Drawing tools, Dimension tool moves a dimension already drawn:** As the [linear dimensions help](https://helpx.adobe.com/illustrator/desktop/measure-and-align/plot-and-measure/plot-linear-dimensions-of-objects.html) describes, a drag with the Dimension tool from a dimension already drawn repositions it, as one undo step, keeping what it measures and its link to the art. By VectorCraft's own rule, the part grabbed follows the pointer: a linear dimension's line moves as far across as the pointer moves, an angular arc moves as far as the pointer moves away from or toward the vertex, and a radius or diameter line turns about the center; the press must be where the canvas hits the dimension, within 8 pixels; with Smart Guides on, a linear dimension's line lands on the level of a parallel dimension line. `dimension.update` takes `at`. `dragging_a_placed_dimension_with_the_dimension_tool_moves_its_line`, `a_dimension_moved_beside_another_lands_on_its_level`, `a_dragged_dimension_follows_the_part_grabbed`, `a_dimension_is_dragged_only_where_the_canvas_hits_it` and `dimension_update_at_moves_the_line_through_the_point` cover it.
**Drawing tools, Dimension tool dimensions selected with their art keep their lines and label:** Object › Expand, Object › Expand Appearance and Object › Path › Outline Stroke leave a dimension selected with art it measures, directly or in selected groups without fills, strokes or effects of their own, as it is and change only the other art; when the new art no longer fits its link, the dimension stays where it was, no longer linked, and undo links it again. `expand_leaves_a_dimension_selected_with_its_art_as_it_is`, `expand_appearance_leaves_a_dimension_selected_with_its_art_as_it_is`, `outline_stroke_leaves_a_dimension_selected_with_its_art_as_it_is`, `a_dimension_selected_with_art_that_has_nothing_to_expand_changes_nothing`, `a_group_effect_expanded_reaches_the_dimension_it_holds`, `expand_keeps_a_dimension_in_its_asset_and_its_variable_binding`, `a_dimension_keeps_no_link_to_the_art_expand_replaced`, `a_dimension_outlasts_the_art_outline_stroke_or_expand_appearance_replaced`, `a_dimension_outlasts_the_stroke_expand_outlined` and `expand_appearance_in_safe_mode_leaves_a_dimension_selected_with_its_art` cover it.