368 lines
14 KiB
Rust
368 lines
14 KiB
Rust
//! Integer TFM loading checked against real INITEX, including nonzero-bc fonts.
|
|
use std::{
|
|
fs,
|
|
path::Path,
|
|
process::Command,
|
|
sync::atomic::{AtomicU64, Ordering},
|
|
};
|
|
use texrra_tex_core::tfm::{FontSize, LigKernCommand, TfmMetrics};
|
|
fn directory() -> std::path::PathBuf {
|
|
static ID: AtomicU64 = AtomicU64::new(0);
|
|
let p = std::env::temp_dir().join(format!(
|
|
"texrra-tfm-{}-{}",
|
|
std::process::id(),
|
|
ID.fetch_add(1, Ordering::Relaxed)
|
|
));
|
|
fs::create_dir_all(&p).unwrap();
|
|
p
|
|
}
|
|
fn oracle(p: &Path, source: &str, success: bool) -> String {
|
|
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()
|
|
.expect("real INITEX required");
|
|
let text = String::from_utf8(r.stdout).unwrap();
|
|
assert_eq!(r.status.success(), success, "{text}");
|
|
text
|
|
}
|
|
fn query(source: &mut String, expected: &mut Vec<String>, font: &TfmMetrics, id: usize) {
|
|
for c in 0..=255u8 {
|
|
source.push_str(&format!("\\immediate\\write16{{TFM={id}/{c}:\\number\\fontcharwd\\F{c}/\\number\\fontcharht\\F{c}/\\number\\fontchardp\\F{c}/\\number\\fontcharic\\F{c}}}\n"));
|
|
let ch = char::from(c);
|
|
expected.push(format!(
|
|
"TFM={id}/{c}:{}/{}/{}/{}",
|
|
font.width(ch),
|
|
font.height(ch),
|
|
font.depth(ch),
|
|
font.italic(ch)
|
|
));
|
|
}
|
|
for n in 0..font.parameter_count() {
|
|
source.push_str(&format!(
|
|
"\\immediate\\write16{{TFM={id}/P{n}:\\number\\fontdimen{}\\F}}\n",
|
|
n + 1
|
|
));
|
|
expected.push(format!("TFM={id}/P{n}:{}", font.param(n)));
|
|
}
|
|
}
|
|
fn assert_values(text: &str, expected: &[String]) {
|
|
let values: Vec<_> = text.lines().filter(|l| l.starts_with("TFM=")).collect();
|
|
assert_eq!(values.len(), expected.len());
|
|
for (i, (a, b)) in values.iter().zip(expected).enumerate() {
|
|
assert_eq!(a, b, "query row {i}");
|
|
}
|
|
}
|
|
#[test]
|
|
fn real_font_slots_parameters_and_integer_at_scaled_sizes_match_initex() {
|
|
let p = directory();
|
|
let (mut source, mut expected) = (String::from("\\catcode123=1\\catcode125=2\n"), Vec::new());
|
|
let mut id = 0;
|
|
for name in [
|
|
"cmr10", "cmr7", "cmr5", "cmbx10", "cmti10", "cmtt10", "cmmi10", "cmsy10", "cmex10",
|
|
] {
|
|
let r = Command::new("kpsewhich")
|
|
.arg(format!("{name}.tfm"))
|
|
.output()
|
|
.unwrap();
|
|
assert!(r.status.success());
|
|
let path = String::from_utf8(r.stdout).unwrap().trim().to_owned();
|
|
let data = fs::read(&path).unwrap();
|
|
let original = TfmMetrics::parse(&data).unwrap();
|
|
for size in [
|
|
FontSize::Design,
|
|
FontSize::At(12 * 65536),
|
|
FontSize::At(7 * 65536),
|
|
FontSize::At(655361),
|
|
FontSize::At(128 * 65536 + 1),
|
|
FontSize::At(255 * 65536 + 7),
|
|
FontSize::At(1023 * 65536 + 31),
|
|
FontSize::Scaled(1),
|
|
FontSize::Scaled(333),
|
|
FontSize::Scaled(1001),
|
|
FontSize::Scaled(1200),
|
|
FontSize::Scaled(32768),
|
|
] {
|
|
let scaled = original.at_size(size).unwrap();
|
|
assert_eq!(scaled, TfmMetrics::parse_at(&data, size).unwrap());
|
|
let spec = match size {
|
|
FontSize::Design => String::new(),
|
|
FontSize::At(n) => format!(" at{n}sp"),
|
|
FontSize::Scaled(n) => format!(" scaled{n}"),
|
|
};
|
|
source.push_str(&format!("\\font\\F={path}{spec} "));
|
|
query(&mut source, &mut expected, &scaled, id);
|
|
id += 1;
|
|
}
|
|
assert_eq!(fs::read(path).unwrap(), data);
|
|
}
|
|
source.push_str("\\end\n");
|
|
let text = oracle(&p, &source, true);
|
|
assert_values(&text, &expected);
|
|
assert!(expected.len() > 28_000);
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
fn put_u16(bytes: &mut Vec<u8>, n: usize) {
|
|
bytes.extend_from_slice(&(n as u16).to_be_bytes());
|
|
}
|
|
fn put_i32(bytes: &mut Vec<u8>, n: i32) {
|
|
bytes.extend_from_slice(&n.to_be_bytes());
|
|
}
|
|
fn fixture() -> Vec<u8> {
|
|
// Independently authored TFM, accepted by TeX. Slots are 64..90, rather
|
|
// than CM's bc=0. Initial/terminal metadata, restart and skip are distinct.
|
|
let mut bytes = Vec::new();
|
|
for n in [55, 2, 64, 90, 3, 2, 2, 2, 6, 1, 1, 3] {
|
|
put_u16(&mut bytes, n);
|
|
}
|
|
bytes.extend_from_slice(&0x12345678u32.to_be_bytes());
|
|
put_i32(&mut bytes, 10 << 20);
|
|
for c in 64..=90 {
|
|
let (tag, r) = match c {
|
|
65 => (1, 0),
|
|
66 => (1, 4),
|
|
67 => (2, 68),
|
|
69 => (3, 0),
|
|
_ => (0, 0),
|
|
};
|
|
bytes.extend_from_slice(&[if c == 71 { 2 } else { 1 }, 0x11, 4 + tag, r]);
|
|
}
|
|
for table in [
|
|
vec![0, 1 << 19, 1 << 20],
|
|
vec![0, 734003],
|
|
vec![0, 209715],
|
|
vec![0, 104857],
|
|
] {
|
|
for n in table {
|
|
put_i32(&mut bytes, n);
|
|
}
|
|
}
|
|
for i in [
|
|
[255, 255, 0, 1],
|
|
[1, 66, 7, 67],
|
|
[128, 68, 128, 0],
|
|
[128, 69, 128, 0],
|
|
[128, 70, 0, 71],
|
|
[255, 0, 0, 4],
|
|
] {
|
|
bytes.extend_from_slice(&i);
|
|
}
|
|
put_i32(&mut bytes, -65536);
|
|
bytes.extend_from_slice(&[69, 0, 70, 71]);
|
|
for n in [1 << 18, 314572, 104857] {
|
|
put_i32(&mut bytes, n);
|
|
}
|
|
assert_eq!(bytes.len(), 55 * 4);
|
|
bytes
|
|
}
|
|
#[test]
|
|
fn raw_bytecode_restart_skips_boundaries_nonzero_slots_and_slant_are_preserved() {
|
|
let p = directory();
|
|
let bytes = fixture();
|
|
fs::write(p.join("custom.tfm"), &bytes).unwrap();
|
|
let font = TfmMetrics::parse(&bytes).unwrap();
|
|
assert_eq!(font.checksum(), 0x12345678);
|
|
assert_eq!(font.design_size_sp(), 655360);
|
|
assert_eq!(font.parameter_count(), 7);
|
|
assert_eq!(font.param(0), 16384);
|
|
assert_eq!(font.param(6), 0);
|
|
assert_eq!(font.boundary_char(), Some(255));
|
|
assert_eq!(font.false_boundary_char(), Some(255));
|
|
assert_eq!(font.boundary_label(), Some(4));
|
|
assert_eq!(font.lig_kern_program().len(), 6);
|
|
assert_eq!(
|
|
font.lig_kern(Some(65), 66),
|
|
Some(LigKernCommand::Ligature {
|
|
operation: 7,
|
|
replacement: 67
|
|
})
|
|
);
|
|
assert_eq!(font.lig_kern(Some(65), 68), None);
|
|
assert_eq!(font.kern('A', 'E'), -40960);
|
|
assert_eq!(font.ligature_slot('B', 'F'), Some(71));
|
|
assert_eq!(font.ligature_slot('A', 'B'), None);
|
|
assert_eq!(
|
|
font.lig_kern(None, 70),
|
|
Some(LigKernCommand::Ligature {
|
|
operation: 0,
|
|
replacement: 71
|
|
})
|
|
);
|
|
let mut source = String::from("\\catcode123=1\\catcode125=2\n");
|
|
let mut expected = Vec::new();
|
|
for (id, size) in [1, 655360, 786432, 128 * 65536 + 1].into_iter().enumerate() {
|
|
let f = font.at_size(FontSize::At(size)).unwrap();
|
|
source.push_str(&format!("\\font\\F=custom at{size}sp "));
|
|
query(&mut source, &mut expected, &f, id);
|
|
source.push_str("\\setbox0=\\hbox{\\F AE}\\immediate\\write16{TFM=K:\\number\\wd0}\n");
|
|
expected.push(format!(
|
|
"TFM=K:{}",
|
|
f.width('A') + f.width('E') + f.kern('A', 'E')
|
|
));
|
|
source.push_str("\\setbox0=\\hbox{\\F BF}\\immediate\\write16{TFM=L:\\number\\wd0}\n");
|
|
expected.push(format!("TFM=L:{}", f.width('G')));
|
|
source.push_str("\\setbox0=\\hbox{\\F F}\\immediate\\write16{TFM=B:\\number\\wd0}\n");
|
|
expected.push(format!("TFM=B:{}", f.width('G')));
|
|
source.push_str("\\setbox0=\\hbox{\\F AB}\\immediate\\write16{TFM=OP:\\number\\wd0}\n");
|
|
expected.push(format!(
|
|
"TFM=OP:{}",
|
|
f.width('A') + f.width('B') + f.width('C')
|
|
));
|
|
}
|
|
source.push_str("\\end\n");
|
|
assert_values(&oracle(&p, &source, true), &expected);
|
|
let mut boundary = bytes.clone();
|
|
let tail = boundary.len() - 12 - 4 - 4;
|
|
boundary[tail - 1] = 0; // last lig instruction's remainder is the boundary label
|
|
let f = TfmMetrics::parse(&boundary).unwrap();
|
|
assert_eq!(f.boundary_label(), Some(0));
|
|
assert_eq!(f.lig_kern(None, b'B'), None);
|
|
fs::write(p.join("boundary.tfm"), &boundary).unwrap();
|
|
let text = oracle(
|
|
&p,
|
|
"\\catcode123=1\\catcode125=2\\font\\F=boundary \\setbox0=\\hbox{\\F B}\\immediate\\write16{TFM=BOUNDARY:\\number\\wd0}\\end\n",
|
|
true,
|
|
);
|
|
assert!(text.contains(&format!("TFM=BOUNDARY:{}", f.width('B'))));
|
|
let mut chained = bytes.clone();
|
|
let lig = 32 + 27 * 4 + 4 * (3 + 2 + 2 + 2);
|
|
chained[lig + 4..lig + 8].copy_from_slice(&[129, 66, 0, 3]);
|
|
let f = TfmMetrics::parse(&chained).unwrap();
|
|
assert_eq!(f.lig_kern(Some(b'A'), b'E'), None);
|
|
fs::write(p.join("chained.tfm"), &chained).unwrap();
|
|
let text = oracle(
|
|
&p,
|
|
"\\catcode123=1\\catcode125=2\\font\\F=chained \\setbox0=\\hbox{\\F AE}\\immediate\\write16{TFM=CHAIN:\\number\\wd0}\\end\n",
|
|
true,
|
|
);
|
|
assert!(text.contains(&format!("TFM=CHAIN:{}", f.width('A') + f.width('E'))));
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
#[test]
|
|
fn validated_empty_fonts_trailing_bytes_zero_entries_and_design_quantization_match_tex() {
|
|
let p = directory();
|
|
let mut empty = Vec::new();
|
|
for n in [12, 2, 256, 255, 1, 1, 1, 1, 0, 0, 0, 0] {
|
|
put_u16(&mut empty, n);
|
|
}
|
|
empty.extend_from_slice(&[0; 4]);
|
|
put_i32(&mut empty, 1 << 20);
|
|
empty.extend_from_slice(&[0; 16]);
|
|
let font = TfmMetrics::parse(&empty).unwrap();
|
|
assert_eq!(font.parameter_count(), 7);
|
|
assert!(!font.has_slot(0));
|
|
fs::write(p.join("custom.tfm"), &empty).unwrap();
|
|
let mut source = "\\catcode123=1\\catcode125=2\\font\\F=custom ".to_owned();
|
|
let mut expected = Vec::new();
|
|
query(&mut source, &mut expected, &font, 0);
|
|
source.push_str("\\end\n");
|
|
assert_values(&oracle(&p, &source, true), &expected);
|
|
let mut bytes = fixture();
|
|
bytes[28..32].copy_from_slice(&((10i32 << 20) + 15).to_be_bytes());
|
|
bytes.extend_from_slice(b"permitted trailing bytes");
|
|
let font = TfmMetrics::parse(&bytes).unwrap();
|
|
assert_eq!(font.design_size_sp(), 655360);
|
|
assert_eq!(font.width('G'), 655360);
|
|
// e-TeX exposes the range-checked metric fields independently of
|
|
// char_exists: zero width_index does not zero the other three fields.
|
|
bytes[32] = 0;
|
|
let font = TfmMetrics::parse(&bytes).unwrap();
|
|
assert!(!font.has_slot(b'@'));
|
|
assert_eq!(font.width('@'), 0);
|
|
assert_eq!(font.height('@'), font.height('A'));
|
|
fs::write(p.join("custom.tfm"), &bytes).unwrap();
|
|
let mut source = "\\catcode123=1\\catcode125=2\\font\\F=custom ".to_owned();
|
|
let mut expected = Vec::new();
|
|
query(&mut source, &mut expected, &font, 1);
|
|
source.push_str("\\end\n");
|
|
assert_values(&oracle(&p, &source, true), &expected);
|
|
// TeX validates the scaled zero entry; it need not be zero in the file.
|
|
let mut bytes = fixture();
|
|
let width_at = 32 + 27 * 4;
|
|
bytes[width_at..width_at + 4].copy_from_slice(&1i32.to_be_bytes());
|
|
fs::write(p.join("custom.tfm"), &bytes).unwrap();
|
|
assert!(TfmMetrics::parse_at(&bytes, FontSize::At(1)).is_ok());
|
|
assert!(TfmMetrics::parse_at(&bytes, FontSize::At(32 * 65536)).is_err());
|
|
oracle(&p, "\\font\\F=custom at1sp \\end\n", true);
|
|
oracle(&p, "\\font\\F=custom at32pt \\end\n", false);
|
|
// A scaled request has its own ratio bound, and its resolved size must
|
|
// still stay below the real pdfTeX 2048pt metric-loader limit.
|
|
let mut large = fixture();
|
|
large[28..32].copy_from_slice(&(100i32 << 20).to_be_bytes());
|
|
fs::write(p.join("custom.tfm"), &large).unwrap();
|
|
let font = TfmMetrics::parse_at(&large, FontSize::Scaled(20000)).unwrap();
|
|
assert_eq!(font.size_sp(), 2000 * 65536);
|
|
let mut source = "\\catcode123=1\\catcode125=2\\font\\F=custom scaled20000 ".to_owned();
|
|
let mut expected = Vec::new();
|
|
query(&mut source, &mut expected, &font, 2);
|
|
source.push_str("\\end\n");
|
|
assert_values(&oracle(&p, &source, true), &expected);
|
|
assert!(TfmMetrics::parse_at(&large, FontSize::Scaled(32768)).is_err());
|
|
oracle(&p, "\\font\\F=custom scaled32768 \\end\n", false);
|
|
let mut negative = fixture();
|
|
let param = negative.len() - 12;
|
|
negative[param..param + 4].copy_from_slice(&(-1i32).to_be_bytes());
|
|
let font = TfmMetrics::parse_at(&negative, FontSize::At(1)).unwrap();
|
|
assert_eq!(font.param(0), -1);
|
|
fs::write(p.join("custom.tfm"), &negative).unwrap();
|
|
assert!(oracle(&p,"\\catcode123=1\\catcode125=2\\font\\F=custom at1sp \\immediate\\write16{TFM=SLANT:\\number\\fontdimen1\\F}\\end\n",true).contains("TFM=SLANT:-1"));
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
#[test]
|
|
fn malformed_font_tables_programs_and_sizes_are_rejected_by_both_loaders() {
|
|
let p = directory();
|
|
let base = fixture();
|
|
let width_at = 32 + 27 * 4;
|
|
let lig_at = width_at + 4 * (3 + 2 + 2 + 2);
|
|
let kern_at = lig_at + 4 * 6;
|
|
let ext_at = kern_at + 4;
|
|
let mut cases = Vec::new();
|
|
for (offset, data) in [
|
|
(0, vec![0, 54]),
|
|
(2, vec![0, 1]),
|
|
(4, vec![1, 1]),
|
|
(28, vec![0, 0, 0, 0]),
|
|
(32, vec![99]),
|
|
(32 + 4 + 3, vec![255]),
|
|
(32 + 4 * 3 + 2, vec![7]),
|
|
(32 + 4 * 3 + 3, vec![67]),
|
|
(width_at, vec![1, 0, 0, 0]),
|
|
(lig_at + 3, vec![99]),
|
|
(lig_at + 4 + 1, vec![99]),
|
|
(lig_at + 4 + 3, vec![99]),
|
|
(lig_at + 4, vec![127]),
|
|
(lig_at + 12 + 3, vec![2]),
|
|
(kern_at, vec![128]),
|
|
(ext_at + 3, vec![99]),
|
|
] {
|
|
let mut bytes = base.clone();
|
|
bytes[offset..offset + data.len()].copy_from_slice(&data);
|
|
cases.push(bytes);
|
|
}
|
|
cases.push(base[..base.len() - 1].to_vec());
|
|
for (i, bytes) in cases.iter().enumerate() {
|
|
assert!(TfmMetrics::parse(bytes).is_err(), "malformed case{i}");
|
|
fs::write(p.join("custom.tfm"), bytes).unwrap();
|
|
oracle(&p, "\\font\\F=custom \\end\n", false);
|
|
}
|
|
fs::write(p.join("custom.tfm"), &base).unwrap();
|
|
for (size, spec) in [
|
|
(FontSize::At(0), "at0pt"),
|
|
(FontSize::At(-1), "at-1sp"),
|
|
(FontSize::At(2048 * 65536), "at2048pt"),
|
|
(FontSize::Scaled(0), "scaled0"),
|
|
(FontSize::Scaled(32769), "scaled32769"),
|
|
] {
|
|
assert!(TfmMetrics::parse_at(&base, size).is_err());
|
|
oracle(&p, &format!("\\font\\F=custom {spec} \\end\n"), false);
|
|
}
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|