Debian Codegraph @

texk/web2c/luatexdir/tex/packaging.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 the terms
8 of the GNU General Public License as published by the Free Software Foundation;
9 either version 2 of the License, or (at your option) any later version.
10
11 LuaTeX is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along with
16 LuaTeX; if not, see <http://www.gnu.org/licenses/>.
17
18 */
19
20 #include "ptexlib.h"
21
22 /*tex
23
24 We're essentially done with the parts of \TeX\ that are concerned with the
25 input (|get_next|) and the output (|ship_out|). So it's time to get heavily
26 into the remaining part, which does the real work of typesetting.
27
28 After lists are constructed, \TeX\ wraps them up and puts them into boxes.
29 Two major subroutines are given the responsibility for this task: |hpack|
30 applies to horizontal lists (hlists) and |vpack| applies to vertical lists
31 (vlists). The main duty of |hpack| and |vpack| is to compute the dimensions
32 of the resulting boxes, and to adjust the glue if one of those dimensions is
33 pre-specified. The computed sizes normally enclose all of the material inside
34 the new box; but some items may stick out if negative glue is used, if the
35 box is overfull, or if a \.{\\vbox} includes other boxes that have been
36 shifted left.
37
38 The subroutine call |hpack(p,w,m)| returns a pointer to an |hlist_node| for a
39 box containing the hlist that starts at |p|. Parameter |w| specifies a width;
40 and parameter |m| is either `|exactly|' or `|additional|'. Thus,
41 |hpack(p,w,exactly)| produces a box whose width is exactly |w|, while
42 |hpack(p,w,additional)| yields a box whose width is the natural width plus
43 |w|. It is convenient to define a macro called `|natural|' to cover the most
44 common case, so that we can say |hpack(p,natural)| to get a box that has the
45 natural width of list |p|.
46
47 Similarly, |vpack(p,w,m)| returns a pointer to a |vlist_node| for a box
48 containing the vlist that starts at |p|. In this case |w| represents a height
49 instead of a width; the parameter |m| is interpreted as in |hpack|.
50
51 The parameters to |hpack| and |vpack| correspond to \TeX's primitives like
52 `\.{\\hbox} \.{to} \.{300pt}', `\.{\\hbox} \.{spread} \.{10pt}'; note that
53 `\.{\\hbox}' with no dimension following it is equivalent to `\.{\\hbox}
54 \.{spread} \.{0pt}'. The |scan_spec| subroutine scans such constructions in
55 the user's input, including the mandatory left brace that follows them, and
56 it puts the specification onto |save_stack| so that the desired box can later
57 be obtained by executing the following code: $$\vbox{\halign{#\hfil\cr
58 |save_ptr:=save_ptr-1;|\cr |hpack(p,saved_value(0),saved_level(0)).|\cr}}$$
59
60 */
61
62 /*tex Scan a box specification and left brace: */
63
64 void scan_spec(group_code c)
65 {
66 int spec_code;
67 boolean done = false ;
68 do {
69 get_x_token();
70 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
71 if (cur_cmd == left_brace_cmd) {
72 spec_code = additional;
73 cur_val = 0;
74 done = true;
75 } else {
76 /*tex todo: attr */
77 back_input();
78 if (scan_keyword("to")) {
79 spec_code = exactly;
80 scan_normal_dimen();
81 } else if (scan_keyword("spread")) {
82 spec_code = additional;
83 scan_normal_dimen();
84 } else {
85 spec_code = additional;
86 cur_val = 0;
87 }
88 }
89 set_saved_record(0, saved_boxspec, spec_code, cur_val);
90 save_ptr++;
91 new_save_level(c);
92 if (!done) {
93 scan_left_brace();
94 }
95 }
96
97 /*tex
98
99 When scanning, special care is necessary to ensure that the special
100 |save_stack| codes are placed just below the new group code, because scanning
101 can change |save_stack| when \.{\\csname} appears.
102
103 This coincides with the text on |dir| and |attr| keywords, as these are
104 exaclty the uses of \.{\\hbox}, \.{\\vbox}, and \.{\\vtop} in the input
105 stream (the others are \.{\\vcenter}, \.{\\valign}, and \.{\\halign}).
106
107 */
108
109 /*tex Scan a box specification and left brace: */
110
111 #define first_char_is(a,A) (cur_cs == 0 && (cur_chr == a || cur_chr == A))
112
113 void scan_full_spec(group_code c, int spec_direction, int just_pack)
114 {
115 int s, i, v, spec_code;
116 boolean done = false ;
117 halfword attr_list;
118 boolean attr_done = false ;
119 boolean dir_done = false ;
120 if (attr_list_cache == cache_disabled)
121 update_attribute_cache();
122 attr_list = attr_list_cache;
123 /*tex The box context: */
124 s = saved_value(0);
125 do {
126 get_x_token();
127 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
128 if (cur_cmd == left_brace_cmd) {
129 goto QUICK;
130 } else {
131 back_input();
132 goto KEYWORDS;
133 }
134 CONTINUE:
135 while (cur_cmd == relax_cmd || cur_cmd == spacer_cmd) {
136 get_x_token();
137 if (cur_cmd == left_brace_cmd) {
138 goto QUICK;
139 } else if (cur_cmd != relax_cmd && cur_cmd != spacer_cmd) {
140 back_input();
141 break;
142 }
143 }
144 KEYWORDS:
145 /*tex Multiple |attr| keys possible (before or after dir). */
146 if (scan_keyword("attr")) {
147 scan_register_num();
148 i = cur_val;
149 scan_optional_equals();
150 scan_int();
151 v = cur_val;
152 if (! attr_done) {
153 attr_list = copy_attribute_list(attr_list_cache);
154 attr_done = true;
155 }
156 attr_list = do_set_attribute(attr_list, i, v);
157 goto CONTINUE;
158 }
159 /*tex We only permit one |(b)dir| directive. */
160 if (! dir_done) {
161 if (scan_keyword("bdir")) {
162 scan_int();
163 check_dir_value(cur_val);
164 spec_direction = cur_val;
165 dir_done = true;
166 goto CONTINUE;
167 }
168 if (scan_keyword("dir")) {
169 scan_direction();
170 spec_direction = cur_val;
171 dir_done = true;
172 goto CONTINUE;
173 }
174 }
175 /*tex Only one |to| or |spread| key possible and it comes last. */
176 if (scan_keyword("to")) {
177 spec_code = exactly;
178 } else if (scan_keyword("spread")) {
179 spec_code = additional;
180 } else {
181 spec_code = additional;
182 cur_val = 0;
183 goto FOUND;
184 }
185 scan_normal_dimen();
186 goto FOUND;
187 QUICK:
188 spec_code = additional;
189 cur_val = 0;
190 done = true;
191 FOUND:
192 add_node_attr_ref(attr_list);
193 set_saved_record(0, saved_boxcontext, 0, s);
194 set_saved_record(1, saved_boxspec, spec_code, cur_val);
195 /*tex Adjust |text_dir_ptr| for |scan_spec|. */
196 if (spec_direction != -1) {
197 set_saved_record(2, saved_boxdir, spec_direction, text_dir_ptr);
198 text_dir_ptr = new_dir(spec_direction);
199 } else {
200 set_saved_record(2, saved_boxdir, spec_direction, null);
201 }
202 set_saved_record(3, saved_boxattr, 0, attr_list);
203 set_saved_record(4, saved_boxpack, 0, just_pack);
204 save_ptr += 5;
205 new_save_level(c);
206 if (! done) {
207 scan_left_brace();
208 }
209 /*tex No gain in |if (body_direction_par != spec_direction)| etc. */
210 eq_word_define(int_base + body_direction_code, spec_direction);
211 eq_word_define(int_base + par_direction_code, spec_direction);
212 eq_word_define(int_base + text_direction_code, spec_direction);
213 }
214
215 /*tex
216
217 To figure out the glue setting, |hpack| and |vpack| determine how much
218 stretchability and shrinkability are present, considering all four orders of
219 infinity. The highest order of infinity that has a nonzero coefficient is
220 then used as if no other orders were present.
221
222 For example, suppose that the given list contains six glue nodes with the
223 respective stretchabilities 3pt, 8fill, 5fil, 6pt, $-3$fil, $-8$fill. Then
224 the total is essentially 2fil; and if a total additional space of 6pt is to
225 be achieved by stretching, the actual amounts of stretch will be 0pt, 0pt,
226 15pt, 0pt, $-9$pt, and 0pt, since only `fil' glue will be considered. (The
227 `fill' glue is therefore not really stretching infinitely with respect to
228 `fil'; nobody would actually want that to happen.)
229
230 The arrays |total_stretch| and |total_shrink| are used to determine how much
231 glue of each kind is present. A global variable |last_badness| is used to
232 implement \.{\\badness}.
233
234 */
235
236 /*tex Glue found by |hpack| or |vpack|. */
237
238 scaled total_stretch[5];
239 scaled total_shrink[5];
240
241 /*tex Badness of the most recently packaged box. */
242
243 int last_badness;
244
245 /*tex
246
247 If the global variable |adjust_tail| is non-null, the |hpack| routine also
248 removes all occurrences of |ins_node|, |mark_node|, and |adjust_node| items
249 and appends the resulting material onto the list that ends at location
250 |adjust_tail|.
251
252 */
253
254 /*tex Tail of adjustment list. */
255
256 halfword adjust_tail;
257
258 /*tex
259
260 Materials in \.{\\vadjust} used with \.{pre} keyword will be appended to
261 |pre_adjust_tail| instead of |adjust_tail|.
262
263 */
264
265 halfword pre_adjust_tail;
266
267 halfword last_leftmost_char;
268 halfword last_rightmost_char;
269
270 /*tex Pointers to the prev and next char of an implicit kern. */
271
272 halfword next_char_p;
273 halfword prev_char_p;
274
275 /*tex This procedure is called repeatedly from inside the line break algorithm. */
276
277 void set_prev_char_p(halfword p)
278 {
279 prev_char_p = p;
280 }
281
282 /*tex
283
284 The kern stretch / shrink code was (or had become) rather weird ... the width
285 field is set, and then used in a second calculation, repeatedly, so why is
286 that ... maybe some some weird left-over ... anyway, the values are so small
287 that in practice they are not significant at all when the backend sees them
288 because a few hundred sp positive or negative are just noise there (so
289 adjustlevel 3 has hardly any consequence for the result but is more
290 efficient).
291
292 */
293
294 scaled char_stretch(halfword p)
295 {
296 internal_font_number f = font(p);
297 int m = font_max_stretch(f);
298 if (m > 0) {
299 int c = character(p);
300 int ef = get_ef_code(f, c);
301 if (ef > 0) {
302 scaled dw = calc_char_width(f, c, m) - char_width(f, c);
303 if (dw > 0) {
304 return round_xn_over_d(dw, ef, 1000);
305 }
306 }
307 }
308 return 0;
309 }
310
311 scaled char_shrink(halfword p)
312 {
313 internal_font_number f = font(p);
314 int m = font_max_shrink(f);
315 if (m > 0) {
316 int c = character(p);
317 int ef = get_ef_code(f, c);
318 if (ef > 0) {
319 scaled dw = char_width(f, c) - calc_char_width(f, c, -m);
320 if (dw > 0) {
321 return round_xn_over_d(dw, ef, 1000);
322 }
323 }
324 }
325 return 0;
326 }
327
328 scaled kern_stretch(halfword p)
329 {
330 int m;
331 scaled d, e, x;
332 scaled w = width(p) ;
333 halfword l;
334 halfword r;
335 if (w == 0) {
336 /*tex Why bother about zero kerns. */
337 return 0;
338 }
339 l = prev_char_p ;
340 if ((l == null) || (vlink(l) != p)) {
341 /*tex We only care about kerns following a char. */
342 return 0;
343 }
344 r = vlink(p);
345 if (r == null) {
346 /*tex We only care about kerns between a char and something else. */
347 }
348 if (!(is_char_node(l) && is_char_node(r))) {
349 /*tex We want two chars (but but don't care about the fonts). */
350 return 0;
351 }
352 /*tex We use the old logic, kind of, but average the ef as we might depend on proper overlap. */
353 m = (font_max_stretch(font(l)) + font_max_stretch(font(r)))/2;
354 if (m == 0) {
355 /*tex nothing to kern */
356 return 0;
357 }
358 d = round_xn_over_d(w, 1000 + m, 1000);
359 /*tex We use the old logic, kind of, but average the ef as we might depend on proper overlap. */
360 e = (get_ef_code(font(l), character(l)) + get_ef_code(font(r), character(r)))/2 ;
361 if (e == 1000) {
362 x = d - w;
363 } else {
364 x = round_xn_over_d(d - w, e, 1000);
365 }
366 return x;
367 }
368
369 scaled kern_shrink(halfword p)
370 {
371 int m;
372 scaled d, e, x;
373 scaled w = width(p) ;
374 halfword l;
375 halfword r;
376 if (w == 0) {
377 /*tex Why bother about zero kerns. */
378 return 0;
379 }
380 l = prev_char_p ;
381 if ((l == null) || (vlink(l) != p)) {
382 /*tex We only care about kerns following a char. */
383 return 0;
384 }
385 r = vlink(p);
386 if (r == null) {
387 /*tex We only care about kerns between a char and something else. */
388 }
389 if (!(is_char_node(l) && is_char_node(r))) {
390 /*tex We want two chars (but but don't care about the fonts). */
391 return 0;
392 }
393 /*tex We use the old logic, kind of, but average the ef as we might depend on proper overlap. */
394 m = (font_max_shrink(font(l)) + font_max_shrink(font(r)))/2;
395 if (m == 0) {
396 /*tex Nothing to kern. */
397 return 0;
398 }
399 d = round_xn_over_d(w, 1000 - m, 1000);
400 e = (get_ef_code(font(l), character(l)) + get_ef_code(font(r), character(r)))/2 ;
401 if (e == 1000) {
402 x = w - d ;
403 } else {
404 x = round_xn_over_d(w - d, e, 1000);
405 }
406 return x;
407 }
408
409 void do_subst_font(halfword p, int ex_ratio)
410 {
411 if (type(p) == disc_node) {
412 halfword r = vlink(pre_break(p));
413 while (r != null) {
414 if (is_char_node(r)) {
415 if (tracing_lost_chars_par > 3 && ! char_exists(font(r), character(r))) {
416 char_warning(font(r), character(r));
417 }
418 do_subst_font(r, ex_ratio);
419 }
420 r = vlink(r);
421 }
422 r = vlink(post_break(p));
423 while (r != null) {
424 if (is_char_node(r)) {
425 if (tracing_lost_chars_par > 3 && ! char_exists(font(r), character(r))) {
426 char_warning(font(r), character(r));
427 }
428 do_subst_font(r, ex_ratio);
429 }
430 r = vlink(r);
431 }
432 r = vlink(no_break(p));
433 while (r != null) {
434 if (is_char_node(r)) {
435 if (tracing_lost_chars_par > 3 && ! char_exists(font(r), character(r))) {
436 char_warning(font(r), character(r));
437 }
438 do_subst_font(r, ex_ratio);
439 }
440 r = vlink(r);
441 }
442 return;
443 }
444 if (! is_char_node(p)) {
445 normal_error("font expansion", "invalid node type");
446 return;
447 } else {
448 internal_font_number f = font(p);
449 int ef = get_ef_code(f, character(p));
450 if (ef == 0)
451 return;
452 if ((font_max_stretch(f) > 0) && (ex_ratio > 0)) {
453 int ex_stretch = ext_xn_over_d(ex_ratio * ef, font_max_stretch(f), 1000000);
454 ex_glyph(p) = fix_expand_value(f, ex_stretch)*1000;
455 } else if ((font_max_shrink(f) > 0) && (ex_ratio < 0)) {
456 int ex_shrink = ext_xn_over_d(ex_ratio * ef, font_max_shrink(f), 1000000);
457 ex_glyph(p) = fix_expand_value(f, ex_shrink)*1000;
458 }
459 }
460 }
461
462 scaled char_pw(halfword p, int side)
463 {
464 internal_font_number f;
465 int c, w;
466 if (side == left_side)
467 last_leftmost_char = null;
468 else
469 last_rightmost_char = null;
470 if (p == null)
471 return 0;
472 if (!is_char_node(p))
473 return 0;
474 f = font(p);
475 if (side == left_side) {
476 c = get_lp_code(f, character(p));
477 last_leftmost_char = p;
478 } else {
479 c = get_rp_code(f, character(p));
480 last_rightmost_char = p;
481 }
482 if (c == 0)
483 return 0;
484 w = quad(f);
485 return round_xn_over_d(w, c, 1000);
486 }
487
488 halfword new_margin_kern(scaled w, halfword p, int side)
489 {
490 halfword k, q;
491 k = new_node(margin_kern_node, side);
492 width(k) = w;
493 if (p == null)
494 normal_error("margin kerning", "invalid pointer to marginal char node");
495 q = new_char(font(p), character(p));
496 margin_char(k) = q;
497 return k;
498 }
499
500 /*tex
501
502 Here we prepare for |hpack|, which is place where we do font substituting
503 when font expansion is being used.
504
505 */
506
507 /*tex The current expansion ratio, needed for recursive call. */
508
509 int font_expand_ratio = 0;
510
511 int ignore_math_skip(halfword p)
512 {
513 if (math_skip_mode == 6) {
514 if (subtype(p) == after) {
515 if (math_skip_boundary(vlink(p))) {
516 return 0;
517 }
518 } else {
519 if (math_skip_boundary(alink(p))) {
520 return 0;
521 }
522 }
523 } else if (math_skip_mode == 7) {
524 if (subtype(p) == after) {
525 if (! math_skip_boundary(vlink(p))) {
526 return 0;
527 }
528 } else {
529 if (! math_skip_boundary(alink(p))) {
530 return 0;
531 }
532 }
533 } else {
534 return 0;
535 }
536 reset_glue_to_zero(p);
537 return 1;
538 }
539
540 halfword hpack(halfword p, scaled w, int m, int pack_direction)
541 {
542 /*tex the box node that will be returned */
543 halfword r;
544 /*tex trails behind |p| */
545 halfword q;
546 /*tex height */
547 scaled h = 0;
548 /*tex depth */
549 scaled d = 0;
550 /*tex natural width */
551 scaled x = 0;
552 scaled_whd whd;
553 /*tex shift amount */
554 scaled s;
555 /*tex order of infinity */
556 int o;
557 /*tex for managing the direction stack */
558 halfword dir_ptr1 = null;
559 /*tex the current direction */
560 int hpack_dir;
561 int disc_level = 0;
562 halfword pack_interrupt[8];
563 scaled font_stretch = 0;
564 scaled font_shrink = 0;
565 int adjust_spacing = adjust_spacing_par;
566 last_badness = 0;
567 /*tex the box node that will be returned */
568 r = new_node(hlist_node, min_quarterword);
569 if (pack_direction == -1) {
570 hpack_dir = text_direction_par;
571 } else {
572 hpack_dir = pack_direction;
573 }
574 box_dir(r) = hpack_dir;
575 /*tex
576
577 A potential optimimization, save a little but neglectable in practice
578 (not so many empty boxes are used):
579
580 \starttyping
581 if (p == null) {
582 width(r) = w;
583 return r;
584 }
585 \stoptyping
586
587 */
588 /*tex push null */
589 push_dir(dir_ptr1,hpack_dir);
590 /*tex hm, adding something to a node address? */
591 q = r + list_offset;
592 vlink(q) = p;
593 if (m == cal_expand_ratio) {
594 /*tex Why not always: */
595 prev_char_p = null;
596 }
597 if (adjust_spacing > 2) {
598 adjust_spacing = 0;
599 }
600 total_stretch[normal] = 0;
601 total_shrink[normal] = 0;
602 total_stretch[sfi] = 0;
603 total_shrink[sfi] = 0;
604 total_stretch[fil] = 0;
605 total_shrink[fil] = 0;
606 total_stretch[fill] = 0;
607 total_shrink[fill] = 0;
608 total_stretch[filll] = 0;
609 total_shrink[filll] = 0;
610 RESWITCH:
611 while ((p != null) || (disc_level > 0)) {
612 if (p == null) {
613 decr(disc_level);
614 p = pack_interrupt[disc_level];
615 goto RESWITCH;
616 }
617 /*tex
618
619 Examine node |p| in the hlist, taking account of its effect on the
620 dimensions of the new box, or moving it to the adjustment list; then
621 advance |p| to the next node.
622
623 */
624 while (is_char_node(p)) {
625 /*tex
626
627 Incorporate character dimensions into the dimensions of the hbox
628 that will contain~it, then move to the next node.
629
630 The following code is part of \TeX's inner loop; i.e., adding
631 another character of text to the user's input will cause each of
632 these instructions to be exercised one more time.
633
634 */
635 if (tracing_lost_chars_par > 3 && ! char_exists(font(p), character(p))) {
636 char_warning(font(p), character(p));
637 }
638 if (m >= cal_expand_ratio) {
639 prev_char_p = p;
640 if (m == cal_expand_ratio) {
641 font_stretch += char_stretch(p);
642 font_shrink += char_shrink(p);
643 } else if (m == subst_ex_font) {
644 do_subst_font(p, font_expand_ratio);
645 }
646 }
647 whd = pack_width_height_depth(hpack_dir, dir_TRT, p, true);
648 x += whd.wd;
649 if (whd.ht > h)
650 h = whd.ht;
651 if (whd.dp > d)
652 d = whd.dp;
653 p = vlink(p);
654 }
655 if (p != null) {
656 switch (type(p)) {
657 case hlist_node:
658 case vlist_node:
659 /*tex
660
661 Incorporate box dimensions into the dimensions of the
662 hbox that will contain~it
663
664 The code here implicitly uses the fact that running
665 dimensions are indicated by |null_flag|, which will be
666 ignored in the calculations because it is a highly
667 negative number.
668
669 */
670 s = shift_amount(p);
671 whd = pack_width_height_depth(hpack_dir, box_dir(p), p, false);
672 x += whd.wd;
673 if (whd.ht - s > h)
674 h = whd.ht - s;
675 if (whd.dp + s > d)
676 d = whd.dp + s;
677 break;
678 case rule_node:
679 case unset_node:
680 x += width(p);
681 if (height(p) > h)
682 h = height(p);
683 if (depth(p) > d)
684 d = depth(p);
685 break;
686 case math_node:
687 /*tex Begin mathskip code. */
688 if (glue_is_zero(p) || ignore_math_skip(p)) {
689 x += surround(p);
690 break;
691 } else {
692 /*tex Fall through. */
693 }
694 /*tex End mathskip code. */
695 case glue_node:
696 /*tex Incorporate glue into the horizontal totals. */
697 x += width(p);
698 o = stretch_order(p);
699 total_stretch[o] = total_stretch[o] + stretch(p);
700 o = shrink_order(p);
701 total_shrink[o] = total_shrink[o] + shrink(p);
702 if (subtype(p) >= a_leaders) {
703 halfword g = leader_ptr(p);
704 if (height(g) > h)
705 h = height(g);
706 if (depth(g) > d)
707 d = depth(g);
708 }
709 break;
710 case kern_node:
711 x += width(p);
712 if (subtype(p) == font_kern && adjust_spacing) {
713 /*tex So only when 1 or 2. */
714 if (m == cal_expand_ratio) {
715 font_stretch = font_stretch + kern_stretch(p);
716 font_shrink = font_shrink + kern_shrink(p);
717 } else if (m == subst_ex_font) {
718 /*tex This is the finalizer. */
719 int k = 0;
720 if (font_expand_ratio > 0) {
721 k = kern_stretch(p);
722 } else if (font_expand_ratio < 0) {
723 k = kern_shrink(p);
724 }
725 ex_kern(p) = k;
726 x += k;
727 }
728 }
729 break;
730 case disc_node:
731 if (m == subst_ex_font)
732 do_subst_font(p, font_expand_ratio);
733 if ((subtype(p) != select_disc) && (vlink(no_break(p)) != null)) {
734 pack_interrupt[disc_level] = vlink(p);
735 incr(disc_level);
736 p = no_break(p);
737 }
738 break;
739 case dir_node:
740 /*tex Adjust the dir stack for the |hpack| routine. */
741 if (subtype(p) == normal_dir) {
742 hpack_dir = dir_dir(p);
743 push_dir_node(dir_ptr1,p);
744 } else {
745 pop_dir_node(dir_ptr1);
746 if (dir_ptr1 != null)
747 hpack_dir = dir_dir(dir_ptr1);
748 }
749 break;
750 case margin_kern_node:
751 if (m == cal_expand_ratio) {
752 int f = font(margin_char(p));
753 do_subst_font(margin_char(p), 1000);
754 if (f != font(margin_char(p)))
755 font_stretch = font_stretch - width(p) - char_pw(margin_char(p), subtype(p));
756 font(margin_char(p)) = f;
757 do_subst_font(margin_char(p), -1000);
758 if (f != font(margin_char(p)))
759 font_shrink = font_shrink - width(p) - char_pw(margin_char(p), subtype(p));
760 font(margin_char(p)) = f;
761 } else if (m == subst_ex_font) {
762 do_subst_font(margin_char(p), font_expand_ratio);
763 width(p) = -char_pw(margin_char(p), subtype(p));
764 }
765 x += width(p);
766 break;
767 case ins_node:
768 case mark_node:
769 case adjust_node:
770 /*tex
771
772 Transfer node |p| to the adjustment list.\Although node
773 |q| is not necessarily the immediate predecessor of node
774 |p|, it always points to some node in the list preceding
775 |p|. Thus, we can delete nodes by moving |q| when
776 necessary. The algorithm takes linear time, and the extra
777 computation does not intrude on the inner loop unless it
778 is necessary to make a deletion.
779
780 */
781 if (adjust_tail != null || pre_adjust_tail != null) {
782 while (vlink(q) != p)
783 q = vlink(q);
784 if (type(p) == adjust_node) {
785 if (adjust_pre(p) != 0)
786 update_adjust_list(pre_adjust_tail);
787 else
788 update_adjust_list(adjust_tail);
789 p = vlink(p);
790 adjust_ptr(vlink(q)) = null;
791 flush_node(vlink(q));
792 } else {
793 vlink(adjust_tail) = p;
794 adjust_tail = p;
795 p = vlink(p);
796 }
797 vlink(q) = p;
798 p = q;
799 }
800 break;
801 default:
802 break;
803 }
804 p = vlink(p);
805 }
806 }
807
808 if (adjust_tail != null)
809 vlink(adjust_tail) = null;
810 if (pre_adjust_tail != null)
811 vlink(pre_adjust_tail) = null;
812 height(r) = h;
813 depth(r) = d;
814 /*tex
815
816 Determine the value of |width(r)| and the appropriate glue setting; then
817 |return| or |goto common_ending|.
818
819 When we get to the present part of the program, |x| is the natural width
820 of the box being packaged.
821
822 */
823 if (m == additional)
824 w = x + w;
825 width(r) = w;
826 x = w - x;
827 /*tex Now |x| is the excess to be made up. */
828 if (x == 0) {
829 glue_sign(r) = normal;
830 glue_order(r) = normal;
831 set_glue_ratio_zero(glue_set(r));
832 goto EXIT;
833 } else if (x > 0) {
834 /*tex
835
836 Determine horizontal glue stretch setting, then |return| or
837 |goto common_ending|.
838
839 If |hpack| is called with |m=cal_expand_ratio| we calculate
840 |font_expand_ratio| and return without checking for overfull or
841 underfull box.
842
843 */
844 if (total_stretch[filll] != 0)
845 o = filll;
846 else if (total_stretch[fill] != 0)
847 o = fill;
848 else if (total_stretch[fil] != 0)
849 o = fil;
850 else if (total_stretch[sfi] != 0)
851 o = sfi;
852 else
853 o = normal;
854 if ((m == cal_expand_ratio) && (o == normal) && (font_stretch > 0)) {
855 font_expand_ratio = divide_scaled_n(x, font_stretch, 1000.0);
856 goto EXIT;
857 }
858 glue_order(r) = (quarterword) o;
859 glue_sign(r) = stretching;
860 if (total_stretch[o] != 0) {
861 glue_set(r) = unfloat((double) x / total_stretch[o]);
862 } else {
863 /*tex There's nothing to stretch. */
864 glue_sign(r) = normal;
865 set_glue_ratio_zero(glue_set(r));
866 }
867 if (o == normal) {
868 if (list_ptr(r) != null) {
869 /*tex
870
871 Report an underfull hbox and |goto common_ending|, if this
872 box is sufficiently bad.
873
874 */
875 last_badness = badness(x, total_stretch[normal]);
876 if (last_badness > hbadness_par) {
877 int callback_id = callback_defined(hpack_quality_callback);
878 if (callback_id > 0) {
879 halfword rule = null;
880 if (last_badness > 100) {
881 run_callback(callback_id, "SdNdd->N","underfull",last_badness,r,abs(pack_begin_line),line,&rule);
882 } else {
883 run_callback(callback_id, "SdNdd->N","loose",last_badness,r,abs(pack_begin_line),line,&rule);
884 }
885 if (rule != null) {
886 while (vlink(q) != null) {
887 q = vlink(q);
888 }
889 couple_nodes(q,rule);
890 }
891 } else {
892 print_ln();
893 if (last_badness > 100) {
894 tprint_nl("Underfull \\hbox (badness ");
895 } else {
896 tprint_nl("Loose \\hbox (badness ");
897 }
898 print_int(last_badness);
899 goto COMMON_ENDING;
900 }
901 }
902 }
903 }
904 goto EXIT;
905 } else {
906 /*tex
907
908 Determine horizontal glue shrink setting, then |return| or |goto
909 common_ending|,
910
911 */
912 if (total_shrink[filll] != 0)
913 o = filll;
914 else if (total_shrink[fill] != 0)
915 o = fill;
916 else if (total_shrink[fil] != 0)
917 o = fil;
918 else if (total_shrink[sfi] != 0)
919 o = sfi;
920 else
921 o = normal;
922 if ((m == cal_expand_ratio) && (o == normal) && (font_shrink > 0)) {
923 font_expand_ratio = divide_scaled_n(x, font_shrink, 1000.0);
924 goto EXIT;
925 }
926 glue_order(r) = (quarterword) o;
927 glue_sign(r) = shrinking;
928 if (total_shrink[o] != 0) {
929 glue_set(r) = unfloat((double) (-x) / (double) total_shrink[o]);
930 } else {
931 /*tex There's nothing to shrink. */
932 glue_sign(r) = normal;
933 set_glue_ratio_zero(glue_set(r));
934 }
935 if ((total_shrink[o] < -x) && (o == normal) && (list_ptr(r) != null)) {
936 int overshoot = -x - total_shrink[normal] ;
937 last_badness = 1000000;
938 /*tex Use the maximum shrinkage */
939 set_glue_ratio_one(glue_set(r));
940 /*tex
941
942 Report an overfull hbox and |goto common_ending|, if this box is
943 sufficiently bad.
944
945 */
946 if ((overshoot > hfuzz_par) || (hbadness_par < 100)) {
947 int callback_id = callback_defined(hpack_quality_callback);
948 halfword rule = null;
949 if (callback_id > 0) {
950 run_callback(callback_id, "SdNdd->N","overfull",overshoot,r,abs(pack_begin_line),line,&rule);
951 } else if (overfull_rule_par > 0) {
952 rule = new_rule(normal_rule);
953 rule_dir(rule) = box_dir(r);
954 width(rule) = overfull_rule_par;
955 }
956 if (rule != null) {
957 while (vlink(q) != null) {
958 q = vlink(q);
959 }
960 couple_nodes(q,rule);
961 }
962 if (callback_id == 0) {
963 print_ln();
964 tprint_nl("Overfull \\hbox (");
965 print_scaled(overshoot);
966 tprint("pt too wide");
967 goto COMMON_ENDING;
968 }
969 }
970 } else if (o == normal) {
971 if (list_ptr(r) != null) {
972 /*tex
973
974 Report a tight hbox and |goto common_ending|, if this box is
975 sufficiently bad.
976
977 */
978 last_badness = badness(-x, total_shrink[normal]);
979 if (last_badness > hbadness_par) {
980 int callback_id = callback_defined(hpack_quality_callback);
981 if (callback_id > 0) {
982 halfword rule = null;
983 run_callback(callback_id, "SdNdd->N","tight",last_badness,r,abs(pack_begin_line),line,&rule);
984 if (rule != null) {
985 while (vlink(q) != null) {
986 q = vlink(q);
987 }
988 couple_nodes(q,rule);
989 }
990 } else {
991 print_ln();
992 tprint_nl("Tight \\hbox (badness ");
993 print_int(last_badness);
994 goto COMMON_ENDING;
995 }
996 }
997 }
998 }
999 goto EXIT;
1000 }
1001
1002 COMMON_ENDING:
1003 /*tex Finish issuing a diagnostic message for an overfull or underfull hbox. */
1004 if (output_active) {
1005 tprint(") has occurred while \\output is active");
1006 } else {
1007 if (pack_begin_line != 0) {
1008 if (pack_begin_line > 0) {
1009 tprint(") in paragraph at lines ");
1010 } else {
1011 tprint(") in alignment at lines ");
1012 }
1013 print_int(abs(pack_begin_line));
1014 tprint("--");
1015 } else {
1016 tprint(") detected at line ");
1017 }
1018 print_int(line);
1019 }
1020
1021 print_ln();
1022 font_in_short_display = null_font;
1023 short_display(list_ptr(r));
1024 print_ln();
1025 begin_diagnostic();
1026 show_box(r);
1027 end_diagnostic(true);
1028 EXIT:
1029 if ((m == cal_expand_ratio) && (font_expand_ratio != 0)) {
1030 font_expand_ratio = fix_int(font_expand_ratio, -1000, 1000);
1031 q = list_ptr(r);
1032 list_ptr(r) = null;
1033 flush_node(r);
1034 /*tex This nested call uses the more or less global font_expand_ratio. */
1035 r = hpack(q, w, subst_ex_font, hpack_dir);
1036 }
1037 while (dir_ptr1 != null)
1038 pop_dir_node(dir_ptr1);
1039 /*tex Here we reset the |font_expand_ratio|. */
1040 font_expand_ratio = 0;
1041 return r;
1042 }
1043
1044 halfword filtered_hpack(halfword p, halfword qt, scaled w, int m, int grp, int pac, int just_pack, halfword attr)
1045 {
1046 halfword q;
1047 if (just_pack) {
1048 q = vlink(p);
1049 } else if (type(p) == temp_node && vlink(p) == null) {
1050 q = vlink(p);
1051 } else {
1052 new_hyphenation(p, qt);
1053 /*tex We don't care about the tail in this case. */
1054 (void) new_ligkern(p, qt);
1055 q = vlink(p);
1056 /*tex Maybe here: |alink(p) = null|. */
1057 /*tex ignores empty anyway. Maybe also pass tail? */
1058 q = lua_hpack_filter(q, w, m, grp, pac, attr);
1059 }
1060 return hpack(q, w, m, pac);
1061 }
1062
1063 /*tex
1064
1065 Here is a function to calculate the natural whd of a (horizontal) node list.
1066
1067 */
1068
1069 scaled_whd natural_sizes(halfword p, halfword pp, glue_ratio g_mult, int g_sign, int g_order, int pack_direction)
1070 {
1071 /*tex shift amount */
1072 scaled s;
1073 /*tex points to a glue specification */
1074 halfword g;
1075 int hpack_dir;
1076 /*tex For recursion */
1077 scaled_whd xx;
1078 scaled_whd whd, siz = { 0, 0, 0 };
1079 scaled gp = 0;
1080 scaled gm = 0;
1081 if (pack_direction == -1) {
1082 hpack_dir = text_direction_par;
1083 } else {
1084 hpack_dir = pack_direction;
1085 }
1086 while (p != pp && p != null) {
1087 while (is_char_node(p) && p != pp) {
1088 if (tracing_lost_chars_par > 3 && ! char_exists(font(p), character(p))) {
1089 char_warning(font(p), character(p));
1090 }
1091 whd = pack_width_height_depth(hpack_dir, dir_TRT, p, true);
1092 siz.wd += whd.wd;
1093 if (whd.ht > siz.ht)
1094 siz.ht = whd.ht;
1095 if (whd.dp > siz.dp)
1096 siz.dp = whd.dp;
1097 p = vlink(p);
1098 }
1099 if (p != pp && p != null) {
1100 switch (type(p)) {
1101 case hlist_node:
1102 case vlist_node:
1103 s = shift_amount(p);
1104 whd = pack_width_height_depth(hpack_dir, box_dir(p), p, false);
1105 siz.wd += whd.wd;
1106 if (whd.ht - s > siz.ht)
1107 siz.ht = whd.ht - s;
1108 if (whd.dp + s > siz.dp)
1109 siz.dp = whd.dp + s;
1110 break;
1111 case rule_node:
1112 case unset_node:
1113 siz.wd += width(p);
1114 if (height(p) > siz.ht)
1115 siz.ht = height(p);
1116 if (depth(p) > siz.dp)
1117 siz.dp = depth(p);
1118 break;
1119 case math_node:
1120 /*tex Begin mathskip code. */
1121 if (glue_is_zero(p) || ignore_math_skip(p)) {
1122 siz.wd += surround(p);
1123 break;
1124 } else {
1125 /*tex Fall through. */
1126 }
1127 /*tex End mathskip code. */
1128 case glue_node:
1129 siz.wd += width(p);
1130 if (g_sign != normal) {
1131 if (g_sign == stretching) {
1132 if (stretch_order(p) == g_order) {
1133 /*tex
1134 |siz.wd += float_round(float_cast(g_mult) * float_cast(stretch(p)))|
1135 */
1136 gp += stretch(p);
1137 }
1138 } else if (shrink_order(p) == g_order) {
1139 /*tex
1140 |siz.wd -= float_round(float_cast(g_mult) * float_cast(shrink(p)));|
1141 */
1142 gm += shrink(p);
1143 }
1144 }
1145 if (subtype(p) >= a_leaders) {
1146 g = leader_ptr(p);
1147 if (height(g) > siz.ht)
1148 siz.ht = height(g);
1149 if (depth(g) > siz.dp)
1150 siz.dp = depth(g);
1151 }
1152 break;
1153 case margin_kern_node:
1154 siz.wd += width(p);
1155 break;
1156 case kern_node:
1157 siz.wd += width(p) + ex_kern(p);
1158 break;
1159 case disc_node:
1160 xx = natural_sizes(no_break(p), null, g_mult, g_sign, g_order, hpack_dir);
1161 siz.wd += xx.wd;
1162 if (xx.ht > siz.ht)
1163 siz.ht = xx.ht;
1164 if (xx.dp > siz.dp)
1165 siz.dp = xx.dp;
1166 break;
1167 default:
1168 break;
1169 }
1170 p = vlink(p);
1171 }
1172
1173 }
1174 if (g_sign != normal) {
1175 if (g_sign == stretching) {
1176 siz.wd += float_round(float_cast(g_mult) * float_cast(gp));
1177 } else {
1178 siz.wd -= float_round(float_cast(g_mult) * float_cast(gm));
1179 }
1180 }
1181 return siz;
1182 }
1183
1184 /*tex
1185
1186 In order to provide a decent indication of where an overfull or underfull box
1187 originated, we use a global variable |pack_begin_line| that is set nonzero
1188 only when |hpack| is being called by the paragraph builder or the alignment
1189 finishing routine.
1190
1191 The source file line where the current paragraph or alignment began; a
1192 negative value denotes alignment:
1193
1194 */
1195
1196 int pack_begin_line;
1197
1198 /*tex
1199
1200 The |vpack| subroutine is actually a special case of a slightly more general
1201 routine called |vpackage|, which has four parameters. The fourth parameter,
1202 which is |max_dimen| in the case of |vpack|, specifies the maximum depth of
1203 the page box that is constructed. The depth is first computed by the normal
1204 rules; if it exceeds this limit, the reference point is simply moved down
1205 until the limiting depth is attained.
1206
1207 */
1208
1209 halfword vpackage(halfword p, scaled h, int m, scaled l, int pack_direction)
1210 {
1211 /*tex the box node that will be returned */
1212 halfword r;
1213 /*tex width */
1214 scaled w = 0;
1215 /*tex depth */
1216 scaled d = 0;
1217 /*tex natural height */
1218 scaled x = 0;
1219 scaled_whd whd;
1220 /*tex shift amount */
1221 scaled s;
1222 /*tex order of infinity */
1223 int o;
1224 last_badness = 0;
1225 r = new_node(vlist_node, 0);
1226 if (pack_direction == -1) {
1227 box_dir(r) = body_direction_par;
1228 } else {
1229 box_dir(r) = pack_direction;
1230 }
1231 subtype(r) = min_quarterword;
1232 shift_amount(r) = 0;
1233 list_ptr(r) = p;
1234 total_stretch[normal] = 0;
1235 total_shrink[normal] = 0;
1236 total_stretch[sfi] = 0;
1237 total_shrink[sfi] = 0;
1238 total_stretch[fil] = 0;
1239 total_shrink[fil] = 0;
1240 total_stretch[fill] = 0;
1241 total_shrink[fill] = 0;
1242 total_stretch[filll] = 0;
1243 total_shrink[filll] = 0;
1244 while (p != null) {
1245 /*tex
1246
1247 Examine node |p| in the vlist, taking account of its effect on the
1248 dimensions of the new box; then advance |p| to the next node.
1249
1250 */
1251 if (is_char_node(p)) {
1252 confusion("vpack");
1253 } else {
1254 switch (type(p)) {
1255 case hlist_node:
1256 case vlist_node:
1257 /*tex
1258
1259 Incorporate box dimensions into the dimensions of the vbox
1260 that will contain it.
1261
1262 */
1263 s = shift_amount(p);
1264 whd = pack_width_height_depth(box_dir(r), box_dir(p), p, false);
1265 if (whd.wd + s > w)
1266 w = whd.wd + s;
1267 x += d + whd.ht;
1268 d = whd.dp;
1269 break;
1270 case rule_node:
1271 case unset_node:
1272 x += d + height(p);
1273 d = depth(p);
1274 if (width(p) > w)
1275 w = width(p);
1276 break;
1277 case glue_node:
1278 /*tex Incorporate glue into the vertical totals. */
1279 x += d;
1280 d = 0;
1281 x += width(p);
1282 o = stretch_order(p);
1283 total_stretch[o] = total_stretch[o] + stretch(p);
1284 o = shrink_order(p);
1285 total_shrink[o] = total_shrink[o] + shrink(p);
1286 if (subtype(p) >= a_leaders) {
1287 halfword g = leader_ptr(p);
1288 if (width(g) > w)
1289 w = width(g);
1290 }
1291 break;
1292 case kern_node:
1293 x += d + width(p);
1294 d = 0;
1295 break;
1296 default:
1297 break;
1298 }
1299 }
1300 p = vlink(p);
1301 }
1302 width(r) = w;
1303 if (d > l) {
1304 x += d - l;
1305 depth(r) = l;
1306 } else {
1307 depth(r) = d;
1308 }
1309 /*tex
1310
1311 Determine the value of |height(r)| and the appropriate glue setting; then
1312 |return| or |goto common_ending|.
1313
1314 When we get to the present part of the program, |x| is the natural height
1315 of the box being packaged.
1316 */
1317 if (m == additional)
1318 h = x + h;
1319 height(r) = h;
1320 x = h - x;
1321 /*tex Now |x| is the excess to be made up. */
1322 if (x == 0) {
1323 glue_sign(r) = normal;
1324 glue_order(r) = normal;
1325 set_glue_ratio_zero(glue_set(r));
1326 return r;
1327 } else if (x > 0) {
1328 /*tex
1329
1330 Determine vertical glue stretch setting, then |return| or |goto
1331 common_ending|.
1332
1333 */
1334 if (total_stretch[filll] != 0)
1335 o = filll;
1336 else if (total_stretch[fill] != 0)
1337 o = fill;
1338 else if (total_stretch[fil] != 0)
1339 o = fil;
1340 else if (total_stretch[sfi] != 0)
1341 o = sfi;
1342 else
1343 o = normal;
1344 glue_order(r) = (quarterword) o;
1345 glue_sign(r) = stretching;
1346 if (total_stretch[o] != 0) {
1347 glue_set(r) = unfloat((double) x / total_stretch[o]);
1348 } else {
1349 /*tex There's nothing to stretch. */
1350 glue_sign(r) = normal;
1351 set_glue_ratio_zero(glue_set(r));
1352 }
1353 if (o == normal) {
1354 if (list_ptr(r) != null) {
1355 /*tex
1356
1357 Report an underfull vbox and |goto common_ending|, if this
1358 box is sufficiently bad.
1359
1360 */
1361 last_badness = badness(x, total_stretch[normal]);
1362 if (last_badness > vbadness_par) {
1363 int callback_id = callback_defined(vpack_quality_callback);
1364 if (callback_id > 0) {
1365 if (last_badness > 100) {
1366 run_callback(callback_id, "SdNdd->","underfull",last_badness,r,abs(pack_begin_line),line);
1367 } else {
1368 run_callback(callback_id, "SdNdd->","loose",last_badness,r,abs(pack_begin_line),line);
1369 }
1370 goto EXIT;
1371 } else {
1372 print_ln();
1373 if (last_badness > 100) {
1374 tprint_nl("Underfull \\vbox (badness ");
1375 } else {
1376 tprint_nl("Loose \\vbox (badness ");
1377 }
1378 print_int(last_badness);
1379 goto COMMON_ENDING;
1380 }
1381 }
1382 }
1383 }
1384 return r;
1385 } else {
1386 /*tex
1387
1388 Determine vertical glue shrink setting, then |return| or |goto
1389 common_ending|.
1390
1391 */
1392 if (total_shrink[filll] != 0)
1393 o = filll;
1394 else if (total_shrink[fill] != 0)
1395 o = fill;
1396 else if (total_shrink[fil] != 0)
1397 o = fil;
1398 else if (total_shrink[sfi] != 0)
1399 o = sfi;
1400 else
1401 o = normal;
1402 glue_order(r) = (quarterword) o;
1403 glue_sign(r) = shrinking;
1404 if (total_shrink[o] != 0) {
1405 glue_set(r) = unfloat((double) (-x) / total_shrink[o]);
1406 } else {
1407 /*tex There's nothing to shrink. */
1408 glue_sign(r) = normal;
1409 set_glue_ratio_zero(glue_set(r));
1410 }
1411 if ((total_shrink[o] < -x) && (o == normal) && (list_ptr(r) != null)) {
1412 int overshoot = -x - total_shrink[normal];
1413 last_badness = 1000000;
1414 /*tex Use the maximum shrinkage */
1415 set_glue_ratio_one(glue_set(r));
1416 /*tex
1417
1418 Report an overfull vbox and |goto common_ending|, if this box is
1419 sufficiently bad.
1420
1421 */
1422 if ((overshoot > vfuzz_par) || (vbadness_par < 100)) {
1423 int callback_id = callback_defined(vpack_quality_callback);
1424 if (callback_id > 0) {
1425 run_callback(callback_id, "SdNdd->","overfull",overshoot,r,abs(pack_begin_line),line);
1426 goto EXIT;
1427 } else {
1428 print_ln();
1429 tprint_nl("Overfull \\vbox (");
1430 print_scaled(-x - total_shrink[normal]);
1431 tprint("pt too high");
1432 goto COMMON_ENDING;
1433 }
1434 }
1435 } else if (o == normal) {
1436 if (list_ptr(r) != null) {
1437 /*tex
1438
1439 Report a tight vbox and |goto common_ending|, if this box is
1440 sufficiently bad.
1441
1442 */
1443 last_badness = badness(-x, total_shrink[normal]);
1444 if (last_badness > vbadness_par) {
1445 int callback_id = callback_defined(vpack_quality_callback);
1446 if (callback_id > 0) {
1447 run_callback(callback_id, "SdNdd->","tight",last_badness,r,abs(pack_begin_line),line);
1448 goto EXIT;
1449 } else {
1450 print_ln();
1451 tprint_nl("Tight \\vbox (badness ");
1452 print_int(last_badness);
1453 goto COMMON_ENDING;
1454 }
1455 }
1456 }
1457 }
1458 return r;
1459 }
1460
1461 COMMON_ENDING:
1462 /*tex Finish issuing a diagnostic message or an overfull or underfull vbox. */
1463 if (output_active) {
1464 tprint(") has occurred while \\output is active");
1465 } else {
1466 if (pack_begin_line != 0) {
1467 /*tex It's actually negative. */
1468 tprint(") in alignment at lines ");
1469 print_int(abs(pack_begin_line));
1470 tprint("--");
1471 } else {
1472 tprint(") detected at line ");
1473 }
1474 print_int(line);
1475 print_ln();
1476 }
1477 begin_diagnostic();
1478 show_box(r);
1479 end_diagnostic(true);
1480 EXIT:
1481 return r;
1482 }
1483
1484 halfword filtered_vpackage(halfword p, scaled h, int m, scaled l, int grp, int pack_direction, int just_pack, halfword attr)
1485 {
1486 halfword q = p;
1487 if (!just_pack)
1488 q = lua_vpack_filter(q, h, m, l, grp, pack_direction, attr);
1489 return vpackage(q, h, m, l, pack_direction);
1490 }
1491
1492 void finish_vcenter(void)
1493 {
1494 halfword p;
1495 unsave();
1496 save_ptr--;
1497 p = vpack(vlink(cur_list.head_field), saved_value(0), saved_level(0), -1);
1498 pop_nest();
1499 p = math_vcenter_group(p);
1500 tail_append(p);
1501 }
1502
1503 void package(int c)
1504 {
1505 halfword saved0, saved2, saved3, saved4;
1506 int grp = cur_group;
1507 scaled d = box_max_depth_par;
1508 unsave();
1509 save_ptr -= 5;
1510 saved0 = saved_value(0);
1511 saved2 = saved_value(2);
1512 saved3 = saved_value(3);
1513 saved4 = saved_value(4);
1514 if (cur_list.mode_field == -hmode) {
1515 cur_box = filtered_hpack(cur_list.head_field, cur_list.tail_field,
1516 saved_value(1), saved_level(1), grp, saved_level(2), saved4, saved3);
1517 subtype(cur_box) = hbox_list;
1518 } else {
1519 cur_box = filtered_vpackage(vlink(cur_list.head_field),
1520 saved_value(1), saved_level(1), d, grp, saved_level(2), saved4, saved3);
1521 if (c == vtop_code) {
1522 /*tex
1523
1524 Read just the height and depth of |cur_box|, for \.{\\vtop}. The
1525 height of a `\.{\\vtop}' box is inherited from the first item on
1526 its list, if that item is an |hlist_node|, |vlist_node|, or
1527 |rule_node|; otherwise the \.{\\vtop} height is zero.
1528
1529 */
1530 scaled h = 0;
1531 halfword p = list_ptr(cur_box);
1532 if ((p != null) && (type(p) <= rule_node)) {
1533 /* hlist, vlist, rule */
1534 h = height(p);
1535 }
1536 depth(cur_box) = depth(cur_box) - h + height(cur_box);
1537 height(cur_box) = h;
1538 }
1539 }
1540 if (saved2 != null) {
1541 /*tex Adjust back |text_dir_ptr| for |scan_spec| */
1542 flush_node_list(text_dir_ptr);
1543 text_dir_ptr = saved2;
1544
1545 }
1546 replace_attribute_list(cur_box, saved3);
1547 pop_nest();
1548 box_end(saved0);
1549 }
1550
1551 /*tex
1552
1553 When a box is being appended to the current vertical list, the baselineskip
1554 calculation is handled by the |append_to_vlist| routine.
1555
1556 */
1557
1558 void append_to_vlist(halfword b, int location)
1559 {
1560 /*tex The deficiency of space between baselines: */
1561 scaled d;
1562 /*tex A new glue node. */
1563 halfword p;
1564 boolean mirrored = (type(b) == hlist_node) && is_mirrored(box_dir(b)) ;
1565 halfword result = null;
1566 halfword next_depth = ignore_depth;
1567 boolean prev_set = false ;
1568 if (lua_appendtovlist_callback(b,location,prev_depth_par,mirrored,&result,&next_depth,&prev_set)) {
1569 while (result != null) {
1570 couple_nodes(cur_list.tail_field, result);
1571 cur_list.tail_field = result;
1572 result = vlink(result);
1573 }
1574 if (prev_set) {
1575 prev_depth_par = next_depth;
1576 }
1577 } else {
1578 if (prev_depth_par > ignore_depth) {
1579 if (mirrored) {
1580 d = width(baseline_skip_par) - prev_depth_par - depth(b);
1581 } else {
1582 d = width(baseline_skip_par) - prev_depth_par - height(b);
1583 }
1584 if (d < line_skip_limit_par) {
1585 p = new_param_glue(line_skip_code);
1586 } else {
1587 p = new_skip_param(baseline_skip_code);
1588 width(p) = d;
1589 }
1590 couple_nodes(cur_list.tail_field, p);
1591 cur_list.tail_field = p;
1592 }
1593 couple_nodes(cur_list.tail_field, b);
1594 cur_list.tail_field = b;
1595 if (mirrored) {
1596 prev_depth_par = height(b);
1597 } else {
1598 prev_depth_par = depth(b);
1599 }
1600 }
1601 }
1602
1603 /*tex
1604
1605 When |saving_vdiscards| is positive then the glue, kern, and penalty nodes
1606 removed by the page builder or by \.{\\vsplit} from the top of a vertical
1607 list are saved in special lists instead of being discarded.
1608
1609 */
1610
1611 /*tex last item removed by page builder */
1612
1613 #define tail_page_disc disc_ptr[copy_code]
1614
1615 /*tex first item removed by page builder */
1616
1617 #define page_disc disc_ptr[last_box_code]
1618
1619 /*tex first item removed by \.{\\vsplit} */
1620
1621 #define split_disc disc_ptr[vsplit_code]
1622
1623 /*tex List pointers. */
1624
1625 halfword disc_ptr[(vsplit_code + 1)];
1626
1627 /*tex
1628
1629 The |vsplit| procedure, which implements \TeX's \.{\\vsplit} operation, is
1630 considerably simpler than |line_break| because it doesn't have to worry about
1631 hyphenation, and because its mission is to discover a single break instead of
1632 an optimum sequence of breakpoints. But before we get into the details of
1633 |vsplit|, we need to consider a few more basic things.
1634
1635 A subroutine called |prune_page_top| takes a pointer to a vlist and returns a
1636 pointer to a modified vlist in which all glue, kern, and penalty nodes have
1637 been deleted before the first box or rule node. However, the first box or
1638 rule is actually preceded by a newly created glue node designed so that the
1639 topmost baseline will be at distance |split_top_skip| from the top, whenever
1640 this is possible without backspacing.
1641
1642 When the second argument |s| is |false| the deleted nodes are destroyed,
1643 otherwise they are collected in a list starting at |split_disc|.
1644
1645 */
1646
1647 halfword prune_page_top(halfword p, boolean s)
1648 {
1649 halfword q;
1650 /*tex Lags one step behind |p|. */
1651 halfword prev_p = temp_head;
1652 halfword r = null;
1653 vlink(temp_head) = p;
1654 while (p != null) {
1655 switch (type(p)) {
1656 case hlist_node:
1657 case vlist_node:
1658 case rule_node:
1659 /*tex Insert glue for |split_top_skip| and set |p:=null|. */
1660 q = new_skip_param(split_top_skip_code);
1661 vlink(prev_p) = q;
1662 vlink(q) = p;
1663 if (width(q) > height(p))
1664 width(q) = width(q) - height(p);
1665 else
1666 width(q) = 0;
1667 p = null;
1668 break;
1669 case boundary_node:
1670 case whatsit_node:
1671 case mark_node:
1672 case ins_node:
1673 prev_p = p;
1674 p = vlink(prev_p);
1675 break;
1676 case glue_node:
1677 case kern_node:
1678 case penalty_node:
1679 q = p;
1680 p = vlink(q);
1681 vlink(q) = null;
1682 vlink(prev_p) = p;
1683 if (s) {
1684 if (split_disc == null)
1685 split_disc = q;
1686 else
1687 vlink(r) = q;
1688 r = q;
1689 } else {
1690 flush_node_list(q);
1691 }
1692 break;
1693 default:
1694 confusion("pruning");
1695 break;
1696 }
1697 }
1698 return vlink(temp_head);
1699 }
1700
1701 /*tex
1702
1703 The next subroutine finds the best place to break a given vertical list so as
1704 to obtain a box of height~|h|, with maximum depth~|d|. A pointer to the
1705 beginning of the vertical list is given, and a pointer to the optimum
1706 breakpoint is returned. The list is effectively followed by a forced break,
1707 i.e., a penalty node with the |eject_penalty|; if the best break occurs at
1708 this artificial node, the value |null| is returned.
1709
1710 */
1711
1712 /*tex The distance from first active node to |cur_p|: */
1713
1714 scaled active_height[10];
1715
1716 /*tex
1717
1718 An array of six |scaled| distances is used to keep track of the height from
1719 the beginning of the list to the current place, just as in |line_break|. In
1720 fact, we use one of the same arrays, only changing its name to reflect its
1721 new significance.
1722
1723 */
1724
1725 #define do_all_six(A) A(1);A(2);A(3);A(4);A(5);A(6);A(7)
1726 #define set_height_zero(A) active_height[A]=0 /* initialize the height to zero */
1727
1728 /*tex
1729
1730 A global variable |best_height_plus_depth| will be set to the natural size of
1731 the box that corresponds to the optimum breakpoint found by |vert_break|.
1732 (This value is used by the insertion-splitting algorithm of the page
1733 builder.)
1734
1735 */
1736
1737 /*tex The height of the best box, without stretching or shrinking: */
1738
1739 scaled best_height_plus_depth;
1740
1741 halfword vert_break(halfword p, scaled h, scaled d)
1742 {
1743 /*tex
1744 If |p| is a glue node, |type(prev_p)| determines whether |p| is a legal
1745 breakpoint, an initial glue node is not a legal breakpoint.
1746 */
1747 halfword prev_p = p;
1748 /*tex penalty value */
1749 int pi = 0;
1750 /*tex badness at a trial breakpoint */
1751 int b;
1752 /*tex |type| of the node following a kern */
1753 int t;
1754 /*tex the smallest badness plus penalties found so far */
1755 int least_cost;
1756 /*tex the most recent break that leads to |least_cost| */
1757 halfword best_place = null;
1758 /*tex depth of previous box in the list */
1759 scaled prev_dp = 0;
1760 least_cost = awful_bad;
1761 do_all_six(set_height_zero);
1762 while (1) {
1763 /*tex
1764
1765 If node |p| is a legal breakpoint, check if this break is the best
1766 known, and |goto done| if |p| is null or if the page-so-far is
1767 already too full to accept more stuff.
1768
1769 A subtle point to be noted here is that the maximum depth~|d| might
1770 be negative, so |cur_height| and |prev_dp| might need to be corrected
1771 even after a glue or kern node.
1772 */
1773 if (p == null) {
1774 pi = eject_penalty;
1775 } else {
1776 /*tex
1777
1778 Use node |p| to update the current height and depth measurements;
1779 if this node is not a legal breakpoint, |goto not_found| or
1780 |update_heights|, otherwise set |pi| to the associated penalty at
1781 the break.
1782
1783 */
1784 switch (type(p)) {
1785 case hlist_node:
1786 case vlist_node:
1787 case rule_node:
1788 cur_height = cur_height + prev_dp + height(p);
1789 prev_dp = depth(p);
1790 goto NOT_FOUND;
1791 break;
1792 case boundary_node:
1793 case whatsit_node:
1794 goto NOT_FOUND;
1795 break;
1796 case glue_node:
1797 if (precedes_break(prev_p))
1798 pi = 0;
1799 else
1800 goto UPDATE_HEIGHTS;
1801 break;
1802 case kern_node:
1803 if (vlink(p) == null)
1804 t = penalty_node;
1805 else
1806 t = type(vlink(p));
1807 if (t == glue_node)
1808 pi = 0;
1809 else
1810 goto UPDATE_HEIGHTS;
1811 break;
1812 case penalty_node:
1813 pi = penalty(p);
1814 break;
1815 case mark_node:
1816 case ins_node:
1817 goto NOT_FOUND;
1818 break;
1819 default:
1820 confusion("vertbreak");
1821 break;
1822 }
1823 }
1824 /*tex
1825
1826 Check if node |p| is a new champion breakpoint; then |goto done| if
1827 |p| is a forced break or if the page-so-far is already too full.
1828
1829 */
1830 if (pi < inf_penalty) {
1831 /*tex Compute the badness, |b|, using |awful_bad| if the box is too full. */
1832 if (cur_height < h) {
1833 if ((active_height[3] != 0) || (active_height[4] != 0) ||
1834 (active_height[5] != 0) || (active_height[6] != 0))
1835 b = 0;
1836 else
1837 b = badness(h - cur_height, active_height[2]);
1838 } else if (cur_height - h > active_height[7]) {
1839 b = awful_bad;
1840 } else {
1841 b = badness(cur_height - h, active_height[7]);
1842 }
1843 if (b < awful_bad) {
1844 if (pi <= eject_penalty)
1845 b = pi;
1846 else if (b < inf_bad)
1847 b = b + pi;
1848 else
1849 b = deplorable;
1850 }
1851 if (b <= least_cost) {
1852 best_place = p;
1853 least_cost = b;
1854 best_height_plus_depth = cur_height + prev_dp;
1855 }
1856 if ((b == awful_bad) || (pi <= eject_penalty))
1857 goto DONE;
1858 }
1859 if ((type(p) < glue_node) || (type(p) > kern_node))
1860 goto NOT_FOUND;
1861 UPDATE_HEIGHTS:
1862 /*tex
1863
1864 Update the current height and depth measurements with respect to a
1865 glue or kern node~|p|. Vertical lists that are subject to the
1866 |vert_break| procedure should not contain infinite shrinkability,
1867 since that would permit any amount of information to ``fit'' on one
1868 page.
1869
1870 */
1871 if (type(p) != kern_node) {
1872 active_height[2 + stretch_order(p)] += stretch(p);
1873 active_height[7] += shrink(p);
1874 if ((shrink_order(p) != normal) && (shrink(p) != 0)) {
1875 if (((unsigned int)ignore_primitive_error_par) & 0x1){
1876 print_ignored_err("Infinite glue shrinkage found in box being split");
1877 } else {
1878 print_err("Infinite glue shrinkage found in box being split");
1879
1880 help4(
1881 "The box you are \\vsplitting contains some infinitely",
1882 "shrinkable glue, e.g., `\\vss' or `\\vskip 0pt minus 1fil'.",
1883 "Such glue doesn't belong there; but you can safely proceed,",
1884 "since the offensive shrinkability has been made finite."
1885 );
1886 error();
1887 }
1888 shrink_order(p) = normal;
1889 }
1890 }
1891 cur_height = cur_height + prev_dp + width(p);
1892 prev_dp = 0;
1893 NOT_FOUND:
1894 if (prev_dp > d) {
1895 cur_height = cur_height + prev_dp - d;
1896 prev_dp = d;
1897 }
1898 prev_p = p;
1899 p = vlink(prev_p);
1900 }
1901 DONE:
1902 return best_place;
1903 }
1904
1905 /*tex
1906
1907 Now we are ready to consider |vsplit| itself. Most of its work is
1908 accomplished by the two subroutines that we have just considered.
1909
1910 Given the number of a vlist box |n|, and given a desired page height |h|, the
1911 |vsplit| function finds the best initial segment of the vlist and returns a
1912 box for a page of height~|h|. The remainder of the vlist, if any, replaces
1913 the original box, after removing glue and penalties and adjusting for
1914 |split_top_skip|. Mark nodes in the split-off box are used to set the values
1915 of |split_first_mark| and |split_bot_mark|; we use the fact that
1916 |split_first_mark(x)=null| if and only if |split_bot_mark(x)=null|.
1917
1918 The original box becomes ``void'' if and only if it has been entirely
1919 extracted. The extracted box is ``void'' if and only if the original box was
1920 void (or if it was, erroneously, an hlist box).
1921
1922 */
1923
1924 /*tex Extract a page of height |h| from box |n|: */
1925
1926 halfword vsplit(halfword n, scaled h, int m)
1927 {
1928 /*tex the box to be split */
1929 halfword v;
1930 /*tex the direction of the box to be split */
1931 int vdir;
1932 /*tex runs through the vlist */
1933 halfword p;
1934 /*tex points to where the break occurs */
1935 halfword q;
1936 /*tex for traversing marks lists */
1937 halfword i;
1938 v = box(n);
1939 vdir = box_dir(v);
1940 flush_node_list(split_disc);
1941 split_disc = null;
1942 for (i = 0; i <= biggest_used_mark; i++) {
1943 delete_split_first_mark(i);
1944 delete_split_bot_mark(i);
1945 }
1946 /*tex Dispense with trivial cases of void or bad boxes. */
1947 if (v == null) {
1948 return null;
1949 }
1950 if (type(v) != vlist_node) {
1951 print_err("\\vsplit needs a \\vbox");
1952 help2(
1953 "The box you are trying to split is an \\hbox.",
1954 "i can't split such a box, so I''ll leave it alone."
1955 );
1956 error();
1957 return null;
1958 }
1959 q = vert_break(list_ptr(v), h, split_max_depth_par);
1960 /*tex
1961
1962 Look at all the marks in nodes before the break, and set the final link
1963 to |null| at the break. It's possible that the box begins with a penalty
1964 node that is the ``best'' break, so we must be careful to handle this
1965 special case correctly.
1966
1967 */
1968 p = list_ptr(v);
1969 if (p == q) {
1970 list_ptr(v) = null;
1971 } else {
1972 while (1) {
1973 if (type(p) == mark_node) {
1974 if (split_first_mark(mark_class(p)) == null) {
1975 set_split_first_mark(mark_class(p), mark_ptr(p));
1976 set_split_bot_mark(mark_class(p), split_first_mark(mark_class(p)));
1977 set_token_ref_count(split_first_mark(mark_class(p)),
1978 token_ref_count(split_first_mark(mark_class(p))) + 2);
1979 } else {
1980 delete_token_ref(split_bot_mark(mark_class(p)));
1981 set_split_bot_mark(mark_class(p), mark_ptr(p));
1982 add_token_ref(split_bot_mark(mark_class(p)));
1983 }
1984 }
1985 if (vlink(p) == q) {
1986 vlink(p) = null;
1987 break;
1988 }
1989 p = vlink(p);
1990 }
1991 }
1992 q = prune_page_top(q, saving_vdiscards_par > 0);
1993 p = list_ptr(v);
1994 list_ptr(v) = null;
1995 flush_node(v);
1996 if (q == null) {
1997 /*tex The |eq_level| of the box stays the same. */
1998 box(n) = null;
1999 } else {
2000 box(n) = filtered_vpackage(q, 0, additional, max_depth_par, split_keep_group, vdir, 0, 0);
2001 }
2002 if (m == exactly) {
2003 return filtered_vpackage(p, h, exactly, split_max_depth_par, split_off_group, vdir, 0, 0);
2004 } else {
2005 return filtered_vpackage(p, 0, additional, max_depth_par, split_off_group, vdir, 0, 0);
2006 }
2007 }
2008
2009 /*tex
2010
2011 Now that we can see what eventually happens to boxes, we can consider the
2012 first steps in their creation. The |begin_box| routine is called when
2013 |box_context| is a context specification, |cur_chr| specifies the type of box
2014 desired, and |cur_cmd=make_box|.
2015
2016 */
2017
2018 void begin_box(int box_context)
2019 {
2020 /*tex run through the current list */
2021 halfword q;
2022 /*tex 0 or |vmode| or |hmode| */
2023 halfword k;
2024 /*tex a box number */
2025 int n;
2026 int spec_direction = -1;
2027 int just_pack = 0;
2028 int split_mode = exactly ;
2029 switch (cur_chr) {
2030 case box_code:
2031 scan_register_num();
2032 cur_box = box(cur_val);
2033 /*tex The box becomes void, at the same level. */
2034 box(cur_val) = null;
2035 break;
2036 case copy_code:
2037 scan_register_num();
2038 cur_box = copy_node_list(box(cur_val));
2039 break;
2040 case last_box_code:
2041 /*tex
2042
2043 If the current list ends with a box node, delete it from the list
2044 and make |cur_box| point to it; otherwise set |cur_box:=null|.
2045
2046 */
2047 cur_box = null;
2048 if (abs(cur_list.mode_field) == mmode) {
2049 you_cant();
2050 help1("Sorry; this \\lastbox will be void.");
2051 error();
2052 } else if ((cur_list.mode_field == vmode) && (cur_list.head_field == cur_list.tail_field)) {
2053 you_cant();
2054 help2(
2055 "Sorry...I usually can't take things from the current page.",
2056 "This \\lastbox will therefore be void."
2057 );
2058 error();
2059 } else {
2060 if (cur_list.head_field != cur_list.tail_field) {
2061 if ((type(cur_list.tail_field) == hlist_node) || (type(cur_list.tail_field) == vlist_node)) {
2062 /*tex Remove the last box */
2063 q = alink(cur_list.tail_field);
2064 if (q == null || vlink(q) != cur_list.tail_field) {
2065 q = cur_list.head_field;
2066 while (vlink(q) != cur_list.tail_field)
2067 q = vlink(q);
2068 }
2069 uncouple_node(cur_list.tail_field);
2070 cur_box = cur_list.tail_field;
2071 shift_amount(cur_box) = 0;
2072 cur_list.tail_field = q;
2073 vlink(cur_list.tail_field) = null;
2074 }
2075 }
2076 }
2077 break;
2078 case vsplit_code:
2079 /*tex
2080
2081 Split off part of a vertical box, make |cur_box| point to it.
2082 Here we deal with things like `\.{\\vsplit 13 to 100pt}'.
2083
2084 */
2085 scan_register_num();
2086 n = cur_val;
2087 if (scan_keyword("upto")) {
2088 split_mode = additional ;
2089 } else if (!scan_keyword("to")) {
2090 print_err("Missing `to' inserted");
2091 help2(
2092 "I'm working on `\\vsplit<box number> to <dimen>';",
2093 "will look for the <dimen> next."
2094 );
2095 error();
2096 }
2097 scan_normal_dimen();
2098 cur_box = vsplit(n, cur_val, split_mode);
2099 break;
2100 default:
2101 /*tex
2102
2103 Initiate the construction of an hbox or vbox, then |return|. Here
2104 is where we enter restricted horizontal mode or internal vertical
2105 mode, in order to make a box.
2106
2107 */
2108 switch (cur_chr) {
2109 case tpack_code:
2110 cur_chr = vtop_code;
2111 just_pack = 1;
2112 break;
2113 case vpack_code:
2114 cur_chr = vtop_code + vmode;
2115 just_pack = 1;
2116 break;
2117 case hpack_code:
2118 cur_chr = vtop_code + hmode;
2119 just_pack = 1;
2120 break;
2121 }
2122 k = cur_chr - vtop_code;
2123 set_saved_record(0, saved_boxcontext, 0, box_context);
2124 switch (abs(cur_list.mode_field)) {
2125 case vmode:
2126 spec_direction = body_direction_par;
2127 break;
2128 case hmode:
2129 spec_direction = text_direction_par;
2130 break;
2131 case mmode:
2132 spec_direction = math_direction_par;
2133 break;
2134 }
2135 if (k == hmode) {
2136 if ((box_context < box_flag) && (abs(cur_list.mode_field) == vmode))
2137 scan_full_spec(adjusted_hbox_group, spec_direction,just_pack);
2138 else
2139 scan_full_spec(hbox_group, spec_direction,just_pack);
2140 } else {
2141 if (k == vmode) {
2142 scan_full_spec(vbox_group, spec_direction,just_pack);
2143 } else {
2144 scan_full_spec(vtop_group, spec_direction,just_pack);
2145 k = vmode;
2146 }
2147 normal_paragraph();
2148 }
2149 push_nest();
2150 /* new */
2151 /* eq_word_define(int_base + no_local_whatsits_code, 0); */
2152 eq_word_define(int_base + no_local_dirs_code, 0);
2153 cur_list.mode_field = -k;
2154 if (k == vmode) {
2155 prev_depth_par = ignore_depth;
2156 if (every_vbox_par != null)
2157 begin_token_list(every_vbox_par, every_vbox_text);
2158 } else {
2159 space_factor_par = 1000;
2160 if (every_hbox_par != null)
2161 begin_token_list(every_hbox_par, every_hbox_text);
2162 }
2163 return;
2164 break;
2165 }
2166 /*tex In simple cases, we use the box immediately. */
2167 box_end(box_context);
2168 }