Fit and clip captured XeLaTeX forms in their device coordinates

This commit is contained in:
gloamxun committed 2026-11-28 19:47:20 +08:00
1 parent c0e2346b05
commit 5ad5c40002
5 files changed
+303 -98

No files matched your search

@@ -1054,3 +1054,69 @@ fn xdv_form_capture_isolated_and_reused_as_one_indirect_object() {
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"));
}
#[test]
fn xdv_form_placement_respects_definition_order_and_crop() {
use lopdf::{Document, Object, content::Content};
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:uxobj @inner bbox 2 1 12 11 width 30bp height 20bp}%
\special{pdf:bxobj @outer bbox 0 -4 80 35}%
\special{pdf:bxobj @inner width 20bp height 12bp depth 2bp}%
\hbox to0pt{\vrule width19pt height11pt\hfil}%
\special{pdf:exobj}%
\kern8pt\special{pdf:uxobj @inner width 35bp height 18bp depth 2bp}%
\special{pdf:exobj}%
\special{pdf:uxobj @outer}}
\shipout\hbox{%
\special{pdf:uxobj @inner bbox 2 1 12 11 width 30bp height 20bp}%
\kern40pt\special{pdf:uxobj @inner bbox 2 1 12 11 matrix 1 .25 -.2 1 4 3 clip 0}}
\shipout\hbox{%
\kern20pt\special{pdf:bcontent}%
\kern10pt\special{pdf:bxobj @local width 40bp height 20bp depth 2bp}%
\special{pdf:code 0 0 1 rg 12 1 7 9 re f}%
\hbox to0pt{\vrule width30pt height7pt\hss}%
\special{pdf:exobj}%
\kern75pt\special{pdf:uxobj @local width 50bp height 23bp depth 2bp}%
\special{pdf:econtent}}
\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 ids = pdf.get_pages();
let page = |id| Content::decode(&pdf.get_page_content(id).unwrap()).unwrap().operations;
let first = page(ids[&1]);
let second = page(ids[&2]);
let numeric = |values: &[Object]| -> Vec<f64> {
values.iter().map(|value| value.as_i64().map(|n| n as f64).or_else(|_| value.as_f64()).unwrap()).collect()
};
let matrices = |operations: &[lopdf::content::Operation]| -> Vec<Vec<f64>> {
operations.iter().filter(|op| op.operator == "cm")
.map(|op| numeric(&op.operands)).collect()
};
assert_eq!(&matrices(&first)[1][..4], &[1.0, 0.0, 0.0, 1.0]);
assert_eq!(numeric(&first.iter().find(|op| op.operator == "re").unwrap().operands),
[2.0, 1.0, 10.0, 10.0]);
assert_eq!(&matrices(&second)[1][..4], &[3.0, 0.0, 0.0, 2.0]);
assert_eq!(&matrices(&second)[2][..4], &[1.0, 0.25, -0.2, 1.0]);
assert_eq!(second.iter().filter(|op| op.operator == "W").count(), 1);
let local = page(ids[&3]);
assert_eq!(matrices(&local)[2], [1.25, 0.0, 0.0, 1.13636, 84.682, -2.0]);
let outer = pdf.objects.values().filter_map(|value| value.as_stream().ok()).find(|stream| {
stream.dict.get(b"Resources").and_then(Object::as_dict)
.and_then(|resources| resources.get(b"XObject")).is_ok()
}).unwrap();
let contents = outer.decompressed_content().unwrap();
let operations = Content::decode(&contents).unwrap().operations;
assert_eq!(&matrices(&operations)[0][..4], &[1.75, 0.0, 0.0, 0.80132]);
}