Allocate compressed PDF object streams during native output
This commit is contained in:
1 parent
d87464fde3
commit
70ebacb056
9 files changed
+315
-46
No files matched your search
@@ -1,5 +1,5 @@
|
||||
//! PDF streams, object streams and cross-reference serialization.
|
||||
use crate::pdf_device::PdfFileOptions;
|
||||
use crate::pdf_device::{PdfFileOptions, page_resources::OBJECT_STREAM_CAPACITY};
|
||||
use std::io::Write;
|
||||
|
||||
pub(crate) fn stream(extra: &str, data: &[u8], level: u8) -> Vec<u8> {
|
||||
@@ -29,44 +29,87 @@ pub(crate) fn stream_bytes(extra: &[u8], data: &[u8], level: u8) -> Vec<u8> {
|
||||
result
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct ObjectPacking {
|
||||
pub streams: Vec<(usize, Vec<usize>)>,
|
||||
pub uncompressed: Option<usize>,
|
||||
}
|
||||
|
||||
pub(super) fn serialize(
|
||||
objects: &[Vec<u8>],
|
||||
info: Option<usize>,
|
||||
options: &PdfFileOptions,
|
||||
compression: u8,
|
||||
) -> Vec<u8> {
|
||||
packing: Option<ObjectPacking>,
|
||||
) -> Result<Vec<u8>, String> {
|
||||
let mut pdf = format!("%PDF-{}.{}\n", options.major, options.minor).into_bytes();
|
||||
let mut offsets = vec![0u64; objects.len() + 1];
|
||||
let mut packed = vec![None; objects.len() + 1];
|
||||
let mut stream_slots = vec![false; objects.len() + 1];
|
||||
let mut next_stream = objects.len() + 1;
|
||||
let execution_order = packing.is_some();
|
||||
let ObjectPacking { streams: mut groups, uncompressed } = packing.unwrap_or_default();
|
||||
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
|
||||
|| options.object_compression == 0
|
||||
{
|
||||
return Err("invalid native PDF object stream slot".into());
|
||||
}
|
||||
for (index, &member) in members.iter().enumerate() {
|
||||
if member == 0 || member > objects.len() || objects[member - 1].is_empty()
|
||||
|| is_pdf_stream(&objects[member - 1])
|
||||
|| packed[member].replace((*id, index)).is_some()
|
||||
{
|
||||
return Err("invalid native PDF object stream member".into());
|
||||
}
|
||||
}
|
||||
}
|
||||
if options.object_compression > 0 {
|
||||
// The page tree precedes the catalog. Info, when eligible, is last.
|
||||
let document_tail = [Some(2), Some(1), info];
|
||||
let order = (1..=objects.len())
|
||||
.filter(|id| !execution_order || !document_tail.contains(&Some(*id)))
|
||||
.chain(document_tail.into_iter().flatten().filter(|_| execution_order));
|
||||
for id in order {
|
||||
let body = &objects[id - 1];
|
||||
if body.is_empty() || packed[id].is_some() || Some(id) == uncompressed
|
||||
|| is_pdf_stream(body)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let level = if Some(id) == info {
|
||||
3
|
||||
} else if body.windows(19).any(|s| s == b"/Subtype /TrueType ")
|
||||
|| body.windows(13).any(|s| s == b"/CIDFontType2")
|
||||
{
|
||||
2
|
||||
} else {
|
||||
1
|
||||
};
|
||||
if options.object_compression < level {
|
||||
continue;
|
||||
}
|
||||
if groups.last().is_none_or(|(_, members)| members.len() == OBJECT_STREAM_CAPACITY) {
|
||||
groups.push((next_stream, Vec::new()));
|
||||
next_stream += 1;
|
||||
}
|
||||
let (stream, members) = groups.last_mut().unwrap();
|
||||
packed[id] = Some((*stream, members.len()));
|
||||
members.push(id);
|
||||
}
|
||||
}
|
||||
let mut offsets = vec![0u64; next_stream];
|
||||
let mut free = vec![None; objects.len() + 1];
|
||||
let mut last_free = 0;
|
||||
let mut groups: Vec<Vec<usize>> = Vec::new();
|
||||
for (i, body) in objects.iter().enumerate() {
|
||||
let id = i + 1;
|
||||
if stream_slots[id] || packed[id].is_some() {
|
||||
continue;
|
||||
}
|
||||
if body.is_empty() {
|
||||
free[last_free] = Some(id);
|
||||
last_free = id;
|
||||
continue;
|
||||
}
|
||||
let level = if Some(id) == info {
|
||||
3
|
||||
} else if body.windows(19).any(|s| s == b"/Subtype /TrueType ")
|
||||
|| body.windows(13).any(|s| s == b"/CIDFontType2")
|
||||
{
|
||||
2
|
||||
} else {
|
||||
1
|
||||
};
|
||||
let eligible =
|
||||
options.object_compression >= level && !body.windows(8).any(|w| w == b"\nstream\n");
|
||||
if eligible {
|
||||
if groups.last().is_none_or(|v| v.len() == 100) {
|
||||
groups.push(Vec::new());
|
||||
}
|
||||
let group = groups.len() - 1;
|
||||
let index = groups[group].len();
|
||||
groups[group].push(id);
|
||||
packed[id] = Some((objects.len() + 1 + group, index));
|
||||
} else {
|
||||
offsets[id] = pdf.len() as u64;
|
||||
pdf.extend_from_slice(format!("{id} 0 obj\n").as_bytes());
|
||||
@@ -75,12 +118,11 @@ pub(super) fn serialize(
|
||||
}
|
||||
}
|
||||
free[last_free] = Some(0);
|
||||
for (i, ids) in groups.iter().enumerate() {
|
||||
let id = objects.len() + 1 + i;
|
||||
offsets.push(pdf.len() as u64);
|
||||
for (id, ids) in groups {
|
||||
offsets[id] = pdf.len() as u64;
|
||||
let mut header = String::new();
|
||||
let mut body = Vec::new();
|
||||
for obj in ids {
|
||||
for obj in &ids {
|
||||
header.push_str(&format!("{obj} {} ", body.len()));
|
||||
body.extend_from_slice(&objects[*obj - 1]);
|
||||
body.push(b'\n');
|
||||
@@ -145,5 +187,9 @@ pub(super) fn serialize(
|
||||
pdf.extend(object);
|
||||
pdf.extend_from_slice(format!("\nendobj\nstartxref\n{xref}\n%%EOF\n").as_bytes());
|
||||
}
|
||||
pdf
|
||||
Ok(pdf)
|
||||
}
|
||||
|
||||
fn is_pdf_stream(body: &[u8]) -> bool {
|
||||
body.windows(8).any(|window| window == b"\nstream\n")
|
||||
}
|
||||
Reference in new issue
Block a user