Read binary TeX inputs and render default ICC colors
This commit is contained in:
1 parent
e9893a5c1b
commit
52db77885d
10 files changed
+379
-19
No files matched your search
@@ -0,0 +1,162 @@
|
||||
//! Make PDF default device color selections explicit for the SVG interpreter.
|
||||
use lopdf::{
|
||||
Dictionary, Document, Object, ObjectId, Stream,
|
||||
content::{Content, Operation},
|
||||
};
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
/// Device space, its replacement resource, and implicit fill/stroke operators.
|
||||
const DEVICES: [(&[u8], &[u8], &str, &str); 3] = [
|
||||
(b"DeviceGray", b"DefaultGray", "g", "G"),
|
||||
(b"DeviceRGB", b"DefaultRGB", "rg", "RG"),
|
||||
(b"DeviceCMYK", b"DefaultCMYK", "k", "K"),
|
||||
];
|
||||
|
||||
pub(super) fn explicit_for_preview(pdf: &[u8]) -> Result<Option<Vec<u8>>, String> {
|
||||
let mut document = Document::load_mem(pdf).map_err(error)?;
|
||||
let mut changed = false;
|
||||
for id in document.get_pages().into_values() {
|
||||
let defaults = defaults(&document, page_resources(&document, id)?)?;
|
||||
if defaults == [false; 3] {
|
||||
continue;
|
||||
}
|
||||
let data = document.get_page_content(id).map_err(error)?;
|
||||
if let Some(data) = explicit_operations(&data, defaults)? {
|
||||
let stream = document.add_object(Stream::new(Dictionary::new(), data));
|
||||
document
|
||||
.get_object_mut(id)
|
||||
.and_then(Object::as_dict_mut)
|
||||
.map_err(error)?
|
||||
.set("Contents", Object::Reference(stream));
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
// Forms with their own resources have a separate color-space scope.
|
||||
let forms: Vec<_> = document
|
||||
.objects
|
||||
.iter()
|
||||
.filter_map(|(&id, object)| {
|
||||
let stream = object.as_stream().ok()?;
|
||||
(stream.dict.get(b"Subtype").and_then(Object::as_name).ok()? == b"Form"
|
||||
&& stream.dict.has(b"Resources"))
|
||||
.then_some(id)
|
||||
})
|
||||
.collect();
|
||||
for id in forms {
|
||||
let form = document
|
||||
.get_object(id)
|
||||
.and_then(Object::as_stream)
|
||||
.map_err(error)?;
|
||||
let defaults = defaults(&document, form.dict.get(b"Resources").ok())?;
|
||||
if defaults == [false; 3] {
|
||||
continue;
|
||||
}
|
||||
let data = if form.dict.has(b"Filter") {
|
||||
form.decompressed_content().map_err(error)?
|
||||
} else {
|
||||
form.content.clone()
|
||||
};
|
||||
if let Some(data) = explicit_operations(&data, defaults)? {
|
||||
let form = document
|
||||
.get_object_mut(id)
|
||||
.and_then(Object::as_stream_mut)
|
||||
.map_err(error)?;
|
||||
form.dict.remove(b"Filter");
|
||||
form.dict.remove(b"DecodeParms");
|
||||
form.set_content(data);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if !changed {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut buffer = Vec::new();
|
||||
document.save_to(&mut buffer).map_err(error)?;
|
||||
Ok(Some(buffer))
|
||||
}
|
||||
|
||||
fn page_resources(document: &Document, mut id: ObjectId) -> Result<Option<&Object>, String> {
|
||||
let mut ancestors = BTreeSet::new();
|
||||
while ancestors.insert(id) {
|
||||
let node = document.get_dictionary(id).map_err(error)?;
|
||||
if let Ok(resources) = node.get(b"Resources") {
|
||||
return Ok(Some(resources));
|
||||
}
|
||||
let Ok(parent) = node.get(b"Parent") else {
|
||||
return Ok(None);
|
||||
};
|
||||
id = parent.as_reference().map_err(error)?;
|
||||
}
|
||||
Err("cycle in PDF preview page resources".into())
|
||||
}
|
||||
|
||||
fn defaults(document: &Document, resources: Option<&Object>) -> Result<[bool; 3], String> {
|
||||
let Some(resources) = resources else {
|
||||
return Ok([false; 3]);
|
||||
};
|
||||
let resources = document
|
||||
.dereference(resources)
|
||||
.map_err(error)?
|
||||
.1
|
||||
.as_dict()
|
||||
.map_err(error)?;
|
||||
let Ok(spaces) = resources.get(b"ColorSpace") else {
|
||||
return Ok([false; 3]);
|
||||
};
|
||||
let spaces = document.dereference(spaces).map_err(error)?.1;
|
||||
let Ok(spaces) = spaces.as_dict() else {
|
||||
return Ok([false; 3]);
|
||||
};
|
||||
let mut enabled = [false; 3];
|
||||
for (present, (_, name, _, _)) in enabled.iter_mut().zip(DEVICES) {
|
||||
if let Ok(value) = spaces.get(name) {
|
||||
*present = !matches!(document.dereference(value).map_err(error)?.1, Object::Null);
|
||||
}
|
||||
}
|
||||
Ok(enabled)
|
||||
}
|
||||
|
||||
fn explicit_operations(data: &[u8], enabled: [bool; 3]) -> Result<Option<Vec<u8>>, String> {
|
||||
if data.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let content = Content::decode(data).map_err(error)?;
|
||||
let mut operations = Vec::with_capacity(content.operations.len());
|
||||
let mut changed = false;
|
||||
for mut operation in content.operations {
|
||||
for (present, (device, name, fill, stroke)) in enabled.into_iter().zip(DEVICES) {
|
||||
if !present {
|
||||
continue;
|
||||
}
|
||||
let op = operation.operator.as_str();
|
||||
if op == fill || op == stroke {
|
||||
let (select, paint) = if op == fill {
|
||||
("cs", "sc")
|
||||
} else {
|
||||
("CS", "SC")
|
||||
};
|
||||
operations.push(Operation::new(select, vec![Object::Name(name.to_vec())]));
|
||||
operation.operator = paint.into();
|
||||
changed = true;
|
||||
break;
|
||||
}
|
||||
if matches!(op, "cs" | "CS")
|
||||
&& let [Object::Name(selected)] = operation.operands.as_mut_slice()
|
||||
&& selected == device
|
||||
{
|
||||
*selected = name.to_vec();
|
||||
changed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
operations.push(operation);
|
||||
}
|
||||
if !changed {
|
||||
return Ok(None);
|
||||
}
|
||||
Content { operations }.encode().map(Some).map_err(error)
|
||||
}
|
||||
|
||||
fn error(error: impl std::fmt::Display) -> String {
|
||||
format!("could not resolve PDF preview color spaces: {error}")
|
||||
}
|
||||
Reference in new issue
Block a user