1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
//! Reading DVI, ported from tectonic's `xdv`.
//!
//! texrs has no stomach and writes no DVI: it stops where the boxes would
//! begin. So why read it? Because the parity contract stops there too. Today
//! the harness compares `\message` streams, which is everything the mouth and
//! the expander produce and nothing the rest of TeX does; the moment texrs sets
//! a character, the reference to compare against is what real tex shipped, and
//! that is a DVI file. This is the reading half of that comparison, written
//! now, while the format can be read against a document whose output is known.
//!
//! It is also the honest shape of the port. tectonic's `xdv` is a parser and an
//! event stream, not a typesetter; the typesetter is `engine_xetex`, which is a
//! transpile of Knuth's C and not something to port a line at a time. What can
//! be carried across is the byte format, and the byte format is `dvitype`'s
//! table in `tex.web` §583–590.
//!
//! What is NOT here: the XeTeX extensions (opcodes 252–254, native fonts and
//! glyph runs). They are recognised and named, because pointing this at an
//! `.xdv` should say what it is rather than fail as if the file were damaged,
//! but their operands are XeTeX's business and texrs will never emit them.
use std::path::Path;
/// One thing a DVI file says.
#[derive(Debug, Clone, PartialEq)]
pub enum Op {
/// The preamble: DVI version, and the comment tex stamps in.
Preamble {
version: u8,
comment: String,
},
/// A page begins, carrying `\count0..9` — which is how a page knows its
/// own number.
BeginPage {
counts: [i32; 10],
},
EndPage,
/// A character set in the current font, at the current position.
SetChar(u32),
/// The same, without moving afterwards.
PutChar(u32),
/// A rule: height and width in scaled points.
Rule {
height: i32,
width: i32,
set: bool,
},
Push,
Pop,
/// Movement, in scaled points. Positive right is right, positive down is
/// DOWN the page — the y axis points the other way from the one most
/// graphics formats use.
Right(i32),
Down(i32),
/// A font is selected by number.
Font(u32),
/// A font is defined: its number, its name, and the size it is used at.
DefineFont {
number: u32,
name: String,
at: i32,
},
/// `\special{…}`, which is how a document says something DVI has no word
/// for — a colour, an included graphic.
Special(String),
/// The postamble, with what tex counted while writing.
Postamble {
pages: u16,
max_stack: u16,
},
/// An XDV extension: XeTeX's, and named rather than decoded.
Extension(u8),
Noop,
}
/// What a DVI file holds, in order.
#[derive(Debug, Clone, Default)]
pub struct Dvi {
pub ops: Vec<Op>,
}
impl Dvi {
/// Parse `bytes` as DVI.
pub fn parse(bytes: &[u8]) -> Result<Dvi, String> {
let mut at = 0usize;
let mut ops = Vec::new();
while at < bytes.len() {
let opcode = bytes[at];
at += 1;
let op = match opcode {
// §585: an opcode below 128 IS the character it sets.
0..=127 => Op::SetChar(opcode as u32),
128..=131 => {
let n = (opcode - 127) as usize;
Op::SetChar(read_unsigned(bytes, &mut at, n)?)
}
132 => Op::Rule {
height: read_signed(bytes, &mut at, 4)?,
width: read_signed(bytes, &mut at, 4)?,
set: true,
},
133..=136 => {
let n = (opcode - 132) as usize;
Op::PutChar(read_unsigned(bytes, &mut at, n)?)
}
137 => Op::Rule {
height: read_signed(bytes, &mut at, 4)?,
width: read_signed(bytes, &mut at, 4)?,
set: false,
},
138 => Op::Noop,
139 => {
let mut counts = [0i32; 10];
for count in counts.iter_mut() {
*count = read_signed(bytes, &mut at, 4)?;
}
// The pointer to the previous page, which a sequential read
// does not need.
let _previous = read_signed(bytes, &mut at, 4)?;
Op::BeginPage { counts }
}
140 => Op::EndPage,
141 => Op::Push,
142 => Op::Pop,
// right1..4, w0, w1..4, x0, x1..4 — all horizontal.
143..=146 => Op::Right(read_signed(bytes, &mut at, (opcode - 142) as usize)?),
147 => Op::Right(0),
148..=151 => Op::Right(read_signed(bytes, &mut at, (opcode - 147) as usize)?),
152 => Op::Right(0),
153..=156 => Op::Right(read_signed(bytes, &mut at, (opcode - 152) as usize)?),
// down1..4, y0, y1..4, z0, z1..4 — all vertical.
157..=160 => Op::Down(read_signed(bytes, &mut at, (opcode - 156) as usize)?),
161 => Op::Down(0),
162..=165 => Op::Down(read_signed(bytes, &mut at, (opcode - 161) as usize)?),
166 => Op::Down(0),
167..=170 => Op::Down(read_signed(bytes, &mut at, (opcode - 166) as usize)?),
// §586: the font number is in the opcode, as the character is.
171..=234 => Op::Font((opcode - 171) as u32),
235..=238 => {
let n = (opcode - 234) as usize;
Op::Font(read_unsigned(bytes, &mut at, n)?)
}
239..=242 => {
let n = (opcode - 238) as usize;
let length = read_unsigned(bytes, &mut at, n)? as usize;
Op::Special(read_string(bytes, &mut at, length)?)
}
243..=246 => {
let n = (opcode - 242) as usize;
let number = read_unsigned(bytes, &mut at, n)?;
let _checksum = read_unsigned(bytes, &mut at, 4)?;
let at_size = read_signed(bytes, &mut at, 4)?;
let _design = read_signed(bytes, &mut at, 4)?;
// The name is in two parts, an area and a file, whose
// lengths come first.
let area = read_unsigned(bytes, &mut at, 1)? as usize;
let file = read_unsigned(bytes, &mut at, 1)? as usize;
let name = read_string(bytes, &mut at, area + file)?;
Op::DefineFont {
number,
name,
at: at_size,
}
}
247 => {
let version = read_unsigned(bytes, &mut at, 1)? as u8;
// num, den, mag — the units the file is written in.
for _ in 0..3 {
read_unsigned(bytes, &mut at, 4)?;
}
let length = read_unsigned(bytes, &mut at, 1)? as usize;
Op::Preamble {
version,
comment: read_string(bytes, &mut at, length)?,
}
}
248 => {
// The final page pointer and the units again, then what tex
// counted: the page height and width, the stack depth, and
// the number of pages.
for _ in 0..6 {
read_unsigned(bytes, &mut at, 4)?;
}
let max_stack = read_unsigned(bytes, &mut at, 2)? as u16;
let pages = read_unsigned(bytes, &mut at, 2)? as u16;
Op::Postamble { pages, max_stack }
}
249 => {
let _postamble = read_unsigned(bytes, &mut at, 4)?;
let _version = read_unsigned(bytes, &mut at, 1)?;
// §590: the file is padded with 223s to a multiple of four,
// and the padding is the end of it.
while at < bytes.len() && bytes[at] == 223 {
at += 1;
}
ops.push(Op::Noop);
break;
}
250..=254 => Op::Extension(opcode),
255 => return Err(format!("byte {}: 255 is not a DVI opcode", at - 1)),
};
ops.push(op);
}
Ok(Dvi { ops })
}
/// Read the file at `path`.
pub fn open(path: impl AsRef<Path>) -> Result<Dvi, String> {
let path = path.as_ref();
let bytes =
std::fs::read(path).map_err(|e| format!("cannot read {}: {e}", path.display()))?;
Dvi::parse(&bytes)
}
/// How many pages the file ships.
pub fn pages(&self) -> usize {
self.ops
.iter()
.filter(|op| matches!(op, Op::BeginPage { .. }))
.count()
}
/// The characters set, in order, as text.
///
/// This is the comparison a parity harness will want when texrs can set a
/// character: not the bytes of the file, which carry positions and font
/// numbers that two correct engines may legitimately differ on, but what
/// ended up on the page.
///
/// The codes are the FONT's, not Unicode. DVI says "set character 11 of the
/// current font", and in cmr10 that is the `ff` ligature — so `different`
/// comes back as `di\u{b}erent`. Mapping it would need the font's encoding,
/// which is a `.tfm` file this does not read; for comparing two engines it
/// does not matter, because both write the same code.
pub fn text(&self) -> String {
self.ops
.iter()
.filter_map(|op| match op {
Op::SetChar(c) | Op::PutChar(c) => char::from_u32(*c),
_ => None,
})
.collect()
}
/// A summary a person reads, which is what `-X dvi` prints.
pub fn summary(&self) -> String {
let mut out = String::new();
for op in &self.ops {
match op {
Op::Preamble { version, comment } => {
out.push_str(&format!("preamble version {version}, {comment:?}\n"));
}
Op::BeginPage { counts } => {
out.push_str(&format!("page \\count0={}\n", counts[0]));
}
Op::DefineFont { number, name, at } => {
out.push_str(&format!("font {number:<5} {name} at {at}sp\n"));
}
Op::Special(text) => out.push_str(&format!("special {text:?}\n")),
Op::Postamble { pages, max_stack } => {
out.push_str(&format!("postamble {pages} page(s), stack {max_stack}\n"));
}
Op::Extension(opcode) => {
out.push_str(&format!(
"extension opcode {opcode} (XeTeX; not decoded)\n"
));
}
_ => {}
}
}
let text = self.text();
if !text.is_empty() {
out.push_str(&format!("text {text:?}\n"));
}
out
}
}
/// How two DVI files differ, in the terms a parity harness cares about.
#[derive(Debug, Clone, PartialEq)]
pub enum Difference {
Pages {
left: usize,
right: usize,
},
/// The characters set differ, with the first place they part company.
Text {
at: usize,
left: String,
right: String,
},
/// One file draws a rule the other does not.
Rules {
left: usize,
right: usize,
},
/// A `\special` one carries and the other does not.
Special {
only_in_left: bool,
text: String,
},
/// The fonts the two files ask for.
Fonts {
left: Vec<String>,
right: Vec<String>,
},
}
impl Dvi {
/// The rules drawn, in order.
fn rules(&self) -> Vec<(i32, i32)> {
self.ops
.iter()
.filter_map(|op| match op {
Op::Rule { height, width, .. } => Some((*height, *width)),
_ => None,
})
.collect()
}
/// The specials carried, in order.
fn specials(&self) -> Vec<String> {
self.ops
.iter()
.filter_map(|op| match op {
Op::Special(text) => Some(text.clone()),
_ => None,
})
.collect()
}
/// The fonts asked for, sorted and deduplicated.
fn fonts(&self) -> Vec<String> {
let mut names: Vec<String> = self
.ops
.iter()
.filter_map(|op| match op {
Op::DefineFont { name, .. } => Some(name.clone()),
_ => None,
})
.collect();
names.sort();
names.dedup();
names
}
/// What differs between two DVI files, in what a reader would notice.
///
/// Deliberately NOT a byte comparison. Two engines that typeset the same
/// document identically still write different files: the comment carries a
/// timestamp, the movement opcodes chosen for one displacement are a
/// choice, and the postamble's pointers depend on where things landed. A
/// parity harness that diffed bytes would report all of that as a
/// divergence and none of it would be one.
///
/// What is compared is what a reader would see: how many pages, the
/// characters set, the rules drawn, the specials carried, the fonts asked
/// for. Positions are left out for the same reason — two correct engines
/// may legitimately round a displacement differently — which is why this
/// says "the same document" rather than "the same file".
pub fn compare(&self, other: &Dvi) -> Vec<Difference> {
let mut out = Vec::new();
if self.pages() != other.pages() {
out.push(Difference::Pages {
left: self.pages(),
right: other.pages(),
});
}
let (left_text, right_text) = (self.text(), other.text());
if left_text != right_text {
let at = left_text
.chars()
.zip(right_text.chars())
.position(|(a, b)| a != b)
.unwrap_or(left_text.chars().count().min(right_text.chars().count()));
out.push(Difference::Text {
at,
left: left_text,
right: right_text,
});
}
let (left_rules, right_rules) = (self.rules(), other.rules());
if left_rules.len() != right_rules.len() {
out.push(Difference::Rules {
left: left_rules.len(),
right: right_rules.len(),
});
}
for text in self.specials() {
if !other.specials().contains(&text) {
out.push(Difference::Special {
only_in_left: true,
text,
});
}
}
for text in other.specials() {
if !self.specials().contains(&text) {
out.push(Difference::Special {
only_in_left: false,
text,
});
}
}
let (left_fonts, right_fonts) = (self.fonts(), other.fonts());
if left_fonts != right_fonts {
out.push(Difference::Fonts {
left: left_fonts,
right: right_fonts,
});
}
out
}
}
/// `n` bytes, big-endian, unsigned. DVI is big-endian throughout.
fn read_unsigned(bytes: &[u8], at: &mut usize, n: usize) -> Result<u32, String> {
if *at + n > bytes.len() {
return Err(format!("byte {at}: the file ends inside an operand"));
}
let mut value: u32 = 0;
for byte in &bytes[*at..*at + n] {
value = (value << 8) | *byte as u32;
}
*at += n;
Ok(value)
}
/// The same, signed: the top bit of the FIRST byte is the sign, so a
/// three-byte -1 is `ff ff ff` and not a very large positive number.
fn read_signed(bytes: &[u8], at: &mut usize, n: usize) -> Result<i32, String> {
if *at + n > bytes.len() {
return Err(format!("byte {at}: the file ends inside an operand"));
}
let mut value: i32 = if bytes[*at] & 0x80 != 0 { -1 } else { 0 };
for byte in &bytes[*at..*at + n] {
value = (value << 8) | *byte as i32;
}
*at += n;
Ok(value)
}
fn read_string(bytes: &[u8], at: &mut usize, n: usize) -> Result<String, String> {
if *at + n > bytes.len() {
return Err(format!("byte {at}: the file ends inside a string"));
}
let text = String::from_utf8_lossy(&bytes[*at..*at + n]).into_owned();
*at += n;
Ok(text)
}
#[cfg(test)]
mod tests {
use super::*;
/// What real tex ships for `src`, or `None` when there is no tex here.
fn tex_dvi(name: &str, src: &str) -> Option<Vec<u8>> {
engine_dvi("tex", name, src)
}
/// The same, from whichever engine is named. pdftex needs telling to write
/// DVI rather than PDF; tex has no such flag and ignores it.
fn engine_dvi(engine: &str, name: &str, src: &str) -> Option<Vec<u8>> {
let dir = std::env::temp_dir().join(format!("texrs_dvi_{}_{name}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).ok()?;
std::fs::write(dir.join("t.tex"), src).ok()?;
let mut command = std::process::Command::new(engine);
if engine != "tex" {
command.arg("-output-format=dvi");
}
let ran = command
.arg("-interaction=batchmode")
.arg("t.tex")
.current_dir(&dir)
.output()
.ok()?;
let _ = ran;
let bytes = std::fs::read(dir.join("t.dvi")).ok();
let _ = std::fs::remove_dir_all(&dir);
bytes
}
#[test]
fn a_page_of_real_tex_reads_back_as_its_text() {
let Some(bytes) = tex_dvi("hello", "Hello DVI world.\n\\bye\n") else {
// No tex here: the parity suite needs one too, so this is the same
// skip those tests take rather than a failure of this one.
return;
};
let dvi = Dvi::parse(&bytes).expect("parses");
assert_eq!(dvi.pages(), 1, "one page");
let text = dvi.text();
assert!(text.contains("Hello"), "{text:?}");
assert!(text.contains("world"), "{text:?}");
// The preamble says what wrote it, and the postamble counts what it
// wrote — the two ends of the file agreeing is the check that the walk
// did not lose its place.
let preamble = dvi.ops.first().expect("an op");
assert!(
matches!(preamble, Op::Preamble { version: 2, .. }),
"{preamble:?}"
);
let counted = dvi.ops.iter().find_map(|op| match op {
Op::Postamble { pages, .. } => Some(*pages),
_ => None,
});
assert_eq!(counted, Some(1), "the postamble counts the page");
assert!(
dvi.ops.iter().any(|op| matches!(op, Op::DefineFont { .. })),
"a page that sets characters defines a font"
);
}
#[test]
fn two_pages_are_two_pages() {
let Some(bytes) = tex_dvi("pages", "One.\n\\eject\nTwo.\n\\bye\n") else {
return;
};
let dvi = Dvi::parse(&bytes).expect("parses");
assert_eq!(dvi.pages(), 2);
// `\count0` is the page number, which is what a page carries in its
// first counter.
let numbers: Vec<i32> = dvi
.ops
.iter()
.filter_map(|op| match op {
Op::BeginPage { counts } => Some(counts[0]),
_ => None,
})
.collect();
assert_eq!(numbers, vec![1, 2]);
}
#[test]
fn a_special_survives_as_the_text_it_carried() {
let Some(bytes) = tex_dvi("special", "\\special{color push rgb 1 0 0}A\n\\bye\n") else {
return;
};
let dvi = Dvi::parse(&bytes).expect("parses");
let specials: Vec<&String> = dvi
.ops
.iter()
.filter_map(|op| match op {
Op::Special(text) => Some(text),
_ => None,
})
.collect();
assert_eq!(specials.len(), 1, "{:?}", dvi.summary());
assert!(specials[0].contains("color push"), "{:?}", specials[0]);
}
#[test]
fn two_engines_typesetting_one_document_agree() {
// The comparison the parity contract will need when texrs ships DVI,
// usable today to check one engine against another: tex and pdftex
// write different bytes for the same document — the comment carries a
// timestamp, and the movement opcodes are a choice — and this must
// report no difference all the same.
let Some(from_tex) = tex_dvi("cmp_tex", "Hello DVI world.\n\\bye\n") else {
return;
};
let Some(from_pdftex) = engine_dvi("pdftex", "cmp_pdftex", "Hello DVI world.\n\\bye\n")
else {
return;
};
let left = Dvi::parse(&from_tex).expect("parses");
let right = Dvi::parse(&from_pdftex).expect("parses");
assert_eq!(
left.compare(&right),
vec![],
"the same document typeset by two engines is the same document"
);
// And what the comparison deliberately ignores: the preamble's comment
// carries the time of the run, so two runs a minute apart write
// different bytes for one document. A harness that compared those
// would report a divergence every time the clock ticked.
let mut later = left.clone();
for op in &mut later.ops {
if let Op::Preamble { comment, .. } = op {
*comment = " TeX output 1999.12.31:2359".to_string();
}
}
assert_ne!(later.ops, left.ops, "the files differ");
assert_eq!(later.compare(&left), vec![], "the documents do not");
}
#[test]
fn a_document_that_differs_says_where() {
let Some(one) = tex_dvi("cmp_one", "Hello.\n\\bye\n") else {
return;
};
let Some(two) = tex_dvi("cmp_two", "Hellp.\n\\bye\n") else {
return;
};
let differences = Dvi::parse(&one)
.unwrap()
.compare(&Dvi::parse(&two).unwrap());
match differences.first() {
Some(Difference::Text { at, left, right }) => {
assert_eq!(
*at, 4,
"the fifth character is where they part: {left} {right}"
);
}
other => panic!("expected a text difference, got {other:?}"),
}
// A page more is a difference of its own.
let Some(long) = tex_dvi("cmp_pages", "One.\n\\eject\nTwo.\n\\bye\n") else {
return;
};
let differences = Dvi::parse(&one)
.unwrap()
.compare(&Dvi::parse(&long).unwrap());
assert!(
differences
.iter()
.any(|d| matches!(d, Difference::Pages { left: 1, right: 2 })),
"{differences:?}"
);
}
#[test]
fn the_operands_are_big_endian_and_signed_where_the_format_says() {
// §583: a number is big-endian, and a signed one takes its sign from
// the top bit of the FIRST byte — so three bytes of 0xff are -1 and
// not 16777215.
let mut at = 0;
assert_eq!(read_unsigned(&[0x01, 0x02], &mut at, 2).unwrap(), 0x0102);
let mut at = 0;
assert_eq!(read_signed(&[0xff, 0xff, 0xff], &mut at, 3).unwrap(), -1);
let mut at = 0;
assert_eq!(read_signed(&[0x7f, 0xff], &mut at, 2).unwrap(), 32767);
let mut at = 0;
assert_eq!(read_signed(&[0x80, 0x00], &mut at, 2).unwrap(), -32768);
// A file that ends inside an operand says so rather than reading past.
let mut at = 0;
assert!(read_unsigned(&[0x01], &mut at, 4).is_err());
assert!(Dvi::parse(&[139, 0, 0]).is_err(), "a truncated page");
}
#[test]
fn what_is_not_dvi_is_refused_and_xetex_is_named() {
// 255 is not an opcode in any version of the format.
assert!(Dvi::parse(&[255]).unwrap_err().contains("not a DVI opcode"));
// An empty file parses to nothing rather than failing: it is not
// damaged, it is empty.
assert!(Dvi::parse(&[]).unwrap().ops.is_empty());
// XeTeX's extensions are named rather than decoded, so pointing this
// at an .xdv says what it is.
let dvi = Dvi::parse(&[253]).unwrap();
assert_eq!(dvi.ops, vec![Op::Extension(253)]);
assert!(dvi.summary().contains("XeTeX"), "{}", dvi.summary());
}
}