From e39e563408aa64b89be507326dd68aabdb25cb68 Mon Sep 17 00:00:00 2001 From: samhyun Date: Sun, 1 Nov 2026 18:21:00 +0900 Subject: [PATCH 1/3] M8: dots halfway between pixels stay put across pans, bands and patches The board's dot grid puts each mark on the device pixel nearest its page position, measured from the view's top-left. At a zoom of 1.0625 a step of 24 is 25.5 pixels, so every second mark falls halfway between two pixels, and a view a whole number of pixels on could put it on the other one: `round` sends halves away from zero, so a mark left of the view's origin went left and one right of it went right, and the origin `(px + offset) / scale` reached the half with another rounding error. A pan moved those marks by a pixel (up to 123 levels off). Positions are now rounded to 1/65536 px and then half up. Co-Authored-By: Claude Opus 5.5 --- crates/render/src/dots.rs | 11 ++++++++++- crates/render/src/dots_tests.rs | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/crates/render/src/dots.rs b/crates/render/src/dots.rs index b13cbdd57..f3effd1f1 100644 --- a/crates/render/src/dots.rs +++ b/crates/render/src/dots.rs @@ -57,7 +57,7 @@ pub(crate) fn draw(pix: &mut Pixmap, view: &View, d: &Dots) { let strong = |k: i64| k.rem_euclid(major) == 0; // The device-pixel span of the mark on line `k` along an axis `len` pixels long from `o`. let span = |k: i64, o: f64, len: i64| { - let a = ((k as f64 * d.step * s - o).round() as i64).saturating_sub(lead); + let a = (nearest_px(k as f64 * d.step * s - o) as i64).saturating_sub(lead); (a.max(0), a.saturating_add(side).min(len)) }; let row = w as usize * 4; @@ -92,6 +92,15 @@ pub(crate) fn draw(pix: &mut Pixmap, view: &View, d: &Dots) { } } +/// The device pixel nearest `p` pixels from the view's top-left. A mark halfway between two pixels +/// goes to the right (or lower) one on either side of the origin, and `p` is first rounded to +/// 1/65536 px: a view a whole number of pixels off (a pan, a band, a patch) reaches it with another +/// rounding error, and would otherwise put the marks that fall halfway on the other pixel. +fn nearest_px(p: f64) -> f64 { + const STEPS: f64 = 65_536.0; + ((p * STEPS).round() / STEPS + 0.5).floor() +} + /// Which line covers each device pixel along an axis: 0 none, 1 a faded one, 2 a strong one. type Cover = Vec; diff --git a/crates/render/src/dots_tests.rs b/crates/render/src/dots_tests.rs index 53d052062..a5ff2edc9 100644 --- a/crates/render/src/dots_tests.rs +++ b/crates/render/src/dots_tests.rs @@ -134,3 +134,27 @@ fn graph_paper_is_strong_every_major_line() { assert_eq!(at(x, -195), strong, "a strong line at {x}"); } } + +/// At a zoom of 1.0625, a step of 24 is 25.5 pixels: every second dot falls halfway between two +/// pixels. A view a whole number of pixels on (a pan, a band, a patch) has to put it on the same +/// one, on whichever side of that view's origin it is. +#[test] +fn dots_halfway_between_pixels_stay_put() { + let (d, page) = page_with_square(); + for lines in [false, true] { + let opts = Options { dots: Some(Dots { lines, ..dots() }), ..Default::default() }; + let s = 1.0625; + let full = View { origin: (0.0, 0.0), scale: s, width: 400, height: 300 }; + let a = render_view_single(&d, page, &full, &opts).unwrap(); + for dx in 1..100u32 { + let dy = (dx * 37) % 100; + let v = View { origin: (f64::from(dx) / s, f64::from(dy) / s), scale: s, width: 250, height: 180 }; + let b = render_view_single(&d, page, &v, &opts).unwrap(); + for y in 0..180 { + for x in 0..250 { + assert_eq!(rgba(&b, x, y), rgba(&a, x + dx, y + dy), "lines {lines}, a pan by ({dx}, {dy}), at {x}, {y}"); + } + } + } + } +} From c8f34acc59052e6d3482396866bae670a5a93e05 Mon Sep 17 00:00:00 2001 From: samhyun Date: Sun, 1 Nov 2026 18:40:05 +0900 Subject: [PATCH 2/3] M8: dots on the 1/65536 px rounding step stay put too Review of #1796: rounding a mark's position to 1/65536 px with `round` sent the halves of that step away from zero, the asymmetry the PR set out to remove, one step further down. At a step of 25.5 - 2^-17 px, a pan by 26 pixels put the first mark a pixel off a full render. Both roundings now go half up. Co-Authored-By: Claude Opus 5.5 --- crates/render/src/dots.rs | 11 ++++++----- crates/render/src/dots_tests.rs | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/crates/render/src/dots.rs b/crates/render/src/dots.rs index f3effd1f1..5d4ae1fc1 100644 --- a/crates/render/src/dots.rs +++ b/crates/render/src/dots.rs @@ -92,13 +92,14 @@ pub(crate) fn draw(pix: &mut Pixmap, view: &View, d: &Dots) { } } -/// The device pixel nearest `p` pixels from the view's top-left. A mark halfway between two pixels -/// goes to the right (or lower) one on either side of the origin, and `p` is first rounded to -/// 1/65536 px: a view a whole number of pixels off (a pan, a band, a patch) reaches it with another -/// rounding error, and would otherwise put the marks that fall halfway on the other pixel. +/// The first device pixel of a mark whose edge is `p` pixels from the view's top-left: the one +/// whose left (or top) edge is nearest, and the right (or lower) one when two are as near. `p` is +/// first rounded to 1/65536 px, halves up as well: a view a whole number of pixels off (a pan, a +/// band, a patch) reaches it with another rounding error, and, rounded either way, would put the +/// marks that fall halfway on the other pixel. fn nearest_px(p: f64) -> f64 { const STEPS: f64 = 65_536.0; - ((p * STEPS).round() / STEPS + 0.5).floor() + ((p * STEPS + 0.5).floor() / STEPS + 0.5).floor() } /// Which line covers each device pixel along an axis: 0 none, 1 a faded one, 2 a strong one. diff --git a/crates/render/src/dots_tests.rs b/crates/render/src/dots_tests.rs index a5ff2edc9..18d96345f 100644 --- a/crates/render/src/dots_tests.rs +++ b/crates/render/src/dots_tests.rs @@ -158,3 +158,25 @@ fn dots_halfway_between_pixels_stay_put() { } } } + +/// A mark that rounds to a half step of 1/65536 px goes the same way on both sides of a view's +/// origin too: at a step of 25.5 − 2⁻¹⁷ px, the first mark is where 1/65536 px rounds up. +#[test] +fn dots_on_the_rounding_step_stay_put() { + let (d, page) = page_with_square(); + let step = 25.5 - 2f64.powi(-17); + for lines in [false, true] { + let opts = Options { dots: Some(Dots { step, lines, ..dots() }), ..Default::default() }; + let full = View { origin: (0.0, 0.0), scale: 1.0, width: 300, height: 200 }; + let a = render_view_single(&d, page, &full, &opts).unwrap(); + for pan in 1..60u32 { + let v = View { origin: (f64::from(pan), f64::from(pan)), scale: 1.0, width: 200, height: 120 }; + let b = render_view_single(&d, page, &v, &opts).unwrap(); + for y in 0..120 { + for x in 0..200 { + assert_eq!(rgba(&b, x, y), rgba(&a, x + pan, y + pan), "lines {lines}, a pan by {pan}, at {x}, {y}"); + } + } + } + } +} From 8c93d0559fbb70d4b9478e286a506cf9abe6447d Mon Sep 17 00:00:00 2001 From: samhyun Date: Sun, 1 Nov 2026 19:03:53 +0900 Subject: [PATCH 3/3] M8: dots quantize their position apart from the view's origin Review of #1796: quantizing the mark's distance from the view's corner took in the error of the view's origin converted to pixels, so a mark one 1/65536 px step short of a half could still cross it. At a step of 24 and a zoom of (75.5 - 2^-17) / 24, a pan by 51 pixels moved the first mark. The mark's position and the origin are now each quantized before the one is taken from the other. The origin rounds its halves down: some 4 x 10^10 pixels out, its error is exactly half a step, and rounded up it moved the marks again. Past 2^37 pixels the step is finer than f64 and values are used as they are, so huge ones no longer overflow to infinity and come out as a mark at the view's corner. Co-Authored-By: Claude Opus 5.5 --- crates/render/src/dots.rs | 22 +++++++++----- crates/render/src/dots_tests.rs | 54 +++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/crates/render/src/dots.rs b/crates/render/src/dots.rs index 5d4ae1fc1..c33e89a22 100644 --- a/crates/render/src/dots.rs +++ b/crates/render/src/dots.rs @@ -57,7 +57,7 @@ pub(crate) fn draw(pix: &mut Pixmap, view: &View, d: &Dots) { let strong = |k: i64| k.rem_euclid(major) == 0; // The device-pixel span of the mark on line `k` along an axis `len` pixels long from `o`. let span = |k: i64, o: f64, len: i64| { - let a = (nearest_px(k as f64 * d.step * s - o) as i64).saturating_sub(lead); + let a = (nearest_px(k as f64 * d.step * s, o) as i64).saturating_sub(lead); (a.max(0), a.saturating_add(side).min(len)) }; let row = w as usize * 4; @@ -92,14 +92,20 @@ pub(crate) fn draw(pix: &mut Pixmap, view: &View, d: &Dots) { } } -/// The first device pixel of a mark whose edge is `p` pixels from the view's top-left: the one -/// whose left (or top) edge is nearest, and the right (or lower) one when two are as near. `p` is -/// first rounded to 1/65536 px, halves up as well: a view a whole number of pixels off (a pan, a -/// band, a patch) reaches it with another rounding error, and, rounded either way, would put the -/// marks that fall halfway on the other pixel. -fn nearest_px(p: f64) -> f64 { +/// The first device pixel of a mark whose edge is `at` pixels from the page origin, in a view whose +/// top-left is `o` pixels from it: the pixel whose left (or top) edge is nearest, and the right (or +/// lower) one when two are as near. A view a whole number of pixels off (a pan, a band, a patch) +/// gets `o` back from its origin with a rounding error, so `at` and `o` are each rounded to +/// 1/65536 px first: the mark's own position decides the pixel, and the view only counts it out +/// from its corner. The mark's halves go up and the origin's down: far out, where the origin's +/// error is half a step, it still comes back to the whole pixel. Past 2³⁷ px the step is finer +/// than `f64` and values are used as they are. +fn nearest_px(at: f64, o: f64) -> f64 { const STEPS: f64 = 65_536.0; - ((p * STEPS + 0.5).floor() / STEPS + 0.5).floor() + const EXACT: f64 = 137_438_953_472.0; // 2³⁷ + let up = |v: f64| if v.abs() < EXACT { (v * STEPS + 0.5).floor() / STEPS } else { v }; + let down = |v: f64| if v.abs() < EXACT { (v * STEPS - 0.5).ceil() / STEPS } else { v }; + (up(at) - down(o) + 0.5).floor() } /// Which line covers each device pixel along an axis: 0 none, 1 a faded one, 2 a strong one. diff --git a/crates/render/src/dots_tests.rs b/crates/render/src/dots_tests.rs index 18d96345f..75932a2bf 100644 --- a/crates/render/src/dots_tests.rs +++ b/crates/render/src/dots_tests.rs @@ -180,3 +180,57 @@ fn dots_on_the_rounding_step_stay_put() { } } } + +/// The view's origin is quantized apart from the marks: converted to pixels, a whole-pixel origin +/// comes back a hair off, and taken together with a mark one step short of a half it would push the +/// mark across. +#[test] +fn dots_one_rounding_step_short_of_a_half_stay_put() { + let (d, page) = page_with_square(); + let s = (75.5 - 2f64.powi(-17)) / 24.0; + for lines in [false, true] { + let opts = Options { dots: Some(Dots { lines, ..dots() }), ..Default::default() }; + let a = render_view_single(&d, page, &View { origin: (0.0, 0.0), scale: s, width: 300, height: 200 }, &opts).unwrap(); + for pan in 1..100u32 { + let v = View { origin: (f64::from(pan) / s, f64::from(pan) / s), scale: s, width: 200, height: 100 }; + let b = render_view_single(&d, page, &v, &opts).unwrap(); + for y in 0..100 { + for x in 0..200 { + assert_eq!(rgba(&b, x, y), rgba(&a, x + pan, y + pan), "lines {lines}, a pan by {pan}, at {x}, {y}"); + } + } + } + } +} + +/// Far out (some 4 × 10¹⁰ pixels), a whole-pixel origin comes back from page units exactly half a +/// rounding step off: rounded up, it would move the marks that fall halfway between pixels. +#[test] +fn far_out_dots_stay_put() { + let (d, page) = page_with_square(); + let s = 0.3; + let at = 42_536_140_695.0; + let opts = Options { dots: Some(Dots { step: 5.0, ..dots() }), ..Default::default() }; + let a = render_view_single(&d, page, &View { origin: (at / s, 0.0), scale: s, width: 420, height: 40 }, &opts).unwrap(); + for dx in 1..200u32 { + let v = View { origin: ((at + f64::from(dx)) / s, 0.0), scale: s, width: 200, height: 40 }; + let b = render_view_single(&d, page, &v, &opts).unwrap(); + for y in 0..40 { + for x in 0..200 { + assert_eq!(rgba(&b, x, y), rgba(&a, x + dx, y), "a pan by {dx}, at {x}, {y}"); + } + } + } +} + +/// Positions too far out to quantize draw nothing rather than a mark at the corner. +#[test] +fn dots_out_past_any_page_draw_nothing() { + let (d, page) = page_with_square(); + let plain = render_view_single(&d, page, &View { origin: (1e304, 0.0), scale: 1.0, width: 40, height: 40 }, &Options::default()).unwrap(); + for (step, x) in [(1e290, 1e304), (1e299, 1e305), (3e303, 1.5e308)] { + let opts = Options { dots: Some(Dots { step, ..dots() }), ..Default::default() }; + let p = render_view_single(&d, page, &View { origin: (x, 0.0), scale: 1.0, width: 40, height: 40 }, &opts).unwrap(); + assert_eq!(p.data(), plain.data(), "a step of {step} at {x}"); + } +}