Apply native PDF page dictionaries and viewport projection

This commit is contained in:
gloamxun committed 2026-11-21 03:31:00 +08:00
1 parent a6ba70aa46
commit d1ddf4bfeb
19 files changed
+410 -40

No files matched your search

+27 -8
View File
@@ -1384,13 +1384,16 @@ fn native_pdf(
return Err("invalid native PDF final stream compression level".into());
}
let raw_objects = unicode.and_then(|options| options.objects);
let page_tree = unicode.map_or(&[][..], |options| options.page_tree_attributes);
let font_attributes: BTreeMap<_, _> = fonts.keys().filter_map(|&font| {
let settings = unicode?.fonts.pdf_settings(font)?;
(!settings.attributes.is_empty()).then_some((font, settings.attributes.as_slice()))
}).collect();
let resources = if raw_objects.is_some()
|| !font_attributes.is_empty()
|| pages.iter().any(|page| !page.pdf_resources.entries.is_empty())
|| !page_tree.is_empty()
|| pages.iter().any(|page| !page.pdf_resources.entries.is_empty()
|| !page.pdf_resources.attributes.is_empty())
{
let (catalog, info) = if raw_objects.is_some() {
(
@@ -1401,7 +1404,7 @@ fn native_pdf(
(None, None)
};
Some(crate::pdf_device::page_resources::ResourceOutput::prepare(
pages, raw_objects, font_attributes, catalog, info,
pages, raw_objects, font_attributes, page_tree, catalog, info,
)?)
} else {
None
@@ -2319,10 +2322,15 @@ fn write_pdf(
};
page_ids.push(page_id);
kids.push(format!("{page_id} 0 R"));
let mut page = format!(
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 {} {}] /Resources ",
physical(width), physical(height),
).into_bytes();
let decorated = !list.pdf_resources.attributes.is_empty();
let mut page = b"<< /Type /Page".to_vec();
if !decorated {
page.extend_from_slice(b" /Parent 2 0 R");
}
if !crate::pdf_device::page_attributes::supplies_media_box(&list.pdf_resources.attributes) {
page.extend_from_slice(format!(" /MediaBox [0 0 {} {}]", physical(width), physical(height)).as_bytes());
}
page.extend_from_slice(b" /Resources ");
if let Some(additional) = remapped.as_ref().map(|resources| &resources.pages[page_index])
&& !additional.is_empty()
{
@@ -2332,7 +2340,13 @@ fn write_pdf(
} else {
page.extend_from_slice(resources.as_bytes());
}
page.extend_from_slice(format!("{annots} /Contents {content_id} 0 R >>").as_bytes());
if decorated {
page.extend_from_slice(format!(" /Contents {content_id} 0 R ").as_bytes());
page.extend_from_slice(&remapped.as_ref().expect("validated page dictionary").page_attributes[page_index]);
page.extend_from_slice(format!(" /Parent 2 0 R{annots} >>").as_bytes());
} else {
page.extend_from_slice(format!("{annots} /Contents {content_id} 0 R >>").as_bytes());
}
if let Some(id) = reserved_page {
objects[id - 1] = page;
} else {
@@ -2456,11 +2470,16 @@ fn write_pdf(
}
}
objects[1] = format!(
"<< /Type /Pages /Kids [{}] /Count {} >>",
"<< /Type /Pages /Kids [{}] /Count {}",
kids.join(" "),
pages.len()
)
.into_bytes();
if let Some(resources) = &remapped && !resources.page_tree_attributes.is_empty() {
objects[1].push(b' ');
objects[1].extend_from_slice(&resources.page_tree_attributes);
}
objects[1].extend_from_slice(b" >>");
let mut catalog = "<< /Type /Catalog /Pages 2 0 R".to_owned();
if !metadata.outline.is_empty() {