Write XeLaTeX named PDF objects and streams in native shipout

This commit is contained in:
gloamxun committed 2026-11-28 18:22:15 +08:00
1 parent c02f678bf7
commit e14361fec6
8 files changed
+551 -43

No files matched your search

@@ -814,3 +814,87 @@ fn xdv_root_annotation_keeps_its_extent_and_destination_across_shipouts() {
assert_eq!(names[2].as_str().unwrap(), b"1");
assert_eq!(names[3].as_array().unwrap()[0].as_reference().unwrap(), page_ids[&1]);
}
#[test]
fn xdv_named_objects_keep_forward_edges_and_file_streams_at_shipout() {
use lopdf::{Document, Object};
use texrra_tex_core::pdf::shipout_lists_to_native_pdf_document;
use texrra_tex_core::resources::{LoadedResource, ResourceKind, ResourceVersion};
struct Device;
impl SourceProvider for Device {
fn read(&mut self, name: &str, _: &str) -> Result<SourceFile, String> {
Err(format!("unexpected input {name}"))
}
fn default_pk_resolution(&mut self) -> Result<i32, String> {
Ok(600)
}
fn resource(&mut self, kind: ResourceKind, name: &str, from: &str) -> Result<LoadedResource, String> {
assert_eq!((kind, name, from), (ResourceKind::PdfImage, "object.bin", "profile.tex"));
Ok(LoadedResource {
version: ResourceVersion {
path: name.into(),
bytes: 3,
sha256: "5240672d7b51756b829ad0ef8d9468b7a078afa2f410484fd3892dab47becb72".into(),
},
data: vec![0, 128, 255],
})
}
}
let state = State::initex().with_etex().with_input_profile(InputProfile::XeTeX);
let result = Executor::new(state, Limits::default())
.with_native_pages()
.run(
source(
r"\catcode123=1 \catcode125=2
\setbox0=\hbox{\special{pdf:obj @discarded << /bad true >>}}
\shipout\hbox{
\special{pdf:put @catalog << /Data @root /Missing @missing >>}
\special{pdf:object @root << /Self @root /Page @nextpage /Items @items >>}
\special{pdf:obj @items [1]}
\special{pdf:put @items 2 @binary}
\special{pdf:fstream @binary (object.bin) << /Tag /First >>}
\special{pdf:put @binary << /Tag /Final >>}
\special{pdf:close @root}
\vrule width7pt height8pt}
\shipout\hbox{
\special{pdf:put @catalog << /Same @root >>}
\vrule width9pt height8pt}
\end",
),
&mut Device,
)
.unwrap();
assert!(result.reads.contains(&Dependency::FileLookup {
name: "object.bin".to_owned(),
from: "profile.tex".to_owned(),
}));
let pages = result.shipout_display_lists().unwrap();
let output = shipout_lists_to_native_pdf_document(&pages, result.state.pdf_final_options()).unwrap();
assert!(output.diagnostics.iter().any(|warning| warning.contains("Object @missing used, but not defined")));
let pdf = Document::load_mem(&output.bytes).unwrap();
let catalog = pdf.catalog().unwrap();
let id = catalog.get(b"Data").unwrap().as_reference().unwrap();
assert_eq!(catalog.get(b"Same").unwrap().as_reference().unwrap(), id);
let root = pdf.get_object(id).unwrap().as_dict().unwrap();
assert_eq!(root.get(b"Self").unwrap().as_reference().unwrap(), id);
assert_eq!(root.get(b"Page").unwrap().as_reference().unwrap(), pdf.get_pages()[&2]);
let items = pdf.get_object(root.get(b"Items").unwrap().as_reference().unwrap()).unwrap().as_array().unwrap();
assert_eq!(items[0].as_i64().unwrap(), 1);
assert_eq!(items[1].as_i64().unwrap(), 2);
let stream = pdf.get_object(items[2].as_reference().unwrap()).unwrap().as_stream().unwrap();
assert_eq!(stream.dict.get(b"Tag").unwrap().as_name().unwrap(), b"Final");
let bytes = if stream.dict.has(b"Filter") {
stream.decompressed_content().unwrap()
} else {
stream.content.clone()
};
assert_eq!(bytes, [0, 128, 255]);
let missing = catalog.get(b"Missing").unwrap().as_reference().unwrap();
assert!(matches!(pdf.get_object(missing), Ok(Object::Null)));
assert!(!output.bytes.windows(4).any(|window| window == b"/bad"));
}