Read file-backed PDF objects at their actual write boundary
This commit is contained in:
1 parent
4f4a4567ea
commit
a958954308
5 files changed
+139
-17
No files matched your search
@@ -2747,7 +2747,9 @@ impl Executor {
|
||||
self.current = node.span;
|
||||
self.tick()?;
|
||||
match &node.operation {
|
||||
OutputOperation::PdfObject(number) => return self.write_pdf_object(*number),
|
||||
OutputOperation::PdfObject(_) => {
|
||||
return self.fail("PDF object reference requires a native PDF shipout");
|
||||
}
|
||||
OutputOperation::PdfImage { image, .. } => {
|
||||
return self.declare_pdf_image(image.id);
|
||||
}
|
||||
|
||||
@@ -310,8 +310,16 @@ impl Executor {
|
||||
} else {
|
||||
Vec::new().into_iter()
|
||||
};
|
||||
let mut pdf_objects = Vec::new();
|
||||
for output in outputs {
|
||||
if let OutputOperation::PdfColor { stack, action } = &output.operation {
|
||||
if let OutputOperation::PdfObject(number) = output.operation {
|
||||
self.current = output.span;
|
||||
self.tick()?;
|
||||
if device.pdf.is_none() {
|
||||
return self.fail("cannot ship a PDF object reference to DVI");
|
||||
}
|
||||
pdf_objects.push((number, output.span));
|
||||
} else if let OutputOperation::PdfColor { stack, action } = &output.operation {
|
||||
if device.pdf.is_none() {
|
||||
return self.fail("cannot ship a PDF color operation to DVI");
|
||||
}
|
||||
@@ -363,6 +371,10 @@ impl Executor {
|
||||
*span,
|
||||
));
|
||||
}
|
||||
for (number, span) in pdf_objects {
|
||||
self.current = span;
|
||||
self.write_pdf_object(number, files)?;
|
||||
}
|
||||
let pdf_links = if has_link_material {
|
||||
self.ship_pdf_link_material(&b, &device, &pdf_graphics_steps, files)?
|
||||
} else {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::*;
|
||||
use crate::pdf_device::page_resources::ObjectBody;
|
||||
use crate::{pdf_device::page_resources::ObjectBody, resources::ResourceKind};
|
||||
|
||||
impl Executor {
|
||||
pub(super) fn scan_pdf_object(
|
||||
@@ -47,6 +47,7 @@ impl Executor {
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let declaration = self.current;
|
||||
let attributes = if self.keyword("stream", files)? {
|
||||
Some(if self.keyword("attr", files)? {
|
||||
self.scan_pdf_bytes(files)?
|
||||
@@ -56,13 +57,20 @@ impl Executor {
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if self.keyword("file", files)? {
|
||||
return self.fail("file-backed \\pdfobj payloads are unsupported in native PDF output");
|
||||
}
|
||||
let bytes = self.scan_pdf_bytes(files)?;
|
||||
let value = match attributes {
|
||||
Some(attributes) => ObjectBody::Stream { attributes, bytes },
|
||||
None => ObjectBody::Direct(bytes),
|
||||
let value = if self.keyword("file", files)? {
|
||||
let tokens = self.scan_pdf_tokens(files)?;
|
||||
let name = self.token_list_text(&tokens);
|
||||
ObjectBody::File {
|
||||
attributes,
|
||||
name: self.decode_filename(name)?,
|
||||
from: self.source_name(declaration),
|
||||
}
|
||||
} else {
|
||||
let bytes = self.scan_pdf_bytes(files)?;
|
||||
match attributes {
|
||||
Some(attributes) => ObjectBody::Stream { attributes, bytes },
|
||||
None => ObjectBody::Direct(bytes),
|
||||
}
|
||||
};
|
||||
let number = self
|
||||
.state
|
||||
@@ -72,7 +80,7 @@ impl Executor {
|
||||
.map_err(|error| self.error(error, self.current))?;
|
||||
if immediate {
|
||||
self.pdf_page_data(files)?;
|
||||
self.write_pdf_object(number)?;
|
||||
self.write_pdf_object(number, files)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -101,13 +109,54 @@ impl Executor {
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn write_pdf_object(&mut self, number: u32) -> Result<(), Error> {
|
||||
pub(super) fn write_pdf_object(
|
||||
&mut self,
|
||||
number: u32,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<(), Error> {
|
||||
self.reads.insert(Dependency::PdfObjects);
|
||||
let object = self
|
||||
.state
|
||||
.pdf_objects
|
||||
.as_deref()
|
||||
.ok_or_else(|| String::from("unknown native PDF object"))
|
||||
.and_then(|objects| objects.unwritten(number))
|
||||
.map_err(|error| self.error(error, self.current))?;
|
||||
let Some(object) = object else { return Ok(()) };
|
||||
let materialized = if let ObjectBody::File {
|
||||
attributes,
|
||||
name,
|
||||
from,
|
||||
} = object
|
||||
{
|
||||
self.reads.insert(Dependency::FileLookup {
|
||||
name: name.clone(),
|
||||
from: from.clone(),
|
||||
});
|
||||
let resource = files
|
||||
.resource(ResourceKind::Tex, name, from)
|
||||
.map_err(|error| {
|
||||
self.error(
|
||||
format!("cannot open PDF object file {name}: {error}"),
|
||||
self.current,
|
||||
)
|
||||
})?;
|
||||
self.check_cancellation()?;
|
||||
Some(match attributes {
|
||||
Some(attributes) => ObjectBody::Stream {
|
||||
attributes: attributes.clone(),
|
||||
bytes: resource.data,
|
||||
},
|
||||
None => ObjectBody::Direct(resource.data),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
self.state
|
||||
.pdf_objects
|
||||
.as_deref_mut()
|
||||
.ok_or_else(|| String::from("unknown native PDF object"))
|
||||
.and_then(|objects| objects.write(number))
|
||||
.expect("existing object table")
|
||||
.write(number, materialized, self.limits.tokens)
|
||||
.map_err(|error| self.error(error, self.current))
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,15 @@ use std::{
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub(crate) enum ObjectBody {
|
||||
Direct(Vec<u8>),
|
||||
Stream { attributes: Vec<u8>, bytes: Vec<u8> },
|
||||
Stream {
|
||||
attributes: Vec<u8>,
|
||||
bytes: Vec<u8>,
|
||||
},
|
||||
File {
|
||||
attributes: Option<Vec<u8>>,
|
||||
name: String,
|
||||
from: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl ObjectBody {
|
||||
@@ -20,12 +28,22 @@ impl ObjectBody {
|
||||
match self {
|
||||
Self::Direct(bytes) => bytes.len(),
|
||||
Self::Stream { attributes, bytes } => attributes.len().saturating_add(bytes.len()),
|
||||
Self::File {
|
||||
attributes,
|
||||
name,
|
||||
from,
|
||||
} => attributes
|
||||
.as_ref()
|
||||
.map_or(0, Vec::len)
|
||||
.saturating_add(name.len())
|
||||
.saturating_add(from.len()),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(&self) -> Result<Object, String> {
|
||||
match self {
|
||||
Self::Direct(bytes) => parse_value(bytes),
|
||||
Self::File { .. } => Err("PDF object file payload was not read before export".into()),
|
||||
Self::Stream { attributes, bytes } => {
|
||||
let mut dictionary = parse_entries(attributes)?;
|
||||
if dictionary.has(b"Length") {
|
||||
@@ -89,8 +107,29 @@ impl PdfObjects {
|
||||
Ok(number)
|
||||
}
|
||||
|
||||
pub(crate) fn write(&mut self, number: u32) -> Result<(), String> {
|
||||
self.body(number)?;
|
||||
pub(crate) fn unwritten(&self, number: u32) -> Result<Option<&ObjectBody>, String> {
|
||||
if self.written.contains(&number) {
|
||||
Ok(None)
|
||||
} else {
|
||||
self.body(number).map(Some)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn write(
|
||||
&mut self,
|
||||
number: u32,
|
||||
materialized: Option<ObjectBody>,
|
||||
limit: usize,
|
||||
) -> Result<(), String> {
|
||||
let old = self.body(number)?;
|
||||
if let Some(value) = materialized {
|
||||
let bytes = (self.bytes - old.len()).saturating_add(value.len());
|
||||
if bytes > limit {
|
||||
return Err("native PDF object payload capacity exceeded".into());
|
||||
}
|
||||
self.values[number as usize - 1] = Some(Arc::new(value));
|
||||
self.bytes = bytes;
|
||||
}
|
||||
self.written.insert(number);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user