Honor pdfTeX page box selection and inherited PDF bounds

This commit is contained in:
gloamxun committed 2026-11-26 17:56:57 +08:00
1 parent 58012d9397
commit e10e0cce53
9 files changed
+227 -66

No files matched your search

+29 -53
View File
@@ -4,6 +4,7 @@ mod destinations;
mod jpeg;
mod icc;
mod imported_fonts;
mod page_boxes;
pub(crate) use imported_fonts::ImportedType1Use;
mod png;
use crate::{
@@ -23,24 +24,19 @@ pub(crate) enum PageSelection {
}
#[derive(Clone, Copy, Debug, Default)]
#[repr(i32)]
pub(crate) enum PageBox {
Media,
Media = 1,
#[default]
Crop,
Bleed,
Trim,
Art,
Crop = 2,
Bleed = 3,
Trim = 4,
Art = 5,
}
impl PageBox {
fn keys(self) -> &'static [&'static [u8]] {
match self {
Self::Media => &[b"MediaBox"],
Self::Crop => &[b"CropBox", b"MediaBox"],
Self::Bleed => &[b"BleedBox", b"CropBox", b"MediaBox"],
Self::Trim => &[b"TrimBox", b"CropBox", b"MediaBox"],
Self::Art => &[b"ArtBox", b"CropBox", b"MediaBox"],
}
}
enum PageBoxSelection {
PdfTeX(i32),
Xdv(PageBox),
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
@@ -561,10 +557,10 @@ impl PdfImage {
resource: LoadedResource,
doc: Document,
selection: PageSelection,
kind: PageBox,
page_box: i32,
remap_type1: bool,
) -> Result<Self, String> {
Self::read_page(resource, doc, kind.keys(), false, remap_type1, |doc, pages| {
Self::read_page(resource, doc, PageBoxSelection::PdfTeX(page_box), remap_type1, |doc, pages| {
match selection {
PageSelection::Number(page) => {
u32::try_from(page).map_err(|_| "required page does not exist".into())
@@ -580,16 +576,8 @@ impl PdfImage {
page: i32,
kind: PageBox,
) -> Result<(Self, [f64; 2]), String> {
let keys = [
kind.keys()[0],
b"CropBox",
b"MediaBox",
b"BleedBox",
b"TrimBox",
b"ArtBox",
];
let doc = Self::open_pdf(&resource)?;
Self::read_page(resource, doc, &keys, true, false, |_, pages| {
Self::read_page(resource, doc, PageBoxSelection::Xdv(kind), false, |_, pages| {
let pages = i64::try_from(pages.len()).map_err(|_| "required page does not exist")?;
let page = match page {
0 => 1,
@@ -622,8 +610,7 @@ impl PdfImage {
fn read_page(
resource: LoadedResource,
doc: Document,
box_keys: &[&[u8]],
keep_page_origin: bool,
box_selection: PageBoxSelection,
remap_type1: bool,
select_page: impl FnOnce(&Document, &BTreeMap<u32, ObjectId>) -> Result<u32, String>,
) -> Result<(Self, [f64; 2]), String> {
@@ -635,32 +622,21 @@ impl PdfImage {
.ok_or("required page does not exist")?;
let chain = page_chain(&doc, page_id)?;
let inherited = |name: &[u8]| chain.iter().find_map(|dict| dict.get(name).ok());
let bounds = box_keys
.iter()
.find_map(|key| inherited(key))
.ok_or("PDF image has no page bounding box")?;
let bounds = resolved(&doc, bounds)?
.as_array()
.map_err(|error| error.to_string())?;
let [a, b, c, d] = bounds.as_slice() else {
return Err("PDF image bounding box must contain four coordinates".into());
let ([left, bottom, right, top], rotation, keep_page_origin) = match box_selection {
PageBoxSelection::PdfTeX(spec) => (
page_boxes::pdftex(&doc, &chain, spec)?,
page_boxes::pdftex_rotation(&doc, &chain),
false,
),
PageBoxSelection::Xdv(kind) => (
page_boxes::xdv(&doc, &chain, kind)?,
inherited(b"Rotate")
.map(|rotate| resolved(&doc, rotate)?.as_i64().map_err(|error| error.to_string()))
.transpose()?.unwrap_or(0),
true,
),
};
let (a, b, c, d) = (
number(&doc, a)?,
number(&doc, b)?,
number(&doc, c)?,
number(&doc, d)?,
);
let [left, bottom, right, top] = [a.min(c), b.min(d), a.max(c), b.max(d)];
let rotation = inherited(b"Rotate")
.map(|rotate| {
resolved(&doc, rotate)?
.as_i64()
.map_err(|error| error.to_string())
})
.transpose()?
.unwrap_or(0)
.rem_euclid(360);
let rotation = rotation.rem_euclid(360);
let (mut matrix, width, height) = match rotation {
0 => (
[1.0, 0.0, 0.0, 1.0, -left, -bottom],