M2: a page whose /MediaBox has no area is US Letter and shows its content (view.render-pages)

- hayro-syntax patch 44: a /MediaBox without area counts as missing, so
  the page is sized, placed and clipped by the same Letter region and its
  content is drawn (MuPDF and PDFium both draw it on 612 x 792).
- pdfcraft-model: Page::crop falls back to Letter for such a box too.
- vendor/README.md: the raw-crop summary names the Letter fallback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Pq7HyCHD1R3rywQBPCHXZA
This commit is contained in:
mtlcl410
2026-11-07 18:44:18 +08:00
co-authored by Claude Opus 5.5
parent cdb94e6611
commit 2c1b903075
5 changed files with 40 additions and 10 deletions
+23 -2
View File
@@ -18,6 +18,10 @@ pub struct Page {
pub dict: Dict,
}
/// The side, in points, at or below which a `/MediaBox` has no area and the page falls back to
/// US Letter (the renderer's `is_nearly_zero`, 1/4096).
const MIN_BOX_SIDE: f64 = 1.0 / 4096.0;
fn rect(doc: &Document, o: Option<&Object>) -> Option<[f64; 4]> {
let o = doc.resolve(o?);
let v: Vec<f64> = o.as_array()?.iter().filter_map(|x| doc.resolve(x).as_f64()).collect();
@@ -25,9 +29,13 @@ fn rect(doc: &Document, o: Option<&Object>) -> Option<[f64; 4]> {
}
impl Page {
/// The visible region (crop box clipped to the media box), in user space.
/// The visible region (crop box clipped to the media box), in user space. A page with no
/// `/MediaBox`, or one without area (a side of at most 1/4096 pt, as the renderer measures
/// it), is US Letter, as the renderer and inspector size it.
pub fn crop(&self, doc: &Document) -> [f64; 4] {
let media = rect(doc, self.dict.get(b"MediaBox")).unwrap_or([0.0, 0.0, 612.0, 792.0]);
let media = rect(doc, self.dict.get(b"MediaBox"))
.filter(|m| m[2] - m[0] > MIN_BOX_SIDE && m[3] - m[1] > MIN_BOX_SIDE)
.unwrap_or([0.0, 0.0, 612.0, 792.0]);
match rect(doc, self.dict.get(b"CropBox")) {
Some(c) => {
let r = [c[0].max(media[0]), c[1].max(media[1]), c[2].min(media[2]), c[3].min(media[3])];
@@ -264,6 +272,19 @@ mod tests {
assert_eq!(p.display_size(&d), (612.0, 792.0));
}
/// 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() {
let d = doc_from(&[
"<< /Type /Catalog /Pages 2 0 R >>".to_string(),
"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_string(),
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 0 0] >>".to_string(),
]);
let p = &pages(&d)[0];
assert_eq!(p.crop(&d), [0.0, 0.0, 612.0, 792.0]);
assert_eq!(p.display_size(&d), (612.0, 792.0));
}
#[test]
fn inherited_copies_stop_at_a_budget() {
let mut d = doc(0);
+6 -3
View File
@@ -5530,9 +5530,12 @@ trailer << /Root 1 0 R >>
let info = crate::inspect(Arc::new(pdf), None).expect("inspect");
assert_eq!((info.pages[0].width, info.pages[0].height), (612.0, 792.0), "inspected size, {media:?}");
}
// The blue square sits at the bottom left of the Letter page.
let page = render_first_page(page_pdf(""));
assert_eq!(rgb_at(&page.rgba, page.width as usize, 30, 792 - 30), [0, 0, 255]);
// The blue square sits at the bottom left of the Letter page, also when the box has no
// area: the page is placed and clipped by the same Letter region it is sized by.
for media in ["", "/MediaBox [0 0 0 0]"] {
let page = render_first_page(page_pdf(media));
assert_eq!(rgb_at(&page.rgba, page.width as usize, 30, 792 - 30), [0, 0, 255], "{media:?}");
}
}
/// A page whose content expands far beyond its compressed stream stops at the expansion budget,
+1 -1
View File
@@ -735,7 +735,7 @@ status = "partial"
commands = []
tools = ["page_render"]
evidence = ["crates/render/src/raster/tests.rs::renders_a_blue_rectangle", "crates/automation/tests/it/automation.rs::open_inspect_render_and_find", "crates/render/src/raster/tests.rs::user_unit_scales_the_page_and_everything_on_it", "crates/render/src/raster/tests.rs::non_interpolated_minification_keeps_thin_strokes", "crates/render/src/raster/tests.rs::a_shrunk_stencil_mask_keeps_thin_strokes", "crates/render/src/raster/tests.rs::minified_image_edges_snap_to_whole_pixels", "crates/render/src/raster/tests.rs::large_non_interpolated_images_are_smoothed_when_enlarged", "crates/render/src/raster/tests.rs::small_non_interpolated_images_keep_hard_pixels_when_enlarged", "crates/render/src/raster/tests.rs::a_page_without_a_media_box_is_letter"]
notes = "hayro bootstrap renderer; own DisplayList devices in M2.6. Pages with /UserUnit are shown at their size in points, as Acrobat reads the value. An image drawn below its resolution is shrunk once to whole device pixels: by area averaging unless it asks for /Interpolate (then Catmull-Rom), so the text of a 300 dpi scan at 100% keeps its strokes, a 1-bit /ImageMask text layer included (it was nearest-neighbour, which dropped them). Enlarged by more than 1%, an image of page-scan size (512 samples or more on its shorter side) is smoothed bilinearly whatever its /Interpolate, so a zoomed scan has no staircases; smaller images keep their hard pixels. A page with no `/MediaBox` is US Letter in the renderer, the page model and the inspector alike. Not measured against Acrobat"
notes = "hayro bootstrap renderer; own DisplayList devices in M2.6. Pages with /UserUnit are shown at their size in points, as Acrobat reads the value. An image drawn below its resolution is shrunk once to whole device pixels: by area averaging unless it asks for /Interpolate (then Catmull-Rom), so the text of a 300 dpi scan at 100% keeps its strokes, a 1-bit /ImageMask text layer included (it was nearest-neighbour, which dropped them). Enlarged by more than 1%, an image of page-scan size (512 samples or more on its shorter side) is smoothed bilinearly whatever its /Interpolate, so a zoomed scan has no staircases; smaller images keep their hard pixels. A page with no `/MediaBox`, or one without area, is US Letter in the renderer, the page model and the inspector alike. Not measured against Acrobat"
[[feature]]
id = "view.render-shadings-patterns"
+2 -2
View File
File diff suppressed because one or more lines are too long
+8 -2
View File
@@ -405,8 +405,14 @@ impl<'a> Page<'a> {
// PdfCraft patch (44): a page with no `/MediaBox`, its own or inherited, is US Letter,
// not A4. The box is required (ISO 32000-2 §7.7.3.3, Table 31) and has no default;
// other readers use Letter, as do PdfCraft's page model and inspector.
let media_box = dict.get::<Rect>(MEDIA_BOX).or(ctx.media_box).unwrap_or(LETTER);
// other readers use Letter, as do PdfCraft's page model and inspector. A box without
// area (`[0 0 0 0]`) counts as missing, so the page is sized, placed and clipped by
// the same Letter region and its content shows.
let media_box = dict
.get::<Rect>(MEDIA_BOX)
.or(ctx.media_box)
.filter(|b| !(b.width() as f32).is_nearly_zero() && !(b.height() as f32).is_nearly_zero())
.unwrap_or(LETTER);
let crop_box = dict
.get::<Rect>(CROP_BOX)