diff --git a/crates/engine/tests/it/dimensions.rs b/crates/engine/tests/it/dimensions.rs index f864877025..17391ae98a 100644 --- a/crates/engine/tests/it/dimensions.rs +++ b/crates/engine/tests/it/dimensions.rs @@ -1956,6 +1956,149 @@ fn an_anchor_beside_a_drawn_dimension_takes_the_point() { assert_eq!(linear_marker(&mut s, zoomed, 125.5, 358.3), (Some((pt(126.0, 358.0), 10.0)), vec!["anchor".to_string()])); } +/// The Smart Guide lines a pointer move at (`x`, `y`) shows, as their ends. +fn guide_lines(s: &mut Session, view: ViewInfo, x: f64, y: f64) -> Vec<(vectorcraft_geom::Point, vectorcraft_geom::Point)> { + use vectorcraft_engine::tools::Overlay; + s.pointer(&PointerEvent::new(PointerKind::Move, x, y), view).unwrap(); + s.overlays(view).into_iter().filter_map(|o| if let Overlay::Line { a, b, .. } = o { Some((a, b)) } else { None }).collect() +} + +/// An edge with anchors at x 100, 200 and 300, and a dimension from 100 to 200 placed 30 points +/// above it, with the Dimension tool active and nothing selected: the session and the dimension. +fn level_start(view: ViewInfo) -> (Session, u64) { + let pt = vectorcraft_geom::Point::new; + let mut s = session(); + run(&mut s, "path.create", json!({"d": "M100 200 L200 200 L300 200 L300 300 L100 300 Z"})); + run(&mut s, "select.none", json!({})); + s.select_tool("dimension", view).unwrap(); + linear_click(&mut s, view, 100.0, 200.0); + linear_click(&mut s, view, 200.0, 200.0); + linear_click(&mut s, view, 150.0, 170.0); + let first = s.doc().unwrap().selection.objects[0].0; + assert_eq!(recipe(&s, first).line_ends(), (pt(100.0, 170.0), pt(200.0, 170.0))); + run(&mut s, "select.none", json!({})); + (s, first) +} + +/// The next dimension, from 200 to 300, placed with the pointer at `y`: where its line lies. +fn level_next(s: &mut Session, view: ViewInfo, y: f64) -> (vectorcraft_geom::Point, vectorcraft_geom::Point) { + linear_click(s, view, 200.0, 200.0); + linear_click(s, view, 300.0, 200.0); + linear_click(s, view, 250.0, y); + let id = s.doc().unwrap().selection.objects[0].0; + recipe(s, id).line_ends() +} + +/// A dimension placed next to another, with its line within the Smart Guides snapping tolerance of +/// the level of the other's parallel line, has its line on that level. A level further off, Smart +/// Guides off, a locked dimension or one that is not parallel leaves the line where the pointer +/// puts it. +#[test] +fn a_dimension_line_placed_beside_another_lands_on_its_level() { + let plain = ViewInfo::default(); + let off = ViewInfo { smart_guides: false, ..plain }; + let pt = vectorcraft_geom::Point::new; + // 2.5 points below the first line's level, and 3.5 above it: on the level. + for y in [172.5, 166.5] { + let (mut s, _) = level_start(plain); + assert_eq!(level_next(&mut s, plain, y), (pt(200.0, 170.0), pt(300.0, 170.0)), "pointer at y {y}"); + } + // 10 points off: where the pointer is. + let (mut s, _) = level_start(plain); + assert_eq!(level_next(&mut s, plain, 180.0), (pt(200.0, 180.0), pt(300.0, 180.0))); + // Smart Guides off: where the pointer is. + let (mut s, _) = level_start(off); + assert_eq!(level_next(&mut s, off, 172.5), (pt(200.0, 172.5), pt(300.0, 172.5))); + // A locked dimension does not pull the line. + let (mut s, first) = level_start(plain); + run(&mut s, "select.set", json!({"ids":[first]})); + run(&mut s, "object.lock", json!({})); + assert_eq!(level_next(&mut s, plain, 172.5), (pt(200.0, 172.5), pt(300.0, 172.5))); + // A dimension of the right edge has a vertical line, which is not parallel to the first: the + // pointer 2.5 points below the first line's level places it where the pointer is. + let (mut s, _) = level_start(plain); + linear_click(&mut s, plain, 300.0, 200.0); + linear_click(&mut s, plain, 300.0, 300.0); + linear_click(&mut s, plain, 330.0, 172.5); + let id = s.doc().unwrap().selection.objects[0].0; + assert_eq!(recipe(&s, id).line_ends(), (pt(330.0, 200.0), pt(330.0, 300.0))); +} + +/// While a dimension is placed with its line on the level of another, one alignment guide runs +/// along the level across both lines. Off the level, after a dropped press and with Alignment +/// Guides off, no guide shows. +#[test] +fn a_dimension_line_on_another_level_shows_one_guide_across_both() { + let plain = ViewInfo::default(); + let pt = vectorcraft_geom::Point::new; + let (mut s, _) = level_start(plain); + linear_click(&mut s, plain, 200.0, 200.0); + linear_click(&mut s, plain, 300.0, 200.0); + let guides = guide_lines(&mut s, plain, 250.0, 172.5); + assert!(guides.iter().any(|(a, b)| near(*a, pt(100.0, 170.0)) && near(*b, pt(300.0, 170.0))), "{guides:?}"); + assert!(guide_lines(&mut s, plain, 250.0, 180.0).is_empty(), "no guide off the level"); + // A press on the level that the app drops cancels the preview, and its guide goes with it. + assert!(!guide_lines(&mut s, plain, 250.0, 172.5).is_empty()); + s.pointer(&PointerEvent::new(PointerKind::Down, 250.0, 172.5), plain).unwrap(); + s.drop_tool_press(plain).unwrap(); + assert!(s.overlays(plain).iter().all(|o| !matches!(o, vectorcraft_engine::tools::Overlay::Line { .. })), "{:?}", s.overlays(plain)); + // Alignment Guides off: the line still lands on the level, with no guide. + let (mut s, _) = level_start(plain); + run(&mut s, "prefs.set", json!({"key": "alignmentGuides", "value": false})); + linear_click(&mut s, plain, 200.0, 200.0); + linear_click(&mut s, plain, 300.0, 200.0); + assert!(guide_lines(&mut s, plain, 250.0, 172.5).is_empty()); + linear_click(&mut s, plain, 250.0, 172.5); + let id = s.doc().unwrap().selection.objects[0].0; + assert_eq!(recipe(&s, id).line_ends(), (pt(200.0, 170.0), pt(300.0, 170.0))); +} + +/// A second dimension of the same two points as a dimension already drawn lands on its level too. +/// Off every level, a pointer moved in small steps while it is placed puts the line at each step, +/// with no pull from the preview of the dimension itself. +#[test] +fn a_dimension_of_the_same_points_lines_up_and_the_line_follows_the_pointer() { + let plain = ViewInfo::default(); + let pt = vectorcraft_geom::Point::new; + let (mut s, first) = level_start(plain); + linear_click(&mut s, plain, 100.0, 200.0); + linear_click(&mut s, plain, 200.0, 200.0); + linear_click(&mut s, plain, 150.0, 172.5); + let id = s.doc().unwrap().selection.objects[0].0; + assert_ne!(id, first); + assert_eq!(recipe(&s, id).line_ends(), (pt(100.0, 170.0), pt(200.0, 170.0))); + let (mut s, _) = level_start(plain); + linear_click(&mut s, plain, 200.0, 200.0); + linear_click(&mut s, plain, 300.0, 200.0); + for y in [190.0, 189.0, 188.0, 187.0, 186.0] { + s.pointer(&PointerEvent::new(PointerKind::Move, 250.0, y), plain).unwrap(); + } + for kind in [PointerKind::Down, PointerKind::Up] { + s.pointer(&PointerEvent::new(kind, 250.0, 186.0), plain).unwrap(); + } + let id = s.doc().unwrap().selection.objects[0].0; + assert_eq!(recipe(&s, id).line_ends(), (pt(200.0, 186.0), pt(300.0, 186.0))); +} + +/// A dimension dragged out from a segment, with its line within the Smart Guides snapping +/// tolerance of the level of another dimension's parallel line, lands on that level. +#[test] +fn a_dimension_dragged_out_beside_another_lands_on_its_level() { + let plain = ViewInfo::default(); + let pt = vectorcraft_geom::Point::new; + // The bottom edge dragged out to 2 points below the level of the first line, 130 points + // above the edge: on the level. + let (mut s, _) = level_start(plain); + for (kind, y) in + [(PointerKind::Move, 300.0), (PointerKind::Down, 300.0), (PointerKind::Drag, 240.0), (PointerKind::Drag, 172.0), (PointerKind::Up, 172.0)] + { + s.pointer(&PointerEvent::new(kind, 160.0, y), plain).unwrap(); + } + let id = s.doc().unwrap().selection.objects[0].0; + let (a, b) = recipe(&s, id).line_ends(); + assert!(near(a, pt(300.0, 170.0)) && near(b, pt(100.0, 170.0)) || near(a, pt(100.0, 170.0)) && near(b, pt(300.0, 170.0)), "{a:?} {b:?}"); +} + /// The page content streams of exported PDF `v`, inflated. fn pdf_content(v: &Value) -> String { let bytes = vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap(); diff --git a/crates/tools/src/dimension.rs b/crates/tools/src/dimension.rs index 3e50415434..e397b1f9ed 100644 --- a/crates/tools/src/dimension.rs +++ b/crates/tools/src/dimension.rs @@ -76,6 +76,12 @@ pub struct DimensionTool { /// for that click (a snap or Shift can move the point away from it): placed with the pointer /// still at either, it is aligned. second: Option<(Point, Point)>, + /// While a Linear dimension is placed with its line on the level of another dimension's line: + /// the ends of that other line. A Smart Guide runs along the level across both lines. + level_guide: Option<(Point, Point)>, + /// While a dimension is placed: the lines of the visible, unlocked linear dimensions in the + /// document before its first preview ([`dimension_lines`]), which its line can line up with. + levels: Vec, } /// What a linear dimension between `a` and `b` measures with the pointer at `p` while it is @@ -285,6 +291,58 @@ fn on_dimensions(cx: &ToolContext, p: Point) -> Option<(Point, bool)> { best.end.map(|(_, q)| (q, true)).or_else(|| best.line.map(|(_, q)| (q, false))) } +/// The line of a linear dimension already drawn: its ends and its direction. +type DimensionLine = ((Point, Point), Vec2); + +/// The lines of the visible, unlocked linear dimensions in the document. Bounded work, as in +/// [`on_dimensions`]. +fn dimension_lines(cx: &ToolContext) -> Vec { + fn visit(n: &Node, budget: &mut usize, out: &mut Vec) { + if *budget == 0 || !n.visible || n.locked { + return; + } + *budget -= 1; + if let Some(d) = &n.extras().dimension { + if d.mode == DimensionMode::Linear { + out.push((d.line_ends(), d.direction())); + } + return; + } + for c in n.children().into_iter().flatten().rev() { + visit(c, budget, out); + } + } + let (mut budget, mut out) = (20_000usize, vec![]); + for n in cx.doc.layers.iter().rev() { + visit(n, &mut budget, &mut out); + } + out +} + +/// Where the line of the linear dimension `d`, placed through `p`, lands on the level of one of +/// the `levels` that runs parallel to it, within `reach` of `p` across the line: the point to +/// place `d` through, and the ends of that other line. The nearest level wins, then the line +/// nearest `p` along it (none nearer than a line that `p` lies across). +fn line_level(levels: &[DimensionLine], d: &Dimension, p: Point, reach: f64) -> Option<(Point, (Point, Point))> { + if d.mode != DimensionMode::Linear { + return None; + } + let (u, normal) = (d.direction(), d.normal()); + let mut best: Option<(f64, f64, f64, (Point, Point))> = None; + for &(ends, direction) in levels { + if u.cross(direction).abs() > 1e-6 { + continue; + } + let shift = (ends.0 - p).dot(normal); + let (s0, s1) = ((ends.0 - p).dot(u), (ends.1 - p).dot(u)); + let along = if s0 * s1 <= 0.0 { 0.0 } else { s0.abs().min(s1.abs()) }; + if shift.is_finite() && along.is_finite() && shift.abs() <= reach && best.is_none_or(|(s, a, _, _)| (shift.abs(), along) < (s, a)) { + best = Some((shift.abs(), along, shift, ends)); + } + } + best.map(|(_, _, shift, ends)| (p + normal * shift, ends)) +} + /// The point of `edge` closest to `p`. fn nearest_on(cx: &ToolContext, edge: &SegmentRef, p: Point) -> Option { use vectorcraft_geom::kurbo::ParamCurveNearest; @@ -435,6 +493,7 @@ impl DimensionTool { p } fn preview(&mut self, cx: &ToolContext, p: Point) -> Vec { + self.level_guide = None; let Some(d) = &mut self.pending else { return vec![] }; if matches!(d.mode, DimensionMode::Radius | DimensionMode::Diameter) && let Some(Target::Radial { center, radius, .. }) = self.press.as_ref().and_then(|(_, t)| t.as_ref()) @@ -462,10 +521,18 @@ impl DimensionTool { if d.resolve(cx.doc).is_err() { return vec![]; } - d.place(p); + // With Smart Guides on, a linear dimension's line lands on the level of a parallel + // dimension line already drawn within the Smart Guides snapping tolerance. The lines are + // read before the first preview, so the preview of this dimension is not one of them. + if !self.began { + self.levels = dimension_lines(cx); + } + let level = if cx.smart_guides { line_level(&self.levels, d, p, cx.snap_tol()) } else { None }; + d.place(level.map_or(p, |(on_level, _)| on_level)); if d.validate().is_err() { return vec![]; } + self.level_guide = level.map(|(_, ends)| ends); let mut actions = vec![]; if !self.began { actions.push(Action::Begin("Create Dimension".into())); @@ -495,6 +562,8 @@ impl DimensionTool { self.snap.clear(); self.snapped = None; self.second = None; + self.level_guide = None; + self.levels.clear(); } /// A plain click: picks a point or a line, or measures a circle's radius. fn click(&mut self, cx: &ToolContext, p: Point, target: Option, mods: Mods) -> Vec { @@ -664,6 +733,7 @@ impl Tool for DimensionTool { self.press = None; if self.pending.is_some() && self.began { self.began = false; + self.level_guide = None; vec![Action::Cancel] } else { vec![] @@ -741,6 +811,21 @@ impl Tool for DimensionTool { if linear_pick { out.extend(self.snap.guides().iter().cloned()); } + // A dimension line placed on the level of another: one alignment guide across both lines, + // unless Alignment Guides are off. + if cx.alignment_guides + && let (Some(d), Some((c, e))) = (&self.pending, self.level_guide) + { + let (a, b) = d.line_ends(); + let u = d.direction(); + let along = |q: &Point| q.to_vec2().dot(u); + let ends = [a, b, c, e]; + if let (Some(lo), Some(hi)) = + (ends.iter().min_by(|p, q| along(p).total_cmp(&along(q))), ends.iter().max_by(|p, q| along(p).total_cmp(&along(q)))) + { + out.push(Overlay::Line { a: *lo, b: *hi, color: cx.smart_guide_color, dashed: false }); + } + } out } } @@ -778,6 +863,21 @@ mod tests { assert!(t.pointer(&c, &ev(PointerKind::Up, 150.0, 60.0)).is_empty(), "the release places nothing"); } + /// Of two dimension lines on one level, a line the pointer lies across wins over a line whose + /// end is nearer the pointer. + #[test] + fn a_line_the_pointer_lies_across_wins_on_one_level() { + let pt = Point::new; + let d = DimensionTool::default().recipe(DimensionMode::Linear, pt(200.0, 200.0), pt(300.0, 200.0), pt(250.0, 200.0), None); + let across = ((pt(0.0, 170.0), pt(400.0, 170.0)), Vec2::new(1.0, 0.0)); + let beside = ((pt(100.0, 170.0), pt(200.0, 170.0)), Vec2::new(1.0, 0.0)); + for levels in [[beside, across], [across, beside]] { + let (at, ends) = line_level(&levels, &d, pt(250.0, 172.0), 4.0).expect("on the level"); + assert!(at.distance(pt(250.0, 170.0)) < 1e-9, "{at:?}"); + assert_eq!(ends, across.0); + } + } + #[test] fn a_dropped_placement_press_does_not_commit_the_dimension() { let (d, _) = doc_with_rect(); diff --git a/docs/dimensions.md b/docs/dimensions.md index 35e164d4f9..762575379b 100644 --- a/docs/dimensions.md +++ b/docs/dimensions.md @@ -51,6 +51,11 @@ The **Dimension Tool** is in the Modify section of the toolbar. Its strip offers point as before. An unlinked dimension that turns has its line where the turn puts it, or, when that would put it between the points, as far past the outermost point as before. + 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 + anywhere in the document has its line on that level. This is VectorCraft's own rule. With + Alignment Guides on, a guide runs along the level across both lines while the dimension is + placed. - **Angular:** hover a corner where two straight segments meet and drag outwards. Or click two straight lines that meet (or would, extended), then click to place. - **Radial:** hover a circle, or a segment that is a perfect arc, and drag: outwards gives its diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index e773fe9478..3b942106c7 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -638,6 +638,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 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 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. **Drawing tools, Dimension tool Arrow Style shows each arrowhead (#8049):** The Arrow Style dropdown in Dimension Tool Options and in the Properties panel's Dimension section draws each style's arrowhead at the end of a short line before its name, in the open list and in the closed field, using the Stroke panel's arrowhead previews. Filled, Open, Tick and None are the same four styles, saved as before. `arrow_style_shows_a_drawn_arrowhead` and `each_arrow_style_draws_its_stroke_arrowhead_at_both_ends` cover it. **Drawing tools, Line Segment from one curve onto a common tangent of another:** By VectorCraft's own rule, with Smart Guides and Snap to Tangent on, a Line Segment drag pressed anywhere on a convex closed path with curves and dragged to within the snapping tolerance of another such path lands on a common tangent of the two ([tangent lines to circles](https://en.wikipedia.org/wiki/Tangent_lines_to_circles)): both ends move to where the line touches the paths, the start sliding along the first path, and each end shows the "tangent" label when Anchor/Path Labels is on, with the line drawn as a guide when Alignment Guides is on. The line is the tangent whose touch point on the second path is nearest the pointer, so moving the pointer around the second path switches between the outer tangent and the one crossing between the paths. The start stays where it was pressed when Shift, Alt or Space is held, when an anchor, center or crossing lies nearer the pointer than the second path does, when the press snapped to an anchor, center or crossing off the first path, when Snap to Tangent is off, when Snap to Grid or pixel snapping places drawn points, or when either path bends inward somewhere or has more than 512 segments. `a_line_from_one_circle_to_another_lands_on_their_common_tangent`, `a_line_from_a_turned_ellipse_lands_on_its_tangent_with_a_circle`, `a_line_near_the_second_path_takes_the_tangent_touching_it_nearest`, `a_line_keeps_its_start_without_a_tangent_snap`, `a_line_to_a_crescent_or_from_an_anchor_beside_the_curve_keeps_its_start` and `a_line_segment_drag_draws_the_common_tangent_as_one_undo_step` cover it.