271 lines
9.5 KiB
Rust
271 lines
9.5 KiB
Rust
//! Real language/pattern data, original resource inputs and paragraph lists.
|
|
#[path = "support/font_world.rs"]
|
|
mod font_world;
|
|
use std::{
|
|
fs,
|
|
path::{Path, PathBuf},
|
|
process::Command,
|
|
sync::atomic::{AtomicU64, Ordering},
|
|
};
|
|
use texrra_tex_core::{
|
|
execution::*,
|
|
resources::{FileWorld, ResourceKind, WorldConfig},
|
|
};
|
|
const SETUP: &str = r"\catcode123=1\catcode125=2\catcode35=6\catcode94=7\defaulthyphenchar=45\font\p=cmr10\p\parindent=0pt\baselineskip=12pt\parfillskip=0pt plus1fil\pretolerance=-1\tolerance=10000\hbadness=10000\vbadness=10000\boxmaxdepth=100pt\hyphenpenalty=50\exhyphenpenalty=50\linepenalty=10\adjdemerits=10000\doublehyphendemerits=10000\finalhyphendemerits=5000\lefthyphenmin=1\righthyphenmin=1\showboxbreadth=1000\showboxdepth=1000\tracingonline=1 ";
|
|
fn directory() -> PathBuf {
|
|
static ID: AtomicU64 = AtomicU64::new(0);
|
|
let p = std::env::temp_dir().join(format!(
|
|
"texrra-hyph-{}-{}",
|
|
std::process::id(),
|
|
ID.fetch_add(1, Ordering::Relaxed)
|
|
));
|
|
fs::create_dir_all(&p).unwrap();
|
|
p
|
|
}
|
|
struct Host(FileWorld);
|
|
impl SourceProvider for Host {
|
|
font_world::font_resources!();
|
|
fn read(&mut self, name: &str, from: &str) -> Result<SourceFile, String> {
|
|
let r = self.0.read(ResourceKind::Tex, name, Path::new(from))?;
|
|
Ok(SourceFile {
|
|
name: r.version.path.display().to_string(),
|
|
data: r.data,
|
|
})
|
|
}
|
|
fn diagnostic(&mut self, _: WriteDestination, _: &str) -> Result<(), String> {
|
|
Ok(())
|
|
}
|
|
}
|
|
fn execute(source: &str, p: &Path, initex: bool, limits: Limits) -> Result<Execution, Error> {
|
|
let config = WorldConfig::from_environment(p.to_owned(), Vec::new()).unwrap();
|
|
let state = if initex {
|
|
State::initex()
|
|
} else {
|
|
State::default()
|
|
};
|
|
Executor::new(
|
|
state.with_etex().with_input_profile(InputProfile::TeX8Bit),
|
|
limits,
|
|
)
|
|
.run(
|
|
SourceFile {
|
|
name: p.join("probe.tex").display().to_string(),
|
|
data: source.as_bytes().to_vec(),
|
|
},
|
|
&mut Host(FileWorld::new(config).unwrap()),
|
|
)
|
|
}
|
|
fn compare(body: &str) -> Execution {
|
|
let p = directory();
|
|
let source = format!("{SETUP}\n{body}\\end\n");
|
|
fs::write(p.join("probe.tex"), &source).unwrap();
|
|
let e = execute(
|
|
&source,
|
|
&p,
|
|
true,
|
|
Limits {
|
|
steps: 2_000_000,
|
|
..Limits::default()
|
|
},
|
|
)
|
|
.unwrap();
|
|
let r = Command::new("pdftex")
|
|
.args(["-ini", "-etex", "-interaction=nonstopmode", "probe.tex"])
|
|
.current_dir(&p)
|
|
.output()
|
|
.expect("actual INITEX required");
|
|
let log = fs::read_to_string(p.join("probe.log")).unwrap();
|
|
assert_eq!(r.status.code(), Some(1), "{log}");
|
|
for error in log.lines().filter(|s| s.starts_with("! ")) {
|
|
assert_eq!(error, "! OK.", "{log}");
|
|
}
|
|
let reports: Vec<_> = e
|
|
.events
|
|
.iter()
|
|
.filter_map(|e| {
|
|
if let Event::ShowBox(r) = e {
|
|
Some(r)
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.collect();
|
|
assert!(!reports.is_empty());
|
|
for report in reports {
|
|
assert!(
|
|
log.contains(&report.text),
|
|
"{body}\nnative:\n{}\nreference:\n{log}",
|
|
report.text
|
|
);
|
|
}
|
|
fs::remove_dir_all(p).unwrap();
|
|
e
|
|
}
|
|
#[test]
|
|
fn pattern_scores_boundaries_even_overrides_and_minima_build_actual_discretionaries() {
|
|
for patterns in [
|
|
"a1ba",
|
|
"a1ba a2b",
|
|
".ab3a",
|
|
"ab1a ba2ba",
|
|
"a1b2a3b4a",
|
|
"a ab1a",
|
|
"a1. .1a",
|
|
] {
|
|
for width in [18, 24, 32, 44] {
|
|
compare(&format!(
|
|
"\\patterns{{{patterns}}}\\hsize={width}pt\\setbox0=\\vbox{{X ababa ababa ababa ababa\\par}}\\showbox0"
|
|
));
|
|
}
|
|
}
|
|
for length in [62, 63, 64, 65] {
|
|
let word = "a".repeat(length);
|
|
compare(&format!(
|
|
"\\patterns{{a1a}}\\hsize=50pt\\setbox0=\\vbox{{X {word}\\par}}\\showbox0"
|
|
));
|
|
}
|
|
for text in ["X (ababa) ababa", "X @ababa ababa", "X ababa\\hbox{} ababa"] {
|
|
compare(&format!(
|
|
"\\patterns{{a1ba}}\\hsize=24pt\\setbox0=\\vbox{{{text}\\par}}\\showbox0"
|
|
));
|
|
}
|
|
for (left, right) in [(0, 0), (1, 2), (3, 2), (63, 1), (-1, 100)] {
|
|
compare(&format!(
|
|
"\\patterns{{a1ba b1a}}\\lefthyphenmin={left}\\righthyphenmin={right}\\hsize=24pt\\setbox0=\\vbox{{X ababa ababa ababa\\par}}\\showbox0"
|
|
));
|
|
}
|
|
}
|
|
#[test]
|
|
fn language_nodes_scopes_and_explicit_setlanguage_drive_per_word_tables() {
|
|
let tables = r"\language=1\patterns{ab1a}\language=2\patterns{aba1b}\language=0\hsize=24pt ";
|
|
for text in [
|
|
r"X ababa \language1 ababa \language2 ababa \language0 ababa",
|
|
r"\language1 X ababa {\language2 ababa} ababa",
|
|
r"\language1 X ababa \lefthyphenmin=3\language2 ababa",
|
|
r"\language1 X ababa \lefthyphenmin=3\setlanguage1 ababa",
|
|
r"\language1 X ababa \setlanguage2 ababa",
|
|
r"\language=-1 X ababa \language=256 ababa \language1 ababa",
|
|
r"X \language1 ab\language2 aba ababa",
|
|
r"\language1 X ababa \setlanguage1 \language0 ababa",
|
|
] {
|
|
let e = compare(&format!(
|
|
"{tables}\\setbox0=\\vbox{{{text}\\par}}\\showbox0"
|
|
));
|
|
assert!(
|
|
e.reads
|
|
.contains(&Dependency::Parameter(Parameter::Language))
|
|
);
|
|
}
|
|
compare(&format!(
|
|
"{tables}\\setbox0=\\hbox{{\\setlanguage2 A\\setlanguage-1 B}}\\showbox0"
|
|
));
|
|
}
|
|
#[test]
|
|
fn exceptions_are_global_per_language_and_override_pattern_breaks() {
|
|
for exceptions in [
|
|
r"\language1\hyphenation{aba-ba}",
|
|
r"{\language1\hyphenation{aba-ba}}",
|
|
r"\language1\hyphenation{ababa}",
|
|
] {
|
|
compare(&format!(
|
|
r"\language1\patterns{{ab1a}}\language2\patterns{{ab1a}}{exceptions}\language0\hsize=24pt\setbox0=\vbox{{X ababa \language1 ababa \language2 ababa\par}}\showbox0"
|
|
));
|
|
}
|
|
let e =
|
|
compare(r"\language1\patterns{ab1a}\hsize=24pt\setbox0=\vbox{X ababa ababa\par}\showbox0");
|
|
let copy: Execution = serde_json::from_slice(&serde_json::to_vec(&e).unwrap()).unwrap();
|
|
assert_eq!(e, copy);
|
|
let p = directory();
|
|
let mut host =
|
|
Host(FileWorld::new(WorldConfig::from_environment(p.clone(), vec![]).unwrap()).unwrap());
|
|
let next = Executor::from_execution(copy, Limits::default())
|
|
.run(
|
|
SourceFile {
|
|
name: p.join("next.tex").display().to_string(),
|
|
data: br"\setbox1=\vbox{X ababa ababa\par}\end".to_vec(),
|
|
},
|
|
&mut host,
|
|
)
|
|
.unwrap();
|
|
assert!(
|
|
next.events
|
|
.iter()
|
|
.any(|e| matches!(e,Event::BuiltParagraph(p) if p.initial_language.language==1))
|
|
);
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
#[test]
|
|
fn real_unmodified_patterns_are_loaded_as_sources_and_consumed_in_paragraphs() {
|
|
for name in ["hyphen.tex", "hyph-en-us.tex", "hyph-en-gb.tex"] {
|
|
let r = Command::new("kpsewhich")
|
|
.arg(name)
|
|
.output()
|
|
.expect("reference installation required");
|
|
assert!(r.status.success());
|
|
let path = String::from_utf8(r.stdout).unwrap().trim().to_owned();
|
|
let original = fs::read(&path).unwrap();
|
|
let e = compare(&format!(
|
|
"\\input {path} \\hsize=62pt\\lefthyphenmin=2\\righthyphenmin=3\\setbox0=\\vbox{{X hyphenation demonstration paragraph construction character algorithm memory parameter hyphenation\\par}}\\showbox0"
|
|
));
|
|
let source = e.sources.iter().find(|s| s.name == path).unwrap();
|
|
assert_eq!(source.data, original);
|
|
assert_eq!(fs::read(path).unwrap(), original);
|
|
assert!(e.events.iter().any(|e|matches!(e,Event::BuiltParagraph(p) if p.nodes.iter().any(|n|matches!(n,TextNode::Discretionary{automatic:true,..})))));
|
|
}
|
|
}
|
|
#[test]
|
|
fn initex_lifecycle_pattern_grammar_duplicates_and_capacity_fail_explicitly() {
|
|
let p = directory();
|
|
for body in [
|
|
r"\patterns{ab1a ab2a}",
|
|
r"\patterns{a12b}",
|
|
r"\patterns{a!b}",
|
|
r"\chardef\A=97\patterns{\A}",
|
|
r"\patterns{a1b}\hsize=100pt X abab\par\patterns{b1a}",
|
|
r"\setlanguage1",
|
|
] {
|
|
let source = format!("{SETUP}{body}\\end\n");
|
|
assert!(
|
|
execute(&source, &p, true, Limits::default()).is_err(),
|
|
"{body}"
|
|
);
|
|
fs::write(p.join("probe.tex"), &source).unwrap();
|
|
let r = Command::new("pdftex")
|
|
.args([
|
|
"-ini",
|
|
"-etex",
|
|
"-interaction=nonstopmode",
|
|
"-halt-on-error",
|
|
"probe.tex",
|
|
])
|
|
.current_dir(&p)
|
|
.output()
|
|
.unwrap();
|
|
assert!(
|
|
!r.status.success(),
|
|
"{body}:{}",
|
|
String::from_utf8_lossy(&r.stdout)
|
|
);
|
|
}
|
|
assert!(
|
|
execute(r"\patterns{a1b}", &p, false, Limits::default())
|
|
.unwrap_err()
|
|
.message
|
|
.contains("INITEX")
|
|
);
|
|
let e = execute(
|
|
&format!("{SETUP}\\patterns{{a1bcdefghijk}}\\end"),
|
|
&p,
|
|
true,
|
|
Limits {
|
|
tokens: 6,
|
|
..Limits::default()
|
|
},
|
|
)
|
|
.unwrap_err();
|
|
assert!(e.message.contains("pattern memory"), "{}", e.message);
|
|
// A successful first pass does not freeze pattern loading; a real hyphenating pass does.
|
|
compare(r"\hsize=100pt\pretolerance=10000\setbox0=\vbox{X ababa\par}\patterns{a1b}\showbox0");
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|