Merge pull request #8979 from dotCooCoo/fix/smooth-never-rougher

Smooth: a drag that reaches only a sliver of the next segment no longer splits it
This commit is contained in:
BFlat authored and GitHub committed 2026-11-22 10:52:20 +07:00
commit 980bd43e34
4 files changed
+65 -14

No files matched your search

+38 -12
View File
@@ -645,14 +645,28 @@ fn smooth_subpath(sp: &SubPath, pts: &[Point], radius: f64, share: usize, tol: f
(!same_anchors(&res, &anchors)).then(|| SubPath::new(res, false))
}
/// The segment `c`, which ends in the drag's reach (`near`) and starts outside it, split where it
/// enters the reach: the part outside, then the part in reach. `None` when no more than a sliver
/// is outside.
fn split_at_reach(c: CubicBez, near: &dyn Fn(Point) -> bool) -> Option<(CubicBez, CubicBez)> {
/// How much of a segment that ends in the drag's reach is in reach ([`split_at_reach`]).
enum Reach {
/// All of it, or all but a sliver.
Whole,
/// No more than a sliver at its end.
End,
/// The part outside the reach, then the part in reach.
Split(CubicBez, CubicBez),
}
/// Share of the tool's reach that the part of a segment in reach must be longer than for
/// [`split_at_reach`] to split the segment. VectorCraft's own value.
const REACH_SLIVER: f64 = 1.0 / 64.0;
/// The segment `c`, which ends in the drag's reach (`near`, `radius` points) and starts outside it,
/// split where it enters the reach. No more than 2% of its parameter range outside the reach is a
/// sliver, and so is a part in reach no longer than [`REACH_SLIVER`] of the reach.
fn split_at_reach(c: CubicBez, near: &dyn Fn(Point) -> bool, radius: f64) -> Reach {
const STEPS: u32 = 32;
let step = 1.0 / f64::from(STEPS);
// Back from the end in reach to the first sample outside, then halving to the crossing.
let outside = (1..=STEPS).map(|i| 1.0 - f64::from(i) * step).find(|t| !near(c.eval(*t)))?;
let Some(outside) = (1..=STEPS).map(|i| 1.0 - f64::from(i) * step).find(|t| !near(c.eval(*t))) else { return Reach::Whole };
let (mut lo, mut hi) = (outside, outside + step);
for _ in 0..20 {
let mid = (lo + hi) / 2.0;
@@ -668,7 +682,11 @@ fn split_at_reach(c: CubicBez, near: &dyn Fn(Point) -> bool) -> Option<(CubicBez
let s = c.subsegment(range);
if straight { CubicBez::new(s.p0, s.p0, s.p3, s.p3) } else { s }
};
(hi > 0.02).then(|| (part(0.0..hi), part(hi..1.0)))
if hi <= 0.02 {
return Reach::Whole;
}
let (far, reach) = (part(0.0..hi), part(hi..1.0));
if polygon_len(reach) <= REACH_SLIVER * radius { Reach::End } else { Reach::Split(far, reach) }
}
/// `anchors` with each run of `marked` ones smoothed by a Gaussian of spread `sigma` and refitted
@@ -695,25 +713,33 @@ fn smooth_open(anchors: &[Anchor], marked: &[bool], near: &dyn Fn(Point) -> bool
}
let mut out = anchors.to_vec();
for (a, b) in runs.into_iter().rev() {
let lo = a.saturating_sub(1);
let hi = (b + 1).min(n - 1);
let (mut lo, mut hi) = (a.saturating_sub(1), (b + 1).min(n - 1));
// How much of the segments leading out of the run is in reach. A segment that is in reach
// only at the run's anchor stays out of the piece, so the piece ends at that anchor rather
// than in a sliver of a segment, which would leave the refit no direction to end along.
let after = if hi > b { split_at_reach(CubicBez::new(out[hi].p, out[hi].h_in, out[b].h_out, out[b].p), near, radius) } else { Reach::Whole };
let before = if lo < a { split_at_reach(CubicBez::new(out[lo].p, out[lo].h_out, out[a].h_in, out[a].p), near, radius) } else { Reach::Whole };
if matches!(after, Reach::End) {
hi = b;
}
if matches!(before, Reach::End) {
lo = a;
}
if hi == lo {
continue;
}
let mut piece = out[lo..=hi].to_vec();
// The anchors outside the reach, kept with the part of their segment that is outside.
let (mut head, mut tail) = (None, None);
if hi > b
if let Reach::Split(far, reach) = after
&& let [.., inner, outer] = piece.as_mut_slice()
&& let Some((far, reach)) = split_at_reach(CubicBez::new(outer.p, outer.h_in, inner.h_out, inner.p), near)
{
inner.h_out = reach.p2;
tail = Some(Anchor { h_in: far.p1, ..*outer });
*outer = Anchor::with_handles(far.p3, reach.p1, far.p2);
}
if lo < a
if let Reach::Split(far, reach) = before
&& let [outer, inner, ..] = piece.as_mut_slice()
&& let Some((far, reach)) = split_at_reach(CubicBez::new(outer.p, outer.h_out, inner.h_in, inner.p), near)
{
inner.h_in = reach.p2;
head = Some(Anchor { h_out: far.p1, ..*outer });
+22
View File
@@ -2692,6 +2692,28 @@ fn a_smooth_drag_that_only_meets_corners_adds_no_undo_step() {
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
}
/// Drags of the Smooth tool again and again along the lower half of an ellipse add no anchors after
/// the first, which splits the two segments the drag's reach ends on, and leave no corner. The
/// anchors at those splits lie on the edge of the next drag's reach, where the segment beyond each
/// one is in reach at that anchor only, so the next drag leaves that segment as it is.
#[test]
fn smooth_drags_again_and_again_along_part_of_an_ellipse_add_no_anchors_or_corners() {
let mut s = session();
let r = s.execute("shape.ellipse", &json!({"x": 100, "y": 100, "width": 200, "height": 120})).unwrap();
let id = NodeId(r["id"].as_u64().unwrap());
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
let drag: Vec<Point> =
(0..=32).map(|i| f64::from(i) / 32.0 * std::f64::consts::PI).map(|a| Point::new(200.0 + 100.0 * a.cos(), 160.0 + 60.0 * a.sin())).collect();
let mut first = None;
for pass in 1..=6 {
smooth_pass(&mut s, &drag);
let p = path(&s, id);
let turns = smooth_anchor_turns(&p.subpaths[0]);
assert!(turns.iter().all(|t| *t < 1.0), "pass {pass} leaves a corner: {turns:?}");
assert_eq!(p.anchor_count(), *first.get_or_insert(p.anchor_count()), "pass {pass}");
}
}
#[test]
fn join_tool_joins_two_lines() {
let mut s = session();
+4 -2
View File
@@ -7206,8 +7206,10 @@ round, such as a rectangle, stays as it is. Closed paths stay closed and open pa
reaches the first or last anchor of an open path, the curve there leaves or arrives along the smoothed path within
`radius` of that end rather than along the first or last segment, so a jagged end turns with the rest of the path; it
keeps the segment's direction when the smoothed path there turns back by 90° or more. Where the stretch ends inside a
path, the curve keeps the direction of the part outside the drag. `object.path.smooth` and Simplify keep each end's
direction.
path, the curve keeps the direction of the part outside the drag. A segment leading out of the stretch that is within
`radius` of the drag for no more than 1/64 of `radius` from the stretch's anchor is left as it is, and the stretch ends
at that anchor, so a call that reaches an anchor an earlier call added at the edge of its reach does not split the
segment beyond it again (the 1/64 share is VectorCraft's own value). `object.path.smooth` and Simplify keep each end's direction.
`path.smoothRegion` keeps an anchor as a corner where the path turns by more than 57.5° (an angle inside the corner
under 122.5°) at the anchor and also over `radius` and over twice `radius` on either side of it (on a closed path that the drag reaches all the
+1
View File
@@ -1554,6 +1554,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
When a Smooth tool drag reaches the first or last anchor of an open path, that anchor stays where it is and the curve there leaves or arrives along the smoothed path within the reach, so a jagged first or last segment no longer sets the end's direction; an end where the smoothed path turns back by 90° or more keeps its segment's direction, and where the refitted stretch ends inside the path the curve keeps the direction of the part outside the drag. This rule is VectorCraft's own (`tests/draw2.rs`, `smoothing_an_open_path_turns_its_jagged_ends_with_the_path` and `a_smooth_drag_turns_only_the_end_it_reaches`).
A Smooth tool drag keeps a corner where the path turns by more than 57.5° (an angle inside the corner under 122.5°) and rounds a wider one. An anchor is kept as a corner when the path turns by that much at the anchor and also over the drag's reach and over twice the reach on either side of it, so a jag or a bump smaller than the reach is still smoothed; [the help page](https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/modify-paths/refine-paths.html) describes the tool as reducing irregularities in a path. The stretches on either side of a kept corner are smoothed and refitted on their own, and the corner keeps its place and the directions the path arrives and leaves along, on every drag, on open and closed paths, the anchor a closed path starts at included. A drag that changes no anchor adds no undo step. The threshold and the two distances are VectorCraft's own values, the same on every drag and at any Fidelity (`tests/draw2.rs`, `the_smooth_tool_keeps_corners_sharper_than_its_threshold`, `smoothing_a_closed_path_keeps_its_corners` and `a_smooth_drag_that_only_meets_corners_adds_no_undo_step`).
A Smooth drag along a curved segment smooths the stretch of it in the drag's reach even when neither of the segment's anchors is in reach, as [the help page](https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/modify-paths/refine-paths.html) describes dragging along the path segment; the segment is cut where the drag enters and leaves the reach, and the parts outside keep their shape (`path.smoothRegion`). Paths of two anchors are smoothed too, and Alt with the Paintbrush smooths a selected two-anchor curve; a two-anchor straight line still gets the closed stroke. How the reach is found on a segment (samples no more than half the reach apart) is VectorCraft's own, and a straight segment is left as it is.
A segment leading out of the stretch a Smooth drag smooths that is in the drag's reach for no more than 1/64 of the reach from the stretch's anchor is left as it is, and the stretch ends at that anchor, so a drag that reaches an anchor an earlier drag added at the edge of its reach does not split the segment beyond it again or leave a corner there (`path.smoothRegion`). The 1/64 share is VectorCraft's own value (`tests/draw2.rs`, `smooth_drags_again_and_again_along_part_of_an_ellipse_add_no_anchors_or_corners`).
Before a press that draws a new path, the Pencil cursor shows a small star, as [Draw freeform paths with the Pencil tool](https://web.archive.org/web/20260926095638/https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/draw-shapes/draw-freeform-paths-with-the-pencil-tool.html) describes; over a selected path within Edit selected paths' distance, which the press continues from an end or redraws, the star is not shown ([Reshape paths with the Pencil tool](https://web.archive.org/web/20260106004407/https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/draw-shapes/reshape-paths-with-the-pencil-tool.html)).
Keep selected in the Pencil and Paintbrush Tool Options is on by default: while it is on, a stroke selects the path it draws or continues and replaces the earlier selection. While it is off, a new stroke leaves nothing selected, the earlier selection included, as the reference application starts the stroke by dropping it (#5752); a continued or redrawn path is deselected while the rest of the selection stays (`path.freehand` `keepSelected`).
With two open paths selected, a Pencil stroke from an end of one that ends within Edit selected paths' distance of an end of the other connects them into one path with the first path's appearance, in one undo step, as [Connect two paths with the Pencil tool](https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/draw-shapes/connect-two-paths-with-the-pencil-tool.html) describes (`path.freehand` `connect`).