Read file-backed PDF objects at their actual write boundary

This commit is contained in:
gloamxun committed 2026-11-21 01:02:40 +08:00
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.current = node.span;
self.tick()?; self.tick()?;
match &node.operation { 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, .. } => { OutputOperation::PdfImage { image, .. } => {
return self.declare_pdf_image(image.id); return self.declare_pdf_image(image.id);
} }
@@ -310,8 +310,16 @@ impl Executor {
} else { } else {
Vec::new().into_iter() Vec::new().into_iter()
}; };
let mut pdf_objects = Vec::new();
for output in outputs { 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() { if device.pdf.is_none() {
return self.fail("cannot ship a PDF color operation to DVI"); return self.fail("cannot ship a PDF color operation to DVI");
} }
@@ -363,6 +371,10 @@ impl Executor {
*span, *span,
)); ));
} }
for (number, span) in pdf_objects {
self.current = span;
self.write_pdf_object(number, files)?;
}
let pdf_links = if has_link_material { let pdf_links = if has_link_material {
self.ship_pdf_link_material(&b, &device, &pdf_graphics_steps, files)? self.ship_pdf_link_material(&b, &device, &pdf_graphics_steps, files)?
} else { } else {
@@ -1,5 +1,5 @@
use super::*; use super::*;
use crate::pdf_device::page_resources::ObjectBody; use crate::{pdf_device::page_resources::ObjectBody, resources::ResourceKind};
impl Executor { impl Executor {
pub(super) fn scan_pdf_object( pub(super) fn scan_pdf_object(
@@ -47,6 +47,7 @@ impl Executor {
} else { } else {
None None
}; };
let declaration = self.current;
let attributes = if self.keyword("stream", files)? { let attributes = if self.keyword("stream", files)? {
Some(if self.keyword("attr", files)? { Some(if self.keyword("attr", files)? {
self.scan_pdf_bytes(files)? self.scan_pdf_bytes(files)?
@@ -56,13 +57,20 @@ impl Executor {
} else { } else {
None None
}; };
if self.keyword("file", files)? { let value = if self.keyword("file", files)? {
return self.fail("file-backed \\pdfobj payloads are unsupported in native PDF output"); let tokens = self.scan_pdf_tokens(files)?;
} let name = self.token_list_text(&tokens);
let bytes = self.scan_pdf_bytes(files)?; ObjectBody::File {
let value = match attributes { attributes,
Some(attributes) => ObjectBody::Stream { attributes, bytes }, name: self.decode_filename(name)?,
None => ObjectBody::Direct(bytes), 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 let number = self
.state .state
@@ -72,7 +80,7 @@ impl Executor {
.map_err(|error| self.error(error, self.current))?; .map_err(|error| self.error(error, self.current))?;
if immediate { if immediate {
self.pdf_page_data(files)?; self.pdf_page_data(files)?;
self.write_pdf_object(number)?; self.write_pdf_object(number, files)?;
} }
Ok(()) 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); 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 self.state
.pdf_objects .pdf_objects
.as_deref_mut() .as_deref_mut()
.ok_or_else(|| String::from("unknown native PDF object")) .expect("existing object table")
.and_then(|objects| objects.write(number)) .write(number, materialized, self.limits.tokens)
.map_err(|error| self.error(error, self.current)) .map_err(|error| self.error(error, self.current))
} }
} }
@@ -12,7 +12,15 @@ use std::{
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub(crate) enum ObjectBody { pub(crate) enum ObjectBody {
Direct(Vec<u8>), 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 { impl ObjectBody {
@@ -20,12 +28,22 @@ impl ObjectBody {
match self { match self {
Self::Direct(bytes) => bytes.len(), Self::Direct(bytes) => bytes.len(),
Self::Stream { attributes, bytes } => attributes.len().saturating_add(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> { fn parse(&self) -> Result<Object, String> {
match self { match self {
Self::Direct(bytes) => parse_value(bytes), Self::Direct(bytes) => parse_value(bytes),
Self::File { .. } => Err("PDF object file payload was not read before export".into()),
Self::Stream { attributes, bytes } => { Self::Stream { attributes, bytes } => {
let mut dictionary = parse_entries(attributes)?; let mut dictionary = parse_entries(attributes)?;
if dictionary.has(b"Length") { if dictionary.has(b"Length") {
@@ -89,8 +107,29 @@ impl PdfObjects {
Ok(number) Ok(number)
} }
pub(crate) fn write(&mut self, number: u32) -> Result<(), String> { pub(crate) fn unwritten(&self, number: u32) -> Result<Option<&ObjectBody>, String> {
self.body(number)?; 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); self.written.insert(number);
Ok(()) Ok(())
} }
+20
View File
@@ -1126,6 +1126,26 @@ schema28 更新。schema 1至26和29仍拒绝,27与28缺少必需 Unicode
[原样模板记录](PUBLISHER_TEMPLATE_MATRIX.md#aps-revtex-原样双栏样稿) [原样模板记录](PUBLISHER_TEMPLATE_MATRIX.md#aps-revtex-原样双栏样稿)
[pdfTeX 的重音布局](https://github.com/TeX-Live/texlive-source/blob/trunk/texk/web2c/pdftexdir/pdftex.web#L28084-L28143) [pdfTeX 的重音布局](https://github.com/TeX-Live/texlive-source/blob/trunk/texk/web2c/pdftexdir/pdftex.web#L28084-L28143)
### 文件型 PDF 对象和真实资源依赖
`\pdfobj file` 与 `\pdfobj stream ... file` 在对象实际写入时
通过宿主读取文件。普通出页先执行原有写入队列,再物化引用的 PDF 对象。
同一对象一旦写入,跨页引用使用该版本的字节。宿主记录请求路径、资源
身份和所读字节版本;检查点保留实际对象状态及真实依赖。
[pdfTeX 的对象写入位置](https://github.com/TeX-Live/texlive-source/blob/trunk/texk/web2c/pdftexdir/pdftex.web)
定向对照覆盖含 NUL 和非 UTF-8 字节的真实流、磁盘直接对象和同一次
出页稍后生成的内容。按实际读取版本物化的三个对象各只读取一次。
两页144 DPI灰度图逐像素一致。保持主文件不变,只编辑图形状态资源,
会话恢复37步,只替换第一页;第二页的 SVG 保持一致。最终原生
PDF 和 SVG 与独立零复用一致。移除会话修订号和测试项目根之后来源
映射一致。该对照验证版本失效和结果,未采样正式性能。
回执位于 `target/p3-pdf-file-object-native`:
- `receipt.json` 为 `e2dc5bc98943f565ebfac15a5cdcf4e9b7538d62cd399e267fcadeea11d7ab76`。
- `asset-edit/receipt.json` 为 `8c581797ef1b4aaba72b38b873e34874c2636ab1f76466fb0d593c3ee78cc81d`。
### 原生 PDF 页面资源与透明度 ### 原生 PDF 页面资源与透明度
`\pdfpageresources` 使用 TeX 的 token 参数和分组恢复。每次出页 `\pdfpageresources` 使用 TeX 的 token 参数和分组恢复。每次出页