Write native XDV PDF versions and compact object streams
This commit is contained in:
1 parent
48d42052b8
commit
b37856eeb0
7 files changed
+102
-20
No files matched your search
@@ -101,10 +101,12 @@ impl Executor {
|
||||
if self.state.output_mode_set != Some(mode) {
|
||||
return self.fail("native pdfoutput cannot change after output has started");
|
||||
}
|
||||
let major = self.read_parameter(Parameter::Pdfmajorversion).width();
|
||||
let minor = self.read_parameter(Parameter::Pdfminorversion).width();
|
||||
if options.major != major || i32::from(options.minor) != minor {
|
||||
return self.fail("native PDF version cannot change after output has started");
|
||||
if self.state.input_profile() != InputProfile::XeTeX {
|
||||
let major = self.read_parameter(Parameter::Pdfmajorversion).width();
|
||||
let minor = self.read_parameter(Parameter::Pdfminorversion).width();
|
||||
if options.major != major || i32::from(options.minor) != minor {
|
||||
return self.fail("native PDF version cannot change after output has started");
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -243,6 +245,15 @@ impl Executor {
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<PdfHeaderOptions, Error> {
|
||||
self.reads.insert(Dependency::PdfFileState);
|
||||
if self.state.input_profile() == InputProfile::XeTeX {
|
||||
// The first XDV page can override these driver defaults through its specials.
|
||||
return Ok(*self.state.pdf_header_options.get_or_insert(PdfHeaderOptions {
|
||||
major: 1,
|
||||
minor: 5,
|
||||
object_compression: 3,
|
||||
draft_mode: false,
|
||||
}));
|
||||
}
|
||||
if self.state.input_profile() == InputProfile::TeX8Bit {
|
||||
self.pdf_copy_imported_fonts();
|
||||
}
|
||||
@@ -349,7 +360,7 @@ impl Executor {
|
||||
let omit_procset = if driver == PdfDriver::PdfTeX {
|
||||
self.read_parameter(Parameter::Pdfomitprocset).width()
|
||||
} else {
|
||||
self.state.xdv_setup.apply_version(&mut file);
|
||||
self.state.xdv_setup.apply_file_options(&mut file);
|
||||
0
|
||||
};
|
||||
Ok(Some(PdfPageData {
|
||||
|
||||
@@ -973,7 +973,7 @@ impl Executor {
|
||||
if shipout.pdf_background.as_ref().is_some_and(|program| !program.xdv_resources.is_empty()) {
|
||||
self.reads.insert(Dependency::PdfCatalogState);
|
||||
}
|
||||
self.state.xdv_setup.apply_version(&mut pdf.file);
|
||||
self.state.xdv_setup.apply_file_options(&mut pdf.file);
|
||||
}
|
||||
self.current = current;
|
||||
Ok((shipout, pdf_forms, probed_page))
|
||||
|
||||
@@ -3826,7 +3826,9 @@ fn write_pdf(
|
||||
} else {
|
||||
None
|
||||
};
|
||||
return objects::serialize(&objects, info, &device.file, level.clamp(0, 9) as u8, packing);
|
||||
return objects::serialize(
|
||||
&objects, info, &device.file, device.driver, level.clamp(0, 9) as u8, packing,
|
||||
);
|
||||
}
|
||||
|
||||
let info_ref = match metadata_object(metadata) {
|
||||
|
||||
@@ -11,6 +11,8 @@ pub(crate) struct Setup {
|
||||
major: Option<i32>,
|
||||
minor: Option<u8>,
|
||||
pub(crate) keep_destinations: bool,
|
||||
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||
no_object_streams: bool,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(crate) outline_open_depth: Option<usize>,
|
||||
#[serde(default, skip_serializing_if = "super::xdv_paper::Paper::is_default")]
|
||||
@@ -76,23 +78,28 @@ impl Setup {
|
||||
};
|
||||
let flags =
|
||||
u32::from_str_radix(digits, radix).map_err(|_| "invalid XDV configuration flags")?;
|
||||
if flags & !0x10 != 0 {
|
||||
const KEEP_DESTINATIONS: u32 = 0x10;
|
||||
const NO_OBJECT_STREAMS: u32 = 0x40;
|
||||
if flags & !(KEEP_DESTINATIONS | NO_OBJECT_STREAMS) != 0 {
|
||||
return Err("unsupported XDV configuration flags".into());
|
||||
}
|
||||
if (sign < 0 && flags != 0) || flags & 0x10 != 0 {
|
||||
self.keep_destinations = flags & 0x10 != 0;
|
||||
if sign < 0 && flags != 0 {
|
||||
self.keep_destinations = false;
|
||||
self.no_object_streams = false;
|
||||
}
|
||||
self.keep_destinations |= flags & KEEP_DESTINATIONS != 0;
|
||||
self.no_object_streams |= flags & NO_OBJECT_STREAMS != 0;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn apply_version(&self, options: &mut PdfFileOptions) {
|
||||
pub(crate) fn apply_file_options(&self, options: &mut PdfFileOptions) {
|
||||
if let Some(value) = self.major {
|
||||
options.major = value;
|
||||
}
|
||||
if let Some(value) = self.minor {
|
||||
options.minor = value;
|
||||
}
|
||||
if options.major == 1 && options.minor < 5 {
|
||||
if self.no_object_streams || (options.major == 1 && options.minor < 5) {
|
||||
options.object_compression = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
//! PDF streams, object streams and cross-reference serialization.
|
||||
use crate::pdf_device::{PdfFileOptions, page_resources::OBJECT_STREAM_CAPACITY};
|
||||
use crate::pdf_device::{PdfDriver, PdfFileOptions, page_resources::OBJECT_STREAM_CAPACITY};
|
||||
use std::io::Write;
|
||||
|
||||
const XDV_OBJECT_STREAM_CAPACITY: usize = 200;
|
||||
|
||||
pub(crate) fn stream(extra: &str, data: &[u8], level: u8) -> Vec<u8> {
|
||||
stream_bytes(extra.as_bytes(), data, level)
|
||||
}
|
||||
@@ -40,10 +42,20 @@ pub(super) fn serialize(
|
||||
objects: &[Vec<u8>],
|
||||
info: Option<usize>,
|
||||
options: &PdfFileOptions,
|
||||
driver: PdfDriver,
|
||||
compression: u8,
|
||||
packing: Option<ObjectPacking>,
|
||||
) -> Result<Vec<u8>, String> {
|
||||
let xdv = driver == PdfDriver::Xdv;
|
||||
let stream_capacity = if xdv {
|
||||
XDV_OBJECT_STREAM_CAPACITY
|
||||
} else {
|
||||
OBJECT_STREAM_CAPACITY
|
||||
};
|
||||
let mut pdf = format!("%PDF-{}.{}\n", options.major, options.minor).into_bytes();
|
||||
if xdv {
|
||||
pdf.extend_from_slice(b"%\xe4\xf0\xed\xf8\n");
|
||||
}
|
||||
let mut packed = vec![None; objects.len() + 1];
|
||||
let mut stream_slots = vec![false; objects.len() + 1];
|
||||
let mut next_stream = objects.len() + 1;
|
||||
@@ -52,7 +64,7 @@ pub(super) fn serialize(
|
||||
for (id, members) in &groups {
|
||||
if *id == 0 || *id > objects.len() || !objects[*id - 1].is_empty()
|
||||
|| std::mem::replace(&mut stream_slots[*id], true)
|
||||
|| members.is_empty() || members.len() > OBJECT_STREAM_CAPACITY
|
||||
|| members.is_empty() || members.len() > stream_capacity
|
||||
|| options.object_compression == 0
|
||||
{
|
||||
return Err("invalid native PDF object stream slot".into());
|
||||
@@ -94,7 +106,7 @@ pub(super) fn serialize(
|
||||
if options.object_compression < level {
|
||||
continue;
|
||||
}
|
||||
if groups.last().is_none_or(|(_, members)| members.len() == OBJECT_STREAM_CAPACITY) {
|
||||
if groups.last().is_none_or(|(_, members)| members.len() == stream_capacity) {
|
||||
groups.push((next_stream, Vec::new()));
|
||||
next_stream += 1;
|
||||
}
|
||||
@@ -165,7 +177,16 @@ pub(super) fn serialize(
|
||||
} else {
|
||||
let id = offsets.len();
|
||||
offsets.push(xref as u64);
|
||||
let mut entries = Vec::new();
|
||||
let offset_width = if xdv {
|
||||
let last_stream = next_stream - 1;
|
||||
let largest_field = (xref as u64).max(last_stream as u64);
|
||||
let bits = u64::BITS - largest_field.leading_zeros();
|
||||
bits.div_ceil(8).max(1) as usize
|
||||
} else {
|
||||
8
|
||||
};
|
||||
let generation_width = if xdv { 2 } else { 4 };
|
||||
let mut entries = Vec::with_capacity(offsets.len() * (1 + offset_width + generation_width));
|
||||
for (obj, offset) in offsets.iter().enumerate() {
|
||||
let (kind, a, b) = if let Some(Some(next)) = free.get(obj) {
|
||||
(0u8, *next as u64, if obj == 0 { 65535 } else { 0 })
|
||||
@@ -175,13 +196,17 @@ pub(super) fn serialize(
|
||||
(1, *offset, 0)
|
||||
};
|
||||
entries.push(kind);
|
||||
entries.extend_from_slice(&a.to_be_bytes());
|
||||
entries.extend_from_slice(&b.to_be_bytes());
|
||||
entries.extend_from_slice(&a.to_be_bytes()[8 - offset_width..]);
|
||||
entries.extend_from_slice(&b.to_be_bytes()[4 - generation_width..]);
|
||||
}
|
||||
let index = if xdv {
|
||||
String::new() // A single contiguous section uses the default Index.
|
||||
} else {
|
||||
format!(" /Index [0 {}]", offsets.len())
|
||||
};
|
||||
let object = stream(
|
||||
&format!(
|
||||
"/Type /XRef /Size {} /W [1 8 4] /Index [0 {}] /Root 1 0 R{info_ref}",
|
||||
offsets.len(),
|
||||
"/Type /XRef /Size {} /W [1 {offset_width} {generation_width}]{index} /Root 1 0 R{info_ref}",
|
||||
offsets.len()
|
||||
),
|
||||
&entries,
|
||||
|
||||
@@ -682,6 +682,10 @@ fn xdv_destinations_use_shipped_page_ids_and_pdf_string_keys() {
|
||||
shipout_lists_to_native_pdf_document(&pages, result.state.pdf_final_options()).unwrap();
|
||||
let pdf = Document::load_mem(&output.bytes).unwrap();
|
||||
assert_eq!(pdf.version, "1.7");
|
||||
let catalog_number = pdf.trailer.get(b"Root").unwrap().as_reference().unwrap().0;
|
||||
let info_number = pdf.trailer.get(b"Info").unwrap().as_reference().unwrap().0;
|
||||
assert!(pdf.reference_table.get(catalog_number).unwrap().is_compressed());
|
||||
assert!(pdf.reference_table.get(info_number).unwrap().is_compressed());
|
||||
let names = pdf.catalog().unwrap().get(b"Names").unwrap().as_dict().unwrap();
|
||||
let id = names.get(b"Dests").unwrap().as_reference().unwrap();
|
||||
let destinations = pdf.get_object(id).unwrap().as_dict().unwrap()
|
||||
|
||||
Reference in new issue
Block a user