Debian Codegraph @

texk/web2c/luatexdir/tex/buildpage.c

raw
1 /*
2
3 Copyright 2009-2010 Taco Hoekwater <[email protected]>
4
5 This file is part of LuaTeX.
6
7 LuaTeX is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
11
12 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
19
20 */
21
22 #include "ptexlib.h"
23
24
25 #define mode mode_par
26 #define head head_par
27 #define tail tail_par
28
29 /*tex
30
31 When \TeX\ appends new material to its main vlist in vertical mode, it uses a
32 method something like |vsplit| to decide where a page ends, except that the
33 calculations are done ``on line'' as new items come in. The main complication
34 in this process is that insertions must be put into their boxes and removed
35 from the vlist, in a more-or-less optimum manner.
36
37 We shall use the term ``current page'' for that part of the main vlist that
38 is being considered as a candidate for being broken off and sent to the
39 user's output routine. The current page starts at |vlink(page_head)|, and it
40 ends at |page_tail|. We have |page_head=page_tail| if this list is empty.
41
42 Utter chaos would reign if the user kept changing page specifications while a
43 page is being constructed, so the page builder keeps the pertinent
44 specifications frozen as soon as the page receives its first box or
45 insertion. The global variable |page_contents| is |empty| when the current
46 page contains only mark nodes and content-less whatsit nodes; it is
47 |inserts_only| if the page contains only insertion nodes in addition to marks
48 and whatsits. Glue nodes, kern nodes, and penalty nodes are discarded until a
49 box or rule node appears, at which time |page_contents| changes to
50 |box_there|. As soon as |page_contents| becomes non-|empty|, the current
51 |vsize| and |max_depth| are squirreled away into |page_goal| and
52 |page_max_depth|; the latter values will be used until the page has been
53 forwarded to the user's output routine. The \.{\\topskip} adjustment is made
54 when |page_contents| changes to |box_there|.
55
56 Although |page_goal| starts out equal to |vsize|, it is decreased by the
57 scaled natural height-plus-depth of the insertions considered so far, and by
58 the \.{\\skip} corrections for those insertions. Therefore it represents the
59 size into which the non-inserted material should fit, assuming that all
60 insertions in the current page have been made.
61
62 The global variables |best_page_break| and |least_page_cost| correspond
63 respectively to the local variables |best_place| and |least_cost| in the
64 |vert_break| routine that we have already studied; i.e., they record the
65 location and value of the best place currently known for breaking the current
66 page. The value of |page_goal| at the time of the best break is stored in
67 |best_size|.
68
69 */
70
71 /*tex the final node on the current page */
72
73 halfword page_tail;
74
75 /*tex what is on the current page so far? */
76
77 int page_contents;
78
79 /*tex maximum box depth on page being built */
80
81 scaled page_max_depth;
82
83 /*tex break here to get the best page known so far */
84
85 halfword best_page_break;
86
87 /*tex the score for this currently best page */
88
89 int least_page_cost;
90
91 /*tex its |page_goal| */
92
93 scaled best_size;
94
95 /*tex
96
97 The page builder has another data structure to keep track of insertions. This
98 is a list of four-word nodes, starting and ending at |page_ins_head|. That
99 is, the first element of the list is node |t$_1$=vlink(page_ins_head)|;
100 node $r_j$ is followed by |t$_{j+1}$=vlink(t$_j$)|; and if there are
101 |n| items we have |$_{n+1}$>=page_ins_head|. The |subtype| field of each
102 node in this list refers to an insertion number; for example, `\.{\\insert
103 250}' would correspond to a node whose |subtype| is |qi(250)| (the same as
104 the |subtype| field of the relevant |ins_node|). These |subtype| fields are
105 in increasing order, and |subtype(page_ins_head)=65535|, so |page_ins_head|
106 serves as a convenient sentinel at the end of the list. A record is present
107 for each insertion number that appears in the current page.
108
109 The |type| field in these nodes distinguishes two possibilities that might
110 occur as we look ahead before deciding on the optimum page break. If
111 |type(r)=inserting_node|, then |height(r)| contains the total of the
112 height-plus-depth dimensions of the box and all its inserts seen so far.
113 |type(r)=split_up_node|, then no more insertions will be made into this box,
114 because at least one previous insertion was too big to fit on the current
115 page; |broken_ptr(r)| points to the node where that insertion will be split,
116 if \TeX\ decides to split it, |broken_ins(r)| points to the insertion node
117 that was tentatively split, and |height(r)| includes also the natural height
118 plus depth of the part that would be split off.
119
120 In both cases, |last_ins_ptr(r)| points to the last |ins_node| encountered
121 for box |qo(subtype(r))| that would be at least partially inserted on the
122 next page; and |best_ins_ptr(r)| points to the last such |ins_node| that
123 should actually be inserted, to get the page with minimum badness among all
124 page breaks considered so far. We have |best_ins_ptr(r)=null| if and only if
125 no insertion for this box should be made to produce this optimum page.
126
127 Pages are built by appending nodes to the current list in \TeX's vertical
128 mode, which is at the outermost level of the semantic nest. This vlist is
129 split into two parts; the ``current page'' that we have been talking so much
130 about already, and the ``contribution list'' that receives new nodes as they
131 are created. The current page contains everything that the page builder has
132 accounted for in its data structures, as described above, while the
133 contribution list contains other things that have been generated by other
134 parts of \TeX\ but have not yet been seen by the page builder. The
135 contribution list starts at |vlink(contrib_head)|, and it ends at the current
136 node in \TeX's vertical mode.
137
138 When \TeX\ has appended new material in vertical mode, it calls the procedure
139 |build_page|, which tries to catch up by moving nodes from the contribution
140 list to the current page. This procedure will succeed in its goal of emptying
141 the contribution list, unless a page break is discovered, i.e., unless the
142 current page has grown to the point where the optimum next page break has
143 been determined. In the latter case, the nodes after the optimum break will
144 go back onto the contribution list, and control will effectively pass to the
145 user's output routine.
146
147 We make |type(page_head)=glue_node|, so that an initial glue node on the
148 current page will not be considered a valid breakpoint.
149
150 */
151
152 void initialize_buildpage(void)
153 {
154 subtype(page_ins_head) = 65535;
155 type(page_ins_head) = split_up_node;
156 vlink(page_ins_head) = page_ins_head;
157
158 type(page_head) = glue_node;
159 subtype(page_head) = normal;
160 }
161
162 /*tex
163
164 An array |page_so_far| records the heights and depths of everything on the
165 current page. This array contains six |scaled| numbers, like the similar
166 arrays already considered in |line_break| and |vert_break|; and it also
167 contains |page_goal| and |page_depth|, since these values are all accessible
168 to the user via |set_page_dimen| commands. The value of |page_so_far[1]| is
169 also called |page_total|. The stretch and shrink components of the \.{\\skip}
170 corrections for each insertion are included in |page_so_far|, but the natural
171 space components of these corrections are not, since they have been
172 subtracted from |page_goal|.
173
174 The variable |page_depth| records the depth of the current page; it has been
175 adjusted so that it is at most |page_max_depth|. The variable |last_glue|
176 points to the glue specification of the most recent node contributed from the
177 contribution list, if this was a glue node; otherwise
178 |last_glue=max_halfword|. (If the contribution list is nonempty, however, the
179 value of |last_glue| is not necessarily accurate.) The variables
180 |last_penalty|, |last_kern|, and |last_node_type| are similar. And finally,
181 |insert_penalties| holds the sum of the penalties associated with all split
182 and floating insertions.
183
184 */
185
186 /*tex height and glue of the current page */
187
188 scaled page_so_far[8];
189
190 /*tex used to implement \.{\\lastskip} */
191
192 halfword last_glue;
193
194 /*tex used to implement \.{\\lastpenalty} */
195
196 int last_penalty;
197
198 /*tex used to implement \.{\\lastkern} */
199
200 scaled last_kern;
201
202 /*tex used to implement \.{\\lastnodetype} */
203
204 int last_node_type;
205
206 /*tex sum of the penalties for held-over insertions */
207
208 int insert_penalties;
209
210 #define print_plus(A,B) do { \
211 if (page_so_far[(A)]!=0) { \
212 tprint(" plus "); \
213 print_scaled(page_so_far[(A)]); \
214 tprint((B)); \
215 } \
216 } while (0)
217
218 void print_totals(void)
219 {
220 print_scaled(page_total);
221 print_plus(2, "");
222 print_plus(3, "fil");
223 print_plus(4, "fill");
224 print_plus(5, "filll");
225 if (page_shrink != 0) {
226 tprint(" minus ");
227 print_scaled(page_shrink);
228 }
229 }
230
231 /*tex
232
233 Here is a procedure that is called when the |page_contents| is changing from
234 |empty| to |inserts_only| or |box_there|.
235
236 */
237
238 #define do_all_six(A) A(1);A(2);A(3);A(4);A(5);A(6);A(7)
239 #define set_page_so_far_zero(A) page_so_far[(A)]=0
240
241 void freeze_page_specs(int s)
242 {
243 page_contents = s;
244 page_goal = vsize_par;
245 page_max_depth = max_depth_par;
246 page_depth = 0;
247 do_all_six(set_page_so_far_zero);
248 least_page_cost = awful_bad;
249 if (tracing_pages_par > 0) {
250 begin_diagnostic();
251 tprint_nl("%% goal height=");
252 print_scaled(page_goal);
253 tprint(", max depth=");
254 print_scaled(page_max_depth);
255 end_diagnostic(false);
256 }
257 }
258
259 /*tex
260
261 The global variable |output_active| is true during the time the user's output
262 routine is driving \TeX.
263
264 */
265
266 /*tex Are we in the midst of an output routine? */
267
268 boolean output_active;
269
270 /*tex
271
272 The page builder is ready to start a fresh page if we initialize the
273 following state variables. (However, the page insertion list is initialized
274 elsewhere.)
275
276 */
277
278 void start_new_page(void)
279 {
280 page_contents = empty;
281 page_tail = page_head;
282 vlink(page_head) = null;
283 last_glue = max_halfword;
284 last_penalty = 0;
285 last_kern = 0;
286 last_node_type = -1;
287 page_depth = 0;
288 page_max_depth = 0;
289 }
290
291 /*tex
292
293 At certain times box \.{\\outputbox} is supposed to be void (i.e., |null|),
294 or an insertion box is supposed to be ready to accept a vertical list. If
295 not, an error message is printed, and the following subroutine flushes the
296 unwanted contents, reporting them to the user.
297
298 */
299
300 static void box_error(int n)
301 {
302 error();
303 begin_diagnostic();
304 tprint_nl("The following box has been deleted:");
305 show_box(box(n));
306 end_diagnostic(true);
307 flush_node_list(box(n));
308 box(n) = null;
309 }
310
311 /*tex
312
313 The following procedure guarantees that a given box register does not contain
314 an \.{\\hbox}.
315
316 */
317
318 static void ensure_vbox(int n)
319 {
320 halfword p = box(n);
321 if (p != null && type(p) == hlist_node) {
322 print_err("Insertions can only be added to a vbox");
323 help3(
324 "Tut tut: You're trying to \\insert into a",
325 "\\box register that now contains an \\hbox.",
326 "Proceed, and I'll discard its present contents."
327 );
328 box_error(n);
329 }
330 }
331
332 /*tex
333
334 \TeX\ is not always in vertical mode at the time |build_page| is called; the
335 current mode reflects what \TeX\ should return to, after the contribution
336 list has been emptied. A call on |build_page| should be immediately followed
337 by `|goto big_switch|', which is \TeX's central control point.
338
339 */
340
341 /*tex Append contributions to the current page. */
342
343 void build_page(void)
344 {
345 /*tex the node being appended */
346 halfword p;
347 /*tex nodes being examined */
348 halfword q, r;
349 /*tex badness and cost of current page */
350 int b, c;
351 /*tex penalty to be added to the badness */
352 int pi = 0;
353 /*tex insertion box number */
354 int n;
355 /*tex sizes used for insertion calculations */
356 scaled delta, h, w;
357 int id, sk, i;
358 if ((vlink(contrib_head) == null) || output_active)
359 return;
360 do {
361 CONTINUE:
362 p = vlink(contrib_head);
363 /*tex Update the values of |last_glue|, |last_penalty|, and |last_kern|. */
364 if (last_glue != max_halfword) {
365 flush_node(last_glue);
366 last_glue = max_halfword;
367 }
368 last_penalty = 0;
369 last_kern = 0;
370 last_node_type = type(p) + 1;
371 if (type(p) == glue_node) {
372 last_glue = new_glue(p);
373 } else if (type(p) == penalty_node) {
374 last_penalty = penalty(p);
375 } else if (type(p) == kern_node) {
376 last_kern = width(p);
377 }
378 /*tex
379
380 Move node |p| to the current page; if it is time for a page break,
381 put the nodes following the break back onto the contribution list,
382 and |return| to the users output routine if there is one.
383
384 The code here is an example of a many-way switch into routines that
385 merge together in different places. Some people call this
386 unstructured programming, but the author doesn't see much wrong with
387 it, as long as the various labels have a well-understood meaning.
388
389 If the current page is empty and node |p| is to be deleted, |goto
390 done1|; otherwise use node |p| to update the state of the current
391 page; if this node is an insertion, |goto contribute|; otherwise if
392 this node is not a legal breakpoint, |goto contribute| or
393 |update_heights|; otherwise set |pi| to the penalty associated with
394 this breakpoint.
395
396 The title of this section is already so long, it seems best to avoid
397 making it more accurate but still longer, by mentioning the fact that
398 a kern node at the end of the contribution list will not be
399 contributed until we know its successor.
400
401 */
402 switch (type(p)) {
403 case hlist_node:
404 case vlist_node:
405 case rule_node:
406 if (page_contents < box_there) {
407 /*tex
408
409 Initialize the current page, insert the \.{\\topskip}
410 glue ahead of |p|, and |goto continue|.
411
412 */
413 if (page_contents == empty)
414 freeze_page_specs(box_there);
415 else
416 page_contents = box_there;
417 q = new_skip_param(top_skip_code);
418 if ((type(p) == hlist_node) && is_mirrored(body_direction_par)) {
419 if (width(q) > depth(p))
420 width(q) = width(q) - depth(p);
421 else
422 width(q) = 0;
423 } else {
424 if (width(q) > height(p))
425 width(q) = width(q) - height(p);
426 else
427 width(q) = 0;
428 }
429 couple_nodes(q, p);
430 couple_nodes(contrib_head, q);
431 goto CONTINUE;
432 } else {
433 /*tex
434
435 Prepare to move a box or rule node to the current page,
436 then |goto contribute|.
437
438 */
439 if ((type(p) == hlist_node) && is_mirrored(body_direction_par)) {
440 page_total = page_total + page_depth + depth(p);
441 page_depth = height(p);
442 } else {
443 page_total = page_total + page_depth + height(p);
444 page_depth = depth(p);
445 }
446 goto CONTRIBUTE;
447
448 }
449 break;
450 case boundary_node:
451 case whatsit_node:
452 goto CONTRIBUTE;
453 break;
454 case glue_node:
455 if (page_contents < box_there)
456 goto DONE1;
457 else if (precedes_break(page_tail))
458 pi = 0;
459 else
460 goto UPDATE_HEIGHTS;
461 break;
462 case kern_node:
463 if (page_contents < box_there)
464 goto DONE1;
465 else if (vlink(p) == null)
466 goto EXIT;
467 else if (type(vlink(p)) == glue_node)
468 pi = 0;
469 else
470 goto UPDATE_HEIGHTS;
471 break;
472 case penalty_node:
473 if (page_contents < box_there)
474 goto DONE1;
475 else
476 pi = penalty(p);
477 break;
478 case mark_node:
479 goto CONTRIBUTE;
480 break;
481 case ins_node:
482 /*tex Append an insertion to the current page and |goto contribute|. */
483 if (page_contents == empty)
484 freeze_page_specs(inserts_only);
485 n = subtype(p);
486 r = page_ins_head;
487 i = 1 ;
488 while (n >= subtype(vlink(r))) {
489 r = vlink(r);
490 i = i + 1 ;
491 }
492 if (subtype(r) != n) {
493 /*tex
494
495 Create a page insertion node with |subtype(r)=qi(n)|, and
496 include the glue correction for box |n| in the current
497 page state.
498
499 We take note of the value of \.{\\skip} |n| and the
500 height plus depth of \.{\\box}~|n| only when the first
501 \.{\\insert}~|n| node is encountered for a new page. A
502 user who changes the contents of \.{\\box}~|n| after that
503 first \.{\\insert}~|n| had better be either extremely
504 careful or extremely lucky, or both.
505
506 */
507 id = callback_defined(build_page_insert_callback);
508 if (id != 0) {
509 run_callback(id, "dd->d",n,i,&sk);
510 } else {
511 sk = n;
512 }
513 q = new_node(inserting_node, n);
514 try_couple_nodes(q, vlink(r));
515 couple_nodes(r, q);
516 r = q;
517 ensure_vbox(n);
518 if (box(n) == null)
519 height(r) = 0;
520 else
521 height(r) = height(box(n)) + depth(box(n));
522 best_ins_ptr(r) = null;
523 q = skip(sk);
524 if (count(n) == 1000)
525 h = height(r);
526 else
527 h = x_over_n(height(r), 1000) * count(n);
528 page_goal = page_goal - h - width(q);
529 if (stretch_order(q) > 1)
530 page_so_far[1 + stretch_order(q)] = page_so_far[1 + stretch_order(q)] + stretch(q);
531 else
532 page_so_far[2 + stretch_order(q)] = page_so_far[2 + stretch_order(q)] + stretch(q);
533 page_shrink = page_shrink + shrink(q);
534 if ((shrink_order(q) != normal) && (shrink(q) != 0)) {
535 print_err("Infinite glue shrinkage inserted from \\skip");
536 print_int(n);
537 help3(
538 "The correction glue for page breaking with insertions",
539 "must have finite shrinkability. But you may proceed,",
540 "since the offensive shrinkability has been made finite."
541 );
542 error();
543 }
544
545 }
546 if (type(r) == split_up_node) {
547 insert_penalties = insert_penalties + float_cost(p);
548 } else {
549 last_ins_ptr(r) = p;
550 delta = page_goal - page_total - page_depth + page_shrink;
551 /*tex This much room is left if we shrink the maximum. */
552 if (count(n) == 1000) {
553 h = height(p);
554 } else {
555 /*tex This much room is needed. */
556 h = x_over_n(height(p), 1000) * count(n);
557 }
558 if (((h <= 0) || (h <= delta))
559 && (height(p) + height(r) <= dimen(n))) {
560 page_goal = page_goal - h;
561 height(r) = height(r) + height(p);
562 } else {
563 /*tex
564
565 Find the best way to split the insertion, and change
566 |type(r)| to |split_up_node|.
567
568 Here is the code that will split a long footnote
569 between pages, in an emergency. The current situation
570 deserves to be recapitulated: Node |p| is an
571 insertion into box |n|; the insertion will not fit,
572 in its entirety, either because it would make the
573 total contents of box |n| greater than \.{\\dimen}
574 |n|, or because it would make the incremental amount
575 of growth |h| greater than the available space
576 |delta|, or both. (This amount |h| has been weighted
577 by the insertion scaling factor, i.e., by \.{\\count}
578 |n| over 1000.) Now we will choose the best way to
579 break the vlist of the insertion, using the same
580 criteria as in the \.{\\vsplit} operation.
581
582 */
583 if (count(n) <= 0) {
584 w = max_dimen;
585 } else {
586 w = page_goal - page_total - page_depth;
587 if (count(n) != 1000)
588 w = x_over_n(w, count(n)) * 1000;
589 }
590 if (w > dimen(n) - height(r))
591 w = dimen(n) - height(r);
592 q = vert_break(ins_ptr(p), w, depth(p));
593 height(r) = height(r) + best_height_plus_depth;
594 if (tracing_pages_par > 0) {
595 /*tex Display the insertion split cost. */
596 begin_diagnostic();
597 tprint_nl("% split");
598 print_int(n);
599 tprint(" to ");
600 print_scaled(w);
601 print_char(',');
602 print_scaled(best_height_plus_depth);
603 tprint(" p=");
604 if (q == null)
605 print_int(eject_penalty);
606 else if (type(q) == penalty_node)
607 print_int(penalty(q));
608 else
609 print_char('0');
610 end_diagnostic(false);
611 }
612 if (count(n) != 1000)
613 best_height_plus_depth = x_over_n(best_height_plus_depth, 1000) * count(n);
614 page_goal = page_goal - best_height_plus_depth;
615 type(r) = split_up_node;
616 broken_ptr(r) = q;
617 broken_ins(r) = p;
618 if (q == null)
619 insert_penalties = insert_penalties + eject_penalty;
620 else if (type(q) == penalty_node)
621 insert_penalties = insert_penalties + penalty(q);
622 }
623 }
624 goto CONTRIBUTE;
625
626 break;
627 default:
628 formatted_error("pagebuilder","invalid node of type %d in vertical mode", type(p));
629 break;
630 }
631 /*tex
632
633 Check if node |p| is a new champion breakpoint; then if it is time
634 for a page break, prepare for output, and either fire up the users
635 output routine and |return| or ship out the page and |goto done|.
636
637 */
638 if (pi < inf_penalty) {
639 /*tex
640
641 Compute the badness, |b|, of the current page, using |awful_bad|
642 if the box is too full.
643
644 */
645 if (page_total < page_goal) {
646 if ((page_so_far[3] != 0) || (page_so_far[4] != 0) ||
647 (page_so_far[5] != 0))
648 b = 0;
649 else
650 b = badness(page_goal - page_total, page_so_far[2]);
651 } else if (page_total - page_goal > page_shrink) {
652 b = awful_bad;
653 } else {
654 b = badness(page_total - page_goal, page_shrink);
655 }
656 if (b < awful_bad) {
657 if (pi <= eject_penalty)
658 c = pi;
659 else if (b < inf_bad)
660 c = b + pi + insert_penalties;
661 else
662 c = deplorable;
663 } else {
664 c = b;
665 }
666 if (insert_penalties >= 10000)
667 c = awful_bad;
668 if (tracing_pages_par > 0) {
669 /*tex Display the page break cost. */
670 begin_diagnostic();
671 tprint_nl("%");
672 tprint(" t=");
673 print_totals();
674 tprint(" g=");
675 print_scaled(page_goal);
676 tprint(" b=");
677 if (b == awful_bad)
678 print_char('*');
679 else
680 print_int(b);
681 tprint(" p=");
682 print_int(pi);
683 tprint(" c=");
684 if (c == awful_bad)
685 print_char('*');
686 else
687 print_int(c);
688 if (c <= least_page_cost)
689 print_char('#');
690 end_diagnostic(false);
691 }
692 if (c <= least_page_cost) {
693 best_page_break = p;
694 best_size = page_goal;
695 least_page_cost = c;
696 r = vlink(page_ins_head);
697 while (r != page_ins_head) {
698 best_ins_ptr(r) = last_ins_ptr(r);
699 r = vlink(r);
700 }
701 }
702 if ((c == awful_bad) || (pi <= eject_penalty)) {
703 /*tex Output the current page at the best place. */
704 fire_up(p);
705 if (output_active) {
706 /*tex User's output routine will act. */
707 goto EXIT;
708 }
709 /*tex The page has been shipped out by default output routine. */
710 goto DONE;
711 }
712 }
713 if ((type(p) < glue_node) || (type(p) > kern_node))
714 goto CONTRIBUTE;
715 UPDATE_HEIGHTS:
716 /*tex
717
718 Go here to record glue in the |active_height| table. Update the
719 current page measurements with respect to the glue or kern specified
720 by node~|p|.
721
722 */
723 if (type(p) != kern_node) {
724 if (stretch_order(p) > 1)
725 page_so_far[1 + stretch_order(p)] = page_so_far[1 + stretch_order(p)] + stretch(p);
726 else
727 page_so_far[2 + stretch_order(p)] = page_so_far[2 + stretch_order(p)] + stretch(p);
728 page_shrink = page_shrink + shrink(p);
729 if ((shrink_order(p) != normal) && (shrink(p) != 0)) {
730 print_err("Infinite glue shrinkage found on current page");
731 help4(
732 "The page about to be output contains some infinitely",
733 "shrinkable glue, e.g., `\\vss' or `\\vskip 0pt minus 1fil'.",
734 "Such glue doesn't belong there; but you can safely proceed,",
735 "since the offensive shrinkability has been made finite."
736 );
737 error();
738 reset_glue_to_zero(p);
739 shrink_order(p) = normal;
740 }
741 }
742 page_total = page_total + page_depth + width(p);
743 page_depth = 0;
744 CONTRIBUTE:
745 /*tex
746
747 Go here to link a node into the current page. Make sure that
748 |page_max_depth| is not exceeded.
749
750 */
751 if (page_depth > page_max_depth) {
752 page_total = page_total + page_depth - page_max_depth;
753 page_depth = page_max_depth;
754 }
755 /*tex Link node |p| into the current page and |goto done|. */
756 couple_nodes(page_tail, p);
757 page_tail = p;
758 try_couple_nodes(contrib_head,vlink(p));
759 vlink(p) = null;
760 goto DONE;
761 DONE1:
762 /*tex Recycle node |p|. */
763 try_couple_nodes(contrib_head,vlink(p));
764 vlink(p) = null;
765 if (saving_vdiscards_par > 0) {
766 if (page_disc == null) {
767 page_disc = p;
768 } else {
769 couple_nodes(tail_page_disc, p);
770 }
771 tail_page_disc = p;
772 } else {
773 flush_node_list(p);
774 }
775 DONE:
776 ;
777 } while (vlink(contrib_head) != null);
778 /*tex Make the contribution list empty by setting its tail to |contrib_head|. */
779 contrib_tail = contrib_head;
780 EXIT:
781 ;
782 }
783
784 /*tex
785
786 When the page builder has looked at as much material as could appear before
787 the next page break, it makes its decision. The break that gave minimum
788 badness will be used to put a completed ``page'' into box \.{\\outputbox},
789 with insertions appended to their other boxes.
790
791 We also set the values of |top_mark|, |first_mark|, and |bot_mark|. The
792 program uses the fact that |bot_mark(x)<>null| implies |first_mark(x)<>null|;
793 it also knows that |bot_mark(x)=null| implies
794 |top_mark(x)=first_mark(x)=null|.
795
796 The |fire_up| subroutine prepares to output the current page at the best
797 place; then it fires up the user's output routine, if there is one, or it
798 simply ships out the page. There is one parameter, |c|, which represents the
799 node that was being contributed to the page when the decision to force an
800 output was made.
801
802 */
803
804 void fire_up(halfword c)
805 {
806 /*tex nodes being examined and/or changed */
807 halfword p, q, r, s;
808 /*tex predecessor of |p| */
809 halfword prev_p;
810 /*tex insertion box number */
811 int n;
812 /*tex should the present insertion be held over? */
813 boolean wait;
814 /*tex saved value of |vbadness| */
815 int save_vbadness;
816 /*tex saved value of |vfuzz| */
817 scaled save_vfuzz;
818 /*tex saved value of |split_top_skip| */
819 halfword save_split_top_skip;
820 /*tex for looping through the marks */
821 halfword i;
822 /*tex Set the value of |output_penalty|. */
823 if (type(best_page_break) == penalty_node) {
824 geq_word_define(int_base + output_penalty_code,
825 penalty(best_page_break));
826 penalty(best_page_break) = inf_penalty;
827 } else {
828 geq_word_define(int_base + output_penalty_code, inf_penalty);
829 }
830
831 for (i = 0; i <= biggest_used_mark; i++) {
832 if (bot_mark(i) != null) {
833 if (top_mark(i) != null)
834 delete_token_ref(top_mark(i));
835 set_top_mark(i, bot_mark(i));
836 add_token_ref(top_mark(i));
837 delete_first_mark(i);
838 }
839 }
840 /*tex
841
842 Put the optimal current page into box |output_box|, update |first_mark|
843 and |bot_mark|, append insertions to their boxes, and put the remaining
844 nodes back on the contribution list.
845
846 As the page is finally being prepared for output, pointer |p| runs
847 through the vlist, with |prev_p| trailing behind; pointer |q| is the tail
848 of a list of insertions that are being held over for a subsequent page.
849
850 */
851 if (c == best_page_break) {
852 /*tex |c| not yet linked in */
853 best_page_break = null;
854 }
855 /*tex Ensure that box |output_box| is empty before output. */
856 if (box(output_box_par) != null) {
857 print_err("\\box");
858 print_int(output_box_par);
859 tprint(" is not void");
860 help2(
861 "You shouldn't use \\box\\outputbox except in \\output routines.",
862 "Proceed, and I'll discard its present contents."
863 );
864 box_error(output_box_par);
865 }
866 /*tex This will count the number of insertions held over. */
867 insert_penalties = 0;
868 save_split_top_skip = split_top_skip_par;
869 if (holding_inserts_par <= 0) {
870 /*tex
871
872 Prepare all the boxes involved in insertions to act as queues. If
873 many insertions are supposed to go into the same box, we want to know
874 the position of the last node in that box, so that we don't need to
875 waste time when linking further information into it. The
876 |last_ins_ptr| fields of the page insertion nodes are therefore used
877 for this purpose during the packaging phase.
878
879 */
880 r = vlink(page_ins_head);
881 while (r != page_ins_head) {
882 if (best_ins_ptr(r) != null) {
883 n = subtype(r);
884 ensure_vbox(n);
885 if (box(n) == null)
886 box(n) = new_null_box();
887 p = box(n) + list_offset;
888 while (vlink(p) != null)
889 p = vlink(p);
890 last_ins_ptr(r) = p;
891 }
892 r = vlink(r);
893 }
894
895 }
896 q = hold_head;
897 vlink(q) = null;
898 prev_p = page_head;
899 p = vlink(prev_p);
900 while (p != best_page_break) {
901 if (type(p) == ins_node) {
902 if (holding_inserts_par <= 0) {
903 /*tex
904
905 Either insert the material specified by node |p| into the
906 appropriate box, or hold it for the next page; also delete
907 node |p| from the current page.
908
909 We will set |best_ins_ptr:=null| and package the box
910 corresponding to insertion node~|r|, just after making the
911 final insertion into that box. If this final insertion is
912 `|split_up_node|', the remainder after splitting and pruning
913 (if any) will be carried over to the next page.
914
915 */
916 r = vlink(page_ins_head);
917 while (subtype(r) != subtype(p))
918 r = vlink(r);
919 if (best_ins_ptr(r) == null) {
920 wait = true;
921 } else {
922 wait = false;
923 s = last_ins_ptr(r);
924 vlink(s) = ins_ptr(p);
925 if (best_ins_ptr(r) == p) {
926 halfword t;
927 /*tex
928
929 Wrap up the box specified by node |r|, splitting node
930 |p| if called for; set |wait:=true| if node |p| holds
931 a remainder after splitting.
932
933 */
934 if (type(r) == split_up_node) {
935 if ((broken_ins(r) == p) && (broken_ptr(r) != null)) {
936 while (vlink(s) != broken_ptr(r))
937 s = vlink(s);
938 vlink(s) = null;
939 split_top_skip_par = split_top_ptr(p);
940 ins_ptr(p) =
941 prune_page_top(broken_ptr(r), false);
942 if (ins_ptr(p) != null) {
943 t = vpack(ins_ptr(p), 0, additional, -1);
944 height(p) = height(t) + depth(t);
945 list_ptr(t) = null;
946 flush_node(t);
947 wait = true;
948 }
949 }
950 }
951 best_ins_ptr(r) = null;
952 n = subtype(r);
953 t = list_ptr(box(n));
954 list_ptr(box(n)) = null;
955 flush_node(box(n));
956 box(n) = vpack(t, 0, additional, body_direction_par);
957
958 } else {
959 while (vlink(s) != null)
960 s = vlink(s);
961 last_ins_ptr(r) = s;
962 }
963 }
964 /*tex
965
966 Either append the insertion node |p| after node |q|, and
967 remove it from the current page, or delete |node(p)|.
968
969 */
970 try_couple_nodes(prev_p, vlink(p));
971 vlink(p) = null;
972 if (wait) {
973 couple_nodes(q, p);
974 q = p;
975 incr(insert_penalties);
976 } else {
977 ins_ptr(p) = null;
978 flush_node(p);
979 }
980 p = prev_p;
981
982 }
983 } else if (type(p) == mark_node) {
984 /*tex Update the values of |first_mark| and |bot_mark|. */
985 if (first_mark(mark_class(p)) == null) {
986 set_first_mark(mark_class(p), mark_ptr(p));
987 add_token_ref(first_mark(mark_class(p)));
988 }
989 if (bot_mark(mark_class(p)) != null)
990 delete_token_ref(bot_mark(mark_class(p)));
991 set_bot_mark(mark_class(p), mark_ptr(p));
992 add_token_ref(bot_mark(mark_class(p)));
993
994 }
995 prev_p = p;
996 p = vlink(prev_p);
997 }
998 split_top_skip_par = save_split_top_skip;
999 /*tex
1000
1001 Break the current page at node |p|, put it in box~|output_box|, and put
1002 the remaining nodes on the contribution list.
1003
1004 When the following code is executed, the current page runs from node
1005 |vlink(page_head)| to node |prev_p|, and the nodes from |p| to
1006 |page_tail| are to be placed back at the front of the contribution list.
1007 Furthermore the heldover insertions appear in a list from
1008 |vlink(hold_head)| to |q|; we will put them into the current page list
1009 for safekeeping while the user's output routine is active. We might have
1010 |q=hold_head|; and |p=null| if and only if |prev_p=page_tail|. Error
1011 messages are suppressed within |vpackage|, since the box might appear to
1012 be overfull or underfull simply because the stretch and shrink from the
1013 \.{\\skip} registers for inserts are not actually present in the box.
1014
1015 */
1016 if (p != null) {
1017 if (vlink(contrib_head) == null) {
1018 contrib_tail = page_tail;
1019 }
1020 couple_nodes(page_tail,vlink(contrib_head));
1021 couple_nodes(contrib_head, p);
1022 vlink(prev_p) = null;
1023 }
1024 save_vbadness = vbadness_par;
1025 vbadness_par = inf_bad;
1026 save_vfuzz = vfuzz_par;
1027 /*tex Inhibit error messages. */
1028 vfuzz_par = max_dimen;
1029 box(output_box_par) = filtered_vpackage(vlink(page_head),
1030 best_size, exactly, page_max_depth, output_group, body_direction_par, 0, 0);
1031 vbadness_par = save_vbadness;
1032 vfuzz_par = save_vfuzz;
1033 if (last_glue != max_halfword)
1034 flush_node(last_glue);
1035 /*tex Start a new current page. This sets |last_glue:=max_halfword|. */
1036 start_new_page();
1037 if (q != hold_head) {
1038 vlink(page_head) = vlink(hold_head);
1039 page_tail = q;
1040 }
1041 /*tex Delete the page-insertion nodes. */
1042 r = vlink(page_ins_head);
1043 while (r != page_ins_head) {
1044 /*tex Todo: couple. */
1045 q = vlink(r);
1046 flush_node(r);
1047 r = q;
1048 }
1049 vlink(page_ins_head) = page_ins_head;
1050 for (i = 0; i <= biggest_used_mark; i++) {
1051 if ((top_mark(i) != null) && (first_mark(i) == null)) {
1052 set_first_mark(i, top_mark(i));
1053 add_token_ref(top_mark(i));
1054 }
1055 }
1056 if (output_routine_par != null) {
1057 if (dead_cycles >= max_dead_cycles_par) {
1058 /*tex Explain that too many dead cycles have occurred in a row. */
1059 print_err("Output loop---");
1060 print_int(dead_cycles);
1061 tprint(" consecutive dead cycles");
1062 help3(
1063 "I've concluded that your \\output is awry; it never does a",
1064 "\\shipout, so I'm shipping \\box\\outputbox out myself. Next time",
1065 "increase \\maxdeadcycles if you want me to be more patient!"
1066 );
1067 error();
1068 } else {
1069 /*tex Fire up the users output routine and |return|. */
1070 output_active = true;
1071 incr(dead_cycles);
1072 push_nest();
1073 mode = -vmode;
1074 prev_depth_par = ignore_depth;
1075 mode_line_par = -line;
1076 begin_token_list(output_routine_par, output_text);
1077 new_save_level(output_group);
1078 normal_paragraph();
1079 scan_left_brace();
1080 return;
1081 }
1082 }
1083 /*tex
1084
1085 Perform the default output routine. The list of heldover insertions,
1086 running from |vlink(page_head)| to |page_tail|, must be moved to the
1087 contribution list when the user has specified no output routine.
1088
1089 */
1090 if (vlink(page_head) != null) {
1091 if (vlink(contrib_head) == null) {
1092 contrib_tail = page_tail;
1093 } else {
1094 vlink(page_tail) = vlink(contrib_head);
1095 }
1096 vlink(contrib_head) = vlink(page_head);
1097 vlink(page_head) = null;
1098 page_tail = page_head;
1099 }
1100 flush_node_list(page_disc);
1101 page_disc = null;
1102 ship_out(static_pdf, box(output_box_par), SHIPPING_PAGE);
1103 box(output_box_par) = null;
1104 }
1105
1106 /*tex
1107
1108 When the user's output routine finishes, it has constructed a vlist in
1109 internal vertical mode, and \TeX\ will do the following:
1110
1111 */
1112
1113 void resume_after_output(void)
1114 {
1115 if ((iloc != null) || ((token_type != output_text) && (token_type != backed_up))) {
1116 /*tex Recover from an unbalanced output routine */
1117 print_err("Unbalanced output routine");
1118 help2(
1119 "Your sneaky output routine has problematic {'s and/or }'s.",
1120 "I can't handle that very well; good luck."
1121 );
1122 error();
1123 /*tex Loops forever if reading from a file, since |null=min_halfword<=0|. */
1124 do {
1125 get_token();
1126 } while (iloc != null);
1127 }
1128 /*tex Conserve stack space in case more outputs are triggered. */
1129 end_token_list();
1130 end_graf(bottom_level);
1131 unsave();
1132 output_active = false;
1133 insert_penalties = 0;
1134 /*tex Ensure that box |output_box| is empty after output. */
1135 if (box(output_box_par) != null) {
1136 print_err("Output routine didn't use all of \\box");
1137 print_int(output_box_par);
1138 help3(
1139 "Your \\output commands should empty \\box\\outputbox,",
1140 "e.g., by saying `\\shipout\\box\\outputbox'.",
1141 "Proceed; I'll discard its present contents."
1142 );
1143 box_error(output_box_par);
1144 }
1145 if (tail != head) {
1146 /*tex Current list goes after heldover insertions. */
1147 try_couple_nodes(page_tail, vlink(head));
1148 page_tail = tail;
1149 }
1150 if (vlink(page_head) != null) {
1151 /* Both go before heldover contributions. */
1152 if (vlink(contrib_head) == null)
1153 contrib_tail = page_tail;
1154 try_couple_nodes(page_tail, vlink(contrib_head));
1155 try_couple_nodes(contrib_head, vlink(page_head));
1156 vlink(page_head) = null;
1157 page_tail = page_head;
1158 }
1159 flush_node_list(page_disc);
1160 page_disc = null;
1161 pop_nest();
1162 normal_page_filter(after_output);
1163 build_page();
1164 }