Promote verified native PDF and box capacity support

This commit is contained in:
gloamxun committed 2026-11-19 14:39:39 +08:00
1 parent 3706afa483
commit 72ed779749
31 files changed
+1042 -78

No files matched your search

@@ -34,6 +34,101 @@ fn messages(e: &Execution) -> Vec<String> {
})
.collect()
}
#[test]
fn transferred_nodes_can_be_repacked_without_spending_the_live_box_capacity() {
// Match the captured INITEX shape of the CVPR box-capacity reproducer.
let material = r"\kern1sp".repeat(500);
let source = format!(
r"\catcode123=1 \catcode125=2
\setbox2=\vbox{{\hrule width2pt height2pt {material}}}
\def\repack{{\ifnum\count0<2050 \advance\count0 by1
\setbox2=\vbox{{\unvbox2}}\expandafter\repack\fi}}
\repack
\message{{REPACKED=\number\count0|BOX=\number\wd2,\number\ht2,\number\dp2}}
\shipout\box2 \end"
);
let execution = Executor::new(
State::initex().with_etex(),
Limits {
steps: 2_000_000,
..Limits::default()
},
)
.with_native_pages()
.run(
SourceFile {
name: "repack.tex".into(),
data: source.into_bytes(),
},
&mut NoFiles,
)
.unwrap();
assert_eq!(messages(&execution), ["REPACKED=2050|BOX=131072,131572,0"]);
let pages: Vec<_> = execution
.events
.iter()
.filter_map(|event| match event {
Event::Shipout(page) => Some(page),
_ => None,
})
.collect();
assert_eq!(pages.len(), 1);
assert_eq!(pages[0].box_node.node_count(), 502);
assert!(execution.state.box_register(2).is_none());
}
#[test]
fn box_capacity_checks_live_copies_retained_pages_and_single_boxes() {
let material = r"\kern1sp".repeat(40);
let run = |body: &str| {
Executor::new(
State::initex().with_etex(),
Limits {
tokens: 64,
..Limits::default()
},
)
.with_native_pages()
.run_detailed(
SourceFile {
name: "box-capacity.tex".into(),
data: format!(r"\catcode123=1 \catcode125=2 {body}\end").into_bytes(),
},
&mut NoFiles,
)
};
let over = [
(format!(r"\setbox0=\vbox{{{material}{material}}}"), 0),
(format!(r"\setbox0=\vbox{{{material}}}\setbox1=\copy0"), 0),
(
format!(r"\shipout\vbox{{{material}}}\shipout\vbox{{{material}}}"),
1,
),
];
for (body, retained) in over {
let failed = run(&body).unwrap_err();
assert!(
failed
.error
.message
.contains("native box node capacity exceeded"),
"{}",
failed.error
);
let published = failed
.evidence
.events
.iter()
.filter(|event| matches!(event, Event::Shipout(_)))
.count();
assert_eq!(published, retained);
}
let body = format!(r"\setbox0=\vbox{{{material}}}\setbox0=\vbox{{{material}}}");
let final_box = run(&body).unwrap().state.box_register(0).unwrap().clone();
assert_eq!(final_box.node_count(), 41);
}
#[test]
fn box_registers_scopes_moves_and_dimensions_preserve_native_contents() {
let e = run(
@@ -148,6 +148,72 @@ fn actual_font_slot_bitmaps_and_identifiers_match_real_initex() {
);
fs::remove_dir_all(dir).unwrap();
}
#[test]
fn assigning_math_families_scans_actual_current_and_family_font_identifiers() {
let setup = r"\font\regular=cmr10 \font\small=cmr7 \let\current=\font \regular ";
let body = r"
\textfont0=\current \scriptfont1=\textfont0
{\small \textfont0=\font \global\scriptscriptfont2=\textfont0
\message{ASSIGNED-LOCAL=[\fontname\textfont0,\fontname\scriptfont1,\fontname\scriptscriptfont2]}}
\message{ASSIGNED-OUTER=[\fontname\font,\fontname\textfont0,\fontname\scriptfont1,\fontname\scriptscriptfont2]}
\textfont3=\scriptscriptfont2
\message{ASSIGNED-COPIED=[\fontname\textfont3]}
";
let native = execute(setup, body, &mut Host::default()).unwrap();
let observed = messages(&native);
assert_eq!(
observed,
[
"ASSIGNED-LOCAL=[cmr7,cmr10,cmr7]",
"ASSIGNED-OUTER=[cmr10,cmr10,cmr10,cmr7]",
"ASSIGNED-COPIED=[cmr7]",
]
);
assert!(native.reads.contains(&Dependency::TextFont));
assert!(
native
.reads
.contains(&Dependency::MathFamily { size: 0, family: 0 })
);
let work = directory("math-family-identifiers");
if let Some((status, _, log)) = reference(&work, setup, body) {
assert_eq!(status, 0, "{log}");
for message in &observed {
assert!(log.contains(message), "{message}\n{log}");
}
let xetex = Command::new("xetex")
.args([
"-ini",
"-etex",
"-interaction=nonstopmode",
"-halt-on-error",
"probe.tex",
])
.current_dir(&work)
.output();
match xetex {
Ok(run) => {
assert!(
run.status.success(),
"{}",
String::from_utf8_lossy(&run.stdout)
);
let log = fs::read_to_string(work.join("probe.log")).unwrap();
for message in &observed {
assert!(log.contains(message), "{message}\n{log}");
}
}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
eprintln!("xetex absent: math family XeTeX oracle skipped");
}
Err(error) => panic!("{error}"),
}
}
let invalid = execute(setup, r"\textfont2=\relax", &mut Host::default()).unwrap_err();
assert!(invalid.message.contains("expected native font identifier"));
fs::remove_dir_all(work).unwrap();
}
#[test]
fn tracing_channels_fatal_errors_and_conversion_timing_match_real_initex() {
let dir = directory("diagnostics");
@@ -246,7 +246,7 @@ fn genuine_fraction_primitives_and_retained_boxes_survive_format_roundtrip() {
let envelope: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
let payload: serde_json::Value =
serde_json::from_str(envelope["payload"].as_str().unwrap()).unwrap();
assert_eq!(payload["schema"], 23);
assert_eq!(payload["schema"], 25);
let state = State::initex().with_etex();
let loaded = FormatImage::decode(&bytes)
.unwrap()
@@ -27,7 +27,13 @@ fn run_native(source: &str) -> Result<Execution, Error> {
Executor::new(
State::initex()
.with_etex()
.with_input_profile(InputProfile::TeX8Bit),
.with_input_profile(InputProfile::TeX8Bit)
.with_job_time(
JobTime::new(2000, 1, 1, 0)
.unwrap()
.with_pdf_clock(0, 0)
.unwrap(),
),
Limits::default(),
)
.with_native_pages()
@@ -66,6 +72,9 @@ fn pair(body: &str) -> (Document, Document, Vec<String>) {
"-halt-on-error",
"catalog.tex",
])
.env("TZ", "UTC")
.env("SOURCE_DATE_EPOCH", "946684800")
.env("FORCE_SOURCE_DATE", "1")
.current_dir(&work)
.output()
.unwrap();
@@ -222,10 +231,11 @@ fn absent_targets_follow_reference_destination_and_xref_behavior() {
}
#[test]
fn format_images_retain_catalog_primitive_and_clear_job_catalog_data() {
let seed = format!("{SETUP}\\pdfcatalog{{/Lang (seed)}}\\dump\n");
fn format_images_retain_dictionary_primitives_and_clear_job_data() {
let seed = format!("{SETUP}\\pdfcatalog{{/Lang (seed)}}\\pdfinfo{{/Author (seed)}}\\dump\n");
let dump = run_native(&seed).unwrap();
assert!(dump.state.pdf_final_options().catalog.is_some());
assert!(!dump.state.pdf_final_options().info.entries.is_empty());
let image = FormatImage::from_dump(dump, Vec::new(), Vec::new())
.unwrap()
.encode()
@@ -244,7 +254,10 @@ fn format_images_retain_catalog_primitive_and_clear_job_catalog_data() {
.run(
SourceFile {
name: "document.tex".into(),
data: format!("\\pdfcatalog{{/PageLayout /SinglePage}}{PAGE}\\end").into_bytes(),
data: format!(
"\\pdfcatalog{{/PageLayout /SinglePage}}\\pdfinfo{{/Title (job)}}{PAGE}\\end"
)
.into_bytes(),
},
&mut RulesOnly,
)
@@ -256,6 +269,11 @@ fn format_images_retain_catalog_primitive_and_clear_job_catalog_data() {
catalog.get(b"PageLayout").unwrap().as_name().unwrap(),
b"SinglePage"
);
assert!(information(&doc).get(b"Author").is_err());
assert_eq!(
information(&doc).get(b"Title").unwrap().as_str().unwrap(),
b"job"
);
}
#[test]
@@ -330,3 +348,116 @@ fn rejects_duplicate_actions_and_freezes_file_options_only_for_an_action() {
));
assert!(native.catalog().unwrap().get(b"Lang").is_err());
}
fn information(pdf: &Document) -> &Dictionary {
get(pdf, &pdf.trailer, b"Info").as_dict().unwrap()
}
fn compare_pdf_information(body: &str, custom_producer: bool) -> (Document, Document) {
let (reference, native, _) = pair(body);
let without_engine_identity = |pdf: &Document| {
let mut values = information(pdf).clone();
values.remove(b"PTEX.Fullbanner");
if !custom_producer {
values.remove(b"Producer");
}
comparable_dictionary(&values)
};
assert_eq!(
without_engine_identity(&reference),
without_engine_identity(&native)
);
if !custom_producer {
assert_eq!(
information(&native)
.get(b"Producer")
.unwrap()
.as_str()
.unwrap(),
b"texrra Route-D native"
);
}
(reference, native)
}
#[test]
fn pdf_info_accumulates_expanded_tokens_and_preserves_pdf_bytes() {
let (_, native) = compare_pdf_information(
&format!(
r"\def\text{{first}}\begingroup
\pdfinfo{{/Title (\text) /Custom <FEFF4E2D> }}\endgroup
\def\text{{second}}\pdfinfo{{/Subject (\text) /Author (^^e9)}}{PAGE}"
),
false,
);
let info = information(&native);
assert_eq!(info.get(b"Title").unwrap().as_str().unwrap(), b"first");
assert_eq!(info.get(b"Subject").unwrap().as_str().unwrap(), b"second");
assert_eq!(info.get(b"Author").unwrap().as_str().unwrap(), b"\xe9");
assert_eq!(
info.get(b"Custom").unwrap().as_str().unwrap(),
b"\xfe\xff\x4e\x2d"
);
}
#[test]
fn pdf_info_default_keys_follow_literal_pdftex_detection_and_final_scope() {
compare_pdf_information(
&format!(
r"\pdfinfo{{/Producer (custom) /Creator (custom) /CreationDate (custom) /ModDate (custom) /Trapped /True}}{PAGE}"
),
true,
);
let (_, literal) = compare_pdf_information(
&format!(r"\pdfinfo{{/Title (contains /CreatorSuffix and /Trapped)}}{PAGE}"),
false,
);
for key in [b"Creator".as_slice(), b"Trapped"] {
assert!(information(&literal).get(key).is_err());
}
let (_, omitted) = compare_pdf_information(
&format!(r"{PAGE}\pdfinfoomitdate=-1 \pdfinfo{{/CreationDate (kept)}}"),
false,
);
assert!(information(&omitted).get(b"ModDate").is_err());
assert_eq!(
information(&omitted)
.get(b"CreationDate")
.unwrap()
.as_str()
.unwrap(),
b"kept"
);
let (_, scoped) = compare_pdf_information(
&format!(r"\begingroup\pdfinfoomitdate=7 {PAGE}\endgroup"),
false,
);
assert_eq!(
information(&scoped)
.get(b"ModDate")
.unwrap()
.as_str()
.unwrap(),
b"D:20000101000000Z"
);
}
#[test]
fn pdf_info_ignores_dvi_and_does_not_freeze_pdf_settings() {
let body = format!(r"\pdfoutput=0 \pdfinfo{{/Title (ignored)}}\pdfoutput=1 {PAGE}");
let (_, ignored) = compare_pdf_information(&body, false);
assert!(information(&ignored).get(b"Title").is_err());
let execution = run_native(&format!("{SETUP}{body}\\end")).unwrap();
assert!(execution.events.iter().any(|event| matches!(
event, texrra_tex_core::execution::Event::Message { text, .. }
if text.contains(r"\pdfinfo") && text.contains("DVI mode")
)));
let (reference, native) = compare_pdf_information(
&format!(
r"\pdfinfo{{/Title (object stream)}}\pdfminorversion=5\pdfobjcompresslevel=3\pdfcompresslevel=6 {PAGE}"
),
false,
);
assert_eq!(reference.version, "1.5");
assert_eq!(native.version, reference.version);
}
@@ -334,7 +334,7 @@ fn old_schema_and_missing_unicode_state_are_rejected_even_with_valid_checksums()
let envelope: serde_json::Value = serde_json::from_slice(&encoded).unwrap();
let original: serde_json::Value =
serde_json::from_str(envelope["payload"].as_str().unwrap()).unwrap();
assert_eq!(original["schema"], 23);
assert_eq!(original["schema"], 25);
for (schema, expected) in [
(1, "incompatible native format"),
(2, "incompatible native format"),
@@ -358,7 +358,9 @@ fn old_schema_and_missing_unicode_state_are_rejected_even_with_valid_checksums()
(20, "incompatible native format"),
(21, "incompatible native format"),
(22, "incompatible native format"),
(23, "glyph_unicode"),
(23, "incompatible native format"),
(24, "incompatible native format"),
(25, "glyph_unicode"),
] {
let mut payload = original.clone();
payload["schema"] = schema.into();
@@ -164,7 +164,7 @@ fn new_format_owns_vcenter_and_boxed_math_without_reinjecting_old_meanings() {
let envelope: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
let payload: serde_json::Value =
serde_json::from_str(envelope["payload"].as_str().unwrap()).unwrap();
assert_eq!(payload["schema"], 23);
assert_eq!(payload["schema"], 25);
let executor = FormatImage::decode(&bytes)
.unwrap()
.new_job(