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
//! What leaves the stomach: `ship_out` (§638-§641), and the state the page
//! builder and the output routine share with the rest of the run (§980-§1028).
//!
//! A shipped page is kept as the box it is, with the `\count0..9` it was
//! shipped under and the `\hoffset`/`\voffset` in force, and drawn into the
//! output file when the run is over ([`Output::pages`]). Drawing it at once
//! would buy nothing -- §640's `hlist_out` reads nothing but the box and the
//! fonts -- and keeping it lets `--dvi` and `--pdf` draw the same pages.
use super::{Kind, Mode, Said, SlotSink, Stomach};
use crate::glue::Glue;
use crate::node::{BoxNode, Scaled, MAX_DIMEN};
/// One page as §640 ships it.
#[derive(Clone, Debug)]
pub struct Shipped {
pub page: BoxNode,
pub counts: [i32; 10],
pub h_offset: Scaled,
pub v_offset: Scaled,
}
/// The output side of the run.
#[derive(Clone, Debug, Default)]
pub struct Output {
/// Every page shipped, in order.
pub pages: Vec<Shipped>,
/// `max_v` and `max_h` (§641): the postamble's page extent.
pub max_v: Scaled,
pub max_h: Scaled,
/// `dead_cycles` (§592): outputs in a row that shipped nothing.
pub dead_cycles: i64,
/// `insert_penalties` (§982).
pub insert_penalties: i64,
/// `\outputpenalty`, as `fire_up` last set it (§1013).
pub output_penalty: i64,
/// `\splitfirstmark` and `\splitbotmark` (§382, §979).
pub split_first_mark: Option<String>,
pub split_bot_mark: Option<String>,
/// The numbers of the five marks (§382), for the mirror.
pub mark_ids: [i64; 5],
/// `last_penalty`, `last_kern` and `last_glue` (§982): what the page
/// builder last took off the contribution list, which `\lastpenalty`
/// and its siblings read when the outer list is empty (§424).
pub last_penalty: i64,
pub last_kern: Scaled,
pub last_glue: Option<Glue>,
/// `page_so_far` (§982), for `\pagegoal` and its siblings.
pub page_goal: Scaled,
pub page_total: Scaled,
pub page_stretch: [Scaled; 4],
pub page_shrink: Scaled,
pub page_depth: Scaled,
/// The file whatsits (`crate::node::OutNode`) of the page just shipped,
/// in the order §619-§637 meet them, for the run to carry out as §1373's
/// `out_what` does (`crate::stomach::run::out_what`).
pub out_whats: Vec<usize>,
/// §639's `]`, held until those have been carried out: a `\write` to the
/// terminal prints between the page number and the bracket.
pub page_end_held: bool,
}
impl Output {
/// Write the page builder's values a document can read into their slots.
pub fn mirror(&self, out: &mut dyn SlotSink) {
use super::Internal::*;
out.set(PageGoal.slot(), self.page_goal);
out.set(PageTotal.slot(), self.page_total);
for (o, v) in self.page_stretch.iter().enumerate() {
out.set(PageStretch(o as u8).slot(), *v);
}
out.set(PageShrink.slot(), self.page_shrink);
out.set(PageDepth.slot(), self.page_depth);
out.set(DeadCycles.slot(), self.dead_cycles);
out.set(InsertPenalties.slot(), self.insert_penalties);
out.set(OutputPenalty.slot(), self.output_penalty);
for (k, id) in self.mark_ids.iter().enumerate() {
out.set(Mark(k as u8).slot(), *id);
}
}
/// §424 for the outer vertical list once the page builder has emptied it.
pub fn last_item(&self) -> (i64, Scaled, Glue) {
(
self.last_penalty,
self.last_kern,
self.last_glue.unwrap_or_default(),
)
}
/// `last_glue=max_halfword` (§1106): no glue was the last thing the page
/// builder took.
pub fn last_glue_is_none(&self) -> bool {
self.last_glue.is_none()
}
}
/// The file whatsits of `list`, in the order `hlist_out` and `vlist_out`
/// (§619-§637) meet them. The box a leader repeats is left out: §1374 does
/// nothing while `doing_leaders`. A mark, an insertion or an adjustment
/// inside a box is not drawn, and neither is what it holds.
fn out_whatsits(list: &[crate::node::Node], into: &mut Vec<usize>) {
for node in list {
match node {
crate::node::Node::Out(w) => into.push(w.action),
crate::node::Node::Box(b) => out_whatsits(&b.list, into),
_ => {}
}
}
}
impl Stomach {
/// `ship_out(p)` (§638-§641), with `h_offset`/`v_offset` as they stand.
pub fn ship_out_box(&mut self, p: BoxNode) {
// §638: `[` and the counts that are not trailing zeros, joined by dots.
let mut j = 9;
while self.counts[j] == 0 && j > 0 {
j -= 1;
}
let shown: Vec<String> = self.counts[..=j].iter().map(|c| c.to_string()).collect();
let number = format!("[{}", shown.join("."));
let note = format!("{number}]");
let tracing = self.int_par("tracingoutput") > 0;
if tracing {
// §638: the page number follows on the same line after a space,
// since `term_offset>0` once the heading is printed.
let mut text = String::from("\nCompleted box being shipped out ");
text.push_str(¬e);
text.push_str(
&self.show_box_text(std::slice::from_ref(&crate::node::Node::Box(p.clone()))),
);
self.diagnostic(&text);
}
let (h_offset, v_offset) = (self.h_offset, self.v_offset);
let mut whats = Vec::new();
// §641: a page more than `max_dimen` in any direction is not shipped.
if p.height > MAX_DIMEN
|| p.depth > MAX_DIMEN
|| p.height + p.depth + v_offset > MAX_DIMEN
|| p.width + h_offset > MAX_DIMEN
{
self.say(Said::Error("Huge page cannot be shipped out".into()));
} else {
let out = &mut self.output;
out.max_v = out.max_v.max(p.height + p.depth + v_offset);
out.max_h = out.max_h.max(p.width + h_offset);
let mut counts = [0i32; 10];
for (c, v) in counts.iter_mut().zip(self.counts.iter()) {
*c = *v as i32;
}
// §640: `vlist_out` or `hlist_out` meets each file whatsit as it
// draws the page.
out_whatsits(&p.list, &mut whats);
out.pages.push(Shipped {
page: p,
counts,
h_offset,
v_offset,
});
}
// §639: `]` after the page under `\tracingoutput<=0`; it was printed
// with the box display otherwise.
if !tracing {
match whats.is_empty() {
true => self.say(Said::Terminal(note)),
false => {
self.say(Said::Terminal(number));
self.output.page_end_held = true;
}
}
}
self.output.out_whats.extend(whats);
// §640: `dead_cycles:=0`, which is the page builder's to count.
self.output.dead_cycles = 0;
if let Some(b) = self.pages.builder.as_mut() {
b.shipped();
}
}
/// `show_box` (§198) of `list` with `\showboxdepth` and
/// `\showboxbreadth` as they stand, naming fonts by their identifiers.
pub fn show_box_text(&self, list: &[crate::node::Node]) -> String {
let esc = self.esc_text();
let d = crate::node::show::Display {
depth: self.int_par("showboxdepth"),
breadth: self.int_par("showboxbreadth"),
esc: &esc,
fonts: self,
};
d.show_box(list)
}
}
impl crate::node::show::FontIds for Stomach {
fn font_id_text(&self, f: usize) -> String {
self.font(f).id_text.clone()
}
}
/// The page builder's half of the run (§980-§1028), over `crate::page`.
#[derive(Default)]
pub struct Pages {
builder: Option<crate::page::PageBuilder>,
/// §1025 has fired and the output routine has not run yet.
pub output_pending: bool,
/// `\end` is waiting for §1054's `its_all_over`.
pub end_requested: bool,
/// Whether `\output` is non-empty: the program says so before every
/// action that can build the page.
pub has_output_routine: bool,
}
impl Stomach {
fn builder(&mut self) -> &mut crate::page::PageBuilder {
let b = self.pages.builder.get_or_insert_with(|| {
crate::page::PageBuilder::new(crate::page::PageParams::default())
});
b
}
/// Read §980's frozen-at-first-box parameters as they stand now; the
/// builder freezes them itself when the page gets its first box.
fn refresh_page_params(&mut self, e: &dyn super::Eqtb) {
let tol = self.tolerances(e);
let has = self.pages.has_output_routine;
let holding = self.int_par("holdinginserts");
let dead = self.int_par("maxdeadcycles");
let p = crate::page::PageParams {
vsize: super::dimen_par(e, "vsize"),
max_depth: super::dimen_par(e, "maxdepth"),
top_skip: super::glue_par(e, "topskip"),
split_top_skip: super::glue_par(e, "splittopskip"),
split_max_depth: super::dimen_par(e, "splitmaxdepth"),
holding_inserts: holding,
max_dead_cycles: dead,
tolerances: tol,
inserts: self.insert_classes(e),
};
let b = self.builder();
b.params = p;
b.has_output_routine = has;
}
/// §981: the registers of every insertion class the contributions or the
/// page hold, read from `\count n`, `\dimen n` and `\skip n`.
fn insert_classes(
&self,
e: &dyn super::Eqtb,
) -> std::collections::BTreeMap<u8, crate::page::InsertClass> {
let mut classes = std::collections::BTreeMap::new();
for node in self.nest[0].list.iter() {
if let crate::node::Node::Ins(ins) = node {
let n = ins.number;
classes
.entry(n)
.or_insert_with(|| crate::page::InsertClass {
count: e.slot(i64::from(n)),
dimen: e.slot(crate::compiler::DIMEN_BASE + i64::from(n)),
skip: super::glue_at(
e,
crate::compiler::SKIP_BASE
+ i64::from(n) * crate::compiler::SKIP_STRIDE,
),
});
}
}
if let Some(b) = &self.pages.builder {
for (n, c) in &b.params.inserts {
classes.entry(*n).or_insert(*c);
}
}
classes
}
/// §994's `build_page`: move the outer list onto the page, and ship or
/// hand to `\output` what the builder fires.
pub fn build_page(&mut self, e: &dyn super::Eqtb) {
if self.output_active() {
return;
}
crate::node::hooks::buildpage_filter(self.build_page_reason);
self.refresh_page_params(e);
let contributions = std::mem::take(&mut self.nest[0].list);
// The insertion boxes are the document's registers; the builder
// reads and writes them through its own table while it runs.
self.lend_insertion_boxes();
let fired = self.builder().contribute(contributions);
self.handle_fired(fired);
}
fn lend_insertion_boxes(&mut self) {
let classes: Vec<u8> = self
.pages
.builder
.as_ref()
.map(|b| b.params.inserts.keys().copied().collect())
.unwrap_or_default();
for n in classes {
let value = self.box_register(u16::from(n)).cloned();
let b = self.builder();
match value {
Some(v) => {
b.boxes.insert(n, v);
}
None => {
b.boxes.remove(&n);
}
}
}
}
/// The builder's boxes back into the registers: `\box255` and every
/// insertion class (§1017-§1022 assign them directly, as `box(n):=`).
fn take_back_boxes(&mut self) {
let Some(b) = self.pages.builder.as_mut() else {
return;
};
let boxes = std::mem::take(&mut b.boxes);
let classes: Vec<u8> = b.params.inserts.keys().copied().collect();
for n in classes {
if !boxes.contains_key(&n) {
self.set_box(u16::from(n), None, true);
}
}
for (n, v) in boxes {
self.set_box(u16::from(n), Some(v), true);
}
}
fn handle_fired(&mut self, fired: Vec<crate::page::Fired>) {
self.take_back_boxes();
self.mirror_page();
for f in fired {
match f {
crate::page::Fired::ShipOut(page) => {
self.set_box(255, None, true);
self.ship_out_box(page);
}
crate::page::Fired::Output {
output_penalty,
dead_cycles,
..
} => {
// §1025: the routine runs with `\box255` full, on a list
// of its own.
self.output.output_penalty = output_penalty;
self.output.dead_cycles = dead_cycles;
let mut l =
super::ListState::new(super::Mode::InternalVertical, super::Opened::Output);
l.mode_line = -self.line;
self.nest.push(l);
self.group_begin();
self.pages.output_pending = true;
return;
}
}
}
}
/// §1026: the output routine has ended; what it left on its list goes
/// back in front of the contributions, and the builder looks again.
pub fn end_output(&mut self, e: &dyn super::Eqtb) {
if self.mode() == super::Mode::Horizontal {
self.end_graf(e);
}
self.group_end();
if !matches!(self.cur().opened, super::Opened::Output) {
return;
}
// §1028.
if self.box_register(255).is_some() {
self.say(Said::Error(
"Output routine didn't use all of \\box255".into(),
));
self.set_box(255, None, true);
}
let Some(l) = self.pop_list() else {
return;
};
self.pages.output_pending = false;
self.output.insert_penalties = 0;
self.refresh_page_params(e);
self.lend_insertion_boxes();
crate::node::hooks::buildpage_filter("after_output");
let fired = self.builder().resume_output(l.list);
self.handle_fired(fired);
}
/// §1054's `its_all_over`, after `\end`: true when there is nothing left
/// to ship; otherwise the residue is pushed out with `\hbox to\hsize{}
/// \vfill\penalty-'10000000000` and the page built again.
pub fn its_all_over(&mut self, e: &dyn super::Eqtb) -> bool {
let page_empty = self
.pages
.builder
.as_ref()
.is_none_or(|b| b.current_page().is_empty() && !b.contributions_pending());
if page_empty && self.nest[0].list.is_empty() && self.output.dead_cycles == 0 {
return true;
}
let hsize = super::dimen_par(e, "hsize");
self.tail_append(crate::node::Node::Box(BoxNode {
width: hsize,
..BoxNode::null()
}));
self.tail_append(crate::node::Node::Glue(crate::node::GlueNode::new(
crate::box_::fill::fill(),
)));
self.tail_append(crate::node::Node::Penalty(-0o10000000000));
self.build_page_reason = "end";
self.build_page(e);
false
}
/// The page builder's readable values into [`Output`], for the mirror.
pub(super) fn mirror_page(&mut self) {
let id = |m: &Option<String>| {
m.as_ref()
.and_then(|t| self.mark_ids.get(t))
.map_or(0, |&n| i64::from(n))
};
let split = [
id(&self.output.split_first_mark),
id(&self.output.split_bot_mark),
];
let page = match self.pages.builder.as_ref() {
Some(b) => [id(&b.top_mark), id(&b.first_mark), id(&b.bot_mark)],
None => [0; 3],
};
self.output.mark_ids = [page[0], page[1], page[2], split[0], split[1]];
let Some(b) = self.pages.builder.as_ref() else {
return;
};
use crate::page::PageDimen;
let out = &mut self.output;
out.page_goal = b.page_so_far(PageDimen::Goal);
out.page_total = b.page_so_far(PageDimen::Total);
for (o, s) in out.page_stretch.iter_mut().enumerate() {
*s = b.page_so_far(PageDimen::Stretch(o));
}
out.page_shrink = b.page_so_far(PageDimen::Shrink);
out.page_depth = b.page_so_far(PageDimen::Depth);
out.insert_penalties = b.insert_penalties();
out.last_penalty = b.last_penalty;
out.last_kern = b.last_kern;
out.last_glue = b.last_glue;
}
}
impl Stomach {
/// The DVI file of the pages shipped (§617-§643), as tex writes it; `None`
/// when no page was shipped, for which tex writes no file (§642).
pub fn dvi(&self) -> Option<Vec<u8>> {
if self.output.pages.is_empty() {
return None;
}
// §617: the comment is stamped from `\year`, `\month`, `\day` and
// `\time` as the run left them.
let time = self.int_par("time");
let comment = format!(
" TeX output {}.{:02}.{:02}:{:02}{:02}",
self.int_par("year"),
self.int_par("month"),
self.int_par("day"),
time / 60,
time % 60
);
let fonts = self
.fonts
.iter()
.enumerate()
.map(|(f, font)| match f {
0 => None,
_ => Some(crate::dvi::out::FontDef {
checksum: font.checksum,
size: font.size as i32,
design_size: font.design_size as i32,
area: String::new(),
name: font.name.clone(),
}),
})
.collect();
let mag = self.int_par("mag") as i32;
let mut out = crate::dvi::out::Out::new(&comment, mag, fonts);
for p in &self.output.pages {
crate::shipout::ship_out(&mut out, &p.page, p.counts, p.h_offset, p.v_offset);
}
out.max_v = self.output.max_v as i32;
out.max_h = self.output.max_h as i32;
Some(out.finish())
}
}
impl Stomach {
/// `show_activities` (§218-§219): every level of the semantic nest,
/// innermost first, with the page's status at the bottom (§986), as
/// `\showlists` displays it.
pub fn show_activities(&self) -> String {
let esc = self.esc_text();
let d = crate::node::show::Display {
depth: self.int_par("showboxdepth"),
breadth: self.int_par("showboxbreadth"),
esc: &esc,
fonts: self,
};
let mut out = String::from("\n");
for (p, l) in self.nest.iter().enumerate().rev() {
let mode = match l.mode {
Mode::Vertical => "vertical",
Mode::Horizontal => "horizontal",
Mode::DisplayMath => "display math",
Mode::InternalVertical => "internal vertical",
Mode::RestrictedHorizontal => "restricted horizontal",
Mode::Math => "math",
};
out.push_str(&format!(
"\n### {mode} mode entered at line {}",
l.mode_line.abs()
));
// §218: the language and hyphen minima a paragraph began with,
// when they are not INITEX's.
if l.mode == Mode::Horizontal && l.prev_graf != 0o40600000 {
out.push_str(&format!(
" (language{}:hyphenmin{},{})",
l.prev_graf % 0o200000,
l.prev_graf / 0o20000000,
(l.prev_graf / 0o200000) % 0o100
));
}
if l.mode_line < 0 {
out.push_str(&format!(" ({esc}output routine)"));
}
if p == 0 {
if let Some(b) = self.pages.builder.as_ref() {
out.push_str(&b.status(&|list| d.show_box(list), &esc));
}
if !l.list.is_empty() {
out.push_str("\n### recent contributions:");
}
}
match l.mode.kind() {
Kind::M => {
let mut text = String::new();
let mut history = String::new();
d.show_mlist_from(&mut text, &l.mlist, &mut history);
out.push_str(&text);
}
_ => out.push_str(d.show_box(&l.list).trim_end_matches('\n')),
}
// §219.
match l.mode.kind() {
Kind::V => {
out.push_str("\nprevdepth ");
match l.prev_depth <= crate::node::IGNORE_DEPTH {
true => out.push_str("ignored"),
false => out.push_str(&crate::dimen::print_scaled(l.prev_depth)),
}
if l.prev_graf != 0 {
out.push_str(&format!(", prevgraf {} line", l.prev_graf));
if l.prev_graf != 1 {
out.push('s');
}
}
}
Kind::H => {
out.push_str(&format!("\nspacefactor {}", l.space_factor));
// `clang`, which §1091 stores with the hyphen minima.
let lang = l.prev_graf % 0o200000;
if l.mode.outer() && lang > 0 {
out.push_str(&format!(", current language {lang}"));
}
}
Kind::M => {
if let Some(f) = &l.incompleat {
out.push_str("this will begin denominator of:");
let mut history = String::new();
let frac = [crate::math::noad::Noad::Fraction(f.clone())];
d.show_mlist_from(&mut out, &frac, &mut history);
}
}
}
}
out
}
}
impl Stomach {
/// §1246's `alter_integer`: `\deadcycles=` (0) and `\insertpenalties=`
/// (1), which the page builder holds.
pub fn alter_integer(&mut self, which: u8, v: i64) {
let b = self.builder();
match which {
0 => b.set_dead_cycles(v),
_ => b.set_insert_penalties(v),
}
if which == 0 {
self.output.dead_cycles = v;
}
self.mirror_page();
}
/// §1245's `alter_page_so_far`: `\pagegoal=` and its seven siblings, in
/// `page_so_far` order (goal, total, the four stretches, shrink, depth).
pub fn alter_page_so_far(&mut self, which: u8, v: Scaled) {
use crate::page::PageDimen;
let d = match which {
0 => PageDimen::Goal,
1 => PageDimen::Total,
2..=5 => PageDimen::Stretch(usize::from(which - 2)),
6 => PageDimen::Shrink,
_ => PageDimen::Depth,
};
self.builder().set_page_so_far(d, v);
self.mirror_page();
}
}