Preserve zero-area PDF sources for importer validation

This commit is contained in:
alehel committed 2026-11-19 06:44:21 +01:00
1 parent 77ea9b701b
commit a1688846ff
2 files changed
+36 -1

No files matched your search

+10 -1
View File
@@ -584,8 +584,17 @@ pub fn page_as_form(dst: &mut Document, src: &Document, page: usize) -> Result<(
// Use the viewer's effective bounds: an oversized CropBox must not reveal artwork
// outside MediaBox or change the watermark's aspect ratio and fit (#7687).
let source_page = pdfcraft_model::Page { obj: pr, dict: d };
let bbox = source_page.crop(src);
let d = &source_page.dict;
// Keep an explicitly empty source empty: callers such as 3D posters reject it.
// The viewer's Letter fallback must not turn that invalid input into a usable picture.
let raw_rect = |key: &[u8]| -> Option<[f64; 4]> {
let obj = src.resolve(d.get(key)?);
let [a, b, c, e] = obj.as_array()?.as_slice() else { return None };
let [Some(x0), Some(y0), Some(x1), Some(y1)] = [a, b, c, e].map(|v| src.resolve(v).as_f64()) else { return None };
[x0, y0, x1, y1].iter().all(|v| v.is_finite()).then(|| [x0.min(x1), y0.min(y1), x0.max(x1), y0.max(y1)])
};
let bbox =
raw_rect(b"CropBox").or_else(|| raw_rect(b"MediaBox")).filter(|b| b[0] == b[2] || b[1] == b[3]).unwrap_or_else(|| source_page.crop(src));
let (w, h) = (bbox[2] - bbox[0], bbox[3] - bbox[1]);
let rotate = d.get(b"Rotate").and_then(|r| src.resolve(r).as_int()).unwrap_or(0).rem_euclid(360);
// Content: the page's streams, decoded and joined. Of a stream damaged partway, the copy draws
+26
View File
@@ -5046,3 +5046,29 @@ fn page_form_clips_crop_to_media_before_rotation() {
}
}
}
/// Importers such as 3D posters must still be able to reject an explicitly empty source.
#[test]
fn page_form_keeps_explicit_zero_area_for_caller_validation() {
for key in [b"MediaBox".as_slice(), b"CropBox".as_slice()] {
for inherited in [false, true] {
let mut src = open(fixture());
let page = pages(&src).unwrap()[0].obj;
src.update_dict(page, |d| d.set(b"Rotate".to_vec(), Object::Int(0))).unwrap();
let target = if inherited { src.get(page).as_dict().unwrap().reference(b"Parent").unwrap() } else { page };
if inherited {
src.update_dict(page, |d| {
d.remove(key);
})
.unwrap();
}
src.update_dict(target, |d| {
d.set(key.to_vec(), Object::Array(vec![20.into(), 30.into(), 100.into(), 30.into()]));
})
.unwrap();
let mut dst = Document::new_empty();
let (_, size) = page_as_form(&mut dst, &src, 0).unwrap();
assert_eq!(size, (80.0, 0.0));
}
}
}