Capture and reuse XeLaTeX PDF form contents

This commit is contained in:
gloamxun committed 2026-11-28 19:29:07 +08:00
1 parent 009fde04f7
commit c0e2346b05
16 files changed
+706 -53

No files matched your search

@@ -1009,3 +1009,48 @@ fn xdv_scoped_code_uses_shared_resource_categories_and_page_defaults() {
assert_eq!(operations[style + 1].operator, "re");
assert_eq!(operations[style + 2].operator, "f");
}
#[test]
fn xdv_form_capture_isolated_and_reused_as_one_indirect_object() {
use lopdf::Document;
use texrra_tex_core::{pdf::shipout_lists_to_native_pdf_document, render::DisplayItem};
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:bxobj @box width 30pt height 20pt}%
\special{pdf:put @resources << /ExtGState << /A << /ca 0.5 >> >> >>}%
\special{pdf:bcontent}\special{pdf:code q /A gs 0 0 10 10 re f Q}%
\special{pdf:econtent}%
\hbox to0pt{\vrule width5pt height7pt\hss}%
\special{pdf:exobj << /Group << /S /Transparency >> >>}%
\special{pdf:put @catalog << /Captured @box >>}%
\special{pdf:uxobj @box}}
\shipout\hbox{\kern20pt\special{pdf:usexobj @box}}
\end"),
&mut TestDevice,
)
.unwrap();
let pages = result.shipout_display_lists().unwrap();
assert_eq!(pages[0].forms.len(), 1);
assert!(pages[0].forms[0].list.items.iter().any(|item| matches!(item, DisplayItem::Rule { .. })));
assert!(!pages[0].items.iter().any(|item| matches!(item, DisplayItem::Rule { .. })));
let output = shipout_lists_to_native_pdf_document(&pages, result.state.pdf_final_options()).unwrap();
let pdf = Document::load_mem(&output.bytes).unwrap();
let object = |page| pdf.get_object(page).unwrap().as_dict().unwrap()
.get(b"Resources").unwrap().as_dict().unwrap()
.get(b"XObject").unwrap().as_dict().unwrap()
.get(b"Fm1").unwrap().as_reference().unwrap();
let ids = pdf.get_pages();
let form = object(ids[&1]);
assert_eq!(object(ids[&2]), form);
assert_eq!(pdf.catalog().unwrap().get(b"Captured").unwrap().as_reference().unwrap(), form);
let stream = pdf.get_object(form).unwrap().as_stream().unwrap();
assert_eq!(stream.dict.get(b"Subtype").unwrap().as_name().unwrap(), b"Form");
let resources = stream.dict.get(b"Resources").unwrap().as_dict().unwrap();
assert!(resources.get(b"ExtGState").unwrap().as_dict().unwrap().has(b"A"));
assert!(stream.dict.get(b"Group").unwrap().as_dict().unwrap().has(b"S"));
}