From c83388338892387f9bd40259ec77c574739a8eaa Mon Sep 17 00:00:00 2001 From: elwrcl Date: Fri, 2 Oct 2026 18:32:58 +0300 Subject: [PATCH] fix: keep the editor's image on physical pixels --- src/editor/view.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/editor/view.rs b/src/editor/view.rs index 23215b1..688e20c 100644 --- a/src/editor/view.rs +++ b/src/editor/view.rs @@ -17,9 +17,15 @@ impl Fit { let (iw, ih) = (image.0 as f32, image.1 as f32); let (x, y, w, h) = area; let scale = (w / iw).min(h / ih).min(1.0 / pixels_per_point); + // Centred, then onto a physical pixel boundary so that at 100 % each + // image pixel is exactly one screen pixel (no half-texel blur). + let snap = |v: f32| (v * pixels_per_point).floor() / pixels_per_point; Fit { scale, - offset: (x + (w - iw * scale) / 2.0, y + (h - ih * scale) / 2.0), + offset: ( + snap(x + (w - iw * scale) / 2.0), + snap(y + (h - ih * scale) / 2.0), + ), } } @@ -73,6 +79,16 @@ mod tests { ); } + #[test] + fn the_image_sits_on_physical_pixels() { + // 1001 - 400 is odd: centring alone lands on half a pixel. + let f = Fit::new((400, 300), (0.0, 0.0, 1001.0, 801.0), 1.0); + assert_eq!(f.offset, (300.0, 250.0)); + let g = Fit::new((400, 300), (0.0, 0.0, 1001.0, 801.0), 1.5); + let px = (g.offset.0 * 1.5, g.offset.1 * 1.5); + assert_eq!(px, (px.0.round(), px.1.round()), "{g:?}"); + } + #[test] fn round_trip() { let f = Fit::new((1920, 1080), (10.0, 40.0, 1000.0, 700.0), 1.25);