Eraser: a drag's outline takes the tip halfway along each step in two more cases

The outline of an Eraser drag takes, at each step, the tip the pen gives
halfway along it, as path.eraseRegion erases. Two cases still used the
tips at the step's ends:

- A round tip whose Size follows the pen drew each step as wide as the
  mean of the tips at its ends. Where an end is held at the 0.1 pt least
  size, that mean differs from the tip halfway along: with Size 10 pt on
  Pressure by 20 pt, a step from no pressure to full pressure showed
  15.05 pt wide where the release erases 10 pt.
- A pen turned or tilted in place at the last point updated the last
  step only when the tip at that point changed. With Size 10 pt on Tilt
  by 20 pt, tilting from 80 to 85 degrees after an upright point keeps
  the end tip at 0.1 pt while the tip halfway along goes from 12.22 pt to
  11.11 pt, and the outline kept the old one. The area outline now keeps
  and compares the tip halfway along the last step.
This commit is contained in:
dotCooCoo
2026-11-20 15:47:54 -08:00
parent 063f550802
commit f88a6b5877
3 changed files with 86 additions and 24 deletions
+4 -10
View File
@@ -1300,12 +1300,6 @@ impl GestureTool {
self.poses.len() == self.points.len()
}
/// The tip at point `i` of the stroke, as `path.eraseRegion` reads the pen there
/// ([`Self::controls_of_point`]).
fn tip_of_point(&self, i: usize) -> [f64; 3] {
self.tip_of_controls(self.controls_of_point(i))
}
/// The tip halfway along the step from point `j` to point `j + 1`, as `path.eraseRegion` reads
/// the pen there: the pressure and tilt halfway between those at the two points, and the
/// bearing and rotation halfway between them the shorter way round. A tip whose angle follows
@@ -1945,7 +1939,7 @@ impl Tool for GestureTool {
};
let mut area = self.area.take().filter(|a| a.fits(key, &self.points)).unwrap_or_else(|| AreaPreview::new(key, self.points[0]));
let sized = |[angle, roundness, size]: [f64; 3]| [angle, roundness, if size_varies { size } else { self.size }];
area.step(&self.points, &|i| sized(self.tip_of_point(i)), &|j| sized(self.tip_between(j)));
area.step(&self.points, &|j| sized(self.tip_between(j)));
let paths = area.paths(&self.points);
o.extend(paths.settled.into_iter().map(|path| Overlay::SharedPath { path, color, width: 1.0, dash: None }));
o.extend(paths.live.into_iter().map(|path| Overlay::Path { path, color, width: 1.0, dashed: false }));
@@ -1953,9 +1947,9 @@ impl Tool for GestureTool {
self.area.set(Some(area));
} else if size_varies {
// With Size on a mode that follows the pen, each step of the drag shows as wide as the
// tip the pen gives at its ends. A long drag's steps are kept from call to call
// ([`LinePreview::segments`]).
let width_of = |j: usize| ((self.tip_of_point(j)[2] + self.tip_of_point(j + 1)[2]) / 2.0 * cx.zoom) as f32;
// tip the pen gives halfway along it, as `path.eraseRegion` reads it. A long drag's
// steps are kept from call to call ([`LinePreview::segments`]).
let width_of = |j: usize| (self.tip_between(j)[2] * cx.zoom) as f32;
if self.points.len() > line::PIECE_SEGMENTS + 1 {
let mut kept = self.line.take().unwrap_or_default();
for (j, path) in kept.segments(&self.points).into_iter().enumerate() {
+15 -14
View File
@@ -157,8 +157,8 @@ pub(super) struct AreaPreview {
sq: Vec<Point>,
/// The last point taken, on the page.
last: Point,
/// The tip at the last point taken, when the tip varies along the drag.
last_tip: Option<Tip>,
/// The tip halfway along the last segment taken, when the tip varies along the drag.
last_middle: Option<Tip>,
/// The tip's radius along each segment, where its tip is round. It is also as far as the
/// segment's area reaches from the segment in the grid's space.
radius: Vec<f64>,
@@ -196,7 +196,7 @@ impl AreaPreview {
side: widest.max(MIN_BRUSH_SIZE),
sq: vec![],
last: first,
last_tip: None,
last_middle: None,
radius: vec![],
to_round: vec![],
pieces: vec![],
@@ -240,20 +240,22 @@ impl AreaPreview {
}
/// Take the points of the drag `pts` added since the last call and check outlines, within this
/// call's limits. When the tip varies along the drag, `tip(i)` gives the tip at point `i`, and
/// segment `j` takes `middle(j)`, the tip halfway along it. A last point whose tip changed since
/// it was taken (a pen turned or tilted in place) gives its segment the new tip.
pub(super) fn step(&mut self, pts: &[Point], tip: &dyn Fn(usize) -> Tip, middle: &dyn Fn(usize) -> Tip) {
/// call's limits. When the tip varies along the drag, segment `j` takes `middle(j)`, the tip
/// halfway along it. A last segment whose tip halfway along it changed since it was taken (a pen
/// turned or tilted in place at its end) takes the new tip, also where the tip at its end stays
/// the same (a size held at its least).
pub(super) fn step(&mut self, pts: &[Point], middle: &dyn Fn(usize) -> Tip) {
self.work = AreaWork::default();
let mut changed = BTreeSet::new();
if self.key.varies
&& let Some(j) = self.sq.len().checked_sub(2)
&& self.last_tip.is_some_and(|t| t != tip(j + 1))
&& self.last_middle.is_some_and(|t| t != middle(j))
{
let k = self.piece_of(j);
let old = self.radius[j];
self.set_tip(j, middle(j));
self.last_tip = Some(tip(j + 1));
let tip = middle(j);
self.set_tip(j, tip);
self.last_middle = Some(tip);
changed.insert(k);
// The pieces near the old tip or the new one are checked again.
let new = self.radius[j];
@@ -266,13 +268,12 @@ impl AreaPreview {
self.sq.push(self.squash * pts[i]);
self.last = pts[i];
self.work.points += 1;
if self.key.varies {
self.last_tip = Some(tip(i));
}
let Some(j) = i.checked_sub(1) else { continue };
if self.key.varies {
self.radius.push(0.0);
self.set_tip(j, middle(j));
let tip = middle(j);
self.set_tip(j, tip);
self.last_middle = Some(tip);
} else {
self.radius.push(self.key.size / 2.0);
}
+67
View File
@@ -3622,6 +3622,73 @@ fn the_eraser_drag_area_takes_the_tip_halfway_along_each_step() {
t.pointer(&cx, &PointerEvent { pressure: 1.0, ..PointerEvent::new(PointerKind::Up, 150.0, 150.0) });
}
/// A step of a round Eraser drag whose Size follows the pen shows as wide as the tip halfway along
/// it, as the release erases: with Size 10 pt on Pressure by up to 20 pt, a step from no pressure
/// (the tip at its 0.1 pt least) to full pressure (30 pt) shows 10 pt wide, not the 15.05 pt
/// halfway between the tips at its ends.
#[test]
fn a_round_eraser_drag_step_is_as_wide_as_the_tip_halfway_along_it() {
let (d, _) = doc_with_rect();
let s = Selection::default();
let p = paint();
let cx = ToolContext { precise_cursors: true, ..cx(&d, &s, &p) };
let mut t = GestureTool::new("eraser");
for (k, v) in [("size", json!(10)), ("sizeMode", json!("pressure")), ("sizeVariation", json!(20))] {
t.set_option(k, &v);
}
t.pointer(&cx, &PointerEvent { pressure: 0.0, ..PointerEvent::new(PointerKind::Down, 50.0, 150.0) });
t.pointer(&cx, &PointerEvent { pressure: 1.0, ..PointerEvent::new(PointerKind::Drag, 150.0, 150.0) });
let o = t.overlays(&cx);
assert!(matches!(&o[..], [Overlay::Path { width, .. }] if (*width - 10.0).abs() < 1e-4), "{o:?}");
t.pointer(&cx, &PointerEvent { pressure: 1.0, ..PointerEvent::new(PointerKind::Up, 150.0, 150.0) });
}
/// A pen tilted in place at the last point of an Eraser drag gives the last step the new tip halfway
/// along it, even where the tip at that point stays the same: with Size 10 pt on Tilt by up to 20 pt,
/// the tip at a pen leaning 80° and at one leaning 85° is the 0.1 pt least, while halfway along a
/// step from an upright pen it goes from 12.22 pt to 11.11 pt.
#[test]
fn the_eraser_drag_area_takes_the_new_tip_halfway_along_a_step_whose_end_tip_stays() {
let (d, _) = doc_with_rect();
let s = Selection::default();
let p = paint();
let cx = ToolContext { precise_cursors: true, ..cx(&d, &s, &p) };
let bounds = |t: &GestureTool| {
drain_area(t, &cx);
let o = t.overlays(&cx);
o.iter()
.filter_map(|o| match o {
Overlay::Path { path, .. } => Some(vectorcraft_geom::Shape::bounding_box(path)),
Overlay::SharedPath { path, .. } => Some(vectorcraft_geom::Shape::bounding_box(&**path)),
_ => None,
})
.reduce(|a, b| a.union(b))
.unwrap_or_else(|| panic!("{o:?}"))
};
// The tip lying level, 4 times as long as it is wide: a step from x 50 to x 150 with a tip
// `size` long reaches `size` / 2 before and after it and `size` / 8 above and below.
let near = |b: vectorcraft_geom::Rect, size: f64| {
let want = (50.0 - size / 2.0, 150.0 - size / 8.0, 150.0 + size / 2.0, 150.0 + size / 8.0);
assert!([b.x0 - want.0, b.y0 - want.1, b.x1 - want.2, b.y1 - want.3].iter().all(|e| e.abs() < 0.01), "{b:?} vs {want:?}");
};
let mut t = GestureTool::new("eraser");
for (k, v) in [("size", json!(10)), ("roundness", json!(25)), ("sizeMode", json!("tilt")), ("sizeVariation", json!(20))] {
t.set_option(k, &v);
}
let press = |t: &mut GestureTool, lean: f32, kind: PointerKind, x: f64| {
let pen = Some(PenPose { tilt: Some([lean, 0.0]), rotation: None, wheel: None });
t.pointer(&cx, &PointerEvent { pen, pressure: 1.0, ..PointerEvent::new(kind, x, 150.0) });
};
press(&mut t, 0.0, PointerKind::Down, 50.0);
press(&mut t, 80.0, PointerKind::Drag, 150.0);
// Tilt reads 1 upright and 1/9 at 80°, 5/9 halfway: 10 + 20 × (2 × 5/9 − 1) pt.
near(bounds(&t), 10.0 + 20.0 / 9.0);
// At 85° Tilt reads 1/18, 19/36 halfway.
press(&mut t, 85.0, PointerKind::Drag, 150.0);
near(bounds(&t), 10.0 + 20.0 / 18.0);
press(&mut t, 85.0, PointerKind::Up, 150.0);
}
/// The outline of a long Eraser drag whose tip turns and changes shape with the pen is built a
/// bounded amount per frame, and once finished it shows nothing inside the area: no point of it
/// lies well inside the area of any step, each step with the tip halfway along it.