437 lines
16 KiB
Rust
437 lines
16 KiB
Rust
//! Genuine TFM slots/encoding and native missing-glyph consumers vs INITEX.
|
|
#[path = "support/font_world.rs"]
|
|
mod font_world;
|
|
use std::{fs, path::PathBuf, process::Command};
|
|
use texrra_tex_core::execution::*;
|
|
const CAT: &str = r"\catcode123=1 \catcode125=2 \catcode35=6 \catcode94=7 ";
|
|
const FONT: &str = r"\defaulthyphenchar=45 \font\p=cmr10 \p \hsize=100pt \baselineskip=12pt \parfillskip=0pt plus1fil \hbadness=10000 ";
|
|
const MATH: &str = include_str!("../../../examples/native-bootstrap/math-fonts.tex");
|
|
#[derive(Default)]
|
|
struct Host {
|
|
diagnostics: Vec<(WriteDestination, String)>,
|
|
order: Vec<String>,
|
|
fail: bool,
|
|
}
|
|
impl SourceProvider for Host {
|
|
font_world::font_resources!();
|
|
fn read(&mut self, _: &str, _: &str) -> Result<SourceFile, String> {
|
|
Err("no files".into())
|
|
}
|
|
fn observe_message(&mut self, s: &str) -> Result<(), String> {
|
|
self.order.push(s.into());
|
|
Ok(())
|
|
}
|
|
fn observe_diagnostic(&mut self, d: WriteDestination, s: &str) -> Result<(), String> {
|
|
if self.fail {
|
|
return Err("diagnostic denied".into());
|
|
}
|
|
self.diagnostics.push((d, s.into()));
|
|
self.order.push(s.into());
|
|
Ok(())
|
|
}
|
|
}
|
|
fn execute(setup: &str, body: &str, host: &mut Host) -> Result<Execution, Error> {
|
|
Executor::new(
|
|
State::initex().with_etex(),
|
|
Limits {
|
|
steps: 1_000_000,
|
|
..Limits::default()
|
|
},
|
|
)
|
|
.run(
|
|
SourceFile {
|
|
name: "font.tex".into(),
|
|
data: (format!("{CAT}{setup}{body}\\end\n")).as_bytes().to_vec(),
|
|
},
|
|
host,
|
|
)
|
|
}
|
|
fn directory(name: &str) -> PathBuf {
|
|
let p = std::env::temp_dir().join(format!("texrra-font-{name}-{}", std::process::id()));
|
|
fs::create_dir_all(&p).unwrap();
|
|
p
|
|
}
|
|
fn reference(dir: &PathBuf, setup: &str, body: &str) -> Option<(i32, String, String)> {
|
|
fs::write(dir.join("probe.tex"), format!("{CAT}{setup}{body}\\end\n")).unwrap();
|
|
match Command::new("pdftex")
|
|
.args([
|
|
"-ini",
|
|
"-etex",
|
|
"-interaction=nonstopmode",
|
|
"-halt-on-error",
|
|
"probe.tex",
|
|
])
|
|
.current_dir(dir)
|
|
.output()
|
|
{
|
|
Ok(o) => Some((
|
|
o.status.code().unwrap_or(-1),
|
|
String::from_utf8_lossy(&o.stdout).into_owned(),
|
|
fs::read_to_string(dir.join("probe.log")).unwrap(),
|
|
)),
|
|
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
|
|
eprintln!("pdftex absent: font oracle skipped");
|
|
None
|
|
}
|
|
Err(e) => panic!("{e}"),
|
|
}
|
|
}
|
|
fn missing_lines(s: &str) -> Vec<String> {
|
|
s.lines()
|
|
.filter(|s| s.starts_with("Missing character:") || s.starts_with("! Missing character:"))
|
|
.map(str::to_owned)
|
|
.collect()
|
|
}
|
|
fn messages(e: &Execution) -> Vec<String> {
|
|
e.events
|
|
.iter()
|
|
.filter_map(|e| {
|
|
if let Event::Message { text, .. } = e {
|
|
Some(text.trim().into())
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.collect()
|
|
}
|
|
#[test]
|
|
fn actual_font_slot_bitmaps_and_identifiers_match_real_initex() {
|
|
let names = [
|
|
"cmr10", "cmr7", "cmr5", "cmmi10", "cmmi7", "cmmi5", "cmsy10", "cmsy7", "cmsy5", "cmex10",
|
|
];
|
|
let mut source = String::from("\\newlinechar=33 ");
|
|
for (f, name) in names.iter().enumerate() {
|
|
source.push_str(&format!("\\font\\f{}={name} ", char::from(b'A' + f as u8)));
|
|
}
|
|
for f in 0..=10 {
|
|
let font = if f == 10 {
|
|
"nullfont".to_string()
|
|
} else {
|
|
format!("f{}", char::from(b'A' + f as u8))
|
|
};
|
|
for chunk in 0..8 {
|
|
source.push_str(&format!("\\message{{!Q{f}-{chunk}="));
|
|
for c in chunk * 32..(chunk + 1) * 32 {
|
|
source.push_str(&format!("\\iffontchar\\{font}{c} Y\\else N\\fi"));
|
|
}
|
|
source.push_str("!}");
|
|
}
|
|
}
|
|
source.push_str(r"\fA\textfont0=\fB\scriptfont1=\fC\scriptscriptfont2=\fD\message{!CURRENT=\iffontchar\font65 Y\else N\fi/\iffontchar\textfont0 65 Y\else N\fi/\iffontchar\scriptfont1 65 Y\else N\fi/\iffontchar\scriptscriptfont2 65 Y\else N\fi!}");
|
|
let mut host = Host::default();
|
|
let e = execute("", &source, &mut host).unwrap();
|
|
let dir = directory("bitmap");
|
|
let Some((status, _, log)) = reference(&dir, "", &source) else {
|
|
fs::remove_dir_all(dir).unwrap();
|
|
return;
|
|
};
|
|
assert_eq!(status, 0, "{log}");
|
|
for msg in messages(&e) {
|
|
assert!(log.contains(&msg), "{msg}\n{log}");
|
|
}
|
|
assert!(e.reads.contains(&Dependency::FontResources));
|
|
assert!(
|
|
e.reads
|
|
.contains(&Dependency::MathFamily { size: 1, family: 1 })
|
|
);
|
|
let encoded_alias = e.state.fonts().glyph_font(e.state.text_font()).unwrap();
|
|
assert!(!encoded_alias.has_slot(200));
|
|
assert!(encoded_alias.has_slot(92));
|
|
assert_eq!(
|
|
encoded_alias.resource.version.path.file_name().unwrap(),
|
|
"cmr10.tfm"
|
|
);
|
|
let copy: Execution = serde_json::from_str(&serde_json::to_string(&e).unwrap()).unwrap();
|
|
assert_eq!(copy, e);
|
|
eprintln!(
|
|
"2816 real INITEX font-slot predicates plus four actual font-identifier queries passed"
|
|
);
|
|
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");
|
|
let mut cases = 0;
|
|
for trace in [-1, 0, 1, 2, 3, 4] {
|
|
for online in [-1, 0, 1] {
|
|
for (setup, body) in [
|
|
(
|
|
FONT,
|
|
r"\message{BEFORE}\setbox0=\hbox{A\char200 B}\message{AFTER}",
|
|
),
|
|
("", r"\setbox0=\hbox{\char65\message{SF=\the\spacefactor}}"),
|
|
(
|
|
MATH,
|
|
r#"\setbox0=\hbox{$a+\mathchar"01C8^2+b$}\message{AFTER}"#,
|
|
),
|
|
] {
|
|
let body = format!(r"\tracinglostchars={trace}\tracingonline={online} {body}");
|
|
let mut host = Host::default();
|
|
let native = execute(setup, &body, &mut host);
|
|
let Some((status, terminal, log)) = reference(&dir, setup, &body) else {
|
|
fs::remove_dir_all(dir).unwrap();
|
|
return;
|
|
};
|
|
assert_eq!(native.is_ok(), status == 0, "case {cases}: {body}\n{log}");
|
|
let native_log = host
|
|
.diagnostics
|
|
.iter()
|
|
.map(|(_, s)| s.as_str())
|
|
.collect::<String>();
|
|
let native_terminal = host
|
|
.diagnostics
|
|
.iter()
|
|
.filter(|(d, _)| *d == WriteDestination::TerminalAndLog)
|
|
.map(|(_, s)| s.as_str())
|
|
.collect::<String>();
|
|
assert_eq!(
|
|
missing_lines(&native_log),
|
|
missing_lines(&log),
|
|
"case {cases}: {body}\n{log}"
|
|
);
|
|
assert_eq!(
|
|
missing_lines(&native_terminal),
|
|
missing_lines(&terminal),
|
|
"terminal case {cases}: {body}"
|
|
);
|
|
if let Ok(e) = native {
|
|
assert_eq!(
|
|
e.events
|
|
.iter()
|
|
.filter(|e| matches!(e, Event::MissingGlyph(_)))
|
|
.count(),
|
|
1
|
|
);
|
|
}
|
|
cases += 1;
|
|
}
|
|
}
|
|
}
|
|
// Math fetch happens at conversion: final local tracing/font assignments matter.
|
|
for body in [
|
|
r#"\tracinglostchars=0\setbox0=\hbox{$\mathchar"01C8\tracinglostchars=2$}"#,
|
|
r#"\tracinglostchars=2\setbox0=\hbox{$\mathchar"01C8\tracinglostchars=0$}"#,
|
|
] {
|
|
let mut host = Host::default();
|
|
execute(MATH, body, &mut host).unwrap();
|
|
let Some((status, _, log)) = reference(&dir, MATH, body) else {
|
|
return;
|
|
};
|
|
assert_eq!(status, 0, "{log}");
|
|
assert_eq!(
|
|
missing_lines(
|
|
&host
|
|
.diagnostics
|
|
.iter()
|
|
.map(|(_, s)| s.as_str())
|
|
.collect::<String>()
|
|
),
|
|
missing_lines(&log)
|
|
);
|
|
cases += 1;
|
|
}
|
|
for newline in [-1, 10, 33, 99, 200] {
|
|
let body =
|
|
format!(r"\newlinechar={newline} \tracinglostchars=2 \setbox0=\hbox{{\char200}}");
|
|
let mut host = Host::default();
|
|
execute(FONT, &body, &mut host).unwrap();
|
|
let Some((status, _, log)) = reference(&dir, FONT, &body) else {
|
|
return;
|
|
};
|
|
assert_eq!(status, 0, "{log}");
|
|
let text = host
|
|
.diagnostics
|
|
.iter()
|
|
.map(|(_, s)| s.as_str())
|
|
.collect::<String>();
|
|
assert!(log.contains(text.trim_end()), "{body}\n{text}\n{log}");
|
|
cases += 1;
|
|
}
|
|
eprintln!("{cases} real INITEX missing-glyph tracing/channel/timing cases passed");
|
|
fs::remove_dir_all(dir).unwrap();
|
|
}
|
|
#[test]
|
|
fn raw_slot_metrics_missing_ligatures_scripts_and_hyphens_match_initex() {
|
|
let dir = directory("metrics");
|
|
let mut cases = Vec::new();
|
|
for slot in 0..256 {
|
|
cases.push((FONT.to_string(), format!(r"\setbox0=\hbox{{\char{slot}}}")));
|
|
}
|
|
for body in [
|
|
r"\setbox0=\hbox{f\char200 i}",
|
|
r"\setbox0=\hbox{f^^c8i}",
|
|
r"\setbox0=\hbox{\char0\char11\char32\char92\char123}",
|
|
] {
|
|
cases.push((FONT.into(), body.into()));
|
|
}
|
|
for formula in [
|
|
r#"a+\mathchar"01C8+b"#,
|
|
r#"a+\mathchar"01C8^2+b"#,
|
|
r#"a^{\mathchar"00C8}+b"#,
|
|
r#"a+{\mathchar"01C8\over b}"#,
|
|
r#"a+\mathchar"21C8+b"#,
|
|
] {
|
|
cases.push((MATH.into(), format!(r"\setbox0=\hbox{{${formula}$}}")));
|
|
}
|
|
let hyp = r"\defaulthyphenchar=200 \font\p=cmr10\p\hsize=40pt\lefthyphenmin=2\righthyphenmin=3\pretolerance=-1\tolerance=10000\parfillskip=0pt plus1fil\hyphenation{con-struc-tion para-graph hy-phen-a-tion}";
|
|
for text in [
|
|
r"\noindent con\-struction\par",
|
|
r"\noindent Alpha construction construction\par",
|
|
r"\noindent Alpha paragraph construction hyphenation\par",
|
|
] {
|
|
cases.push((hyp.into(), format!(r"\setbox0=\vbox{{{text}}}")));
|
|
}
|
|
for (i, (setup, body)) in cases.iter().enumerate() {
|
|
let body = format!(
|
|
r"\tracinglostchars=2 {body}\newlinechar=33\message{{!DIM=\number\wd0/\number\ht0/\number\dp0!}}"
|
|
);
|
|
let mut host = Host::default();
|
|
let e = execute(setup, &body, &mut host).unwrap();
|
|
let Some((status, _, log)) = reference(&dir, setup, &body) else {
|
|
fs::remove_dir_all(dir).unwrap();
|
|
return;
|
|
};
|
|
assert_eq!(status, 0, "case {i}: {body}\n{log}");
|
|
let native_log = host
|
|
.diagnostics
|
|
.iter()
|
|
.map(|(_, s)| s.as_str())
|
|
.collect::<String>();
|
|
assert_eq!(
|
|
missing_lines(&native_log),
|
|
missing_lines(&log),
|
|
"missing count case {i}: {body}\n{log}"
|
|
);
|
|
let actual = messages(&e).pop().unwrap();
|
|
let target = log.lines().find(|l| l.starts_with("DIM=")).unwrap();
|
|
let dims = |s: &str| {
|
|
s.trim_start_matches("DIM=")
|
|
.split('/')
|
|
.map(|n| n.parse::<i64>().unwrap())
|
|
.collect::<Vec<_>>()
|
|
};
|
|
assert!(
|
|
dims(&actual)
|
|
.iter()
|
|
.zip(dims(target))
|
|
.all(|(a, b)| (*a - b).abs() <= 1),
|
|
"dimension case {i}: {body}\n{actual} vs {target}\n{log}"
|
|
);
|
|
}
|
|
eprintln!(
|
|
"{} real INITEX raw-slot/ligature/math/discretionary dimension cases passed",
|
|
cases.len()
|
|
);
|
|
fs::remove_dir_all(dir).unwrap();
|
|
}
|
|
#[test]
|
|
fn font_state_scopes_observer_failures_and_unsupported_resources_stay_strict() {
|
|
let mut host = Host::default();
|
|
let e=execute(FONT,r"\tracinglostchars=1{\tracinglostchars=2\setbox0=\hbox{\char200}}\setbox0=\hbox{\char201}{\globaldefs=1\tracinglostchars=0}\setbox0=\hbox{\char202}",&mut host).unwrap();
|
|
assert_eq!(
|
|
host.diagnostics.iter().map(|(d, _)| *d).collect::<Vec<_>>(),
|
|
[WriteDestination::TerminalAndLog, WriteDestination::Log]
|
|
);
|
|
assert_eq!(
|
|
e.state.parameter(Parameter::Tracinglostchars),
|
|
ParameterValue::Integer(0)
|
|
);
|
|
let mut host = Host {
|
|
fail: true,
|
|
..Host::default()
|
|
};
|
|
assert!(
|
|
execute(FONT, r"\tracinglostchars=2\char200", &mut host)
|
|
.unwrap_err()
|
|
.message
|
|
.contains("diagnostic denied")
|
|
);
|
|
let dir = directory("dynamic-positive");
|
|
for definition in [r"\font\p=cmr10 at12pt", r"\font\p=cmr7\p A"] {
|
|
let body = format!("{definition}\\p\\setbox0=\\hbox{{AV}}\\message{{DIM=\\number\\wd0}}");
|
|
let e = execute(FONT, &body, &mut Host::default()).unwrap();
|
|
if let Some((status, _, log)) = reference(&dir, FONT, &body) {
|
|
assert_eq!(status, 0, "{log}");
|
|
assert!(
|
|
log.contains(&format!("DIM={}", e.state.box_register(0).unwrap().width)),
|
|
"{log}"
|
|
);
|
|
}
|
|
}
|
|
fs::remove_dir_all(dir).unwrap();
|
|
for body in [
|
|
r"\font\p=missingfont",
|
|
r"\iffontchar\nullfont256\fi",
|
|
r"\iffontchar\nullfont-1\fi",
|
|
r"中文",
|
|
] {
|
|
assert!(execute("", body, &mut Host::default()).is_err(), "{body}");
|
|
}
|
|
}
|