Anchor Point: a repeated linked handle request records no undo step

path.setHandle with link: true treated a request as a change whenever the
whole anchor differed after the other handle was turned again. Turning it
again from its stored end measures its length anew, which can round its
end differently in the last bit, so the same request sent twice recorded
a second Reshape step and moved the other handle by one unit in the last
place.

The request now also counts as unchanged when the anchor is smooth, the
requested handle is already at (x, y), and the other handle sits exactly
where turning it puts it at some positive length, with the same
arithmetic. Any other handle is turned and records a step, including one
slightly off line, a short one at right angles, one on the same side, and
a retracted one that is pulled out.
This commit is contained in:
dotCooCoo
2026-11-18 16:13:30 -08:00
parent f2ffbe81d0
commit cd21f4c1bc
2 changed files with 135 additions and 1 deletions
+73 -1
View File
@@ -499,7 +499,7 @@ fn link_handle(s: &mut Session, p: &Value, id: NodeId, si: usize, ai: usize, out
sp.anchors.get(ai).is_some_and(|a| {
let mut linked = *a;
linked.link_handle(out, pos, sp.has_segment(ai, !out));
linked == *a
linked == *a || ((if out { a.has_in() } else { a.has_out() }) && linked_already(a, out, pos))
})
});
if unchanged {
@@ -515,6 +515,78 @@ fn link_handle(s: &mut Session, p: &Value, id: NodeId, si: usize, ai: usize, out
ok()
}
/// Whether a smooth anchor `a` already has its `out` (or incoming) handle at `pos` and its other
/// handle exactly where [`Anchor::link_handle`] puts it for that handle at some positive length.
/// Turning the other handle again from its stored end measures its length anew, which can round
/// the end differently in the last bit; a handle that passes this test only gets that rounding,
/// and any other handle is turned. `p + u * len` here is the same arithmetic as in
/// `Anchor::link_handle`, `dir / l` included.
fn linked_already(a: &Anchor, out: bool, pos: Point) -> bool {
let (moved, other) = if out { (a.h_out, a.h_in) } else { (a.h_in, a.h_out) };
let dir = a.p - pos;
let l = dir.x.hypot(dir.y);
if a.kind != AnchorKind::Smooth || moved != pos || !(l > 0.0 && l.is_finite()) {
return false;
}
let u = dir / l;
// The lengths that give each stored coordinate exactly; the smallest length both ranges hold
// is checked against the whole end.
let (mut lo, mut hi) = (0.0_f64, f64::INFINITY);
for (p, o, u) in [(a.p.x, other.x, u.x), (a.p.y, other.y, u.y)] {
if u == 0.0 {
if o != p {
return false;
}
continue;
}
let Some((first, last)) = lengths_onto(p, u, o) else {
return false;
};
(lo, hi) = (lo.max(first), hi.min(last));
}
lo <= hi && lo > 0.0 && lo.is_finite() && a.p + u * lo == other
}
/// The lengths `len` for which `p + u * len` rounds to exactly `o`, as their smallest and largest,
/// for a `u` other than 0. The rounded sum moves the same way as `len`, so a range around the
/// unrounded answer is widened until it brackets `o` and then halved down to each end.
fn lengths_onto(p: f64, u: f64, o: f64) -> Option<(f64, f64)> {
let s = u.signum();
let below = |len: f64| s * (p + u * len) < s * o;
let above = |len: f64| s * (p + u * len) > s * o;
let c = (o - p) / u;
let mut d = (o.abs() + p.abs()).max(f64::MIN_POSITIVE) * f64::EPSILON / u.abs();
let mut bracket = None;
for _ in 0..64 {
if below(c - d) && above(c + d) {
bracket = Some((c - d, c + d));
break;
}
d *= 2.0;
}
let (lo, hi) = bracket?;
let first = halve(lo, hi, |len| !below(len)).1;
let last = halve(lo, hi, above).0;
(first <= last).then_some((first, last))
}
/// Narrows `lo..hi`, where `pred` is false at `lo` and true at `hi` and changes once between
/// them, to two neighboring values with the same property.
fn halve(mut lo: f64, mut hi: f64, pred: impl Fn(f64) -> bool) -> (f64, f64) {
for _ in 0..2200 {
let m = lo + (hi - lo) / 2.0;
if m <= lo || m >= hi {
break;
}
if pred(m) {
hi = m;
} else {
lo = m;
}
}
(lo, hi)
}
/// `path.moveAnchor`: one anchor to (x, y), its handles keeping their offsets (the Anchor Point
/// tool's Shift-drag).
fn move_anchor(s: &mut Session, p: &Value) -> Result<Value> {
@@ -234,3 +234,65 @@ fn a_linked_handle_makes_the_anchor_smooth() {
assert!(s.doc().unwrap().doc == before, "a refused request changed the document");
assert_eq!(undo_steps(&s), undo);
}
/// A linked `path.setHandle` request repeated as it was changes nothing and records no undo step,
/// even where turning the other handle again from its stored end rounds it differently, near the
/// origin and 3e6 pt from it. On a smooth anchor whose other handle is off line by 1e-8, linking
/// the handle where it is still turns the other one into line and records an undo step, at both
/// places. 3.9e6 pt from the origin, a 1.5e-9 pt handle at right angles and a handle 1e-9 pt off
/// line are turned too, and so is an other handle on the same side as the linked one.
#[test]
fn a_repeated_linked_handle_records_no_undo_step() {
let (mut s, id) = long_path(100);
let near = anchor(&s, id, 5).p;
for p in [near, Point::new(3e6, -2e6)] {
let corner =
Anchor { p, h_in: p + Vec2::new(117.73528366134629, 927.4795508230089), h_out: p + Vec2::new(3.0, 0.0), kind: AnchorKind::Corner };
let path = Arc::make_mut(&mut s.doc_mut().unwrap().doc).node_mut(id).unwrap().path_data_mut().unwrap();
path.subpaths[0].anchors[5] = corner;
for i in 0..40 {
let t = f64::from(i) * 0.37;
let to = p + Vec2::new(8.028549152229672 * t.cos() + 0.1 * f64::from(i), -9.388200339328929 * t.sin() - 0.03 * f64::from(i));
let link = json!({"id": id.0, "anchor": 5, "which": "out", "x": to.x, "y": to.y, "link": true});
s.execute("path.setHandle", &link).unwrap();
let (steps, a) = (undo_steps(&s), anchor(&s, id, 5));
s.execute("path.setHandle", &link).unwrap();
assert_eq!(undo_steps(&s), steps, "request {i} at {p:?} repeated recorded an undo step");
assert_eq!(anchor(&s, id, 5), a, "request {i} at {p:?} repeated changed the anchor");
}
let skewed = Anchor { p, h_in: p + Vec2::new(-10.0, 1e-8), h_out: p + Vec2::new(10.0, 0.0), kind: AnchorKind::Smooth };
let path = Arc::make_mut(&mut s.doc_mut().unwrap().doc).node_mut(id).unwrap().path_data_mut().unwrap();
path.subpaths[0].anchors[5] = skewed;
let steps = undo_steps(&s);
s.execute("path.setHandle", &json!({"id": id.0, "anchor": 5, "which": "out", "x": skewed.h_out.x, "y": skewed.h_out.y, "link": true}))
.unwrap();
assert_eq!(undo_steps(&s), steps + 1, "the off-line handle at {p:?} was left as it was");
let a = anchor(&s, id, 5);
assert!(a.h_out == skewed.h_out && a.h_in.y == p.y, "{a:?}");
}
// Far from the origin, a short handle at right angles and a handle 1e-9 pt off line are still
// turned into line.
let p = Point::new(3.9e6, 0.0);
for h_in in [Point::new(p.x, 1.5e-9), Point::new(p.x - 10.0, 1e-9)] {
let off = Anchor { p, h_in, h_out: p + Vec2::new(10.0, 0.0), kind: AnchorKind::Smooth };
let path = Arc::make_mut(&mut s.doc_mut().unwrap().doc).node_mut(id).unwrap().path_data_mut().unwrap();
path.subpaths[0].anchors[5] = off;
let steps = undo_steps(&s);
s.execute("path.setHandle", &json!({"id": id.0, "anchor": 5, "which": "out", "x": off.h_out.x, "y": off.h_out.y, "link": true})).unwrap();
assert_eq!(undo_steps(&s), steps + 1, "the handle at {h_in:?} was left as it was");
let a = anchor(&s, id, 5);
assert!(a.h_in.y == 0.0 && a.h_in.x < p.x, "{a:?}");
}
// An other handle on the same side as the linked one is turned to the far side.
let p = near;
let same = Anchor { p, h_in: p + Vec2::new(5.0, 0.0), h_out: p + Vec2::new(10.0, 0.0), kind: AnchorKind::Smooth };
let path = Arc::make_mut(&mut s.doc_mut().unwrap().doc).node_mut(id).unwrap().path_data_mut().unwrap();
path.subpaths[0].anchors[5] = same;
let steps = undo_steps(&s);
s.execute("path.setHandle", &json!({"id": id.0, "anchor": 5, "which": "out", "x": same.h_out.x, "y": same.h_out.y, "link": true})).unwrap();
assert_eq!(undo_steps(&s), steps + 1, "the handle on the same side was left as it was");
assert!(anchor(&s, id, 5).h_in.distance(p + Vec2::new(-5.0, 0.0)) < 1e-9, "{:?}", anchor(&s, id, 5));
}