Merge XDV document information in shipout order
This commit is contained in:
1 parent
6826efbeb1
commit
dd695a8945
9 files changed
+251
-21
No files matched your search
@@ -258,7 +258,7 @@ pub(crate) fn check_pdf_nesting(bytes: &[u8]) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_value(bytes: &[u8]) -> Result<Object, String> {
|
||||
pub(super) fn parse_value(bytes: &[u8]) -> Result<Object, String> {
|
||||
check_pdf_nesting(bytes)?;
|
||||
// The content parser accepts complete PDF arrays, including references
|
||||
// inside them. Requiring the final operator also rejects a partially
|
||||
@@ -303,7 +303,7 @@ pub(crate) fn font_attributes_affect_paint(bytes: &[u8]) -> Result<bool, String>
|
||||
Ok(attributes.iter().any(|(name, _)| name != b"ToUnicode"))
|
||||
}
|
||||
|
||||
fn encode_value(object: Object) -> Result<Vec<u8>, String> {
|
||||
pub(super) fn encode_value(object: Object) -> Result<Vec<u8>, String> {
|
||||
if let Object::Stream(stream) = object {
|
||||
let mut output = encode_value(Object::Dictionary(stream.dict))?;
|
||||
output.extend_from_slice(b"\nstream\n");
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
//! Document dictionary specials executed in XDV page order.
|
||||
use lopdf::{Object, StringFormat};
|
||||
|
||||
use super::{
|
||||
PdfInfo,
|
||||
page_resources::{encode_value, parse_entries, parse_value},
|
||||
};
|
||||
|
||||
const TEXT_KEYS: &[&[u8]] = &[
|
||||
b"Title",
|
||||
b"Author",
|
||||
b"Subject",
|
||||
b"Keywords",
|
||||
b"Creator",
|
||||
b"Producer",
|
||||
b"Contents",
|
||||
b"Subj",
|
||||
b"TU",
|
||||
b"T",
|
||||
b"TM",
|
||||
];
|
||||
|
||||
const INFO_STRING_KEYS: &[&[u8]] = &[
|
||||
b"Title",
|
||||
b"Author",
|
||||
b"Subject",
|
||||
b"Keywords",
|
||||
b"Creator",
|
||||
b"Producer",
|
||||
b"CreationDate",
|
||||
b"ModDate",
|
||||
];
|
||||
|
||||
pub(crate) fn merge_docinfo(
|
||||
current: &[u8],
|
||||
special: &[u8],
|
||||
) -> Result<(Vec<u8>, Vec<String>), String> {
|
||||
let Object::Dictionary(additions) = parse_value(special)? else {
|
||||
return Err("pdf:docinfo requires a PDF dictionary".into());
|
||||
};
|
||||
let mut document = parse_entries(current)?;
|
||||
let mut warnings = Vec::new();
|
||||
for (key, value) in &additions {
|
||||
let mut value = value.clone();
|
||||
convert_text(key, &mut value, &mut warnings)?;
|
||||
document.set(key.clone(), value);
|
||||
}
|
||||
|
||||
let encoded = encode_value(Object::Dictionary(document))?;
|
||||
let entries = encoded
|
||||
.trim_ascii()
|
||||
.strip_prefix(b"<<")
|
||||
.and_then(|body| body.strip_suffix(b">>"))
|
||||
.expect("the PDF encoder emits a dictionary");
|
||||
Ok((entries.to_vec(), warnings))
|
||||
}
|
||||
|
||||
fn convert_text(key: &[u8], value: &mut Object, warnings: &mut Vec<String>) -> Result<(), String> {
|
||||
match value {
|
||||
Object::String(bytes, format)
|
||||
if TEXT_KEYS.contains(&key)
|
||||
&& !bytes.starts_with(&[0xfe, 0xff])
|
||||
&& !bytes.is_ascii() =>
|
||||
{
|
||||
if let Ok(text) = std::str::from_utf8(bytes) {
|
||||
*bytes = [0xfe, 0xff]
|
||||
.into_iter()
|
||||
.chain(text.encode_utf16().flat_map(u16::to_be_bytes))
|
||||
.collect();
|
||||
*format = StringFormat::Hexadecimal;
|
||||
} else {
|
||||
warnings.push(format!(
|
||||
"xdvipdfmx: warning: Input string conversion (to UTF16BE) failed for {}...\n",
|
||||
String::from_utf8_lossy(key),
|
||||
));
|
||||
}
|
||||
}
|
||||
Object::Array(values) => {
|
||||
for value in values {
|
||||
convert_text(key, value, warnings)?;
|
||||
}
|
||||
}
|
||||
Object::Dictionary(entries) => {
|
||||
for (key, value) in entries.iter_mut() {
|
||||
convert_text(key, value, warnings)?;
|
||||
}
|
||||
}
|
||||
Object::Reference(_) => {
|
||||
return Err("unsupported reference in XDV document information".into());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn information_dictionary(
|
||||
info: PdfInfo<'_>,
|
||||
diagnostics: &mut Vec<String>,
|
||||
) -> Result<Vec<u8>, String> {
|
||||
let mut dictionary = parse_entries(info.entries)?;
|
||||
// Identify the native producer, while respecting a creator set by the document.
|
||||
if !dictionary.has(b"Creator") {
|
||||
dictionary.set("Creator", Object::string_literal("TeX"));
|
||||
}
|
||||
for &key in INFO_STRING_KEYS {
|
||||
match dictionary.get(key) {
|
||||
Ok(Object::String(bytes, _)) if bytes.is_empty() => {
|
||||
dictionary.remove(key);
|
||||
}
|
||||
Ok(Object::String(_, _)) | Err(_) => {}
|
||||
Ok(_) => {
|
||||
diagnostics.push(format!(
|
||||
"xdvipdfmx: warning: {:?} removed from DocInfo because its value is not a string.",
|
||||
String::from_utf8_lossy(key),
|
||||
));
|
||||
dictionary.remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
if !dictionary.has(b"Producer") {
|
||||
dictionary.set("Producer", Object::string_literal("texrra Route-D native"));
|
||||
}
|
||||
if !dictionary.has(b"CreationDate")
|
||||
&& let Some(date) = info.date
|
||||
{
|
||||
dictionary.set("CreationDate", Object::string_literal(date));
|
||||
}
|
||||
encode_value(Object::Dictionary(dictionary))
|
||||
}
|
||||
Reference in new issue
Block a user