Advance native LaTeX execution and preview
This commit is contained in:
1 parent
91f9d76b16
commit
2741546ca8
414 files changed
+99872
-933
No files matched your search
@@ -0,0 +1,422 @@
|
||||
//! Owned native alignment execution, independently checked against pdfTeX.
|
||||
#[path = "support/font_world.rs"]
|
||||
mod font_world;
|
||||
use std::{
|
||||
fs,
|
||||
process::Command,
|
||||
sync::atomic::{AtomicU64, Ordering},
|
||||
};
|
||||
use texrra_tex_core::execution::*;
|
||||
|
||||
const SETUP: &str = r"\catcode123=1\catcode125=2\catcode35=6\catcode38=4
|
||||
\font\f=cmr10\f\hsize=100pt\vsize=100pt\baselineskip=12pt
|
||||
\boxmaxdepth=100pt\hbadness=10000\vbadness=10000\newlinechar=-1 ";
|
||||
struct Host;
|
||||
impl SourceProvider for Host {
|
||||
font_world::font_resources!();
|
||||
fn read(&mut self, name: &str, _: &str) -> Result<SourceFile, String> {
|
||||
Err(format!("unexpected input {name}"))
|
||||
}
|
||||
}
|
||||
fn markers(text: &str) -> Vec<String> {
|
||||
let mut out = Vec::new();
|
||||
let mut rest = text;
|
||||
while let Some(i) = rest.find("ALIGN[") {
|
||||
rest = &rest[i..];
|
||||
let end = rest.find(';').expect("complete alignment audit") + 1;
|
||||
out.push(rest[..end].to_owned());
|
||||
rest = &rest[end..];
|
||||
}
|
||||
out
|
||||
}
|
||||
fn compare(body: &str) -> Execution {
|
||||
static ID: AtomicU64 = AtomicU64::new(0);
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"texrra-align-{}-{}",
|
||||
std::process::id(),
|
||||
ID.fetch_add(1, Ordering::Relaxed)
|
||||
));
|
||||
fs::create_dir_all(&dir).unwrap();
|
||||
let source = format!(
|
||||
"{SETUP}{body}\\message{{ALIGN[PACK]=\\number\\wd0,\\number\\ht0,\\number\\dp0,\\the\\badness;}}\\showboxbreadth=1000\\showboxdepth=30\\showbox0\\end\n"
|
||||
);
|
||||
fs::write(dir.join("probe.tex"), &source).unwrap();
|
||||
// Preserve source and reference before running native, so failures retain
|
||||
// both sides instead of preventing the independent oracle from executing.
|
||||
let reference = Command::new("pdftex")
|
||||
.args(["-ini", "-etex", "-interaction=nonstopmode", "probe.tex"])
|
||||
.current_dir(&dir)
|
||||
.output()
|
||||
.unwrap();
|
||||
let log = fs::read_to_string(dir.join("probe.log")).unwrap();
|
||||
assert_eq!(reference.status.code(), Some(1), "{dir:?}: {body}\n{log}");
|
||||
for line in log.lines().filter(|line| line.starts_with("! ")) {
|
||||
assert_eq!(line, "! OK.", "{dir:?}: {body}\n{log}");
|
||||
}
|
||||
let e = Executor::new(
|
||||
State::initex().with_etex(),
|
||||
Limits {
|
||||
steps: 2_000_000,
|
||||
..Limits::default()
|
||||
},
|
||||
)
|
||||
.run(
|
||||
SourceFile {
|
||||
name: "probe.tex".into(),
|
||||
data: source.into_bytes(),
|
||||
},
|
||||
&mut Host,
|
||||
)
|
||||
.unwrap_or_else(|error| panic!("{dir:?}: {body}: {error}"));
|
||||
let actual: Vec<_> = e
|
||||
.events
|
||||
.iter()
|
||||
.filter_map(|event| match event {
|
||||
Event::Message { text, .. } => Some(markers(text)),
|
||||
_ => None,
|
||||
})
|
||||
.flatten()
|
||||
.collect();
|
||||
assert_eq!(actual, markers(&log), "{dir:?}: {body}");
|
||||
fs::write(dir.join("native.json"), serde_json::to_vec(&e).unwrap()).unwrap();
|
||||
let reports: Vec<_> = e
|
||||
.events
|
||||
.iter()
|
||||
.filter_map(|event| match event {
|
||||
Event::ShowBox(r) => Some(r),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(reports.len(), 1);
|
||||
assert!(
|
||||
log.contains(&reports[0].text),
|
||||
"{dir:?}: {body}\nnative:\n{}\nreference:\n{log}",
|
||||
reports[0].text
|
||||
);
|
||||
e
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn horizontal_alignment_columns_spans_skips_and_pack_specs_match_pdftex() {
|
||||
for spec in ["", "to80pt", "spread10pt", "to5pt"] {
|
||||
for body in [
|
||||
r"#\hfil&\hskip10pt#\hfil\cr A&B\cr CCC&D\cr",
|
||||
r"\hfil#\hfil&#\cr A&BBB\cr CC&D\cr",
|
||||
r"#&#&#\cr A&B&C\cr \omit Long\span\omit material&D\cr",
|
||||
r"#&#&#\cr \omit Wide\span\omit cell\span\omit text\cr",
|
||||
r"#&&\hfil#\cr A&B&C&D\cr AA&BB\cr",
|
||||
r"\tabskip=2pt plus1fil#\tabskip=3pt plus2fil&#\tabskip=4pt plus1fil\cr A&B\cr C&D\cr",
|
||||
r"#&#&#\cr A\cr B&C\cr D&E&F\cr",
|
||||
] {
|
||||
compare(&format!(
|
||||
r"\tabskip=1pt plus2pt minus1pt\setbox0=\vbox{{\halign {spec}{{{body}}}}}"
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn templates_execute_macros_groups_assignments_hooks_and_nested_lists() {
|
||||
for body in [
|
||||
r"\def\id#1{#1}\setbox0=\vbox{\halign{\id#\hfil&\hbox{#}\cr A&BC\cr DEF&G\cr}}",
|
||||
r"\let\bgroup={\let\egroup=}\setbox0=\vbox{\halign{\hbox\bgroup#\egroup&#\cr A&B\cr C&D\cr}}",
|
||||
r"\count0=7\everycr={\noalign{\message{ALIGN[HOOK]=\the\currentgrouptype,\the\count0;}}}\setbox0=\vbox{\halign{#&#\cr \count0=9\message{ALIGN[CELL]=\the\currentgrouptype,\the\count0;}A&B\cr C&D\cr}}\message{ALIGN[OUT]=\the\currentgrouptype,\the\count0;}",
|
||||
r"\def\p{##}\setbox0=\vbox{\halign{\span\p\hfil&#\cr A&B\cr C&D\cr}}",
|
||||
r"\setbox0=\vbox{\halign{#&#\cr A&B\cr\noalign{\vskip3pt\hrule height1pt\vskip2pt}C&D\cr\crcr}}",
|
||||
r"\setbox0=\vbox{\halign{#&#\cr \vbox{\halign{#&#\cr A&B\cr C&D\cr}}&E\cr F&G\cr}}",
|
||||
r"\setbox0=\vbox{\hbox{Before}\halign{#&#\cr A&B\cr C&D\cr}\hbox{After}}",
|
||||
r"\setbox0=\vbox{\halign{#&#\cr A\mark{first}&B\mark{second}\cr C&D\cr}}",
|
||||
r"\setbox0=\vbox{\halign{#&&#&#\cr A&B&C&D&E&F&G\cr ABC&DE&FG\cr}}",
|
||||
r"\setbox0=\vbox{\halign{#&#\cr \hbox{A}\span B\cr C&D\cr}}",
|
||||
r"\tabskip=2pt\setbox0=\vbox{\halign{\global\tabskip=3pt\count1=1#&#\cr A&B\cr}}\message{ALIGN[SKIP]=\the\tabskip,\the\count1;}",
|
||||
r"\def\row{A&B\cr}\def\cell{\omit}\setbox0=\vbox{\halign{#\hfil&#\cr\row\cell C&D\cr}}",
|
||||
r"\setbox0=\vbox{\halign{#&#\cr {\def\amp{\char38}\amp}&B\cr C&D\cr}}",
|
||||
r"\everycr={\noalign{\penalty17}}\setbox0=\vbox{\halign{#&#\cr A&B\cr C&D\cr}}",
|
||||
] {
|
||||
compare(body);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn vertical_alignment_uses_vertical_cells_and_horizontal_rows() {
|
||||
for spec in ["", "to40pt", "spread5pt"] {
|
||||
for body in [
|
||||
r"#\vfil&\vskip3pt#\cr\hbox{A}&\hbox{B}\cr\hbox{CCC}&\hbox{g}\cr",
|
||||
r"#&#&#\cr\hbox{Ag}\span\hbox{B}&\hbox{C}\cr\hbox{D}&\hbox{E}&\hbox{F}\cr",
|
||||
] {
|
||||
compare(&format!(
|
||||
r"\tabskip=1pt plus1fil\setbox0=\hbox{{\valign {spec}{{{body}}}}}"
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn unset_rows_own_real_cells_and_glyph_geometry() {
|
||||
let e = compare(r"\setbox0=\vbox{\halign{#\hfil&#\hfil\cr A&BB\cr CCC&D\cr}}");
|
||||
let b = e.state.box_register(0).unwrap();
|
||||
let BoxContent::Vertical(v) = &b.content else {
|
||||
panic!("owned vertical list")
|
||||
};
|
||||
let rows: Vec<_> = v
|
||||
.iter()
|
||||
.filter_map(|n| match n {
|
||||
NativeVerticalNode::Box(b) => Some(b),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(rows.len(), 2);
|
||||
assert_eq!(rows[0].width, rows[1].width);
|
||||
for row in rows {
|
||||
let BoxContent::Horizontal(h) = &row.content else {
|
||||
panic!("owned row")
|
||||
};
|
||||
let cells: Vec<_> = h
|
||||
.iter()
|
||||
.filter_map(|n| match n {
|
||||
TextNode::Box(b) => Some(b.native_box.as_ref().unwrap()),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(cells.len(), 2);
|
||||
assert_eq!(cells[0].height, row.height);
|
||||
assert!(!row.geometry().unwrap().items.is_empty());
|
||||
}
|
||||
let roundtrip: Execution = serde_json::from_slice(&serde_json::to_vec(&e).unwrap()).unwrap();
|
||||
assert_eq!(e, roundtrip);
|
||||
assert!(e.reads.contains(&Dependency::Parameter(Parameter::Tabskip)));
|
||||
assert!(
|
||||
e.reads
|
||||
.contains(&Dependency::TokenParameter(TokenParameter::Everycr))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alignment_shipouts_match_independent_dvi_glyphs_and_rules() {
|
||||
use texrra_tex_core::math_engine_tex::MathItem;
|
||||
for (case,body) in [
|
||||
r"\vbox{\halign{#\hfil&\hskip10pt#\hfil\cr A&B\cr CCC&D\cr}}",
|
||||
r"\vbox{\tabskip=1pt plus1fil\halign to80pt{#&#&#\cr A&B&C\cr \omit Long\span\omit material&D\cr}}",
|
||||
r"\vbox{\halign{\vrule#&#\vrule\cr A&B\cr\noalign{\hrule height1pt}g&D\cr}}",
|
||||
r"\hbox{\tabskip=1pt plus1fil\valign to40pt{#\vfil&#\cr\hbox{Ag}&\hbox{B}\cr\hbox{CC}&\hbox{D}\cr}}",
|
||||
r"\vbox{\halign{#&#\cr\vbox{\halign{#&#\cr A&B\cr C&D\cr}}&E\cr F&G\cr}}",
|
||||
].iter().enumerate() {
|
||||
let dir=std::env::temp_dir().join(format!("texrra-align-dvi-{}-{case}",std::process::id()));
|
||||
fs::create_dir_all(&dir).unwrap();
|
||||
let source=format!("{SETUP}\\pdfoutput=0\\setbox0={body}\\setbox1=\\hbox{{X\\box0}}\\shipout\\copy1\\end\n");
|
||||
fs::write(dir.join("probe.tex"),&source).unwrap();
|
||||
let output=Command::new("pdftex").args(["-ini","-etex","-interaction=nonstopmode","-halt-on-error","probe.tex"]).current_dir(&dir).output().unwrap();
|
||||
assert!(output.status.success(),"{dir:?}: {}",String::from_utf8_lossy(&output.stdout));
|
||||
let output=Command::new("python3").arg(std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../scripts/native-math-dvi-oracle.py")).arg(dir.join("probe.dvi")).output().unwrap();
|
||||
assert!(output.status.success(),"{}",String::from_utf8_lossy(&output.stderr));
|
||||
fs::write(dir.join("reference.json"),&output.stdout).unwrap();
|
||||
let reference:serde_json::Value=serde_json::from_slice(&output.stdout).unwrap();
|
||||
let execution=Executor::new(State::initex().with_etex(),Limits::default()).with_native_pages().run(SourceFile{name:"probe.tex".into(),data:source.into_bytes()},&mut Host).unwrap();
|
||||
fs::write(dir.join("native.json"),serde_json::to_vec(&execution).unwrap()).unwrap();
|
||||
let ships:Vec<_>=execution.events.iter().filter_map(|e|match e{Event::Shipout(s)=>Some(s),_=>None}).collect();
|
||||
assert_eq!(ships.len(),1);
|
||||
let page=&reference[0];
|
||||
let anchor=&page["glyphs"][0];
|
||||
assert_eq!(anchor["slot"],88);
|
||||
let (ox,oy)=(anchor["x"].as_i64().unwrap(),anchor["y"].as_i64().unwrap());
|
||||
let mut expected:Vec<_>=page["glyphs"].as_array().unwrap().iter().map(|v| (v["font"].as_str().unwrap().to_owned(),v["size"].as_i64().unwrap(),v["slot"].as_u64().unwrap() as u8,v["x"].as_i64().unwrap()-ox,v["y"].as_i64().unwrap()-oy)).collect();
|
||||
let mut rules:Vec<_>=page["rules"].as_array().unwrap().iter().map(|v|(v["x"].as_i64().unwrap()-ox,v["y"].as_i64().unwrap()-oy,v["width"].as_i64().unwrap(),v["height"].as_i64().unwrap())).collect();
|
||||
let (mut actual,mut actual_rules)=(Vec::new(),Vec::new());
|
||||
for item in ships[0].box_node.geometry().unwrap().items {
|
||||
match item.item {
|
||||
MathItem::NativeGlyph{font,slot,size,..}=>actual.push((font.name.clone(),size,slot,item.x,item.y)),
|
||||
MathItem::Rule{width,height}=>actual_rules.push((item.x,item.y,width,height)),
|
||||
_=>panic!("non-native material"),
|
||||
}
|
||||
}
|
||||
actual.sort();expected.sort();rules.sort();actual_rules.sort();
|
||||
assert_eq!(actual.len(),expected.len(),"{dir:?}");
|
||||
for (a,b) in actual.iter().zip(&expected) {
|
||||
assert_eq!((&a.0,a.1,a.2),(&b.0,b.1,b.2),"{dir:?}");
|
||||
assert!((a.3-b.3).abs()<=1 && (a.4-b.4).abs()<=1,"{dir:?}: {a:?} != {b:?}");
|
||||
}
|
||||
assert_eq!(actual_rules.len(),rules.len(),"{dir:?}");
|
||||
for (a,b) in actual_rules.iter().zip(&rules) {
|
||||
assert!((a.0-b.0).abs()<=1 && (a.1-b.1).abs()<=1 && (a.2-b.2).abs()<=1 && (a.3-b.3).abs()<=1,"{dir:?}: {a:?} != {b:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn misplaced_and_unbalanced_alignments_fail_explicitly() {
|
||||
for body in [
|
||||
r"\cr",
|
||||
r"\span",
|
||||
r"\omit",
|
||||
r"\noalign{}",
|
||||
r"\halign{A\cr}",
|
||||
r"\halign{##\cr}",
|
||||
r"\halign{#\cr A&B\cr}",
|
||||
r"\halign{#\cr A",
|
||||
r"\hbox{\halign{#\cr A\cr}}",
|
||||
r"\halign{#\cr {A\cr}",
|
||||
r"\halign{#\cr\end}",
|
||||
r"\everycr={\message{BAD}}\halign{#\cr A\cr}",
|
||||
] {
|
||||
let source = format!("{SETUP}{body}\\end");
|
||||
let result = Executor::new(State::initex().with_etex(), Limits::default()).run(
|
||||
SourceFile {
|
||||
name: "bad.tex".into(),
|
||||
data: source.into_bytes(),
|
||||
},
|
||||
&mut Host,
|
||||
);
|
||||
assert!(result.is_err(), "{body}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_templates_and_spans_obey_actual_capacity_limits() {
|
||||
let prefix = r"\relax ".repeat(20);
|
||||
let repeated = format!("\\halign{{{prefix}#&&{prefix}#\\cr A&B&C&D\\cr}}");
|
||||
let cases = [
|
||||
(repeated, 64usize, "template capacity"),
|
||||
(
|
||||
format!("\\halign{{#&&#\\cr {}A\\cr}}", r"A\span ".repeat(256)),
|
||||
1_000_000,
|
||||
"span exceeds 256",
|
||||
),
|
||||
];
|
||||
for (body, tokens, expected) in cases {
|
||||
let source = format!("{SETUP}{body}\\end");
|
||||
let error = Executor::new(
|
||||
State::initex().with_etex(),
|
||||
Limits {
|
||||
steps: 1_000_000,
|
||||
tokens,
|
||||
..Limits::default()
|
||||
},
|
||||
)
|
||||
.run(
|
||||
SourceFile {
|
||||
name: "capacity.tex".into(),
|
||||
data: source.into_bytes(),
|
||||
},
|
||||
&mut Host,
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(error.message.contains(expected), "{error}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alignment_cancellation_stops_before_later_cell_effects() {
|
||||
struct Stop {
|
||||
cancellation: Cancellation,
|
||||
messages: Vec<String>,
|
||||
}
|
||||
impl SourceProvider for Stop {
|
||||
font_world::font_resources!();
|
||||
fn read(&mut self, _: &str, _: &str) -> Result<SourceFile, String> {
|
||||
Err("no files".into())
|
||||
}
|
||||
fn observe_message(&mut self, text: &str) -> Result<(), String> {
|
||||
self.messages.push(text.into());
|
||||
self.cancellation.cancel();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
let cancellation = Cancellation::default();
|
||||
let mut host = Stop {
|
||||
cancellation: cancellation.clone(),
|
||||
messages: Vec::new(),
|
||||
};
|
||||
let source = format!(r"{SETUP}\halign{{#&#\cr\message{{FIRST}}A&\message{{SECOND}}B\cr}}\end");
|
||||
let error = Executor::new(State::initex().with_etex(), Limits::default())
|
||||
.with_cancellation(cancellation)
|
||||
.run(
|
||||
SourceFile {
|
||||
name: "cancel.tex".into(),
|
||||
data: source.into_bytes(),
|
||||
},
|
||||
&mut host,
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(error.message.contains("compilation cancelled"), "{error}");
|
||||
assert_eq!(host.messages, ["FIRST"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dumped_alignment_meanings_parameters_and_templates_execute_in_a_new_job() {
|
||||
let seed = format!(
|
||||
r"{SETUP}\tabskip=2pt plus1fil\everycr{{\noalign{{\penalty19}}}}\def\row{{A&B\cr}}\dump"
|
||||
);
|
||||
let state = State::initex().with_etex();
|
||||
let dump = Executor::new(state.clone(), Limits::default())
|
||||
.run(
|
||||
SourceFile {
|
||||
name: "seed.tex".into(),
|
||||
data: seed.as_bytes().to_vec(),
|
||||
},
|
||||
&mut Host,
|
||||
)
|
||||
.unwrap();
|
||||
let bytes = FormatImage::from_dump(dump, Vec::new(), Vec::new())
|
||||
.unwrap()
|
||||
.encode()
|
||||
.unwrap();
|
||||
let executor = FormatImage::decode(&bytes)
|
||||
.unwrap()
|
||||
.new_job(
|
||||
state.profile(),
|
||||
state.input_profile(),
|
||||
JobTime::new(2026, 11, 17, 0).unwrap(),
|
||||
Limits::default(),
|
||||
|_| Ok(seed.as_bytes().to_vec()),
|
||||
)
|
||||
.unwrap();
|
||||
let source = r"\setbox0=\vbox{\halign to60pt{#&#\cr\row C&D\cr}}\end";
|
||||
let loaded = executor
|
||||
.run(
|
||||
SourceFile {
|
||||
name: "document.tex".into(),
|
||||
data: source.as_bytes().to_vec(),
|
||||
},
|
||||
&mut Host,
|
||||
)
|
||||
.unwrap();
|
||||
let bootstrap = Executor::new(state, Limits::default())
|
||||
.run(
|
||||
SourceFile {
|
||||
name: "seed.tex".into(),
|
||||
data: seed.into_bytes(),
|
||||
},
|
||||
&mut Host,
|
||||
)
|
||||
.unwrap();
|
||||
let fresh = Executor::from_execution(bootstrap, Limits::default())
|
||||
.run(
|
||||
SourceFile {
|
||||
name: "document.tex".into(),
|
||||
data: source.as_bytes().to_vec(),
|
||||
},
|
||||
&mut Host,
|
||||
)
|
||||
.unwrap();
|
||||
let a = loaded.state.box_register(0).unwrap();
|
||||
let b = fresh.state.box_register(0).unwrap();
|
||||
assert_eq!((a.width, a.height, a.depth), (b.width, b.height, b.depth));
|
||||
assert!(
|
||||
a.geometry().unwrap() == b.geometry().unwrap(),
|
||||
"dump/load changed glyph geometry, font identity or source origins"
|
||||
);
|
||||
assert_eq!(
|
||||
loaded.state.parameter(Parameter::Tabskip),
|
||||
fresh.state.parameter(Parameter::Tabskip)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn literal_character_constants_and_brace_control_sequences_preserve_alignment_depth() {
|
||||
for body in [
|
||||
r"\setbox0=\vbox{\halign{#&#\cr\count0=`{\message{ALIGN[CHAR]=\the\count0;}A&B\cr C&D\cr}}",
|
||||
r"\setbox0=\vbox{\halign{#&#\cr\count0=`}\message{ALIGN[CHAR]=\the\count0;}A&B\cr C&D\cr}}",
|
||||
r"\let\bgroup={\let\egroup=}\setbox0=\vbox{\halign{#&#\cr\def\open{\bgroup}\hbox\open A\egroup&B\cr C&D\cr}}",
|
||||
r"\def\row{{\ifnum0=`}\fi\ifnum0=`{\fi}\cr}\setbox0=\vbox{\halign{#&#\cr A&B\row C&D\row}}",
|
||||
r"\setbox0=\vbox{\halign{#&#\cr\futurelet\next\relax {A}&B\cr C&D\cr}}",
|
||||
] {
|
||||
compare(body);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user