214 lines
6.7 KiB
Rust
214 lines
6.7 KiB
Rust
//! Native PDF file/stream options and explicit unsupported DVI protection.
|
|
#[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-pdf-config-{}-{}",
|
|
std::process::id(),
|
|
N.fetch_add(1, Ordering::Relaxed)
|
|
));
|
|
fs::create_dir_all(&p).unwrap();
|
|
p
|
|
}
|
|
#[test]
|
|
fn actual_compressed_pdf_objects_fonts_pages_and_late_writes_reach_the_cli() {
|
|
let p = directory();
|
|
let source =
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples/native-bootstrap/pdf-config.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 pdf = fs::read(p.join("native.pdf")).unwrap();
|
|
assert!(pdf.starts_with(b"%PDF-1.7"));
|
|
let text = String::from_utf8_lossy(&pdf);
|
|
assert!(text.contains("/Type /ObjStm") && text.contains("/Type /XRef"));
|
|
let r = Command::new("pdfinfo")
|
|
.arg("native.pdf")
|
|
.current_dir(&p)
|
|
.output()
|
|
.unwrap();
|
|
assert!(r.status.success());
|
|
let info = String::from_utf8_lossy(&r.stdout);
|
|
assert!(
|
|
info.lines()
|
|
.any(|s| s.starts_with("Pages:") && s.ends_with('2')),
|
|
"{info}"
|
|
);
|
|
let r = Command::new("pdftotext")
|
|
.args(["-layout", "native.pdf", "-"])
|
|
.current_dir(&p)
|
|
.output()
|
|
.unwrap();
|
|
assert!(r.status.success());
|
|
assert_eq!(
|
|
String::from_utf8_lossy(&r.stdout)
|
|
.matches("Native compressed pages")
|
|
.count(),
|
|
2
|
|
);
|
|
assert_eq!(
|
|
fs::read(p.join("config.aux")).unwrap(),
|
|
b"PAGE=1;ZIP=9\nPAGE=2;ZIP=0\n"
|
|
);
|
|
let trace: serde_json::Value =
|
|
serde_json::from_slice(&fs::read(p.join("resources.json")).unwrap()).unwrap();
|
|
for suffix in ["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))
|
|
);
|
|
}
|
|
assert_eq!(fs::read(source).unwrap(), before);
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
#[test]
|
|
fn invalid_or_changed_pdf_modes_and_versions_keep_original_pdf_and_source() {
|
|
let p = directory();
|
|
for (body, expected) in [
|
|
(r"\pdfminorversion=10\shipout\hbox{}\end", "PDF version"),
|
|
(r"\shipout\hbox{}\pdfminorversion=7\end", "PDF version"),
|
|
(r"\pdfcompresslevel=10\shipout\hbox{}\end", "compression"),
|
|
(
|
|
r"\shipout\hbox{}\pdfoutput=2\shipout\hbox{}\end",
|
|
"pdfoutput",
|
|
),
|
|
(r"\pdfoutput=0\shipout\hbox{}\end", "DVI"),
|
|
] {
|
|
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));
|
|
assert!(
|
|
String::from_utf8_lossy(&r.stderr).contains(expected),
|
|
"{}",
|
|
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 byte_pdf_profile_uses_real_absolute_comparisons_and_device_options() {
|
|
let dir = directory();
|
|
let source = r"\catcode123=1\catcode125=2
|
|
\ifdefined\pdftexversion
|
|
\ifnum\pdftexversion=140\message{ABI=\the\pdftexversion}\fi
|
|
\let\compare\ifpdfabsnum
|
|
\compare-2147483647>2147483646 \message{NUM=YES}\fi
|
|
\ifpdfabsnum-7=-8 \message{WRONG}\else\message{UNEQUAL=YES}\fi
|
|
\ifpdfabsdim-2pt=2pt \pdfminorversion=7 \message{DIM=YES}\fi
|
|
\else
|
|
\message{ABI=UNAVAILABLE}
|
|
\fi
|
|
\shipout\hbox{}\end";
|
|
fs::write(dir.join("probe.tex"), source).unwrap();
|
|
let reference = Command::new("pdftex")
|
|
.args([
|
|
"-ini",
|
|
"-etex",
|
|
"-output-format=pdf",
|
|
"-interaction=nonstopmode",
|
|
"-halt-on-error",
|
|
"probe.tex",
|
|
])
|
|
.current_dir(&dir)
|
|
.output()
|
|
.unwrap();
|
|
assert!(
|
|
reference.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&reference.stdout)
|
|
);
|
|
assert!(
|
|
fs::read(dir.join("probe.pdf"))
|
|
.unwrap()
|
|
.starts_with(b"%PDF-1.7")
|
|
);
|
|
for (profile, version) in [("tex8bit", b"%PDF-1.7"), ("unicode", b"%PDF-1.4")] {
|
|
if profile == "unicode" {
|
|
fs::write(
|
|
dir.join("probe.tex"),
|
|
r"\catcode123=1\catcode125=2
|
|
\ifdefined\pdftexversion\message{ABI=140}\else\message{ABI=UNAVAILABLE}\fi
|
|
\shipout\hbox{}\end",
|
|
)
|
|
.unwrap();
|
|
}
|
|
let actual = native_fonts::command()
|
|
.args([
|
|
"native",
|
|
"--job",
|
|
"--initex",
|
|
"--etex",
|
|
"--input-profile",
|
|
profile,
|
|
"probe.tex",
|
|
"native.pdf",
|
|
])
|
|
.current_dir(&dir)
|
|
.output()
|
|
.unwrap();
|
|
assert!(
|
|
actual.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&actual.stderr)
|
|
);
|
|
let terminal = String::from_utf8_lossy(&actual.stderr);
|
|
if profile == "tex8bit" {
|
|
let oracle = String::from_utf8_lossy(&reference.stdout);
|
|
for mark in ["ABI=140", "NUM=YES", "UNEQUAL=YES", "DIM=YES"] {
|
|
assert!(
|
|
terminal.contains(mark) && oracle.contains(mark),
|
|
"{terminal}\n{oracle}"
|
|
);
|
|
}
|
|
assert!(!terminal.contains("WRONG"), "{terminal}");
|
|
} else {
|
|
assert!(terminal.contains("ABI=UNAVAILABLE"), "{terminal}");
|
|
}
|
|
assert!(
|
|
fs::read(dir.join("native.pdf"))
|
|
.unwrap()
|
|
.starts_with(version)
|
|
);
|
|
}
|
|
fs::remove_dir_all(dir).unwrap();
|
|
}
|