From bbba4b2585f2ff90ef16e7644ddb8d61dfa88d06 Mon Sep 17 00:00:00 2001 From: Muhammad Saad Date: Fri, 23 Oct 2026 03:09:51 +0400 Subject: [PATCH] M10: let a small image fill its fixed page (#3273 review) The fit scale was capped with MAX_SIDE, which bounds a page side, not a ratio. Past that scale the image came out smaller than the sheet: a 4 x 2 image at 1200 dpi covered 3456 pt of a 14400 pt page instead of all of it, and a 30 x 30 JFIF at 65535 dpi covered 475 pt of Letter. The fit scale needs no cap of its own. It is min(pw / w, ph / h), so the drawn size is at most the page's by construction, and w and h are above zero because a decoded image has at least one pixel and every decoder here falls back to 72 dpi rather than pass on a non-positive resolution. The guards against a zero divisor went with the cap, with a comment saying why the division is defined. Found by the review bot. The test was written before the fix and observed failing at 3456 x 1728. --- crates/create/src/lib.rs | 13 ++++++++++--- crates/create/src/tests.rs | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/crates/create/src/lib.rs b/crates/create/src/lib.rs index ed978de807..76bd79a362 100644 --- a/crates/create/src/lib.rs +++ b/crates/create/src/lib.rs @@ -698,9 +698,16 @@ impl ImagePage { let Self::Fixed { width, height, turn } = self else { return ((w, h), (w, h, 0.0, 0.0)) }; // A square image keeps the page as given; only a clear difference turns the sheet. let (pw, ph) = if turn && (w > h) != (width > height) && w != h { (height, width) } else { (width, height) }; - // `w` and `h` come from a decoded image, so both are above zero; `max` keeps the - // division defined even if a future decoder ever reports an empty one. - let k = (pw / w.max(f64::MIN_POSITIVE)).min(ph / h.max(f64::MIN_POSITIVE)).min(MAX_SIDE); + // A scale is a ratio, not a length, so `MAX_SIDE` has no business capping it: it bounds + // a page side, which `ImagePage::check` has already applied. Capping the scale with it + // only shrank a small image on a large sheet (a 4 x 2 image at 1200 dpi covered 3456 pt + // of a 14400 pt page instead of all of it). The fit scale needs no cap of its own: it is + // `min(pw / w, ph / h)`, so the drawn size is the page's by construction. + // + // `w` and `h` are above zero, so the division is defined: a decoded image has at least + // one pixel, and every decoder here falls back to 72 dpi rather than pass on a + // non-positive resolution (`xppu > 0 && yppu > 0` for PNG, `> 1.0` for JPEG). + let k = (pw / w).min(ph / h); let (dw, dh) = (w * k, h * k); ((pw, ph), (dw, dh, (pw - dw) / 2.0, (ph - dh) / 2.0)) } diff --git a/crates/create/src/tests.rs b/crates/create/src/tests.rs index 260713763d..f380340fee 100644 --- a/crates/create/src/tests.rs +++ b/crates/create/src/tests.rs @@ -2296,3 +2296,19 @@ fn an_unusable_page_size_is_refused_and_nothing_is_written() { // The largest page PDF allows is fine. assert!(from_images_with(&images, ImageResolution::Embedded, ImagePage::fixed((14_400.0, 14_400.0), true)).is_ok()); } + +/// A fixed page is filled by the image whatever its resolution: a tiny image at a high dpi is +/// enlarged to the sheet, not left small on it. `MAX_SIDE` bounds a page side, which +/// `ImagePage::check` has already applied, so it must not also cap the fit scale. +#[test] +fn a_tiny_high_dpi_image_still_fills_its_fixed_page() { + let images = [("photo.png".into(), png_bytes(true))]; + // 4 x 2 px at 1200 dpi is 0.24 x 0.12 pt: the fit scale is 60000, far past MAX_SIDE. + let page = ImagePage::fixed((MAX_SIDE, MAX_SIDE), false); + let doc = reopen(&from_images_with(&images, ImageResolution::Dpi(1200.0), page).unwrap()); + let p = pages(&doc); + let c = doc.resolve(p[0].get(b"Contents").unwrap()); + let Object::Stream(s) = &*c else { panic!("expected a content stream") }; + let text = String::from_utf8_lossy(&s.decoded().unwrap()).into_owned(); + assert!(text.contains("14400.000 0 0 7200.000 0.000 3600.000 cm"), "the image should span the full 14400 pt page: {text}"); +}