Resolve imported PDF Type 1 device fonts in native previews
This commit is contained in:
1 parent
c0970bdbf6
commit
b640b26eff
12 files changed
+586
-106
No files matched your search
@@ -0,0 +1,94 @@
|
||||
//! Append private preview objects without rewriting the exported geometry.
|
||||
use lopdf::{
|
||||
Dictionary, Document, Object, ObjectId,
|
||||
content::{Content, Operation},
|
||||
};
|
||||
use std::io::Write;
|
||||
|
||||
pub(super) fn append_objects(
|
||||
pdf: &[u8],
|
||||
document: &Document,
|
||||
objects: impl Iterator<Item = ObjectId>,
|
||||
) -> Result<Vec<u8>, String> {
|
||||
let suffix = b"startxref";
|
||||
let start = pdf
|
||||
.windows(suffix.len())
|
||||
.rposition(|part| part == suffix)
|
||||
.ok_or("native PDF startxref missing")?
|
||||
+ suffix.len();
|
||||
let previous: usize = std::str::from_utf8(&pdf[start..])
|
||||
.ok()
|
||||
.and_then(|tail| tail.split_ascii_whitespace().next())
|
||||
.and_then(|value| value.parse().ok())
|
||||
.filter(|offset| *offset < start)
|
||||
.ok_or("invalid native PDF startxref")?;
|
||||
let mut bytes = pdf.to_vec();
|
||||
bytes.push(b'\n');
|
||||
let mut offsets = Vec::new();
|
||||
for id @ (number, generation) in objects {
|
||||
offsets.push((number, generation, bytes.len()));
|
||||
writeln!(bytes, "{number} {generation} obj").unwrap();
|
||||
write_value(&mut bytes, &document.objects[&id])?;
|
||||
bytes.extend_from_slice(b"\nendobj\n");
|
||||
}
|
||||
let xref = bytes.len();
|
||||
bytes.extend_from_slice(b"xref\n");
|
||||
for (number, generation, offset) in offsets {
|
||||
writeln!(bytes, "{number} 1\n{offset:010} {generation:05} n ").unwrap();
|
||||
}
|
||||
let mut trailer = Dictionary::new();
|
||||
for key in ["Root", "Info", "ID"] {
|
||||
if let Ok(value) = document.trailer.get(key.as_bytes()) {
|
||||
trailer.set(key, value.clone());
|
||||
}
|
||||
}
|
||||
trailer.set("Size", i64::from(document.max_id) + 1);
|
||||
trailer.set("Prev", previous as i64);
|
||||
bytes.extend_from_slice(b"trailer\n");
|
||||
write_dictionary(&mut bytes, &trailer)?;
|
||||
writeln!(bytes, "\nstartxref\n{xref}\n%%EOF").unwrap();
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
fn write_dictionary(bytes: &mut Vec<u8>, dictionary: &Dictionary) -> Result<(), String> {
|
||||
bytes.extend_from_slice(b"<<");
|
||||
for (name, value) in dictionary.iter() {
|
||||
write_value(bytes, &Object::Name(name.clone()))?;
|
||||
bytes.push(b' ');
|
||||
write_value(bytes, value)?;
|
||||
bytes.push(b' ');
|
||||
}
|
||||
bytes.extend_from_slice(b">>");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_value(bytes: &mut Vec<u8>, value: &Object) -> Result<(), String> {
|
||||
// lopdf 0.26's writer rounds all Real objects to two decimal places.
|
||||
match value {
|
||||
Object::Real(value) => write!(bytes, "{value}").unwrap(),
|
||||
Object::Dictionary(dictionary) => write_dictionary(bytes, dictionary)?,
|
||||
Object::Array(values) => {
|
||||
bytes.push(b'[');
|
||||
for value in values {
|
||||
write_value(bytes, value)?;
|
||||
bytes.push(b' ');
|
||||
}
|
||||
bytes.push(b']');
|
||||
}
|
||||
Object::Stream(stream) => {
|
||||
write_dictionary(bytes, &stream.dict)?;
|
||||
bytes.extend_from_slice(b"\nstream\n");
|
||||
bytes.extend_from_slice(&stream.content);
|
||||
bytes.extend_from_slice(b"\nendstream");
|
||||
}
|
||||
other => {
|
||||
let encoded = Content {
|
||||
operations: [Operation::new("", vec![other.clone()])],
|
||||
}
|
||||
.encode()
|
||||
.map_err(|error| format!("could not encode preview PDF object: {error}"))?;
|
||||
bytes.extend_from_slice(&encoded[..encoded.len() - 2]);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user