Debian Codegraph @

texk/web2c/luatexdir/tex/align.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 void fin_align(void);
25 void init_row(void);
26 void init_col(void);
27
28 #define noDEBUG
29
30 /*tex
31
32 It's sort of a miracle whenever \.{\\halign} and \.{\\valign} work, because
33 they cut across so many of the control structures of \TeX. Therefore the
34 present page is probably not the best place for a beginner to start reading
35 this program; it is better to master everything else first.
36
37 Let us focus our thoughts on an example of what the input might be, in order
38 to get some idea about how the alignment miracle happens. The example doesn't
39 do anything useful, but it is sufficiently general to indicate all of the
40 special cases that must be dealt with; please do not be disturbed by its
41 apparent complexity and meaninglessness.
42
43 \starttyping
44 \tabskip 2pt plus 3pt
45 \halign to 300pt{u1#v1&
46 \hskip 50pt \tabskip 1pt plus 1fil u2#v2&
47 \hskip 50pt u3#v3\cr
48 \hskip 25pt a1&\omit a2&\vrule\cr
49 \hskip 25pt \noalign\{\vskip 3pt}
50 \hskip 25pt b1\span b2\cr
51 \hskip 25pt \omit&c2\span\omit\cr}
52 \stoptyping
53
54 Here's what happens:
55
56 \startitemize
57
58 \startitem
59 When `\.{\\halign to 300pt\{}' is scanned, the |scan_spec| routine
60 places the 300pt dimension onto the |save_stack|, and an
61 |align_group| code is placed above it. This will make it possible to
62 complete the alignment when the matching `\.\}' is found.
63 \stopitem
64
65 \startitem
66 The preamble is scanned next. Macros in the preamble are not
67 expanded, except as part of a tabskip specification. For example, if
68 \.{u2} had been a macro in the preamble above, it would have been
69 expanded, since \TeX\ must look for `\.{minus...}' as part of the
70 tabskip glue. A ``preamble list'' is constructed based on the user's
71 preamble; in our case it contains the following seven items:
72
73 \starttabulate
74 \NC \type{\glue 2pt plus 3pt} \NC the tabskip preceding column 1 \NC \NR
75 \NC \type{\alignrecord} of width $-\infty$ \NC preamble info for column 1 \NC \NR
76 \NC \type{\glue 2pt plus 3pt} \NC the tabskip between columns 1 and 2 \NC \NR
77 \NC \type{\alignrecord} of width $-\infty$ \NC preamble info for column 2 \NC \NR
78 \NC \type{\glue 1pt plus 1fil} \NC the tabskip between columns 2 and 3 \NC \NR
79 \NC \type{\alignrecord} of width $-\infty$ \NC preamble info for column 3 \NC \NR
80 \NC \type{\glue 1pt plus 1fil} \NC the tabskip following column 3 \NC \NR
81 \stoptabulate
82
83 These ``alignrecord'' entries have the same size as an |unset_node|,
84 since they will later be converted into such nodes. These alignrecord
85 nodes have no |depth| field; this is split into |u_part| and
86 |v_part|, and they point to token lists for the templates of the
87 alignment. For example, the |u_part| field in the first alignrecord
88 points to the token list `\.{u1}', i.e., the template preceding the
89 `\.\#' for column~1. Furthermore, They have a |span_ptr| instead of a
90 |node_attr| field, and these |span_ptr| fields are initially set to
91 the value |end_span|, for reasons explained below.
92 \stopitem
93
94 \startitem
95 \TeX\ now looks at what follows the \.{\\cr} that ended the preamble.
96 It is not `\.{\\noalign}' or `\.{\\omit}', so this input is put back
97 to be read again, and the template `\.{u1}' is fed to the scanner.
98 Just before reading `\.{u1}', \TeX\ goes into restricted horizontal
99 mode. Just after reading `\.{u1}', \TeX\ will see `\.{a1}', and then
100 (when the {\.\&} is sensed) \TeX\ will see `\.{v1}'. Then \TeX\ scans
101 an |endv| token, indicating the end of a column. At this point an
102 |unset_node| is created, containing the contents of the current hlist
103 (i.e., `\.{u1a1v1}'). The natural width of this unset node replaces
104 the |width| field of the alignrecord for column~1; in general, the
105 alignrecords will record the maximum natural width that has occurred
106 so far in a given column.
107 \stopitem
108
109 \startitem
110 Since `\.{\\omit}' follows the `\.\&', the templates for column~2 are
111 now bypassed. Again \TeX\ goes into restricted horizontal mode and
112 makes an |unset_node| from the resulting hlist; but this time the
113 hlist contains simply `\.{a2}'. The natural width of the new unset
114 box is remembered in the |width| field of the alignrecord for
115 column~2.
116 \stopitem
117
118 \startitem
119 A third |unset_node| is created for column 3, using essentially the
120 mechanism that worked for column~1; this unset box contains
121 `\.{u3\\vrule v3}'. The vertical rule in this case has running
122 dimensions that will later extend to the height and depth of the
123 whole first row, since each |unset_node| in a row will eventually
124 inherit the height and depth of its enclosing box.
125 \stopitem
126
127 \startitem
128 The first row has now ended; it is made into a single unset box
129 comprising the following seven items:
130
131 \starttyping
132 \glue 2pt plus 3pt
133 \unsetbox for 1 column: u1a1v1
134 \glue 2pt plus 3pt
135 \unsetbox for 1 column: a2
136 \glue 1pt plus 1fil
137 \unsetbox for 1 column: u3\vrule v3
138 \glue 1pt plus 1fil
139 \stoptyping
140
141 The width of this unset row is unimportant, but it has the correct
142 height and depth, so the correct baselineskip glue will be computed
143 as the row is inserted into a vertical list.
144 \stopitem
145
146 \startitem
147 Since `\.{\\noalign}' follows the current \.{\\cr}, \TeX\ appends
148 additional material (in this case \.{\\vskip 3pt}) to the vertical
149 list. While processing this material, \TeX\ will be in internal
150 vertical mode, and |no_align_group| will be on |save_stack|.
151 \stopitem
152
153 \startitem
154 The next row produces an unset box that looks like this:
155
156 \starttyping
157 \glue 2pt plus 3pt
158 \unsetbox for 2 columns: u1b1v1u2b2v2
159 \glue 1pt plus 1fil
160 \unsetbox for 1 column: {(empty)}
161 \glue 1pt plus 1fil
162 \stoptyping
163
164 The natural width of the unset box that spans columns 1~and~2 is
165 stored in a ``span node,'' which we will explain later; the
166 |span_ptr| field of the alignrecord for column~1 now points to the
167 new span node, and the |span_ptr| of the span node points to
168 |end_span|.
169 \stopitem
170
171 \startitem
172
173 The final row produces the unset box
174
175 \starttyping
176 \glue 2pt plus 3pt\cr
177 \unsetbox for 1 column: {(empty)}
178 \glue 2pt plus 3pt\cr
179 \unsetbox for 2 columns: u2c2v2
180 \glue 1pt plus 1fil
181 \stoptyping
182
183 A new span node is attached to the alignrecord for column 2.
184 \stopitem
185
186 \startitem
187 The last step is to compute the true column widths and to change all
188 the unset boxes to hboxes, appending the whole works to the vertical
189 list that encloses the \.{\\halign}. The rules for deciding on the
190 final widths of each unset column box will be explained below.
191 \stopitem
192
193 \stopitemize
194
195 Note that as \.{\\halign} is being processed, we fearlessly give up control
196 to the rest of \TeX. At critical junctures, an alignment routine is called
197 upon to step in and do some little action, but most of the time these
198 routines just lurk in the background. It's something like post-hypnotic
199 suggestion.
200
201 We have mentioned that alignrecords contain no |height| or |depth| fields.
202 Their |glue_sign| and |glue_order| are pre-empted as well, since it is
203 necessary to store information about what to do when a template ends. This
204 information is called the |extra_info| field.
205
206 */
207
208 /*tex The pointer to \<u_j> token list: */
209
210 #define u_part(A) vlink((A)+depth_offset)
211
212 /*tex The pointer to \<v_j> token list */
213
214 #define v_part(A) vinfo((A)+depth_offset)
215
216 /*tex A column spanning list */
217
218 #define span_ptr(A) vinfo((A)+1)
219
220 /*tex Info to remember during template */
221
222 #define extra_info(A) vinfo((A)+list_offset)
223
224 /*tex
225
226 Alignments can occur within alignments, so a small stack is used to access
227 the alignrecord information. At each level we have a |preamble| pointer,
228 indicating the beginning of the preamble list; a |cur_align| pointer,
229 indicating the current position in the preamble list; a |cur_span| pointer,
230 indicating the value of |cur_align| at the beginning of a sequence of spanned
231 columns; a |cur_loop| pointer, indicating the tabskip glue before an
232 alignrecord that should be copied next if the current list is extended; and
233 the |align_state| variable, which indicates the nesting of braces so that
234 \.{\\cr} and \.{\\span} and tab marks are properly intercepted. There also
235 are pointers |cur_head| and |cur_tail| to the head and tail of a list of
236 adjustments being moved out from horizontal mode to vertical~mode, and alike
237 |cur_pre_head| and |cur_pre_tail| for pre-adjust lists.
238
239 The current values of these nine quantities appear in global variables; when
240 they have to be pushed down, they are stored in 6-word nodes, and |align_ptr|
241 points to the topmost such node.
242
243 */
244
245 /*tex This could be in |texnodes.h| but it's documented here. */
246
247 /*tex The current preamble list: */
248
249 #define preamble vlink(align_head)
250
251 /*tex The current position in the preamble list: */
252
253 pointer cur_align = null;
254
255 /*tex The start of the currently spanned columns in the preamble list: */
256
257 pointer cur_span = null;
258
259 /*tex A place to copy when extending a periodic preamble: */
260
261 pointer cur_loop = null;
262
263 /*tex The most recently pushed-down alignment stack node: */
264
265 pointer align_ptr = null;
266
267 /*tex Adjustment list pointers: */
268
269 pointer cur_head = null, cur_tail = null;
270
271 /*tex Pre-adjustment list pointers: */
272
273 pointer cur_pre_head = null, cur_pre_tail = null;
274
275 /*tex
276
277 The |align_state| and |preamble| variables are initialized elsewhere.
278
279 Alignment stack maintenance is handled by a pair of trivial routines called
280 |push_alignment| and |pop_alignment|.
281
282 (HH:) It makes not much sense to add support for an \.{attr} keyword to
283 \.{\\halign} and \.{\\valign} because then we need to decide if we tag rows
284 or cells or both or come up with \.{cellattr} and \.{rowattr} and such. But
285 then it even makes sense to have explicit commands (in addition to the
286 seperator) to tags individual cells. Too muss hassle for now and the
287 advantages are not that large.
288
289 */
290
291 static void push_alignment(void)
292 {
293 /*tex The new alignment stack node: */
294 pointer p;
295 p = new_node(align_stack_node, 0);
296 vinfo(p + 1) = align_ptr;
297 vlink(p + 1) = cur_align;
298 vinfo(p + 2) = preamble;
299 vlink(p + 2) = cur_span;
300 vinfo(p + 3) = cur_loop;
301 vlink(p + 3) = align_state;
302 vinfo(p + 4) = cur_head;
303 vlink(p + 4) = cur_tail;
304 vinfo(p + 5) = cur_pre_head;
305 vlink(p + 5) = cur_pre_tail;
306 align_ptr = p;
307 cur_head = new_node(temp_node, 0);
308 cur_pre_head = new_node(temp_node, 0);
309 }
310
311 static void pop_alignment(void)
312 {
313 /*tex The top alignment stack node: */
314 pointer p;
315 flush_node(cur_head);
316 flush_node(cur_pre_head);
317 p = align_ptr;
318 cur_pre_tail = vlink(p + 5);
319 cur_pre_head = vinfo(p + 5);
320 cur_tail = vlink(p + 4);
321 cur_head = vinfo(p + 4);
322 align_state = vlink(p + 3);
323 cur_loop = vinfo(p + 3);
324 cur_span = vlink(p + 2);
325 preamble = vinfo(p + 2);
326 cur_align = vlink(p + 1);
327 align_ptr = vinfo(p + 1);
328 flush_node(p);
329 }
330
331 /*tex
332
333 \TeX\ has eight procedures that govern alignments: |init_align| and
334 |fin_align| are used at the very beginning and the very end; |init_row| and
335 |fin_row| are used at the beginning and end of individual rows; |init_span|
336 is used at the beginning of a sequence of spanned columns (possibly involving
337 only one column); |init_col| and |fin_col| are used at the beginning and end
338 of individual columns; and |align_peek| is used after \.{\\cr} to see whether
339 the next item is \.{\\noalign}.
340
341 We shall consider these routines in the order they are first used during the
342 course of a complete \.{\\halign}, namely |init_align|, |align_peek|,
343 |init_row|, |init_span|, |init_col|, |fin_col|, |fin_row|, |fin_align|.
344
345 The preamble is copied directly, except that \.{\\tabskip} causes a change to
346 the tabskip glue, thereby possibly expanding macros that immediately follow
347 it. An appearance of \.{\\span} also causes such an expansion.
348
349 Note that if the preamble contains `\.{\\global\\tabskip}', the
350 `\.{\\global}' token survives in the preamble and the `\.{\\tabskip}' defines
351 new tabskip glue (locally).
352
353 */
354
355 static void get_preamble_token(void)
356 {
357 RESTART:
358 get_token();
359 while ((cur_chr == span_code) && (cur_cmd == tab_mark_cmd)) {
360 /*tex This token will be expanded once. */
361 get_token();
362 if (cur_cmd > max_command_cmd) {
363 expand();
364 get_token();
365 }
366 }
367 if (cur_cmd == endv_cmd)
368 fatal_error("(interwoven alignment preambles are not allowed)");
369 if ((cur_cmd == assign_glue_cmd)
370 && (cur_chr == glue_base + tab_skip_code)) {
371 scan_optional_equals();
372 scan_glue(glue_val_level);
373 if (global_defs_par > 0)
374 geq_define(glue_base + tab_skip_code, glue_ref_cmd, cur_val);
375 else
376 eq_define(glue_base + tab_skip_code, glue_ref_cmd, cur_val);
377 goto RESTART;
378 }
379 }
380
381 /*tex
382
383 When \.{\\halign} or \.{\\valign} has been scanned in an appropriate mode,
384 \TeX\ calls |init_align|, whose task is to get everything off to a good
385 start. This mostly involves scanning the preamble and putting its information
386 into the preamble list.
387
388 */
389
390 void init_align(void)
391 {
392 /*tex |warning_index| value for error messages */
393 pointer save_cs_ptr;
394 /*tex for short-term temporary use */
395 pointer p, r;
396 /*tex \.{\\halign} or \.{\\valign}, usually */
397 save_cs_ptr = cur_cs;
398 push_alignment();
399 /*tex enter a new alignment level */
400 align_state = -1000000;
401 /*tex
402
403 When \.{\\halign} is used as a displayed formula, there should be no
404 other pieces of mlists present.
405
406 */
407 if ((cur_list.mode_field == mmode) && ((cur_list.tail_field != cur_list.head_field) || (incompleat_noad_par != null))) {
408 const char *hlp[] = {
409 "Displays can use special alignments (like \\eqalignno)",
410 "only if nothing but the alignment itself is between $$'s.",
411 "So I've deleted the formulas that preceded this alignment.",
412 NULL
413 };
414 tex_error("Improper \\halign inside $$'s", hlp);
415 flush_math();
416 }
417 /*tex Enter a new semantic level. */
418 push_nest();
419 /*tex
420
421 In vertical modes, |prev_depth| already has the correct value. But if we
422 are in |mmode| (displayed formula mode), we reach out to the enclosing
423 vertical mode for the |prev_depth| value that produces the correct
424 baseline calculations.
425 */
426 if (cur_list.mode_field == mmode) {
427 cur_list.mode_field = -vmode;
428 prev_depth_par = nest[nest_ptr - 2].prev_depth_field;
429 } else if (cur_list.mode_field > 0) {
430 cur_list.mode_field = -(cur_list.mode_field);
431 }
432 scan_spec(align_group);
433 /*tex Scan the preamble. */
434 preamble = null;
435 cur_align = align_head;
436 cur_loop = null;
437 scanner_status = aligning;
438 warning_index = save_cs_ptr;
439 align_state = -1000000;
440 /*tex At this point, |cur_cmd=left_brace|. */
441 while (true) {
442 /*tex Append the current tabskip glue to the preamble list. */
443 r = new_param_glue(tab_skip_code);
444 vlink(cur_align) = r;
445 cur_align = vlink(cur_align);
446 if (cur_cmd == car_ret_cmd) {
447 /*tex \.{\\cr} ends the preamble. */
448 break;
449 }
450 /*tex
451
452 Scan preamble text until |cur_cmd| is |tab_mark| or |car_ret| and
453 then scan the template \<u_j>, putting the resulting token list in
454 |hold_token_head|. Spaces are eliminated from the beginning of a
455 template.
456
457 */
458 p = hold_token_head;
459 token_link(p) = null;
460 while (1) {
461 get_preamble_token();
462 if (cur_cmd == mac_param_cmd)
463 break;
464 if ((cur_cmd <= car_ret_cmd) && (cur_cmd >= tab_mark_cmd)
465 && (align_state == -1000000)) {
466 if ((p == hold_token_head) && (cur_loop == null) && (cur_cmd == tab_mark_cmd)) {
467 cur_loop = cur_align;
468 } else {
469 const char *hlp[] = {
470 "There should be exactly one # between &'s, when an",
471 "\\halign or \\valign is being set up. In this case you had",
472 "none, so I've put one in; maybe that will work.",
473 NULL
474 };
475 back_input();
476 tex_error("Missing # inserted in alignment preamble", hlp);
477 break;
478 }
479 } else if ((cur_cmd != spacer_cmd) || (p != hold_token_head)) {
480 r = get_avail();
481 token_link(p) = r;
482 p = token_link(p);
483 token_info(p) = cur_tok;
484 }
485 }
486 r = new_node(align_record_node, 0);
487 vlink(cur_align) = r;
488 /*tex A new align record: */
489 cur_align = vlink(cur_align);
490 span_ptr(cur_align) = end_span;
491 width(cur_align) = null_flag;
492 u_part(cur_align) = token_link(hold_token_head);
493 /*tex
494
495 Scan the template \<v_j>, putting the resulting token list in
496 |hold_token_head|.
497
498 */
499 p = hold_token_head;
500 token_link(p) = null;
501 while (1) {
502 CONTINUE:
503 get_preamble_token();
504 if ((cur_cmd <= car_ret_cmd) && (cur_cmd >= tab_mark_cmd) && (align_state == -1000000))
505 break;
506 if (cur_cmd == mac_param_cmd) {
507 const char *hlp[] = {
508 "There should be exactly one # between &'s, when an",
509 "\\halign or \\valign is being set up. In this case you had",
510 "more than one, so I'm ignoring all but the first.",
511 NULL
512 };
513 tex_error("Only one # is allowed per tab", hlp);
514 goto CONTINUE;
515 }
516 r = get_avail();
517 token_link(p) = r;
518 p = token_link(p);
519 token_info(p) = cur_tok;
520 }
521 r = get_avail();
522 token_link(p) = r;
523 p = token_link(p);
524 /*tex Put \.{\\endtemplate} at the end: */
525 token_info(p) = end_template_token;
526 v_part(cur_align) = token_link(hold_token_head);
527 }
528 scanner_status = normal;
529 new_save_level(align_group);
530 if (every_cr_par != null)
531 begin_token_list(every_cr_par, every_cr_text);
532 /*tex Look for \.{\\noalign} or \.{\\omit}. */
533 align_peek();
534 }
535
536 /*tex
537
538 The tricky part about alignments is getting the templates into the scanner at
539 the right time, and recovering control when a row or column is finished.
540
541 We usually begin a row after each \.{\\cr} has been sensed, unless that
542 \.{\\cr} is followed by \.{\\noalign} or by the right brace that terminates
543 the alignment. The |align_peek| routine is used to look ahead and do the
544 right thing; it either gets a new row started, or gets a \.{\\noalign}
545 started, or finishes off the alignment.
546
547 */
548
549 void align_peek(void)
550 {
551 RESTART:
552 align_state = 1000000;
553 do {
554 get_x_or_protected();
555 } while (cur_cmd == spacer_cmd);
556 if (cur_cmd == no_align_cmd) {
557 scan_left_brace();
558 new_save_level(no_align_group);
559 if (cur_list.mode_field == -vmode)
560 normal_paragraph();
561 } else if (cur_cmd == right_brace_cmd) {
562 fin_align();
563 } else if ((cur_cmd == car_ret_cmd) && (cur_chr == cr_cr_code)) {
564 /*tex Ignore \.{\\crcr}. */
565 goto RESTART;
566 } else {
567 /*tex Start a new row. */
568 init_row();
569 /*tex Start a new column and replace what we peeked at. */
570 init_col();
571 }
572 }
573
574
575 /*tex
576
577 The parameter to |init_span| is a pointer to the alignrecord where the next
578 column or group of columns will begin. A new semantic level is entered, so
579 that the columns will generate a list for subsequent packaging.
580
581 */
582
583 static void init_span(pointer p)
584 {
585 push_nest();
586 if (cur_list.mode_field == -hmode) {
587 space_factor_par = 1000;
588 } else {
589 prev_depth_par = ignore_depth;
590 normal_paragraph();
591 }
592 cur_span = p;
593 }
594
595 /*tex
596
597 To start a row (i.e., a `row' that rhymes with `dough' but not with `bough'),
598 we enter a new semantic level, copy the first tabskip glue, and change from
599 internal vertical mode to restricted horizontal mode or vice versa. The
600 |space_factor| and |prev_depth| are not used on this semantic level, but we
601 clear them to zero just to be tidy.
602
603 */
604
605 void init_row(void)
606 {
607 push_nest();
608 cur_list.mode_field = (-hmode - vmode) - cur_list.mode_field;
609 if (cur_list.mode_field == -hmode)
610 space_factor_par = 0;
611 else
612 prev_depth_par = 0;
613 tail_append(new_glue(preamble));
614 subtype(cur_list.tail_field) = tab_skip_code + 1;
615 cur_align = vlink(preamble);
616 cur_tail = cur_head;
617 cur_pre_tail = cur_pre_head;
618 init_span(cur_align);
619 }
620
621 /*tex
622
623 When a column begins, we assume that |cur_cmd| is either |omit| or else the
624 current token should be put back into the input until the \<u_j> template has
625 been scanned. (Note that |cur_cmd| might be |tab_mark| or |car_ret|.) We also
626 assume that |align_state| is approximately 1000000 at this time. We remain in
627 the same mode, and start the template if it is called for.
628
629 */
630
631 void init_col(void)
632 {
633 extra_info(cur_align) = cur_cmd;
634 if (cur_cmd == omit_cmd)
635 align_state = 0;
636 else {
637 back_input();
638 begin_token_list(u_part(cur_align), u_template);
639 }
640 /*tex now |align_state=1000000| */
641 }
642
643
644 /*tex
645
646 The scanner sets |align_state| to zero when the \<u_j> template ends. When a
647 subsequent \.{\\cr} or \.{\\span} or tab mark occurs with |align_state=0|,
648 the scanner activates the following code, which fires up the \<v_j> template.
649 We need to remember the |cur_chr|, which is either |cr_cr_code|, |cr_code|,
650 |span_code|, or a character code, depending on how the column text has ended.
651
652 This part of the program had better not be activated when the preamble to
653 another alignment is being scanned, or when no alignment preamble is active.
654
655 */
656
657 void insert_vj_template(void)
658 {
659 if ((scanner_status == aligning) || (cur_align == null))
660 fatal_error("(interwoven alignment preambles are not allowed)");
661 cur_cmd = extra_info(cur_align);
662 extra_info(cur_align) = cur_chr;
663 if (cur_cmd == omit_cmd)
664 begin_token_list(omit_template, v_template);
665 else
666 begin_token_list(v_part(cur_align), v_template);
667 align_state = 1000000;
668 }
669
670 /*tex Determine the stretch order */
671
672 #define determine_stretch_order() do { \
673 if (total_stretch[filll]!= 0) o = filll; \
674 else if (total_stretch[fill] != 0) o = fill; \
675 else if (total_stretch[fil] != 0) o = fil; \
676 else if (total_stretch[sfi] != 0) o = sfi; \
677 else o=normal; \
678 } while (0)
679
680 /*tex Determine the shrink order */
681
682 #define determine_shrink_order() do { \
683 if (total_shrink[filll] != 0) o = filll; \
684 else if (total_shrink[fill] != 0) o = fill; \
685 else if (total_shrink[fil] != 0) o = fil; \
686 else if (total_shrink[sfi] != 0) o = sfi; \
687 else o=normal; \
688 } while (0)
689
690 /*tex
691
692 When the |endv| command at the end of a \<v_j> template comes through the
693 scanner, things really start to happen; and it is the |fin_col| routine that
694 makes them happen. This routine returns |true| if a row as well as a column
695 has been finished.
696
697 */
698
699 boolean fin_col(void)
700 {
701 /*tex the alignrecord after the current one */
702 pointer p;
703 /*tex temporary pointers for list manipulation */
704 pointer q, r;
705 /*tex a new span node */
706 pointer s;
707 /*tex a new unset box */
708 pointer u;
709 /*tex natural width */
710 scaled w;
711 /*tex order of infinity */
712 unsigned char o;
713 /*tex span counter */
714 halfword n;
715 if (cur_align == null)
716 confusion("endv");
717 q = vlink(cur_align);
718 if (q == null)
719 confusion("endv");
720 if (align_state < 500000)
721 fatal_error("(interwoven alignment preambles are not allowed)");
722 p = vlink(q);
723 /*tex If the preamble list has been traversed, check that the row has ended. */
724 if ((p == null) && (extra_info(cur_align) < cr_code)) {
725 if (cur_loop != null) {
726 /*tex Lengthen the preamble periodically: */
727 r = new_node(align_record_node, 0);
728 vlink(q) = r;
729 /*tex A new align record: */
730 p = vlink(q);
731 span_ptr(p) = end_span;
732 width(p) = null_flag;
733 cur_loop = vlink(cur_loop);
734 /*tex Copy the templates from node |cur_loop| into node |p|. */
735 q = hold_token_head;
736 r = u_part(cur_loop);
737 while (r != null) {
738 s = get_avail();
739 token_link(q) = s;
740 q = token_link(q);
741 token_info(q) = token_info(r);
742 r = token_link(r);
743 }
744 token_link(q) = null;
745 u_part(p) = token_link(hold_token_head);
746 q = hold_token_head;
747 r = v_part(cur_loop);
748 while (r != null) {
749 s = get_avail();
750 token_link(q) = s;
751 q = token_link(q);
752 token_info(q) = token_info(r);
753 r = token_link(r);
754 }
755 token_link(q) = null;
756 v_part(p) = token_link(hold_token_head);
757 cur_loop = vlink(cur_loop);
758 r = new_glue(cur_loop);
759 vlink(p) = r;
760 } else {
761 const char *hlp[] = {
762 "You have given more \\span or & marks than there were",
763 "in the preamble to the \\halign or \\valign now in progress.",
764 "So I'll assume that you meant to type \\cr instead.",
765 NULL
766 };
767 extra_info(cur_align) = cr_code;
768 tex_error("Extra alignment tab has been changed to \\cr", hlp);
769 }
770 }
771 if (extra_info(cur_align) != span_code) {
772 unsave();
773 new_save_level(align_group);
774 /*tex Package an unset box for the current column and record its width. */
775 if (cur_list.mode_field == -hmode) {
776 adjust_tail = cur_tail;
777 pre_adjust_tail = cur_pre_tail;
778 u = filtered_hpack(cur_list.head_field, cur_list.tail_field, 0, additional, align_set_group, -1, 0, 0);
779 w = width(u);
780 cur_tail = adjust_tail;
781 adjust_tail = null;
782 cur_pre_tail = pre_adjust_tail;
783 pre_adjust_tail = null;
784 } else {
785 u = filtered_vpackage(vlink(cur_list.head_field), 0, additional, 0, align_set_group, -1, 0, 0);
786 w = height(u);
787 }
788 /*tex This represents a span count of 1: */
789 n = min_quarterword;
790 if (cur_span != cur_align) {
791 /*tex Update width entry for spanned columns. */
792 q = cur_span;
793 do {
794 incr(n);
795 q = vlink(vlink(q));
796 } while (q != cur_align);
797 if (n > max_quarterword) {
798 /*tex This can happen, but won't. */
799 confusion("too many spans");
800 }
801 q = cur_span;
802 while (span_span(span_ptr(q)) < n) {
803 q = span_ptr(q);
804 }
805 if (span_span(span_ptr(q)) > n) {
806 s = new_span_node(span_ptr(q), n, w);
807 span_ptr(q) = s;
808 } else if (width(span_ptr(q)) < w) {
809 width(span_ptr(q)) = w;
810 }
811 } else if (w > width(cur_align)) {
812 width(cur_align) = w;
813 }
814 type(u) = unset_node;
815 span_count(u) = (quarterword) n;
816 determine_stretch_order();
817 glue_order(u) = o;
818 glue_stretch(u) = total_stretch[o];
819 determine_shrink_order();
820 glue_sign(u) = o;
821 glue_shrink(u) = total_shrink[o];
822 pop_nest();
823 vlink(cur_list.tail_field) = u;
824 cur_list.tail_field = u;
825 /*tex Copy the tabskip glue between columns. */
826 tail_append(new_glue(vlink(cur_align)));
827 subtype(cur_list.tail_field) = tab_skip_code + 1;
828 if (extra_info(cur_align) >= cr_code) {
829 return true;
830 }
831 init_span(p);
832 }
833 align_state = 1000000;
834 do {
835 get_x_or_protected();
836 } while (cur_cmd == spacer_cmd);
837 cur_align = p;
838 init_col();
839 return false;
840 }
841
842 /*tex
843
844 A span node is a 3-word record containing |width|, |span_span|, and
845 |span_ptr| fields. The |span_span| field indicates the number of spanned
846 columns; the |span_ptr| field points to a span node for the same starting
847 column, having a greater extent of spanning, or to |end_span|, which has the
848 largest possible |span_span| field; the |width| field holds the largest
849 natural width corresponding to a particular set of spanned columns.
850
851 A list of the maximum widths so far, for spanned columns starting at a given
852 column, begins with the |span_ptr| field of the alignrecord for that column.
853 The code has to make sure that there is room for |span_ptr| in both the
854 alignrecord and the span nodes, which is why |span_ptr| replaces |node_attr|.
855
856 The |new_span_node| function is defined in |texnodes.c|.
857
858 */
859
860 /*tex This is normally |alink|: */
861
862 #ifndef span_span
863 # define span_span(A) vlink((A)+1)
864 #endif
865
866 /*tex
867
868 At the end of a row, we append an unset box to the current vlist (for
869 \.{\\halign}) or the current hlist (for \.{\\valign}). This unset box
870 contains the unset boxes for the columns, separated by the tabskip glue.
871 Everything will be set later.
872
873 */
874
875 void fin_row(void)
876 {
877 /*tex The new unset box: */
878 pointer p;
879 if (cur_list.mode_field == -hmode) {
880 p = filtered_hpack(cur_list.head_field, cur_list.tail_field, 0,
881 additional, fin_row_group, -1, 0, 0);
882 pop_nest();
883 if (cur_pre_head != cur_pre_tail)
884 append_list(cur_pre_head, cur_pre_tail);
885 append_to_vlist(p,lua_key_index(alignment));
886 if (cur_head != cur_tail)
887 append_list(cur_head, cur_tail);
888 } else {
889 p = filtered_vpackage(vlink(cur_list.head_field),
890 0, additional, max_depth_par, fin_row_group, -1, 0, 0);
891 pop_nest();
892 vlink(cur_list.tail_field) = p;
893 cur_list.tail_field = p;
894 space_factor_par = 1000;
895 }
896 type(p) = unset_node;
897 glue_stretch(p) = 0;
898 if (every_cr_par != null)
899 begin_token_list(every_cr_par, every_cr_text);
900 align_peek();
901 /*tex Note that |glue_shrink(p)=0| since |glue_shrink==shift_amount|. */
902 }
903
904 /*tex
905
906 Finally, we will reach the end of the alignment, and we can breathe a sigh of
907 relief that memory hasn't overflowed. All the unset boxes will now be set so
908 that the columns line up, taking due account of spanned columns.
909
910 */
911
912 void fin_align(void)
913 {
914 /*tex registers for the list operations */
915 pointer p, q, r, s, u, rr;
916 /*tex width of column */
917 scaled t, w;
918 /*tex shift offset for unset boxes */
919 scaled o;
920 /*tex matching span amount */
921 halfword n;
922 /*tex temporary storage for |overfull_rule| */
923 scaled rule_save;
924 /*tex temporary storage for |prev_depth| */
925 halfword pd;
926 /*tex temporary storage for |new_glue| */
927 halfword ng;
928 /*tex The |align_group| was for individual entries: */
929 if (cur_group != align_group)
930 confusion("align1");
931 unsave();
932 /*tex The |align_group| was for the whole alignment: */
933 if (cur_group != align_group)
934 confusion("align0");
935 unsave();
936 if (nest[nest_ptr - 1].mode_field == mmode) {
937 o = display_indent_par;
938 } else {
939 o = 0;
940 }
941 /*tex
942
943 Go through the preamble list, determining the column widths and
944 changing the alignrecords to dummy unset boxes.
945
946 It's time now to dismantle the preamble list and to compute the
947 column widths. Let $w_{ij}$ be the maximum of the natural widths of
948 all entries that span columns $i$ through $j$, inclusive. The
949 alignrecord for column~$i$ contains $w_{ii}$ in its |width| field,
950 and there is also a linked list of the nonzero $w_{ij}$ for
951 increasing $j$, accessible via the |info| field; these span nodes
952 contain the value $j-i+|min_quarterword|$ in their |link| fields. The
953 values of $w_{ii}$ were initialized to |null_flag|, which we regard
954 as $-\infty$.
955
956 The final column widths are defined by the formula $$w_j=\max_{1\L
957 i\L j}\biggl( w_{ij}-\sum_{i\L k<j}(t_k+w_k)\biggr),$$ where $t_k$ is
958 the natural width of the tabskip glue between columns $k$ and~$k+1$.
959 However, if $w_{ij}=-\infty$ for all |i| in the range |1<=i<=j|
960 (i.e., if every entry that involved column~|j| also involved
961 column~|j+1|), we let $w_j=0$, and we zero out the tabskip glue after
962 column~|j|.
963
964 \TeX\ computes these values by using the following scheme: First
965 $w_1=w_{11}$. Then replace $w_{2j}$ by $\max(w_{2j},w_{1j}-t_1-w_1)$,
966 for all $j>1$. Then $w_2=w_{22}$. Then replace $w_{3j}$ by
967 $\max(w_{3j},w_{2j}-t_2-w_2)$ for all $j>2$; and so on. If any $w_j$
968 turns out to be $-\infty$, its value is changed to zero and so is the
969 next tabskip.
970
971 */
972 q = vlink(preamble);
973 do {
974 flush_list(u_part(q));
975 flush_list(v_part(q));
976 p = vlink(vlink(q));
977 if (width(q) == null_flag) {
978 /*tex Nullify |width(q)| and the tabskip glue following this column. */
979 width(q) = 0;
980 r = vlink(q);
981 reset_glue_to_zero(r);
982 }
983 if (span_ptr(q) != end_span) {
984 /*tex
985
986 Merge the widths in the span nodes of |q| with those of |p|,
987 destroying the span nodes of |q|.
988
989 Merging of two span-node lists is a typical exercise in the
990 manipulation of linearly linked data structures. The essential
991 invariant in the following |repeat| loop is that we want to
992 dispense with node |r|, in |q|'s list, and |u| is its successor;
993 all nodes of |p|'s list up to and including |s| have been
994 processed, and the successor of |s| matches |r| or precedes |r|
995 or follows |r|, according as |link(r)=n| or |link(r)>n| or
996 |link(r)<n|.
997
998 */
999 t = width(q) + width(vlink(q));
1000 r = span_ptr(q);
1001 s = end_span;
1002 span_ptr(s) = p;
1003 n = min_quarterword + 1;
1004 do {
1005 width(r) = width(r) - t;
1006 u = span_ptr(r);
1007 while (span_span(r) > n) {
1008 s = span_ptr(s);
1009 n = span_span(span_ptr(s)) + 1;
1010 }
1011 if (span_span(r) < n) {
1012 span_ptr(r) = span_ptr(s);
1013 span_ptr(s) = r;
1014 decr(span_span(r));
1015 s = r;
1016 } else {
1017 if (width(r) > width(span_ptr(s)))
1018 width(span_ptr(s)) = width(r);
1019 flush_node(r);
1020 }
1021 r = u;
1022 } while (r != end_span);
1023 }
1024 type(q) = unset_node;
1025 span_count(q) = min_quarterword;
1026 height(q) = 0;
1027 depth(q) = 0;
1028 glue_order(q) = normal;
1029 glue_sign(q) = normal;
1030 glue_stretch(q) = 0;
1031 glue_shrink(q) = 0;
1032 q = p;
1033 } while (q != null);
1034 /*tex
1035
1036 Package the preamble list, to determine the actual tabskip glue amounts,
1037 and let |p| point to this prototype box.
1038
1039 Now the preamble list has been converted to a list of alternating unset
1040 boxes and tabskip glue, where the box widths are equal to the final
1041 column sizes. In case of \.{\\valign}, we change the widths to heights,
1042 so that a correct error message will be produced if the alignment is
1043 overfull or underfull.
1044
1045 */
1046 decr(save_ptr);
1047 pack_begin_line = -cur_list.ml_field;
1048 if (cur_list.mode_field == -vmode) {
1049 rule_save = overfull_rule_par;
1050 /*tex Prevent the rule from being packaged. */
1051 overfull_rule_par = 0;
1052 p = hpack(preamble, saved_value(0), saved_level(0), -1);
1053 overfull_rule_par = rule_save;
1054 } else {
1055 q = vlink(preamble);
1056 do {
1057 height(q) = width(q);
1058 width(q) = 0;
1059 q = vlink(vlink(q));
1060 } while (q != null);
1061 p = filtered_vpackage(preamble,
1062 saved_value(0), saved_level(0), max_depth_par, preamble_group, -1, 0, 0);
1063 q = vlink(preamble);
1064 do {
1065 width(q) = height(q);
1066 height(q) = 0;
1067 q = vlink(vlink(q));
1068 } while (q != null);
1069 }
1070 pack_begin_line = 0;
1071 /*tex Set the glue in all the unset boxes of the current list. */
1072 q = vlink(cur_list.head_field);
1073 s = cur_list.head_field;
1074 while (q != null) {
1075 if (!is_char_node(q)) {
1076 if (type(q) == unset_node) {
1077 /*tex
1078
1079 We set the unset box |q| and the unset boxes in it. The unset
1080 box |q| represents a row that contains one or more unset
1081 boxes, depending on how soon \.{\\cr} occurred in that row.
1082
1083 */
1084 if (cur_list.mode_field == -vmode) {
1085 type(q) = hlist_node;
1086 subtype(q) = align_row_list;
1087 width(q) = width(p);
1088 } else {
1089 type(q) = vlist_node;
1090 subtype(q) = align_row_list;
1091 height(q) = height(p);
1092 }
1093 glue_order(q) = glue_order(p);
1094 glue_sign(q) = glue_sign(p);
1095 glue_set(q) = glue_set(p);
1096 shift_amount(q) = o;
1097 r = vlink(list_ptr(q));
1098 if (type(r) != unset_node) {
1099 formatted_error("alignment","bad box");
1100 }
1101 s = vlink(list_ptr(p));
1102 do {
1103 /*tex
1104
1105 We set the glue in node |r| and change it from an unset
1106 node. A box made from spanned columns will be followed by
1107 tabskip glue nodes and by empty boxes as if there were no
1108 spanning. This permits perfect alignment of subsequent
1109 entries, and it prevents values that depend on floating
1110 point arithmetic from entering into the dimensions of any
1111 boxes.
1112
1113 */
1114 n = span_count(r);
1115 t = width(s);
1116 w = t;
1117 u = hold_head;
1118 while (n > min_quarterword) {
1119 decr(n);
1120 /*tex
1121
1122 Append tabskip glue and an empty box to list |u|, and
1123 update |s| and |t| as the prototype nodes are passed.
1124
1125 */
1126 s = vlink(s);
1127 ng = new_glue(s);
1128 vlink(u) = ng;
1129 u = vlink(u);
1130 subtype(u) = tab_skip_code + 1;
1131 t = t + width(s);
1132 if (glue_sign(p) == stretching) {
1133 if (stretch_order(s) == glue_order(p))
1134 t = t + round(float_cast(glue_set(p)) * float_cast(stretch(s)));
1135 } else if (glue_sign(p) == shrinking) {
1136 if (shrink_order(s) == glue_order(p))
1137 t = t - round(float_cast(glue_set(p)) * float_cast(shrink(s)));
1138 }
1139 s = vlink(s);
1140 rr = new_null_box();
1141 vlink(u) = rr;
1142 u = vlink(u);
1143 t = t + width(s);
1144 subtype(u) = align_cell_list;
1145 if (cur_list.mode_field == -vmode) {
1146 width(u) = width(s);
1147 } else {
1148 type(u) = vlist_node;
1149 height(u) = width(s);
1150 }
1151 }
1152 if (cur_list.mode_field == -vmode) {
1153 /*tex
1154
1155 Make the unset node |r| into an |hlist_node| of width
1156 |w|, setting the glue as if the width were |t|.
1157
1158 */
1159 height(r) = height(q);
1160 depth(r) = depth(q);
1161 if (t == width(r)) {
1162 glue_sign(r) = normal;
1163 glue_order(r) = normal;
1164 set_glue_ratio_zero(glue_set(r));
1165 } else if (t > width(r)) {
1166 glue_sign(r) = stretching;
1167 if (glue_stretch(r) == 0)
1168 set_glue_ratio_zero(glue_set(r));
1169 else
1170 glue_set(r) = unfloat((double) (t - width(r)) / glue_stretch(r));
1171 } else {
1172 glue_order(r) = glue_sign(r);
1173 glue_sign(r) = shrinking;
1174 if (glue_shrink(r) == 0)
1175 set_glue_ratio_zero(glue_set(r));
1176 else if ((glue_order(r) == normal) && (width(r) - t > glue_shrink(r)))
1177 set_glue_ratio_one(glue_set(r));
1178 else
1179 glue_set(r) = unfloat((double) (width(r) - t) / glue_shrink(r));
1180 }
1181 width(r) = w;
1182 type(r) = hlist_node;
1183 subtype(r) = align_cell_list;
1184
1185 } else {
1186 /*tex
1187
1188 Make the unset node |r| into a |vlist_node| of height
1189 |w|, setting the glue as if the height were |t|.
1190
1191 */
1192 width(r) = width(q);
1193 if (t == height(r)) {
1194 glue_sign(r) = normal;
1195 glue_order(r) = normal;
1196 set_glue_ratio_zero(glue_set(r));
1197 } else if (t > height(r)) {
1198 glue_sign(r) = stretching;
1199 if (glue_stretch(r) == 0)
1200 set_glue_ratio_zero(glue_set(r));
1201 else
1202 glue_set(r) = unfloat((t - height(r)) / glue_stretch(r));
1203 } else {
1204 glue_order(r) = glue_sign(r);
1205 glue_sign(r) = shrinking;
1206 if (glue_shrink(r) == 0)
1207 set_glue_ratio_zero(glue_set(r));
1208 else if ((glue_order(r) == normal) && (height(r) - t > glue_shrink(r)))
1209 set_glue_ratio_one(glue_set(r));
1210 else
1211 glue_set(r) = unfloat((height(r) - t) / glue_shrink(r));
1212 }
1213 height(r) = w;
1214 type(r) = vlist_node;
1215 subtype(r) = align_cell_list;
1216 }
1217 shift_amount(r) = 0;
1218 if (u != hold_head) {
1219 /*tex Append blank boxes to account for spanned nodes. */
1220 vlink(u) = vlink(r);
1221 vlink(r) = vlink(hold_head);
1222 r = u;
1223 }
1224
1225 r = vlink(vlink(r));
1226 s = vlink(vlink(s));
1227 } while (r != null);
1228
1229 } else if (type(q) == rule_node) {
1230 /*tex
1231
1232 Make the running dimensions in rule |q| extend to the
1233 boundaries of the alignment.
1234
1235 */
1236 if (is_running(width(q)))
1237 width(q) = width(p);
1238 if (is_running(height(q)))
1239 height(q) = height(p);
1240 if (is_running(depth(q)))
1241 depth(q) = depth(p);
1242 if (o != 0) {
1243 r = vlink(q);
1244 vlink(q) = null;
1245 q = hpack(q, 0, additional, -1);
1246 shift_amount(q) = o;
1247 subtype(q) = align_cell_list;
1248 vlink(q) = r;
1249 vlink(s) = q;
1250 }
1251 }
1252 }
1253 s = q;
1254 q = vlink(q);
1255 }
1256 flush_node_list(p);
1257 pop_alignment();
1258 /*tex
1259
1260 We now have a completed alignment, in the list that starts at
1261 |cur_list.head_field| and ends at |cur_list.tail_field|. This list will
1262 be merged with the one that encloses it. (In case the enclosing mode is
1263 |mmode|, for displayed formulas, we will need to insert glue before and
1264 after the display; that part of the program will be deferred until we're
1265 more familiar with such operations.)
1266
1267 */
1268 pd = prev_depth_par;
1269 p = vlink(cur_list.head_field);
1270 q = cur_list.tail_field;
1271 pop_nest();
1272 if (cur_list.mode_field == mmode) {
1273 finish_display_alignment(p, q, pd);
1274 } else {
1275 prev_depth_par = pd;
1276 vlink(cur_list.tail_field) = p;
1277 if (p != null)
1278 cur_list.tail_field = q;
1279 if (cur_list.mode_field == vmode) {
1280 if (!output_active)
1281 lua_node_filter_s(buildpage_filter_callback,lua_key_index(alignment));
1282 build_page();
1283 }
1284 }
1285 }
1286
1287 /*tex
1288
1289 The token list |omit_template| just referred to is a constant token list that
1290 contains the special control sequence \.{\\endtemplate} only.
1291
1292 */
1293
1294 void initialize_alignments(void)
1295 {
1296 token_info(omit_template) = end_template_token;
1297 span_span(end_span) = max_quarterword + 1;
1298 span_ptr(end_span) = null;
1299 }