Advance native LaTeX execution and preview

This commit is contained in:
gloamxun committed 2026-11-18 22:17:21 +08:00
1 parent 91f9d76b16
commit 2741546ca8
414 files changed
+99872 -933

No files matched your search

+524
View File
@@ -0,0 +1,524 @@
//! The native CLI enters the Route-D executor before the server/v1 compiler.
#[path = "support/native_fonts.rs"]
mod native_fonts;
use std::{
fs,
path::PathBuf,
process::Command,
sync::atomic::{AtomicU64, Ordering},
};
fn directory() -> PathBuf {
static ID: AtomicU64 = AtomicU64::new(0);
let path = std::env::temp_dir().join(format!(
"texrra-native-cli-{}-{}",
std::process::id(),
ID.fetch_add(1, Ordering::Relaxed)
));
fs::create_dir_all(&path).unwrap();
path
}
#[test]
fn native_cli_executes_input_macros_registers_and_exports_a_real_pdf() {
let directory = directory();
fs::write(
directory.join("bootstrap.tex"),
r"\countdef\counter=0 \counter=4 \def\greet#1{Hello, #1.}\endinput",
)
.unwrap();
fs::write(directory.join("main.tex"), "\\input bootstrap\n\\message{NATIVE-READY=\\the\\counter}\n\\greet{Route-D}\\par\n{\\counter=9 \\greet{local}}\\par\n\\ifnum\\counter=4 Bootstrap restored.\\else BAD\\fi\\end\n").unwrap();
let output = native_fonts::command()
.args(["native", "main.tex", "native.pdf"])
.current_dir(&directory)
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert!(String::from_utf8_lossy(&output.stdout).contains("native Route-D"));
assert!(String::from_utf8_lossy(&output.stderr).contains("NATIVE-READY=4"));
let pdf = fs::read(directory.join("native.pdf")).unwrap();
assert!(pdf.starts_with(b"%PDF-"));
match Command::new("pdftotext")
.args(["native.pdf", "native.txt"])
.current_dir(&directory)
.output()
{
Ok(output) => {
assert!(output.status.success());
let text = fs::read_to_string(directory.join("native.txt")).unwrap();
assert!(text.contains("Hello, Route-D."), "{text}");
assert!(text.contains("Hello, local."), "{text}");
assert!(text.contains("Bootstrap restored."), "{text}");
assert!(!text.contains("BAD"), "{text}");
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
eprintln!("pdftotext absent: native PDF text check skipped")
}
Err(e) => panic!("{e}"),
}
fs::remove_dir_all(directory).unwrap();
}
#[test]
fn unsupported_native_latex_is_an_error_and_cannot_overwrite_an_existing_pdf() {
let directory = directory();
fs::write(
directory.join("unsupported.tex"),
"\\documentclass{article}\n\\begin{document}Text\\end{document}",
)
.unwrap();
fs::write(directory.join("existing.pdf"), b"preserve").unwrap();
let output = native_fonts::command()
.args(["native", "unsupported.tex", "existing.pdf"])
.current_dir(&directory)
.output()
.unwrap();
assert!(!output.status.success());
assert!(String::from_utf8_lossy(&output.stderr).contains("undefined native control sequence"));
assert!(String::from_utf8_lossy(&output.stderr).contains("unsupported.tex:1:1"));
assert_eq!(
fs::read(directory.join("existing.pdf")).unwrap(),
b"preserve"
);
fs::remove_dir_all(directory).unwrap();
}
#[test]
fn native_pdf_rejects_overwriting_its_source() {
let directory = directory();
fs::write(directory.join("main.tex"), "Keep this source.").unwrap();
let output = native_fonts::command()
.args(["native", "main.tex", "main.tex"])
.current_dir(&directory)
.output()
.unwrap();
assert!(!output.status.success());
assert_eq!(
fs::read_to_string(directory.join("main.tex")).unwrap(),
"Keep this source."
);
fs::remove_dir_all(directory).unwrap();
}
#[test]
fn native_initex_starts_without_format_category_setup() {
let directory = directory();
fs::write(directory.join("init.tex"), r"\ifnum\catcode123=12 \catcode123=1 \catcode125=2 \message{INITEX-YES}\else\message{WRONG}\fi\end").unwrap();
let output = native_fonts::command()
.args(["native", "--initex", "init.tex", "init.pdf"])
.current_dir(&directory)
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert!(String::from_utf8_lossy(&output.stderr).contains("INITEX-YES"));
assert!(!String::from_utf8_lossy(&output.stderr).contains("WRONG"));
fs::remove_dir_all(directory).unwrap();
}
#[test]
fn native_etex_profile_executes_extended_language_without_a_preloaded_format() {
let directory = directory();
fs::write(
directory.join("etex.tex"),
concat!(
r"\catcode123=1 \catcode125=2 \catcode35=6 ",
r"\defaulthyphenchar=45 \font\nativefont=cmr10 \nativefont \hsize=468pt \baselineskip=12pt \parfillskip=0pt plus1fil ",
r"\protected\def\p{EXTENDED}\edef\saved{\p}",
r"\toks300={\saved}\count32767=\numexpr(1+2)*5/2\relax ",
r"\unless\ifnum\count32767=8 \errmessage{bad expression}\fi ",
r"\the\toks300\message{ETEX-SEMANTICS=\the\count32767}\end"
),
)
.unwrap();
let output = native_fonts::command()
.args(["native", "--initex", "--etex", "etex.tex", "etex.pdf"])
.current_dir(&directory)
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert!(String::from_utf8_lossy(&output.stderr).contains("ETEX-SEMANTICS=8"));
assert!(
fs::read(directory.join("etex.pdf"))
.unwrap()
.starts_with(b"%PDF-")
);
let base = native_fonts::command()
.args(["native", "--initex", "etex.tex", "base.pdf"])
.current_dir(&directory)
.output()
.unwrap();
assert!(!base.status.success());
assert!(!directory.join("base.pdf").exists());
fs::remove_dir_all(directory).unwrap();
}
#[test]
fn unmodified_latex_entry_advances_past_capability_guards_to_a_real_missing_primitive() {
let texmf = std::env::var_os("TEXRRA_ORACLE_TEXMF")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("/usr/share/texlive/texmf-dist"));
let entry = texmf.join("tex/latex/base/latex.ltx");
if !entry.exists() {
eprintln!("installed LaTeX source absent: real format boundary probe skipped");
return;
}
let directory = directory();
let before = fs::read(&entry).unwrap();
let output = native_fonts::command()
.args(["native", "--job", "--initex", "--etex"])
.arg(&entry)
.arg(directory.join("format.pdf"))
.current_dir(&directory)
.output()
.unwrap();
assert!(
!output.status.success(),
"incomplete native format must fail"
);
let diagnostic = String::from_utf8_lossy(&output.stderr);
assert!(
diagnostic.contains("TeX errmessage: LaTeX requires expl3"),
"{diagnostic}"
);
assert!(diagnostic.contains("latex.ltx:1115:15"), "{diagnostic}");
assert!(!diagnostic.contains("LaTeX requires e-TeX"));
assert!(diagnostic.contains("./texsys.aux found"), "{diagnostic}");
let log = fs::read_to_string(directory.join("format.log")).unwrap();
assert!(log.contains(r"\maxdimen=\dimen10"), "{log}");
assert!(log.contains("parameters,"), "{log}");
let aux = fs::read_to_string(directory.join("texsys.aux")).unwrap();
assert!(aux.contains(':') && aux.ends_with("\n\n"), "{aux}");
assert!(!directory.join("format.pdf").exists());
assert_eq!(fs::read(&entry).unwrap(), before);
fs::remove_dir_all(directory).unwrap();
}
#[test]
fn native_quantities_are_executed_and_printed_into_a_real_pdf() {
let dir = directory();
fs::write(
dir.join("quantities.tex"),
concat!(
"\\catcode123=1 \\catcode125=2 \\catcode35=6\n",
r"\defaulthyphenchar=45 \font\nativefont=cmr10 \nativefont \hsize=468pt \baselineskip=12pt \parfillskip=0pt plus1fil ",
r"\dimendef\width=300 \width=2.5pt \advance\width by1.25pt ",
r"\skipdef\flex=301 \flex=1pt plus2fil minus.5pt ",
r#"\mathchardef\symbol="1234 "#,
r"Native width: \the\width.\par Math character code: \number\symbol.\par",
r"\ifdim\width>3pt\message{QUANTITY-CHECK=OK}\else\errmessage{bad quantity}\fi\end"
),
)
.unwrap();
let output = native_fonts::command()
.args([
"native",
"--initex",
"--etex",
"quantities.tex",
"quantities.pdf",
])
.current_dir(&dir)
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert!(String::from_utf8_lossy(&output.stderr).contains("QUANTITY-CHECK=OK"));
assert!(
fs::read(dir.join("quantities.pdf"))
.unwrap()
.starts_with(b"%PDF-")
);
match Command::new("pdftotext")
.args(["quantities.pdf", "quantities.txt"])
.current_dir(&dir)
.output()
{
Ok(output) => {
assert!(output.status.success());
let text = fs::read_to_string(dir.join("quantities.txt")).unwrap();
assert!(text.contains("Native width: 3.75pt."), "{text}");
assert!(text.contains("Math character code: 4660."), "{text}");
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
eprintln!("pdftotext absent: PDF text probe skipped")
}
Err(e) => panic!("{e}"),
}
fs::remove_dir_all(dir).unwrap();
}
#[test]
fn native_initex_measures_multiple_paragraphs_and_renders_fixed_lines() {
let dir = directory();
fs::write(
dir.join("paragraphs.tex"),
include_str!("../examples/native-bootstrap/paragraphs.tex"),
)
.unwrap();
let output = native_fonts::command()
.args([
"native",
"--initex",
"--etex",
"paragraphs.tex",
"paragraphs.pdf",
])
.current_dir(&dir)
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert!(
String::from_utf8_lossy(&output.stderr)
.contains("Native paragraph construction completed.")
);
assert!(
fs::read(dir.join("paragraphs.pdf"))
.unwrap()
.starts_with(b"%PDF-")
);
match Command::new("pdftotext")
.args(["paragraphs.pdf", "paragraphs.txt"])
.current_dir(&dir)
.output()
{
Ok(output) => {
assert!(output.status.success());
let text = fs::read_to_string(dir.join("paragraphs.txt")).unwrap();
assert!(text.contains("210.0pt."), "{text}");
assert!(text.contains("previous line depth"), "{text}");
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
eprintln!("pdftotext absent: paragraph PDF extraction skipped")
}
Err(e) => panic!("{e}"),
}
fs::remove_dir_all(dir).unwrap();
}
#[test]
fn native_boxes_pack_store_copy_and_paint_actual_contents() {
let dir = directory();
fs::write(
dir.join("boxes.tex"),
include_str!("../examples/native-bootstrap/boxes.tex"),
)
.unwrap();
let output = native_fonts::command()
.args(["native", "--initex", "--etex", "boxes.tex", "boxes.pdf"])
.current_dir(&dir)
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert!(String::from_utf8_lossy(&output.stderr).contains("Native box packing completed."));
match Command::new("pdftotext")
.args(["boxes.pdf", "boxes.txt"])
.current_dir(&dir)
.output()
{
Ok(output) => {
assert!(output.status.success());
let text = fs::read_to_string(dir.join("boxes.txt")).unwrap();
let text = text.split_whitespace().collect::<Vec<_>>().join(" ");
assert!(text.contains("Vertical top"), "{text}");
assert!(text.contains("Nested native box"), "{text}");
assert!(text.contains("beside native text."), "{text}");
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
eprintln!("pdftotext absent: box PDF extraction skipped")
}
Err(e) => panic!("{e}"),
}
fs::remove_dir_all(dir).unwrap();
}
#[test]
fn native_math_tokens_noads_families_and_penalties_render_a_real_pdf() {
let dir = directory();
fs::write(
dir.join("math-fonts.tex"),
include_str!("../examples/native-bootstrap/math-fonts.tex"),
)
.unwrap();
fs::write(
dir.join("math.tex"),
include_str!("../examples/native-bootstrap/math.tex"),
)
.unwrap();
let output = native_fonts::command()
.args(["native", "--initex", "--etex", "math.tex", "math.pdf"])
.current_dir(&dir)
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert!(
String::from_utf8_lossy(&output.stderr).contains("Native math noad execution completed.")
);
assert!(
fs::read(dir.join("math.pdf"))
.unwrap()
.starts_with(b"%PDF-")
);
match Command::new("pdftotext")
.args(["math.pdf", "math.txt"])
.current_dir(&dir)
.output()
{
Ok(o) => {
assert!(o.status.success());
let s = fs::read_to_string(dir.join("math.txt")).unwrap();
assert!(s.contains("Restricted"), "{s}");
assert!(s.contains("Prohibited"), "{s}");
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
eprintln!("pdftotext absent: math extraction skipped")
}
Err(e) => panic!("{e}"),
};
fs::remove_dir_all(dir).unwrap();
}
#[test]
fn native_interline_penalties_drive_actual_multi_page_pdf_export() {
let directory = directory();
fs::write(
directory.join("interline.tex"),
include_str!("../examples/native-bootstrap/interline.tex"),
)
.unwrap();
let output = native_fonts::command()
.args([
"native",
"--initex",
"--etex",
"interline.tex",
"interline.pdf",
])
.current_dir(&directory)
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert!(String::from_utf8_lossy(&output.stdout).contains("6 pages, native Route-D"));
assert!(
String::from_utf8_lossy(&output.stderr)
.contains("Native interline page execution completed.")
);
match Command::new("pdftotext")
.args(["-raw", "interline.pdf", "-"])
.current_dir(&directory)
.output()
{
Ok(o) => {
assert!(o.status.success());
let text = String::from_utf8(o.stdout).unwrap();
let pages: Vec<Vec<_>> = text
.split('\u{c}')
.map(|p| p.split_whitespace().collect::<Vec<_>>())
.filter(|p| !p.is_empty())
.collect();
assert_eq!(
pages,
vec![
vec!["First"],
vec!["Second", "Third", "Fourth", "Fifth"],
vec!["Sixth", "Seventh"],
vec!["Eighth"],
vec!["Ninth", "con-"],
vec!["struction", "Kept", "Together", "Here"]
]
);
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
eprintln!("pdftotext absent: interline PDF text check skipped")
}
Err(e) => panic!("{e}"),
}
fs::remove_dir_all(directory).unwrap();
}
#[test]
fn native_display_transitions_and_penalties_render_actual_multi_page_pdf() {
let directory = directory();
fs::write(
directory.join("display.tex"),
include_str!("../examples/native-bootstrap/display.tex"),
)
.unwrap();
fs::write(
directory.join("math-fonts.tex"),
include_str!("../examples/native-bootstrap/math-fonts.tex"),
)
.unwrap();
let output = native_fonts::command()
.args(["native", "--initex", "--etex", "display.tex", "display.pdf"])
.current_dir(&directory)
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert!(String::from_utf8_lossy(&output.stdout).contains("3 pages, native Route-D"));
assert!(
String::from_utf8_lossy(&output.stderr).contains("Native display execution completed.")
);
match Command::new("pdftotext")
.args(["-raw", "display.pdf", "-"])
.current_dir(&directory)
.output()
{
Ok(o) => {
assert!(o.status.success());
let text = String::from_utf8(o.stdout).unwrap();
let pages: Vec<_> = text
.split('\u{c}')
.map(|p| p.split_whitespace().collect::<Vec<_>>().join(" "))
.filter(|p| !p.is_empty())
.collect();
assert_eq!(pages.len(), 3, "{text}");
assert!(pages[0].contains("Before forced page."), "{pages:?}");
assert!(pages[1].contains("After forced page."), "{pages:?}");
assert!(
pages[2].contains("After formula starts another page."),
"{pages:?}"
);
assert!(pages[2].contains("Boxed display."), "{pages:?}");
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
eprintln!("pdftotext absent: native display PDF check skipped")
}
Err(e) => panic!("{e}"),
}
fs::remove_dir_all(directory).unwrap();
}