From 5204abbb08789a94a9482f872ebace839bf96bf8 Mon Sep 17 00:00:00 2001 From: mtlcl410 Date: Sun, 8 Nov 2026 12:37:57 +0800 Subject: [PATCH] M2: a page inherits a grandparent's /MediaBox, origin kept, tested (view.render-pages) Tests only: a /MediaBox two /Pages levels up, with its origin away from 0 0, gives the page its crop, size and view matrix, and the renderer sizes such a page at that box. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Pq7HyCHD1R3rywQBPCHXZA --- crates/model/src/lib.rs | 17 +++++++++++++++++ crates/render/src/raster/tests.rs | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/crates/model/src/lib.rs b/crates/model/src/lib.rs index 5509f95e98..037edb0b30 100644 --- a/crates/model/src/lib.rs +++ b/crates/model/src/lib.rs @@ -286,6 +286,23 @@ mod tests { assert_eq!(p.display_size(&d), (300.0, 400.0)); } + /// The box can come from further up: a grandparent `/Pages` node's `/MediaBox`, through a + /// `/Pages` node without one, with its origin away from 0 0 kept in the crop and view matrix. + #[test] + fn a_page_inherits_a_grandparents_media_box_with_its_origin() { + let d = doc_from(&[ + "<< /Type /Catalog /Pages 2 0 R >>".to_string(), + "<< /Type /Pages /Kids [3 0 R] /Count 1 /MediaBox [100 200 400 600] >>".to_string(), + "<< /Type /Pages /Parent 2 0 R /Kids [4 0 R] /Count 1 >>".to_string(), + "<< /Type /Page /Parent 3 0 R >>".to_string(), + ]); + let p = &pages(&d)[0]; + assert_eq!(p.crop(&d), [100.0, 200.0, 400.0, 600.0]); + assert_eq!(p.display_size(&d), (300.0, 400.0)); + assert_eq!(p.view_matrix(&d), view_matrix_for(0, [100.0, 200.0, 400.0, 600.0])); + assert_ne!(p.view_matrix(&d), view_matrix_for(0, [0.0, 0.0, 300.0, 400.0]), "the origin is kept"); + } + /// A `/MediaBox` without area (`[0 0 0 0]`) is US Letter too, as the renderer draws it. #[test] fn a_page_whose_media_box_has_no_area_is_letter() { diff --git a/crates/render/src/raster/tests.rs b/crates/render/src/raster/tests.rs index c6a56c66f0..f0e0e937f0 100644 --- a/crates/render/src/raster/tests.rs +++ b/crates/render/src/raster/tests.rs @@ -5559,6 +5559,26 @@ trailer << /Root 1 0 R >> assert_eq!((info.pages[0].width, info.pages[0].height), (300.0, 400.0), "inspected size"); } +/// The same through a `/Pages` node without a box: the page takes its grandparent's `/MediaBox`. +#[test] +fn a_page_inherits_its_grandparents_media_box() { + let pdf = b"%PDF-1.4 +1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj +2 0 obj << /Type /Pages /Kids [5 0 R] /Count 1 /MediaBox [0 0 300 400] >> endobj +5 0 obj << /Type /Pages /Parent 2 0 R /Kids [3 0 R] /Count 1 >> endobj +3 0 obj << /Type /Page /Parent 5 0 R /Contents 4 0 R >> endobj +4 0 obj << /Length 25 >> stream +0 0 1 rg 10 10 50 50 re f +endstream endobj +trailer << /Root 1 0 R >> +%%EOF" + .to_vec(); + let page = render_first_page(pdf.clone()); + assert_eq!((page.width, page.height), (300, 400), "rendered size"); + let info = crate::inspect(Arc::new(pdf), None).expect("inspect"); + assert_eq!((info.pages[0].width, info.pages[0].height), (300.0, 400.0), "inspected size"); +} + /// A page whose content expands far beyond its compressed stream stops at the expansion budget, /// which is 64 times the compressed size and at least `MAX_DECODED_STREAM`, and the page says so. #[test]