519 lines
19 KiB
Rust
519 lines
19 KiB
Rust
//! Raw TFM bytecode, cursor/stack behavior and actual native box lists compared
|
|
//! one-for-one with an independent real INITEX job. No Type1/Unicode shaping.
|
|
use std::{
|
|
fs,
|
|
path::PathBuf,
|
|
process::Command,
|
|
sync::atomic::{AtomicU64, Ordering},
|
|
};
|
|
use texrra_tex_core::{
|
|
execution::*,
|
|
resources::{FileWorld, LoadedResource, ResourceKind, WorldConfig},
|
|
};
|
|
#[path = "support/tfm_program.rs"]
|
|
mod tfm_program;
|
|
use tfm_program::{Op, font};
|
|
const SETUP: &str = r"\catcode123=1\catcode125=2\catcode35=6\newlinechar=-1\showboxbreadth=1000\showboxdepth=50\tracingonline=1\tracinglostchars=1\hfuzz=100pt\hbadness=10000\baselineskip=12pt\hsize=1000pt\parfillskip=0pt plus1fil ";
|
|
fn directory() -> PathBuf {
|
|
static ID: AtomicU64 = AtomicU64::new(0);
|
|
let p = std::env::temp_dir().join(format!(
|
|
"texrra-ligature-{}-{}",
|
|
std::process::id(),
|
|
ID.fetch_add(1, Ordering::Relaxed)
|
|
));
|
|
fs::create_dir_all(&p).unwrap();
|
|
p
|
|
}
|
|
struct Host(FileWorld);
|
|
impl SourceProvider for Host {
|
|
fn read(&mut self, name: &str, from: &str) -> Result<SourceFile, String> {
|
|
NoFiles.read(name, from)
|
|
}
|
|
fn resource(&mut self, k: ResourceKind, n: &str, f: &str) -> Result<LoadedResource, String> {
|
|
self.0.read(k, n, std::path::Path::new(f))
|
|
}
|
|
}
|
|
fn compare(p: &std::path::Path, source: &str, labels: &[String]) -> Execution {
|
|
fs::write(p.join("probe.tex"), source).unwrap();
|
|
let e = Executor::new(
|
|
State::initex().with_etex(),
|
|
Limits {
|
|
steps: 5_000_000,
|
|
tokens: 16_000_000,
|
|
..Limits::default()
|
|
},
|
|
)
|
|
.run(
|
|
SourceFile {
|
|
name: "probe.tex".into(),
|
|
data: source.as_bytes().to_vec(),
|
|
},
|
|
&mut Host(FileWorld::new(WorldConfig::new(p)).unwrap()),
|
|
)
|
|
.unwrap();
|
|
let r = Command::new("pdftex")
|
|
.args(["-ini", "-etex", "-interaction=nonstopmode", "probe.tex"])
|
|
.current_dir(p)
|
|
.output()
|
|
.unwrap();
|
|
let log = fs::read_to_string(p.join("probe.log")).unwrap();
|
|
assert_eq!(r.status.code(), Some(1), "{log}");
|
|
for line in log.lines().filter(|l| l.starts_with("! ")) {
|
|
assert_eq!(line, "! OK.", "{log}");
|
|
}
|
|
let native: Vec<_> = e
|
|
.events
|
|
.iter()
|
|
.filter_map(|e| {
|
|
if let Event::ShowBox(r) = e {
|
|
Some(r.text.as_str())
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.collect();
|
|
let reference: Vec<_> = log
|
|
.split("> \\box0=")
|
|
.skip(1)
|
|
.map(|s| format!("> \\box0={}", s.split("! OK.").next().unwrap()))
|
|
.collect();
|
|
assert_eq!(native.len(), labels.len());
|
|
assert_eq!(reference.len(), labels.len());
|
|
for ((a, b), label) in native.iter().zip(&reference).zip(labels) {
|
|
assert_eq!(a.trim_end(), b.trim_end(), "{label}; actual job {p:?}");
|
|
}
|
|
let missing = e
|
|
.events
|
|
.iter()
|
|
.filter(|e| matches!(e, Event::MissingGlyph(_)))
|
|
.count();
|
|
assert_eq!(missing, log.matches("Missing character:").count(), "{p:?}");
|
|
eprintln!("{} exact native boxes matched real INITEX", labels.len());
|
|
e
|
|
}
|
|
#[test]
|
|
fn all_operation_bytes_retention_pass_and_real_character_boundaries_match_initex() {
|
|
let p = directory();
|
|
let (mut source, mut labels) = (SETUP.to_owned(), Vec::new());
|
|
for op in 0..128u8 {
|
|
let bytes = font(
|
|
&[
|
|
(Some(b'A'), b'B', Op::Lig(op, b'C')),
|
|
(Some(b'C'), b'B', Op::Lig(0, b'D')),
|
|
(Some(b'A'), b'C', Op::Lig(0, b'E')),
|
|
(Some(b'C'), b'D', Op::Lig(0, b'F')),
|
|
(Some(b'D'), b'D', Op::Kern(12345)),
|
|
(Some(b'E'), b'B', Op::Kern(-23456)),
|
|
(Some(b'A'), b'D', Op::Kern(0)),
|
|
],
|
|
None,
|
|
);
|
|
fs::write(p.join(format!("op{op}.tfm")), bytes).unwrap();
|
|
source.push_str(&format!("\\font\\F=op{op} \\F "));
|
|
for body in [
|
|
"AB",
|
|
"ABD",
|
|
"ABBD",
|
|
"A\\char66D",
|
|
"\\chardef\\B=66 A\\B D",
|
|
"\\def\\B{B}A\\B D",
|
|
"A{}BD",
|
|
"A\\relax BD",
|
|
"AD",
|
|
] {
|
|
source.push_str(&format!("\\setbox0=\\hbox{{{body}}}\\showbox0\n"));
|
|
labels.push(format!("op{op}/{body}"));
|
|
}
|
|
}
|
|
source.push_str("\\end");
|
|
compare(&p, &source, &labels);
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
#[test]
|
|
fn boundaries_missing_raw_characters_noboundary_and_component_markers_match_initex() {
|
|
let p = directory();
|
|
let (mut source, mut labels) = (SETUP.to_owned(), Vec::new());
|
|
fs::write(p.join("plain.tfm"), font(&[], None)).unwrap();
|
|
source.push_str("\\font\\G=plain ");
|
|
for (kind, bchar) in [("false", 255), ("existing", b'Z')] {
|
|
for op in [0, 1, 2, 3, 4, 5, 6, 7, 8, 11] {
|
|
for side in ["left", "right", "both", "kern", "missing-first"] {
|
|
let programs = match side {
|
|
"left" => vec![
|
|
(None, b'A', Op::Lig(op, b'B')),
|
|
(Some(b'B'), b'A', Op::Lig(0, b'C')),
|
|
],
|
|
"right" => vec![
|
|
(Some(b'A'), bchar, Op::Lig(op, b'B')),
|
|
(Some(b'B'), bchar, Op::Lig(0, b'C')),
|
|
],
|
|
"both" => vec![
|
|
(None, b'A', Op::Lig(0, b'B')),
|
|
(Some(b'B'), bchar, Op::Lig(op, b'C')),
|
|
],
|
|
"missing-first" => vec![
|
|
(None, bchar, Op::Lig(op, b'B')),
|
|
(Some(b'B'), bchar, Op::Lig(0, b'C')),
|
|
],
|
|
_ => vec![
|
|
(None, b'A', Op::Kern(0)),
|
|
(Some(b'A'), bchar, Op::Kern(-12345)),
|
|
],
|
|
};
|
|
let name = format!("{kind}{side}{op}");
|
|
fs::write(p.join(format!("{name}.tfm")), font(&programs, Some(bchar))).unwrap();
|
|
source.push_str(&format!("\\font\\F={name} \\F "));
|
|
for body in [
|
|
"A".to_owned(),
|
|
"AA".into(),
|
|
"A\\noboundary".into(),
|
|
"\\noboundary A".into(),
|
|
"A\\noboundary A".into(),
|
|
"\\noboundary\\relax A".into(),
|
|
"A{}A".into(),
|
|
"A\\char255 A".into(),
|
|
"\\char255 A".into(),
|
|
"A\\char90".into(),
|
|
"\\char90 A".into(),
|
|
"\\char255".into(),
|
|
"A".into(),
|
|
"\\char255\\G A".into(),
|
|
"\\char255{\\G A}".into(),
|
|
"\\char255\\hbox{\\G A}B".into(),
|
|
"\\char255\\hbox{}A".into(),
|
|
"\\hbox{\\char255}\\hbox{\\G A}".into(),
|
|
] {
|
|
source.push_str(&format!("\\setbox0=\\hbox{{{body}}}\\showbox0\n"));
|
|
labels.push(format!("{name}/{body}"));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
source.push_str("\\end");
|
|
compare(&p, &source, &labels);
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
#[test]
|
|
fn font_instruction_cycles_consume_actual_execution_budget() {
|
|
let p = directory();
|
|
fs::write(
|
|
p.join("loop.tfm"),
|
|
font(&[(Some(b'A'), b'B', Op::Lig(1, b'A'))], None),
|
|
)
|
|
.unwrap();
|
|
let error = Executor::new(
|
|
State::initex(),
|
|
Limits {
|
|
steps: 500,
|
|
..Limits::default()
|
|
},
|
|
)
|
|
.run(
|
|
SourceFile {
|
|
name: "loop.tex".into(),
|
|
data: br"\catcode123=1\catcode125=2\font\F=loop\setbox0=\hbox{\F AB}\end".to_vec(),
|
|
},
|
|
&mut Host(FileWorld::new(WorldConfig::new(&p)).unwrap()),
|
|
)
|
|
.unwrap_err();
|
|
assert!(
|
|
error.message.contains("font program instruction limit"),
|
|
"{error:?}"
|
|
);
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn independent_multistep_programs_stack_components_kerns_and_restart_match_initex() {
|
|
let p = directory();
|
|
let (mut source, mut labels) = (SETUP.to_owned(), Vec::new());
|
|
let mut random = 0x8a35bd71u32;
|
|
let mut next = || {
|
|
random ^= random << 13;
|
|
random ^= random >> 17;
|
|
random ^= random << 5;
|
|
random
|
|
};
|
|
for index in 0..96 {
|
|
let mut programs = Vec::new();
|
|
let ops = [0, 1, 2, 3, 4, 5, 6, 7, 8, 11, 127];
|
|
for left in std::iter::once(None).chain((b'A'..=b'H').map(Some)) {
|
|
for right in (b'A'..=b'H').chain(std::iter::once(255)) {
|
|
if left.is_none() && right == 255 {
|
|
continue;
|
|
}
|
|
if next() % 3 == 0 {
|
|
continue;
|
|
}
|
|
let high = left
|
|
.unwrap_or(b'A')
|
|
.max(if right == 255 { b'A' } else { right });
|
|
let replacement = high + 1 + (next() % (u32::from(b'Z' - high))) as u8;
|
|
let op = if next() % 4 == 0 {
|
|
Op::Kern((next() % 100000) as i32 - 50000)
|
|
} else {
|
|
Op::Lig(ops[next() as usize % ops.len()], replacement)
|
|
};
|
|
programs.push((left, right, op));
|
|
}
|
|
}
|
|
let name = format!("chain{index}");
|
|
let bytes = font(&programs, Some(255));
|
|
fs::write(p.join(format!("{name}.tfm")), bytes).unwrap();
|
|
source.push_str(&format!("\\font\\F={name} \\F "));
|
|
for body in [
|
|
"ABCDEF",
|
|
"HGFEDCBA",
|
|
"AABBDDAA",
|
|
"AB\\noboundary CDE",
|
|
"\\noboundary ABCDEF",
|
|
"ABCDEFGH\\noboundary",
|
|
"AB\\char255 CD",
|
|
] {
|
|
source.push_str(&format!("\\setbox0=\\hbox{{{body}}}\\showbox0\n"));
|
|
labels.push(format!("{name}/{body}"));
|
|
}
|
|
}
|
|
// A character label has a restart indirection; it is not the direct
|
|
// entry used by the left boundary. The independent real loader validates it.
|
|
let mut bytes = font(
|
|
&[
|
|
(Some(b'A'), b'B', Op::Lig(3, b'C')),
|
|
(Some(b'C'), b'B', Op::Kern(0)),
|
|
],
|
|
None,
|
|
);
|
|
let get = |bytes: &[u8], n: usize| {
|
|
u16::from_be_bytes(bytes[2 * n..2 * n + 2].try_into().unwrap()) as usize
|
|
};
|
|
let lf = get(&bytes, 0);
|
|
let nl = get(&bytes, 8);
|
|
let instruction_start = 24 + 8 + 26 * 4 + 32 * 4;
|
|
for slot in b'A'..=b'Z' {
|
|
let info = 32 + usize::from(slot - b'A') * 4;
|
|
if bytes[info + 2] & 3 == 1 {
|
|
bytes[info + 3] += 3;
|
|
}
|
|
}
|
|
let target = bytes[35];
|
|
bytes[35] = 0;
|
|
let prefix = [129, 0, 0, target, 129, 0, 0, target, 129, 0, 0, target];
|
|
bytes.splice(instruction_start..instruction_start, prefix);
|
|
bytes[0..2].copy_from_slice(&((lf + 3) as u16).to_be_bytes());
|
|
bytes[16..18].copy_from_slice(&((nl + 3) as u16).to_be_bytes());
|
|
fs::write(p.join("restart.tfm"), bytes).unwrap();
|
|
source.push_str("\\font\\F=restart \\F ");
|
|
for body in ["AB", "ABB", "A\\char66"] {
|
|
source.push_str(&format!("\\setbox0=\\hbox{{{body}}}\\showbox0\n"));
|
|
labels.push(format!("restart/{body}"));
|
|
}
|
|
source.push_str("\\end");
|
|
compare(&p, &source, &labels);
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
#[test]
|
|
fn original_components_control_real_explicit_hyphen_breaks_and_noboundary_paragraph_start() {
|
|
let p = directory();
|
|
fs::write(
|
|
p.join("hyph.tfm"),
|
|
font(
|
|
&[
|
|
(Some(b'A'), b'B', Op::Lig(0, b'C')),
|
|
(Some(b'A'), b'D', Op::Lig(3, b'B')),
|
|
(None, b'A', Op::Lig(0, b'E')),
|
|
],
|
|
None,
|
|
),
|
|
)
|
|
.unwrap();
|
|
let mut source = format!("{SETUP}\\font\\F=hyph \\F \\hyphenchar\\F=66 ");
|
|
let mut labels = Vec::new();
|
|
for body in [
|
|
"\\noboundary AB",
|
|
"\\noboundary AD",
|
|
"\\noboundary B",
|
|
"A\\char66",
|
|
"\\noboundary AB C",
|
|
"\\noboundary\\noboundary AB",
|
|
] {
|
|
source.push_str(&format!("\\setbox0=\\vbox{{{body}\\par}}\\showbox0\n"));
|
|
labels.push(body.to_owned());
|
|
}
|
|
source.push_str("\\end");
|
|
let e = compare(&p, &source, &labels);
|
|
assert!(e.events.iter().any(|e|matches!(e,Event::BuiltParagraph(p)if p.nodes.iter().any(|n|matches!(n,TextNode::Discretionary{automatic:false,hyphen_char:None,..})))));
|
|
assert!(e.events.iter().any(|e|matches!(e,Event::BuiltParagraph(p)if p.nodes.iter().any(|n|matches!(n,TextNode::Glyph{slot:b'C',original,origins,ligature:true,..}if original=="AB" && origins.len()==2 && origins[0].end==origins[1].start)))));
|
|
let restored: Execution = serde_json::from_slice(&serde_json::to_vec(&e).unwrap()).unwrap();
|
|
assert_eq!(e, restored);
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn automatic_reconstitution_ripples_keep_pass_boundaries_and_initial_punctuation_match_initex() {
|
|
let p = directory();
|
|
let (mut source, mut labels) = (
|
|
format!(
|
|
"{SETUP}\\pretolerance=-1\\tolerance=10000\\uchyph=1\\lefthyphenmin=1\\righthyphenmin=1\\lccode80=0\\lccode81=0\\hyphenation{{a-b-c-d-e-f-g-h b-c-d-e-f-g-h}}"
|
|
),
|
|
Vec::new(),
|
|
);
|
|
let programs = vec![
|
|
vec![
|
|
(Some(b'A'), b'B', Op::Lig(0, b'I')),
|
|
(Some(b'C'), b'D', Op::Lig(0, b'J')),
|
|
(Some(b'E'), b'F', Op::Lig(0, b'K')),
|
|
(Some(b'B'), b'C', Op::Lig(0, b'L')),
|
|
(Some(b'D'), b'E', Op::Lig(0, b'M')),
|
|
(Some(b'F'), b'G', Op::Lig(0, b'N')),
|
|
],
|
|
vec![
|
|
(Some(b'A'), b'B', Op::Lig(1, b'I')),
|
|
(Some(b'I'), b'B', Op::Lig(0, b'J')),
|
|
(Some(b'C'), b'D', Op::Lig(7, b'K')),
|
|
(Some(b'K'), b'D', Op::Kern(-12345)),
|
|
(Some(b'E'), b'F', Op::Lig(2, b'M')),
|
|
],
|
|
vec![
|
|
(None, b'A', Op::Lig(2, b'I')),
|
|
(Some(b'I'), b'B', Op::Lig(0, b'J')),
|
|
(None, b'B', Op::Lig(3, b'M')),
|
|
(Some(b'M'), b'B', Op::Lig(0, b'N')),
|
|
(Some(b'H'), 255, Op::Lig(1, b'K')),
|
|
(Some(b'K'), 255, Op::Lig(0, b'L')),
|
|
],
|
|
vec![
|
|
(Some(b'P'), b'A', Op::Lig(1, b'R')),
|
|
(Some(b'R'), b'A', Op::Lig(2, b'S')),
|
|
(Some(b'B'), b'C', Op::Lig(0, b'I')),
|
|
(Some(b'C'), b'D', Op::Lig(0, b'J')),
|
|
(Some(b'D'), b'E', Op::Lig(0, b'K')),
|
|
],
|
|
vec![
|
|
(Some(b'P'), b'A', Op::Lig(0, b'R')),
|
|
(Some(b'R'), b'B', Op::Kern(12345)),
|
|
(Some(b'B'), b'C', Op::Lig(0, b'I')),
|
|
(Some(b'C'), b'D', Op::Lig(0, b'J')),
|
|
],
|
|
];
|
|
for (index, commands) in programs.iter().enumerate() {
|
|
fs::write(
|
|
p.join(format!("hyph{index}.tfm")),
|
|
font(commands, Some(255)),
|
|
)
|
|
.unwrap();
|
|
source.push_str(&format!("\\font\\F=hyph{index} \\F\\hyphenchar\\F=90 "));
|
|
for width in [10, 15, 20, 25, 35, 50, 80] {
|
|
for text in [
|
|
"X ABCDEFGH ABCDEFGH",
|
|
"X PABCDEFGH QABCDEFGH",
|
|
"X AB{}CDEFGH ABCDEFGH",
|
|
] {
|
|
source.push_str(&format!(
|
|
"\\hsize={width}pt\\setbox0=\\vbox{{{text}\\par}}\\showbox0\n"
|
|
));
|
|
labels.push(format!("reconstitute{index}/{width}/{text}"));
|
|
}
|
|
}
|
|
}
|
|
source.push_str("\\end");
|
|
compare(&p, &source, &labels);
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
#[test]
|
|
fn automatic_reconstitution_keeps_the_same_font_right_character_kern() {
|
|
let p = directory();
|
|
let program = font(
|
|
&[
|
|
(Some(b'B'), b'P', Op::Kern(-52428)),
|
|
(Some(b'B'), 255, Op::Kern(26214)),
|
|
(Some(b'P'), b'Q', Op::Lig(0, b'S')),
|
|
],
|
|
Some(255),
|
|
);
|
|
fs::write(p.join("punctuation.tfm"), &program).unwrap();
|
|
fs::write(p.join("other.tfm"), &program).unwrap();
|
|
let mut source = format!(
|
|
r"{SETUP}\font\F=punctuation \font\G=other \F\hyphenchar\F=90
|
|
\tolerance=10000\uchyph=1\lefthyphenmin=1\righthyphenmin=1
|
|
\lccode80=0\lccode81=0\hyphenation{{a-b}}"
|
|
);
|
|
let mut labels = Vec::new();
|
|
for pretolerance in [10000, -1] {
|
|
for text in ["X ABP X", "X ABPQ X", r"X AB{\G P} X", "X AB X"] {
|
|
source.push_str(&format!(
|
|
r"\pretolerance={pretolerance}\setbox0=\vbox{{{text}\par}}\showbox0"
|
|
));
|
|
labels.push(format!("{pretolerance}/{text}"));
|
|
}
|
|
}
|
|
source.push_str(r"\end");
|
|
compare(&p, &source, &labels);
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn cyclic_hyphen_branch_consumes_job_budget_without_disabling_ligatures() {
|
|
let p = directory();
|
|
fs::write(
|
|
p.join("hyphloop.tfm"),
|
|
font(&[(Some(b'A'), b'C', Op::Lig(1, b'A'))], None),
|
|
)
|
|
.unwrap();
|
|
let source = format!(
|
|
"{SETUP}\\font\\F=hyphloop \\F\\hyphenchar\\F=67\\uchyph=1\\lefthyphenmin=1\\righthyphenmin=1\\hyphenation{{a-b}}\\pretolerance=-1 X AB AB\\par\\end"
|
|
);
|
|
let error = Executor::new(
|
|
State::initex().with_etex(),
|
|
Limits {
|
|
steps: 1000,
|
|
..Limits::default()
|
|
},
|
|
)
|
|
.run(
|
|
SourceFile {
|
|
name: "hyphloop.tex".into(),
|
|
data: source.as_bytes().to_vec(),
|
|
},
|
|
&mut Host(FileWorld::new(WorldConfig::new(&p)).unwrap()),
|
|
)
|
|
.unwrap_err();
|
|
assert!(
|
|
error.message.contains("reconstitute instruction limit"),
|
|
"{error:?}"
|
|
);
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn every_reconstitution_operation_byte_uses_its_actual_cut_and_stop_rules() {
|
|
let p = directory();
|
|
let (mut source, mut labels) = (
|
|
format!(
|
|
"{SETUP}\\pretolerance=-1\\tolerance=10000\\uchyph=1\\lefthyphenmin=1\\righthyphenmin=1\\hyphenation{{a-b-c-d-e-f}}"
|
|
),
|
|
Vec::new(),
|
|
);
|
|
for operation in 0..128u8 {
|
|
let commands = [
|
|
(Some(b'A'), b'B', Op::Lig(operation, b'I')),
|
|
(Some(b'I'), b'B', Op::Lig(0, b'J')),
|
|
(Some(b'A'), b'I', Op::Lig(0, b'K')),
|
|
(Some(b'I'), b'C', Op::Lig(0, b'L')),
|
|
(Some(b'C'), b'D', Op::Lig(0, b'M')),
|
|
(Some(b'D'), b'E', Op::Kern(0)),
|
|
(Some(b'E'), b'F', Op::Kern(-12345)),
|
|
];
|
|
fs::write(p.join(format!("cut{operation}.tfm")), font(&commands, None)).unwrap();
|
|
source.push_str(&format!("\\font\\F=cut{operation} \\F\\hyphenchar\\F=90 "));
|
|
for width in [10, 15, 25, 50] {
|
|
source.push_str(&format!(
|
|
"\\hsize={width}pt\\setbox0=\\vbox{{X ABCDEF ABCDEF\\par}}\\showbox0\n"
|
|
));
|
|
labels.push(format!("reconstitute op{operation}/{width}"));
|
|
}
|
|
}
|
|
source.push_str("\\end");
|
|
compare(&p, &source, &labels);
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|