fix: keep the editor's image on physical pixels
This commit is contained in:
1 parent
f6723b9740
commit
c833883388
1 file changed
+17
-1
+17
-1
@@ -17,9 +17,15 @@ impl Fit {
|
|||||||
let (iw, ih) = (image.0 as f32, image.1 as f32);
|
let (iw, ih) = (image.0 as f32, image.1 as f32);
|
||||||
let (x, y, w, h) = area;
|
let (x, y, w, h) = area;
|
||||||
let scale = (w / iw).min(h / ih).min(1.0 / pixels_per_point);
|
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 {
|
Fit {
|
||||||
scale,
|
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]
|
#[test]
|
||||||
fn round_trip() {
|
fn round_trip() {
|
||||||
let f = Fit::new((1920, 1080), (10.0, 40.0, 1000.0, 700.0), 1.25);
|
let f = Fit::new((1920, 1080), (10.0, 40.0, 1000.0, 700.0), 1.25);
|
||||||
|
|||||||
Reference in new issue
Block a user