diff --git a/crates/paint/src/brush_tests.rs b/crates/paint/src/brush_tests.rs index 110f236ee66..86f76eaee8a 100644 --- a/crates/paint/src/brush_tests.rs +++ b/crates/paint/src/brush_tests.rs @@ -185,6 +185,30 @@ fn pencil_dabs_sit_on_the_pixel_grid() { assert_eq!(s.rgba(9, 5)[3], 1.0); } +#[test] +fn pencil_1px_line_is_a_clean_8_connected_staircase() { + // #2139: a 1 px Pencil line is one pixel per column (or row, when steep) on the ideal line + // between the end pixels: no doubled pixels at the steps. + let b = BrushSettings { aliased: true, hardness: 1.0, size: 1.0, ..brush() }; + let cases = [((2.5, 2.5), (22.5, 9.5)), ((3.5, 30.5), (10.5, 5.5)), ((1.2, 1.7), (25.8, 4.1)), ((5.5, 5.5), (17.5, 17.5)), ((20.3, 3.6), (4.9, 12.2))]; + for ((ax, ay), (bx, by)) in cases { + let s = paint(&b, &[StrokePoint::new(ax, ay, 1.0), StrokePoint::new(bx, by, 1.0)], 40, 40); + let painted: Vec<(i32, i32)> = (0..40).flat_map(|y| (0..40).map(move |x| (x, y))).filter(|&(x, y)| s.rgba(x, y)[3] > 0.0).collect(); + let (a, e) = ((ax.floor(), ay.floor()), (bx.floor(), by.floor())); + let steep = (e.1 - a.1).abs() > (e.0 - a.0).abs(); + let major = |p: (f64, f64)| if steep { (p.1, p.0) } else { (p.0, p.1) }; + let ((m0, n0), (m1, n1)) = (major(a), major(e)); + assert_eq!(painted.len() as f64, (m1 - m0).abs() + 1.0, "{a:?}->{e:?}: one pixel per step, got {painted:?}"); + for &(x, y) in &painted { + let (m, n) = major((f64::from(x), f64::from(y))); + let ideal = n0 + (n1 - n0) * (m - m0) / (m1 - m0); + assert!((n - ideal).abs() <= 0.5 + 1e-9, "{a:?}->{e:?}: ({x},{y}) is off the line"); + } + let columns: std::collections::BTreeSet = painted.iter().map(|&(x, y)| major((f64::from(x), f64::from(y))).0 as i64).collect(); + assert_eq!(columns.len(), painted.len(), "{a:?}->{e:?}: a column holds two pixels"); + } +} + // ---------- spacing ---------- #[test] diff --git a/crates/paint/src/dynamics.rs b/crates/paint/src/dynamics.rs index 3318ca39ccd..5e8b2259147 100644 --- a/crates/paint/src/dynamics.rs +++ b/crates/paint/src/dynamics.rs @@ -19,6 +19,9 @@ pub const SMOOTHING_STEP_MS: f64 = 16.0; /// space their dabs further apart (Photoshop: "the speed of the cursor determines the spacing"). pub const SPEED_SPACING_MS: f64 = 8.0; +/// Longest pixel-grid segment (in pixels) before falling back to distance spacing. +const MAX_GRID_STEPS: f64 = 1e7; + /// Densest speed spacing, in pixels between dabs (a slow, long stroke never floods the buffer). const MIN_SPEED_STEP: f64 = 0.5; /// Maximum airbrush catch-up dabs for one input segment. @@ -182,6 +185,9 @@ pub struct PathWalker { /// Speed spacing (Spacing unchecked): one step per this many ms of stroke time. speed: Option, speed_acc: f64, + /// Pixel-grid stepping for small aliased tips: the dab-centre offset inside a pixel (see + /// [`PathWalker::pixel_grid`]). + grid: Option, } impl PathWalker { @@ -197,9 +203,41 @@ impl PathWalker { time_acc: 0.0, speed: None, speed_acc: 0.0, + grid: None, } } + /// Step pixel by pixel instead of by distance: one step per pixel along the segment's major + /// axis, on the line between the pixels holding its ends, each at the pixel's dab centre + /// (`offset` 0.5 for odd tip diameters, 0 for even ones, as `render::grid_center` snaps). A + /// small aliased tip (the Pencil) then draws a clean 8-connected staircase: stepping by + /// distance put two dabs in a row wherever the line crossed a pixel corner (#2139). + pub fn pixel_grid(mut self, offset: f64) -> Self { + self.grid = Some(offset); + self + } + + /// Emits the pixel steps from `a` to `p` (see [`PathWalker::pixel_grid`]). False, emitting + /// nothing, when the span is not finite or implausibly long: distance spacing takes over. + fn grid_steps(&mut self, a: &StrokePoint, p: &StrokePoint, offset: f64, out: &mut impl FnMut(StepInput)) -> bool { + let cell = |v: f64| if offset > 0.0 { v.floor() } else { v.round() }; + let (x0, y0) = (cell(a.x), cell(a.y)); + let (dx, dy) = (cell(p.x) - x0, cell(p.y) - y0); + let n = dx.abs().max(dy.abs()); + if !n.is_finite() || n > MAX_GRID_STEPS { + return false; + } + let steps = n as u64; + for i in 1..=steps { + let i = i as f64; + let mut q = lerp_pt(a, p, i / n); + q.x = x0 + (dx * i / n).round() + offset; + q.y = y0 + (dy * i / n).round() + offset; + self.emit(q, out); + } + true + } + /// Space steps by stroke time instead of distance ([`SPEED_SPACING_MS`]): faster movement /// spreads them out. Segments without timestamps are spaced by distance. pub fn speed_spacing(mut self, interval_ms: f64) -> Self { @@ -237,12 +275,14 @@ impl PathWalker { let dt = p.time - a.time; match self.speed.filter(|_| dt > 0.0 && dt.is_finite()) { None => { - while self.next_at <= len + 1e-9 { - let q = lerp_pt(&a, &p, (self.next_at / len).min(1.0)); - self.emit(q, out); - self.next_at += step_len(&q).max(0.25); + if !self.grid.is_some_and(|g| self.grid_steps(&a, &p, g, out)) { + while self.next_at <= len + 1e-9 { + let q = lerp_pt(&a, &p, (self.next_at / len).min(1.0)); + self.emit(q, out); + self.next_at += step_len(&q).max(0.25); + } + self.next_at -= len; } - self.next_at -= len; } Some(iv) => { self.speed_acc += dt; @@ -632,9 +672,14 @@ impl DabGenerator { let brush = brush.bounded_for_render(); let interval = brush.build_up.then(|| 1000.0 / f64::from(brush.build_up_rate.clamp(0.1, 1000.0))); let dual_on = brush.dual_brush.enabled; + let mut walker = if brush.spacing_enabled { PathWalker::new(interval) } else { PathWalker::new(interval).speed_spacing(SPEED_SPACING_MS) }; + // A small aliased tip whose dabs are at most a pixel apart steps pixel by pixel (#2139). + if brush.aliased && brush.size * brush.spacing.max(0.01) <= 1.0 { + walker = walker.pixel_grid(f64::from(crate::render::grid_center(0.0, 0.0, brush.size).0)); + } Self { smoother: Smoother::new(&brush.smoothing, zoom), - walker: if brush.spacing_enabled { PathWalker::new(interval) } else { PathWalker::new(interval).speed_spacing(SPEED_SPACING_MS) }, + walker, dual_walker: dual_on.then(|| PathWalker::new(None)), builder: DabBuilder::new(&brush), dual: dual_on.then(|| DualBuilder::new(&brush)),