From bcc8bcf6cc9a9e3c05c5362162b7ecadf1134e5d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 19 Nov 2026 15:37:10 -0800 Subject: [PATCH] Quick Pen: a length drag measures the curve against the canvas, not its handles A cubic earlier in the path whose handle lies past the canvas while the curve stays on it no longer blocks every length drag at the end of the path. --- crates/tools/src/quickpen.rs | 9 ++++++--- crates/tools/src/quickpen/tests.rs | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/crates/tools/src/quickpen.rs b/crates/tools/src/quickpen.rs index 2d33815648..38f8e3fca1 100644 --- a/crates/tools/src/quickpen.rs +++ b/crates/tools/src/quickpen.rs @@ -25,7 +25,7 @@ use std::sync::Arc; use serde_json::{Value, json}; use vectorcraft_doc::{Document, MAX_COORD, Node, NodeId, NodeKind}; -use vectorcraft_geom::{Anchor, AnchorKind, BezPath, Point, SubPath, Vec2}; +use vectorcraft_geom::{Anchor, AnchorKind, BezPath, PathData, Point, SubPath, Vec2}; use crate::draw2::FEEDBACK; use crate::guides::{DrawSnap, Leave}; @@ -363,9 +363,12 @@ fn selected_alone(cx: &ToolContext, id: NodeId) -> bool { sel == [id] || cx.doc.parent_of(id).is_some_and(|p| sel == [p] && cx.doc.node(p).is_some_and(|n| matches!(n.kind, NodeKind::Compound { .. }))) } -/// Whether every point and handle of `sp` lies on the canvas. +/// Whether `sp` lies on the canvas: every point and handle does, or else the curve itself (its +/// extrema, not its handles) does. fn on_canvas(sp: &SubPath) -> bool { - sp.anchors.iter().all(|a| [a.p, a.h_in, a.h_out].iter().all(|q| q.x.abs() <= MAX_COORD && q.y.abs() <= MAX_COORD)) + let within = |v: f64| v.abs() <= MAX_COORD; + sp.anchors.iter().all(|a| [a.p, a.h_in, a.h_out].iter().all(|q| within(q.x) && within(q.y))) + || PathData::new(vec![sp.clone()]).bounds().is_some_and(|b| [b.x0, b.y0, b.x1, b.y1].into_iter().all(within)) } fn point_tol(cx: &ToolContext) -> f64 { diff --git a/crates/tools/src/quickpen/tests.rs b/crates/tools/src/quickpen/tests.rs index b43319860d..bab7a01dc5 100644 --- a/crates/tools/src/quickpen/tests.rs +++ b/crates/tools/src/quickpen/tests.rs @@ -317,3 +317,23 @@ fn other_ends_have_no_piece() { closed.closed = true; assert_eq!(length::end_piece(&closed, false), None); } + +/// A control point may lie past the canvas while the curve stays on it; a curve that reaches past +/// the canvas is refused. +#[test] +fn the_canvas_check_measures_the_curve_not_its_control_points() { + let m = MAX_COORD; + let cubic = |out_x: f64| { + let mut a = Anchor::corner(pt(m - 100.0, 0.0)); + a.h_out = pt(out_x, 0.0); + let mut b = Anchor::corner(pt(m - 100.0, 100.0)); + b.h_in = b.p; + SubPath::new(vec![a, b], false) + }; + assert!(on_canvas(&cubic(m - 100.0))); + assert!(on_canvas(&cubic(m + 100.0))); + assert!(!on_canvas(&cubic(m + 1000.0))); + let mut far = start(pt(m + 1.0, 0.0)); + far.anchors.push(Anchor::corner(pt(0.0, 0.0))); + assert!(!on_canvas(&far)); +}