From 4f89536718ac65f28a0c396f130cb404c4190bd1 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 18 Nov 2026 06:28:21 -0800 Subject: [PATCH] Pen: the rubber band over the first anchor or another path's end is the segment a click adds Over the path's first anchor, a click closes the path on that anchor; over an end of another open path, it joins the two, and the segment arrives along that end's own handle. The preview ended at the pointer and arrived without a handle, so the segment moved by up to the pick tolerance at the click and changed shape when the other end was smooth. The preview now ends on the anchor the click uses, with that anchor's handle, and picks the anchor at the pointer before snapping, in the order a press does. Part of #8296. --- crates/engine/tests/it/main.rs | 1 + crates/engine/tests/it/pen_rubber_band.rs | 136 ++++++++++++++++++++++ crates/tools/src/pen.rs | 44 ++++++- docs/target-app-parity.md | 1 + 4 files changed, 177 insertions(+), 5 deletions(-) create mode 100644 crates/engine/tests/it/pen_rubber_band.rs diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index 9227ee199d..eccd6e08b6 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -27,6 +27,7 @@ mod param_keys; mod paste_many; mod pen_click_after_smooth; mod pen_endpoint_picking; +mod pen_rubber_band; mod ruler_guide_nodes; mod ruler_guide_perf; mod ruler_origin; diff --git a/crates/engine/tests/it/pen_rubber_band.rs b/crates/engine/tests/it/pen_rubber_band.rs new file mode 100644 index 0000000000..6f7d3ae5b0 --- /dev/null +++ b/crates/engine/tests/it/pen_rubber_band.rs @@ -0,0 +1,136 @@ +//! The Pen's rubber band after a smooth anchor is the segment the next click adds: a new corner, +//! a close on the first anchor, or a join with an end of another open path. + +use serde_json::json; +use vectorcraft_doc::NodeKind; +use vectorcraft_engine::{Session, ViewInfo}; +use vectorcraft_geom::{CubicBez, PathData, PathEl, Point}; +use vectorcraft_tools::{Overlay, PointerEvent, PointerKind}; + +fn session() -> Session { + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 800, "height": 600})).unwrap(); + s +} + +fn view() -> ViewInfo { + ViewInfo { zoom: 1.0, smart_guides: false, snap_to_grid: false, snap_to_pixel: false, snap_to_point: false, ..Default::default() } +} + +fn at(s: &mut Session, v: ViewInfo, kind: PointerKind, p: Point) { + s.pointer(&PointerEvent::new(kind, p.x, p.y), v).unwrap(); +} + +/// A press at `p` dragged to `out`: a smooth anchor with its outgoing handle at `out`. +fn smooth(s: &mut Session, v: ViewInfo, p: Point, out: Point) { + at(s, v, PointerKind::Down, p); + at(s, v, PointerKind::Drag, out); + at(s, v, PointerKind::Up, out); +} + +fn click(s: &mut Session, v: ViewInfo, p: Point) { + at(s, v, PointerKind::Down, p); + at(s, v, PointerKind::Up, p); +} + +/// The rubber band shown with the pointer hovering at `h`, as one cubic segment. +fn band(s: &mut Session, v: ViewInfo, h: Point) -> CubicBez { + at(s, v, PointerKind::Move, h); + let paths: Vec> = + s.overlays(v).into_iter().filter_map(|o| if let Overlay::Path { path, .. } = o { Some(path.elements().to_vec()) } else { None }).collect(); + match paths.as_slice() { + [els] => match els.as_slice() { + [PathEl::MoveTo(p0), PathEl::CurveTo(p1, p2, p3)] => CubicBez::new(*p0, *p1, *p2, *p3), + other => panic!("the rubber band is one curve, got {other:?}"), + }, + other => panic!("one rubber band, got {other:?}"), + } +} + +fn paths(s: &Session) -> Vec { + let mut v = vec![]; + s.doc().unwrap().doc.walk(|n| { + if let NodeKind::Path { path, .. } = &n.kind { + v.push(path.clone()); + } + }); + v +} + +/// An open path whose first anchor, at `end`, is smooth with its incoming handle at `h_in`. +fn smooth_ended_path(s: &mut Session, end: Point, h_in: Point, other: Point) { + s.execute( + "path.create", + &json!({"anchors": [{"x": end.x, "y": end.y, "in": [h_in.x, h_in.y], "out": [2.0 * end.x - h_in.x, 2.0 * end.y - h_in.y]}, {"x": other.x, "y": other.y}]}), + ) + .unwrap(); + s.execute("select.none", &json!({})).unwrap(); +} + +#[test] +fn rubber_band_is_the_corner_a_click_adds() { + let (mut s, v) = (session(), view()); + s.select_tool("pen", v).unwrap(); + smooth(&mut s, v, Point::new(100.0, 100.0), Point::new(150.0, 60.0)); + let h = Point::new(300.0, 200.0); + let preview = band(&mut s, v, h); + click(&mut s, v, h); + let [path] = paths(&s).try_into().unwrap(); + assert_eq!(path.subpaths[0].segment(0), preview); +} + +#[test] +fn rubber_band_on_the_first_anchor_is_the_closing_segment() { + let (mut s, v) = (session(), view()); + s.select_tool("pen", v).unwrap(); + let first = Point::new(100.0, 100.0); + smooth(&mut s, v, first, Point::new(150.0, 60.0)); + click(&mut s, v, Point::new(300.0, 100.0)); + smooth(&mut s, v, Point::new(300.0, 300.0), Point::new(350.0, 340.0)); + // Within the pick tolerance of the first anchor: the band ends on the anchor, not the pointer. + let preview = band(&mut s, v, Point::new(102.0, 101.0)); + assert_eq!(preview.p3, first); + click(&mut s, v, Point::new(102.0, 101.0)); + let [path] = paths(&s).try_into().unwrap(); + let sp = &path.subpaths[0]; + assert!(sp.closed); + assert_eq!(sp.segment(2), preview); +} + +#[test] +fn rubber_band_on_another_paths_end_is_the_joining_segment() { + let (mut s, v) = (session(), view()); + let end = Point::new(400.0, 300.0); + smooth_ended_path(&mut s, end, Point::new(430.0, 250.0), Point::new(500.0, 300.0)); + s.select_tool("pen", v).unwrap(); + smooth(&mut s, v, Point::new(100.0, 100.0), Point::new(150.0, 60.0)); + let preview = band(&mut s, v, Point::new(402.0, 301.0)); + assert_eq!((preview.p2, preview.p3), (Point::new(430.0, 250.0), end)); + click(&mut s, v, Point::new(402.0, 301.0)); + let [path] = paths(&s).try_into().unwrap(); + assert_eq!(path.subpaths[0].anchors.len(), 3); + assert_eq!(path.subpaths[0].segment(0), preview); +} + +#[test] +fn rubber_band_picks_the_end_at_the_pointer_before_grid_snapping() { + let mut s = session(); + let mut prefs = s.prefs.clone(); + prefs.gridline_every = 100.0; + prefs.grid_subdivisions = 1; + s.apply_prefs(prefs); + let v = ViewInfo { snap_to_grid: true, ..view() }; + // The end is off the grid: the pointer next to it snaps to (100, 300), out of its pick tolerance, + // and a press there still joins at the pointer. + let end = Point::new(63.0, 303.0); + smooth_ended_path(&mut s, end, Point::new(90.0, 250.0), Point::new(13.0, 303.0)); + s.select_tool("pen", v).unwrap(); + smooth(&mut s, v, Point::new(300.0, 500.0), Point::new(350.0, 450.0)); + let pointer = Point::new(64.0, 303.0); + let preview = band(&mut s, v, pointer); + assert_eq!((preview.p2, preview.p3), (Point::new(90.0, 250.0), end)); + click(&mut s, v, pointer); + let [path] = paths(&s).try_into().unwrap(); + assert_eq!(path.subpaths[0].anchors.len(), 3); + assert_eq!(path.subpaths[0].segment(0), preview); +} diff --git a/crates/tools/src/pen.rs b/crates/tools/src/pen.rs index 0322c82ca3..ace3879347 100644 --- a/crates/tools/src/pen.rs +++ b/crates/tools/src/pen.rs @@ -51,6 +51,8 @@ pub struct PenTool { /// The anchor a press placed, while the drag shapes its handles. drag: Option, hover: Option, + /// The pointer at the last hover before snapping: a press picks anchors there first. + hover_raw: Option, /// The incoming handle of the anchor being dragged out, as last previewed. in_h: Point, /// The first anchor, as it was when a press on it began closing the path, and its outgoing @@ -297,6 +299,7 @@ impl Tool for PenTool { match ev.kind { PointerKind::Move => { self.hover = Some(p); + self.hover_raw = Some(ev.pos); vec![] } PointerKind::Down => { @@ -522,17 +525,19 @@ impl Tool for PenTool { // The rubber band preference hides only the preview segment; Measurement Labels decides the readout. if self.drag.is_none() && self.handle.is_none() - && let (Some((id, _, _, last, out)), Some(h)) = (self.active(cx), self.hover) + && let (Some((id, si, first, last, out)), Some(h)) = (self.active(cx), self.hover) { if cx.pen_rubber_band { // The segment a click at `h` adds: it leaves the last anchor along its outgoing - // handle and arrives at the new corner anchor without a handle. + // handle and arrives at the anchor the click ends it on, with that anchor's handle. + let raw = self.hover_raw.unwrap_or(h); + let (end, end_h) = click_end(cx, id, si, first, last, raw, h); let mut bp = BezPath::new(); bp.move_to(last); - if out.distance(last) > 1e-9 { - bp.curve_to(out, h, h); + if out.distance(last) > 1e-9 || end_h.distance(end) > 1e-9 { + bp.curve_to(out, end_h, end); } else { - bp.line_to(h); + bp.line_to(end); } o.push(Overlay::Path { path: bp, color: cx.doc.layer_color(id), width: 1.0, dashed: false }); } @@ -641,6 +646,35 @@ impl PenTool { } } +/// Where a click ends the next segment of subpath `si` of path `id` (first anchor `first`, last +/// anchor `last`), and the incoming handle it arrives with, for a pointer at `raw` that snaps to +/// `h`. The anchors are picked in the order a press picks them: at `raw` first, then at `h`. On the +/// first anchor the path closes, with that anchor's incoming handle retracted +/// ([`PenTool::begin_close`]). On an end of another open path the two join, and the segment arrives +/// with that end's own handle on the joining side. Anywhere else the click adds a corner anchor at `h`. +fn click_end(cx: &ToolContext, id: NodeId, si: usize, first: Point, last: Point, raw: Point, h: Point) -> (Point, Point) { + let tol = cx.tol(5.0).max(cx.point_tol()); + let closing_end = last_anchor(cx, id, Some(si)).filter(|(_, ai)| *ai > 0).map(|_| first); + match pick_endpoint(closing_end, last, raw, tol, Endpoint::First) { + Some(Endpoint::First) => return (first, first), + Some(Endpoint::Last) => return (h, h), + None => {} + } + let raw_other = open_end_at(cx, raw, tol, Some((id, si))); + if raw_other.is_none() && h != raw { + match pick_endpoint(closing_end, last, h, tol, Endpoint::First) { + Some(Endpoint::First) => return (first, first), + Some(Endpoint::Last) => return (h, h), + None => {} + } + } + let joined = raw_other.or_else(|| open_end_at(cx, h, tol, Some((id, si)))).and_then(|(other, osi, at_first)| { + let sp = cx.doc.node(other)?.path_data()?.subpaths.get(osi)?; + if at_first { sp.anchors.first().map(|a| (a.p, a.h_in)) } else { sp.anchors.last().map(|a| (a.p, a.h_out)) } + }); + joined.unwrap_or((h, h)) +} + /// The first anchor of subpath `si` of path `id`. pub(crate) fn first_anchor(cx: &ToolContext, id: NodeId, si: usize) -> Option { cx.doc.node(id)?.path_data()?.subpaths.get(si)?.anchors.first().copied() diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index cdcdc27ca8..796bc7bfee 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1182,6 +1182,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - **Drawing readout without the rubber band:** with Enable Rubber Band for the Pen or Curvature tool off and Measurement Labels on, the Pen, the Smart Pen and the Curvature tool hide the preview segment but still show the D / Angle / dX / dY readout at the snapped pointer before the next click, and the Curvature tool also shows it when going on from the current end of a selected path. Measurement Labels show distance and placement while drawing ([the help page](https://helpx.adobe.com/illustrator/desktop/measure-and-align/grids-and-guides/smart-guides-options.html)), and the rubber band preference turns off the path preview ([the help page](https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/draw-shapes/preview-paths-drawn.html)). Showing the readout with the rubber band off is VectorCraft's own rule (`pen::tests::measurement_label_shows_with_the_rubber_band_off`, and `the_measurement_label_shows_with_the_rubber_band_off` in `draw2::curvature::tests` and `addons::smart_pen::tests`); scores and estimates unchanged. - **Pen handle drags in the Info panel:** while a Pen drag pulls a handle out of an anchor, the Info panel shows the change in x and y (W, H), the distance (D) and the angle, as the Info panel overview in the [archived user guide](https://web.archive.org/web/20220911143322/https://helpx.adobe.com/pdf/cs6/illustrator_reference.pdf#page=76) describes for the Pen. The values run from the anchor being shaped to the end of the handle the drag shapes: the outgoing handle of a new or added anchor or of the last anchor, and the incoming handle of the first anchor when the drag closes the path. Shift gives the constrained handle, Space moves the anchor with its handle, and the readout starts past the 3-pixel drag threshold and ends when the button comes up, the press is dropped or the tool changes. These details are VectorCraft's own rules. Tests: the `info_readout_*` tests in `pen::tests`; scores and estimates unchanged. - **Pen rubber band after a smooth anchor:** the preview segment to the pointer is the curve a click there adds, leaving the last anchor along its outgoing handle and arriving at the new corner anchor without a handle, so the segment keeps its shape when the anchor is placed, as [the help page](https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/draw-shapes/preview-paths-drawn.html) describes. Before, the preview was a different curve, and the segment changed shape at the click (#8296, in part; `pen::tests::rubber_band_is_the_segment_a_click_adds`); scores and estimates unchanged. + - **Pen rubber band over an anchor a click ends on:** over the path's first anchor, the preview segment ends on that anchor, as the closing segment does; over an end of another open path, it ends on that end and arrives along the end's own handle, as the joining segment does. The preview picks these anchors at the pointer before snapping, as a press does. Before, the preview ended at the pointer and arrived without a handle, so the segment moved by up to the pick tolerance at the click, and changed shape when the other end was smooth (#8296, in part; the `pen_rubber_band` tests in `engine/tests/it`); scores and estimates unchanged. - The desktop app keeps the Color panel's Show Options or Hide Options between sessions, and `ui.colorPanel` sets its mode and options as its panel menu does. - Preferences that act (#394, in part): Show Tool Tips, Double Click To Isolate, Zoom with Mouse Wheel (the wheel zooms about the pointer, Shift-wheel scrolls up and down, Cmd/Ctrl-wheel sideways; Alt/Option-wheel and a trackpad pinch zoom about the pointer either way), Use Precise Cursors; Selection & Anchor Display › Tolerance (objects, anchors and handles), Object Selection by Path Only, Command Click to Select Objects Behind (cycling down the stack), Zoom to Selection, anchor Size, Highlight anchors on mouse over; Type › Number of Recent Fonts (up to 15); Guides & Grid › guide and grid Color and Style (lines or dots) and Grids In Back. Then Snap to Point (its distance, for moves, drawn points and reference points), Move Locked and Hidden Artwork with Artboard, Enable Rubber Band for the Pen and Curvature tools, Handles style, Show handles when multiple anchors are selected, Hide Corner Widget for angles greater than; General › Transform Pattern Tiles (the transforms' and dialogs' Transform Patterns, Align and Distribute, art moved or duplicated with an artboard, and objects moved whole with direct-selected anchors) and Select Same Tint %; Type › Size/Leading, Tracking and Baseline Shift increments (`type.step`, Cmd+Shift+. and Cmd+Shift+, for the size, the Type tool's Alt+arrows), Enable Missing Glyph Protection and Fill New Type Objects With Placeholder Text. Then Smart Guides › Color, Alignment Guides, Anchor/Path Labels, Measurement Labels, Transform Tools, Object Highlighting (with Smart Guides on) and Snapping Tolerance (4 px by default, for every smart guide snap), then Construction Guides and their Angles (#506: a drawn point near a line at one of those angles through the point it leaves from lands on it). Then Type › Type Object Selection by Path Only (type is picked on the baseline of any of its lines, area type's frame or the path of type on a path, no longer anywhere in its bounds; the Type tools and the Eyedropper still take a click among the characters). Then General › Anti-aliased Artwork (off, the canvas draws the art with hard edges). Then Guides & Grid › Show Pixel Grid (Above 600% Zoom), a line at every document pixel over the art in Pixel Preview, turning with a rotated view as the document grid now does too. Then General › Show The Home Screen When No Documents Are Open (off, closing the last document leaves an empty window; the Home button still opens the screen). Then Units › Numbers Without Units Are Points (on by default: a number typed with no unit into a field in picas is read in points, a typed unit still wins; dimmed unless a unit is Picas). Then User Interface › Auto-Collapse Iconic Panels (off by default: on, a click away from a panel popped out of the icon column puts it away). Then Performance › Animated Zoom (on, with GPU Performance, which it needs and which dims it when off: the Zoom tool dragged sideways zooms about the press, right in and left out, and held still zooms on, Alt out; off, a drag zooms to the area dragged across, now drawn as a marquee), User Interface › Large Tabs (taller document tabs) and Scale Cursor Proportional to UI (off, the tool cursors keep their size whatever the UI Scaling), and File Handling › Display Bitmaps as Anti-aliased Images in Pixel Preview (off by default: Pixel Preview draws images with each pixel taking its nearest image pixel). Type › Auto Size New Area Type works too (new area type gets Auto Size). They reach agents too (`prefs.set`, then `pointer_gesture` or `press_key`; `ui.wheel` on the control channel). Then Smart Guides › Spacing Guides (moves space the selection evenly with its row or column, see Drawing aids). Then Hyphenation › Exceptions (comma-separated words that never break, or break only at their hyphens; `prefs.set` and restore push them into the hyphenator, #394). Then Type › Show Font Names in English (off, font menus label a family by its native-language name when the font has one; documents still use the English family, #394). Then General › Display Print Size at 100% Zoom (off by default: View › Actual Size is one document point per screen point; on, one document inch fills 96 screen points, an inch at the reference density display scaling is set against; `view.actualSize` / `prefs.set`, #394). Then Guides & Grid › Gridline every and Subdivisions (changing either sets the grid of every open document; new and opened documents take that grid too, except a native file and the native document an SVG, PDF, `.ai` or EPS file carries, which open with the grid they were saved with; `file.new`, `document.open`, `file.newFromTemplate`, `file.revert`, #394). Then Appearance of Black › On Screen (Display All Blacks Accurately by default: pure CMYK black shows through the CMYK profile, a dark grey; Display All Blacks as Rich Black shows it on the canvas as RGB black, also in CMYK documents drawn by ink planes, while rich blacks, black tints and images show as before; `RenderOptions::rich_black`, #394). Then Appearance of Black › Printing / Exporting (Output All Blacks Accurately by default; Output All Blacks as Rich Black draws pure CMYK black as RGB black in RGB raster exports and Print as Bitmap; CMYK ink exports and the document stay unchanged; `prefs.blackOutput` / `richBlack`, #394). Then Units › Identify Objects By (Object Name by default: Layers, Variables and Layer Options show and edit free names; XML ID shows and edits AI-style XML ids, `_` for a space and `_xHH_` escapes, and `layer.setProps` / rename refuse invalid ids; the document still stores the object name, #394). Then Type › Highlight Alternate Glyphs (off by default; on, pink shows behind OpenType substitutions on screen, such as ligatures and contextual forms; Document Setup's Highlight Substituted Glyphs stays separate; `prefs.set` key `highlightAlternateGlyphs`, #394). Then Units › East Asian Type (the bullet below). Then User Interface › Open Documents As Tabs (see Window › Arrange). Two stored options still do nothing yet: Plug-ins & Scratch Disks › Primary and Secondary, and Clipboard Handling › Legacy vector clipboard (the audit below). - Preferences › General › Scroll Sideways with Cmd/Ctrl + Wheel (on by default; dimmed while Zoom with Mouse Wheel is on): while Zoom with Mouse Wheel is off, Cmd/Ctrl with a mouse wheel's whole notches scrolls the canvas sideways instead of zooming. Alt + Cmd/Ctrl-wheel and a pinch on a Mac trackpad, on a touch screen or in the web build still zoom about the pointer, and once a trackpad gesture holds Alt, it zooms until it ends, also after Alt is let go. Under either wheel preference, Cmd/Ctrl with a touchpad zooms about the pointer.