Write XeLaTeX document name trees and attachments

This commit is contained in:
gloamxun committed 2026-11-28 20:54:56 +08:00
1 parent d627614af8
commit dcc6a3984c
9 files changed
+446 -14

No files matched your search

@@ -1204,3 +1204,57 @@ fn xdv_imported_image_matches_forward_catalog_and_page_references() {
assert_eq!(pdf.get_object(stamp).unwrap().as_str().unwrap(), b"proof");
assert_eq!(form.dict.get(b"Subtype").unwrap().as_name().unwrap(), b"Form");
}
#[test]
fn xdv_name_trees_keep_binary_keys_and_share_the_catalog_root() {
use lopdf::{Document, Object};
use texrra_tex_core::pdf::shipout_lists_to_native_pdf_document;
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
\shipout\hbox{%
\special{pdf:put @catalog << /Copy @names >>}%
\special{pdf:put @names << /Pages << /Names [(page) @nextpage] >> >>}%
\special{pdf:dest (mixed) [@thispage /FitH 55]}%
\special{pdf:names /Dests [(manual) [@nextpage /Fit] (mixed) [@nextpage /Fit] (unreached) [@nextpage /Fit]]}%
\special{pdf:ann width 5pt height 2pt << /Subtype /Link /Dest (manual) >>}%
\special{pdf:ann width 5pt height 2pt << /Subtype /Link /Dest (mixed) >>}%
\special{pdf:names /IDS [<ff> 9 (b) 8 (a) 7 <6100> 6 (c) 5 (d) 4 (e) 3 (f) 2 <00> 1]}%
\special{pdf:names /AP (first) @leaf}%
\vrule width5pt height3pt}
\shipout\hbox{\special{pdf:obj @leaf << /Proof (shared) >>}\vrule width5pt height3pt}
\end"),
&mut TestDevice,
).unwrap();
let pages = result.shipout_display_lists().unwrap();
let output = shipout_lists_to_native_pdf_document(&pages, result.state.pdf_final_options()).unwrap();
let pdf = Document::load_mem(&output.bytes).unwrap();
let resolve = |value: &Object| pdf.get_object(value.as_reference().unwrap()).unwrap();
let id = pdf.catalog().unwrap().get(b"Names").unwrap().as_reference().unwrap();
assert_eq!(pdf.catalog().unwrap().get(b"Copy").unwrap().as_reference().unwrap(), id);
let root = pdf.get_dictionary(id).unwrap();
let ids = resolve(root.get(b"IDS").unwrap()).as_dict().unwrap();
assert!(!ids.has(b"Limits"));
let children = ids.get(b"Kids").unwrap().as_array().unwrap();
let keys: Vec<_> = children.iter().flat_map(|child| {
let node = resolve(child).as_dict().unwrap();
let values = node.get(b"Names").unwrap().as_array().unwrap();
let limits = node.get(b"Limits").unwrap().as_array().unwrap();
assert_eq!(limits[0].as_str().unwrap(), values[0].as_str().unwrap());
assert_eq!(limits[1].as_str().unwrap(), values[values.len()-2].as_str().unwrap());
values.iter().step_by(2).map(|key| key.as_str().unwrap().to_vec())
}).collect();
assert_eq!(keys, [b"\0".as_slice(), b"a", b"a\0", b"b", b"c", b"d", b"e", b"f", b"\xff"]);
let pages = pdf.get_pages();
let dests = resolve(root.get(b"Dests").unwrap()).as_dict().unwrap()
.get(b"Names").unwrap().as_array().unwrap();
assert_eq!(dests.len(), 4);
assert_eq!(resolve(&dests[1]).as_array().unwrap()[0].as_reference().unwrap(), pages[&2]);
assert_eq!(resolve(&dests[3]).as_array().unwrap()[0].as_reference().unwrap(), pages[&1]);
let ap = resolve(root.get(b"AP").unwrap()).as_dict().unwrap()
.get(b"Names").unwrap().as_array().unwrap();
assert_eq!(resolve(&ap[1]).as_dict().unwrap().get(b"Proof").unwrap().as_str().unwrap(), b"shared");
assert!(output.diagnostics.iter().any(|message| message.contains("Object @mixed already defined.")));
}