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
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user