Files
texrra-engine/tests/native_device.rs
T

222 lines
7.4 KiB
Rust

//! Actual native PDF device state, without changing original TeX geometry.
#[path = "support/native_fonts.rs"]
mod native_fonts;
use std::{
fs,
path::PathBuf,
process::Command,
sync::atomic::{AtomicU64, Ordering},
};
fn directory() -> PathBuf {
static N: AtomicU64 = AtomicU64::new(0);
let p = std::env::temp_dir().join(format!(
"texrra-native-device-{}-{}",
std::process::id(),
N.fetch_add(1, Ordering::Relaxed)
));
fs::create_dir_all(&p).unwrap();
p
}
#[test]
fn real_device_fixture_keeps_each_physical_page_and_actual_late_aux_writes() {
let p = directory();
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let source = root.join("examples/native-bootstrap/device.tex");
let before = fs::read(&source).unwrap();
let r = native_fonts::command()
.args([
"native",
"--job",
"--initex",
"--etex",
"--resource-manifest",
"resources.json",
])
.arg(&source)
.arg("native.pdf")
.current_dir(&p)
.output()
.unwrap();
assert!(r.status.success(), "{}", String::from_utf8_lossy(&r.stderr));
let r = Command::new("pdfinfo")
.args(["-f", "1", "-l", "3", "native.pdf"])
.current_dir(&p)
.output()
.unwrap();
assert!(r.status.success());
let text = String::from_utf8(r.stdout).unwrap();
let sizes: Vec<Vec<f64>> = text
.lines()
.filter(|s| s.starts_with("Page"))
.filter_map(|s| {
s.split_once("size:").map(|(_, s)| {
s.split_whitespace()
.take(3)
.filter_map(|s| s.parse().ok())
.collect()
})
})
.collect();
assert_eq!(sizes.len(), 3, "{text}");
assert_eq!(sizes[0], [320.0, 240.0]);
assert_eq!(sizes[1], [280.0, 210.0]);
assert!((sizes[2][0] - 255.104).abs() < 0.002, "{text}");
assert_eq!(
fs::read(p.join("device.aux")).unwrap(),
b"SHIP=1;MAG=1200\nSHIP=2;MAG=1200\nSHIP=3;MAG=1200\n"
);
let trace: serde_json::Value =
serde_json::from_slice(&fs::read(p.join("resources.json")).unwrap()).unwrap();
for suffix in [
"device.tex",
"math-fonts.tex",
"cmr10.tfm",
"cmmi7.tfm",
"cmr10.pfb",
"cmmi7.pfb",
] {
assert!(
trace["reads"]
.as_array()
.unwrap()
.iter()
.any(|v| v["path"].as_str().unwrap().ends_with(suffix)),
"missing {suffix}"
);
}
assert_eq!(fs::read(source).unwrap(), before);
fs::remove_dir_all(p).unwrap();
}
#[test]
fn illegal_or_incompatible_magnification_and_huge_pages_protect_existing_pdf() {
let p = directory();
for (body, expected) in [
(r"\mag=0\shipout\hbox{}\end", "magnification"),
(r"\mag=-1\dimen0=1truept\end", "magnification"),
(r"\mag=32769\shipout\hbox{}\end", "magnification"),
(
r"\mag=1200\dimen0=1truept\mag=1000\shipout\hbox{}\end",
"magnification",
),
(
r"\shipout\hbox{}\mag=1200\shipout\hbox{}\end",
"magnification",
),
(
r"\hoffset=16383pt\shipout\hbox{\vrule width100pt}\end",
"huge native page",
),
(r"\pdfgamma=1000\end", "undefined native control sequence"),
] {
let source = format!("\\catcode123=1\\catcode125=2\n{body}\n");
fs::write(p.join("probe.tex"), &source).unwrap();
fs::write(p.join("native.pdf"), b"KEEP").unwrap();
let r = native_fonts::command()
.args([
"native",
"--job",
"--initex",
"--etex",
"probe.tex",
"native.pdf",
])
.current_dir(&p)
.output()
.unwrap();
assert_eq!(r.status.code(), Some(1), "{body}");
assert!(
String::from_utf8_lossy(&r.stderr).contains(expected),
"{body}: {}",
String::from_utf8_lossy(&r.stderr)
);
assert_eq!(fs::read(p.join("native.pdf")).unwrap(), b"KEEP");
assert_eq!(fs::read_to_string(p.join("probe.tex")).unwrap(), source);
}
fs::remove_dir_all(p).unwrap();
}
#[test]
fn xdv_page_setup_edit_updates_the_local_sheet_and_inherited_origin() {
use std::sync::Arc;
use texrra_engine::native::{
CompileRequest, Entry, NativeCompiler, ProjectDefinition, ProjectSnapshot,
};
use texrra_tex_core::{
execution::{InputProfile, JobTime},
resources::WorldConfig,
};
let root = directory().canonicalize().unwrap();
let entry = root.join("probe.tex");
let sheet = root.join("sheet.tex");
let prefix = r"\catcode123=1 \catcode125=2
\pdfpagewidth=11182899sp \pdfpageheight=16445440sp
\shipout\hbox{\special{pdf:pagesize width 170bp height 250bp xoffset 24bp}\vrule width10pt height4pt}
";
let padding = format!("{}\n", "\\relax ".repeat(32)).repeat(48);
let tail = r"\input sheet.tex
\shipout\hbox{\sheet\vrule width10pt height4pt}
\shipout\hbox{\vrule width10pt height4pt}
\shipout\hbox{\special{pdf:pagesize default}\vrule width10pt height4pt}
\end
";
let from_sheet = |width, x| -> Arc<[u8]> {
format!("\\def\\sheet{{\\special{{pdf:pagesize width {width}bp xoffset {x}bp}}}}\n")
.into_bytes()
.into()
};
let world = WorldConfig::new(&root);
let snapshot = ProjectSnapshot::new(ProjectDefinition {
configuration: world.clone(),
revision: 1,
roots: [root.clone()].into(),
files: [
(
entry.clone(),
format!("{prefix}{padding}{tail}").into_bytes().into(),
),
(sheet.clone(), from_sheet(190, 36)),
]
.into(),
directories: Default::default(),
aliases: Default::default(),
generated_roots: [root.clone()].into(),
generated: Default::default(),
})
.unwrap();
let time = JobTime::new(2026, 11, 27, 0).unwrap();
let mut request = CompileRequest::new(world, entry, "result.pdf", time);
request.entry = Entry::Initex;
request.input_profile = InputProfile::XeTeX;
request.etex = true;
let compiler = NativeCompiler::default().with_entry_checkpoints(prefix.len() + 60);
let first = compiler
.compile(request.clone().with_project(snapshot.clone()))
.unwrap();
let updated = snapshot
.with_file_edits(2, [(sheet, from_sheet(205, 46))].into())
.unwrap();
let revised = request.with_project(updated);
let next = compiler.compile(revised.clone()).unwrap();
assert!(next.entry_checkpoint_work.reused_steps > 0);
let widths: Vec<_> = next.pages.iter().map(|page| page.width).collect();
assert_eq!(
widths,
[170, 205, 170, 170].map(|bp| (f64::from(bp) * 65_781.76).round() as i64)
);
let delta = next.preview_since(Some(first.revision())).unwrap();
let changed: Vec<_> = delta.pages.iter().map(|page| page.index).collect();
assert_eq!(changed, [1, 2]);
let full = NativeCompiler::default().compile(revised).unwrap();
assert_eq!(next.pdf, full.pdf);
assert_eq!(next.source_map.pages, full.source_map.pages);
assert_eq!(
next.preview_since(None).unwrap().pages,
full.preview_since(None).unwrap().pages
);
fs::remove_dir_all(root).unwrap();
}