beta-0.89.2
[luatex.git] / source / texk / web2c / luatexdir / tex / postlinebreak.w
blobb78b599aac69a0a663cf8c144119ec92bb180070
1 % postlinebreak.w
3 % Copyright 2006-2010 Taco Hoekwater <taco@@luatex.org>
5 % This file is part of LuaTeX.
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.
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.
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/>.
20 @ @c
23 #include "ptexlib.h"
25 @ So far we have gotten a little way into the |line_break| routine, having
26 covered its important |try_break| subroutine. Now let's consider the
27 rest of the process.
29 The main loop of |line_break| traverses the given hlist,
30 starting at |vlink(temp_head)|, and calls |try_break| at each legal
31 breakpoint. A variable called |auto_breaking| is set to true except
32 within math formulas, since glue nodes are not legal breakpoints when
33 they appear in formulas.
35 The current node of interest in the hlist is pointed to by |cur_p|. Another
36 variable, |prev_p|, is usually one step behind |cur_p|, but the real
37 meaning of |prev_p| is this: If |type(cur_p)=glue_node| then |cur_p| is a legal
38 breakpoint if and only if |auto_breaking| is true and |prev_p| does not
39 point to a glue node, penalty node, explicit kern node, or math node.
41 @ The total number of lines that will be set by |post_line_break|
42 is |best_line-prev_graf-1|. The last breakpoint is specified by
43 |break_node(best_bet)|, and this passive node points to the other breakpoints
44 via the |prev_break| links. The finishing-up phase starts by linking the
45 relevant passive nodes in forward order, changing |prev_break| to
46 |next_break|. (The |next_break| fields actually reside in the same memory
47 space as the |prev_break| fields did, but we give them a new name because
48 of their new significance.) Then the lines are justified, one by one.
50 The |post_line_break| must also keep an dir stack, so that it can
51 output end direction instructions at the ends of lines
52 and begin direction instructions at the beginnings of lines.
55 #define next_break prev_break /*new name for |prev_break| after links are reversed */
57 /* the ints are actually halfwords */
58 void ext_post_line_break(int paragraph_dir,
59 int right_skip,
60 int left_skip,
61 int protrude_chars,
62 halfword par_shape_ptr,
63 int adjust_spacing,
64 halfword inter_line_penalties_ptr,
65 int inter_line_penalty,
66 int club_penalty,
67 halfword club_penalties_ptr,
68 halfword widow_penalties_ptr,
69 int widow_penalty,
70 int broken_penalty,
71 halfword final_par_glue,
72 halfword best_bet,
73 halfword last_special_line,
74 scaled second_width,
75 scaled second_indent,
76 scaled first_width,
77 scaled first_indent, halfword best_line)
80 boolean have_directional = true;
81 halfword q, r; /* temporary registers for list manipulation */
82 halfword k;
83 scaled w;
84 boolean glue_break; /* was a break at glue? */
85 boolean disc_break; /*was the current break at a discretionary node? */
86 boolean post_disc_break; /*and did it have a nonempty post-break part? */
87 scaled cur_width; /*width of line number |cur_line| */
88 scaled cur_indent; /*left margin of line number |cur_line| */
89 int pen; /*use when calculating penalties between lines */
90 halfword cur_p; /* |cur_p|, but localized */
91 halfword cur_line; /*the current line number being justified */
93 dir_ptr = cur_list.dirs_field;
94 /* Reverse the links of the relevant passive nodes, setting |cur_p| to
95 the first breakpoint; */
96 /* The job of reversing links in a list is conveniently regarded as the job
97 of taking items off one stack and putting them on another. In this case we
98 take them off a stack pointed to by |q| and having |prev_break| fields;
99 we put them on a stack pointed to by |cur_p| and having |next_break| fields.
100 Node |r| is the passive node being moved from stack to stack.
102 q = break_node(best_bet);
103 #if 0
104 used_discs = used_disc(best_bet);
105 #endif
106 /* |has_direction| */
107 cur_p = null;
108 do {
109 r = q;
110 q = prev_break(q);
111 next_break(r) = cur_p;
112 cur_p = r;
113 } while (q != null);
114 /* |cur_p| is now the first breakpoint; */
116 cur_line = cur_list.pg_field + 1; /* prevgraf+1 */
118 do {
119 /* Justify the line ending at breakpoint |cur_p|, and append it to the
120 current vertical list, together with associated penalties and other
121 insertions; */
122 /* The current line to be justified appears in a horizontal list starting
123 at |vlink(temp_head)| and ending at |cur_break(cur_p)|. If |cur_break(cur_p)| is
124 a glue node, we reset the glue to equal the |right_skip| glue; otherwise
125 we append the |right_skip| glue at the right. If |cur_break(cur_p)| is a
126 discretionary node, we modify the list so that the discretionary break
127 is compulsory, and we set |disc_break| to |true|. We also append
128 the |left_skip| glue at the left of the line, unless it is zero. */
130 #if 0
131 tprint("BEGIN OF LINE ");
132 print_int(cur_break(cur_p));
133 breadth_max = 100000;
134 depth_threshold = 100000;
135 show_node_list(temp_head);
136 #endif
138 /* DIR: Insert dir nodes at the beginning of the current line; */
139 for (q = dir_ptr; q != null; q = vlink(q)) {
140 halfword tmp = new_dir(dir_dir(q));
141 halfword nxt = vlink(temp_head);
142 delete_attribute_ref(node_attr(tmp));
143 node_attr(tmp) = node_attr(temp_head);
144 add_node_attr_ref(node_attr(tmp));
145 couple_nodes(temp_head, tmp);
146 try_couple_nodes(tmp, nxt); /* \.{\\break}\.{\\par} */
148 if (dir_ptr != null) {
149 flush_node_list(dir_ptr);
150 dir_ptr = null;
153 /* Modify the end of the line to reflect the nature of the break and to
154 include \.{\\rightskip}; also set the proper value of |disc_break|; */
155 /* At the end of the following code, |q| will point to the final node on the
156 list about to be justified. In the meanwhile |r| will point to the
157 node we will use to insert end-of-line stuff after. |q==null| means
158 we use the final position of |r| */
160 /* begin mathskip code */
161 if (temp_head != null) {
162 q = temp_head;
163 while(q != null) {
164 if (type(q) == math_node) {
165 surround(q) = 0 ;
166 if (glue_ptr(q) != zero_glue) {
167 delete_glue_ref(glue_ptr(q));
168 glue_ptr(q) = zero_glue;
170 break;
171 } else if ((type(q) == hlist_node) && (subtype(q) == indent_list)) {
172 /* go on */
173 } else if (is_char_node(q)) {
174 break;
175 } else if (non_discardable(q)) {
176 break;
177 } else if (type(q) == kern_node && subtype(q) != explicit_kern && subtype(q) != italic_kern) {
178 break;
180 q = vlink(q);
183 /* end mathskip code */
185 r = cur_break(cur_p);
186 q = null;
187 disc_break = false;
188 post_disc_break = false;
189 glue_break = false;
192 if (r == null) {
193 for (r = temp_head; vlink(r) != null; r = vlink(r));
194 if (r == final_par_glue) {
195 /* This should almost always be true... */
196 /* TODO assert ? */
197 q = r;
198 /* |q| refers to the last node of the line (and paragraph) */
199 r = alink(r);
201 /* |r| refers to the node after which the dir nodes should be closed */
202 } else if (type(r) == math_node) {
203 surround(r) = 0;
204 /* begin mathskip code */
205 if (glue_ptr(r) != zero_glue) {
206 delete_glue_ref(glue_ptr(r));
207 glue_ptr(r) = zero_glue;
209 /* end mathskip code */
210 } else if (type(r) == glue_node) {
211 delete_glue_ref(glue_ptr(r));
212 glue_ptr(r) = right_skip;
213 subtype(r) = right_skip_code + 1;
214 incr(glue_ref_count(right_skip));
215 glue_break = true;
216 /* |q| refers to the last node of the line */
217 q = r;
218 r = alink(r);
219 assert(vlink(r) == q);
220 /* |r| refers to the node after which the dir nodes should be closed */
221 } else if (type(r) == disc_node) {
222 halfword a = alink(r);
223 halfword v = vlink(r);
224 assert(a != null);
225 assert(v != null);
226 switch (subtype(r)) {
227 case select_disc:
228 if (vlink_pre_break(r) != null) {
229 flush_node_list(vlink_pre_break(r));
230 vlink_pre_break(r) = null;
231 tlink_pre_break(r) = null;
233 if (vlink_no_break(r) != null) {
234 couple_nodes(a, vlink_no_break(r));
235 couple_nodes(tlink_no_break(r), r);
236 vlink_no_break(r) = null;
237 tlink_no_break(r) = null;
240 assert(type(a) == disc_node && subtype(a) == init_disc);
241 flush_node_list(vlink_no_break(a));
242 vlink_no_break(a) = null;
243 tlink_no_break(a) = null;
244 flush_node_list(vlink_pre_break(a));
245 vlink_pre_break(a) = null;
246 tlink_pre_break(a) = null;
247 flush_node_list(vlink_post_break(a));
248 vlink_post_break(a) = null;
249 tlink_post_break(a) = null;
251 break;
252 case init_disc:
253 assert(type(v) == disc_node && subtype(v) == select_disc);
254 subtype(v) = syllable_disc; /* not special any more */
255 flush_node_list(vlink_no_break(v));
256 vlink_no_break(v) = vlink_post_break(r);
257 tlink_no_break(v) = tlink_post_break(r);
258 vlink_post_break(r) = null;
259 tlink_post_break(r) = null;
260 default:
261 if (vlink_no_break(r) != null) {
262 flush_node_list(vlink_no_break(r));
263 vlink_no_break(r) = null;
264 tlink_no_break(r) = null;
266 if (vlink_pre_break(r) != null) {
267 couple_nodes(a, vlink_pre_break(r));
268 couple_nodes(tlink_pre_break(r), r);
269 vlink_pre_break(r) = null;
270 tlink_pre_break(r) = null;
273 if (vlink_post_break(r) != null) {
274 couple_nodes(r, vlink_post_break(r));
275 couple_nodes(tlink_post_break(r), v);
276 vlink_post_break(r) = null;
277 tlink_post_break(r) = null;
278 post_disc_break = true;
280 disc_break = true;
281 } else if (type(r) == kern_node) {
282 width(r) = 0;
285 /* DIR: Adjust the dir stack based on dir nodes in this line; */
286 /* TODO what about the previousparagraph ??? */
287 if (have_directional) {
288 halfword e;
289 halfword p;
290 for (e = vlink(temp_head); e != null && e != cur_break(cur_p); e = vlink(e)) {
291 if (type(e) == dir_node) {
292 if (dir_dir(e) >= 0) {
293 dir_ptr = do_push_dir_node(dir_ptr, e);
294 } else if (dir_ptr != null && dir_dir(dir_ptr) == (dir_dir(e) + dir_swap)) {
295 dir_ptr = do_pop_dir_node(dir_ptr);
299 assert(e == cur_break(cur_p));
301 /* DIR: Insert dir nodes at the end of the current line; */
302 e = vlink(r);
303 for (p = dir_ptr; p != null; p = vlink(p)) {
304 halfword s = new_dir(dir_dir(p) - dir_swap);
305 delete_attribute_ref(node_attr(s));
306 node_attr(s) = node_attr(r);
307 add_node_attr_ref(node_attr(s));
308 couple_nodes(r, s);
309 try_couple_nodes(s, e);
310 r = s;
313 if (passive_right_box(cur_p) != null) {
314 /* TODO: CHECK has |s| below a |alink| ? */
315 halfword s = copy_node_list(passive_right_box(cur_p));
316 halfword e = vlink(r);
317 couple_nodes(r, s);
318 try_couple_nodes(s, e);
319 r = s;
321 if (q == null) {
322 q = r;
324 /* Now [q] refers to the last node on the line */
326 /* FIXME from this point on we no longer keep alink() valid */
328 /* at this point |q| is the rightmost breakpoint; the only exception is
329 the case of a discretionary break with non-empty |pre_break|, then |q|
330 has been changed to the last node of the |pre_break| list */
331 /* If the par ends with a \break command, the last line is utterly empty.
332 That is the case of |q==temp_head| */
333 if (q != temp_head && protrude_chars > 0) {
334 halfword p, ptmp;
335 if (disc_break && (is_char_node(q) || (type(q) != disc_node))) {
336 p = q; /* |q| has been reset to the last node of |pre_break| */
337 ptmp = p;
338 } else {
339 p = alink(q); /* get |vlink(p) = q| */
340 assert(vlink(p) == q);
341 ptmp = p;
343 p = find_protchar_right(vlink(temp_head), p);
344 w = char_pw(p, right_side);
345 if (w != 0) { /* we have found a marginal kern, append it after |ptmp| */
346 k = new_margin_kern(-w, last_rightmost_char, right_side);
347 delete_attribute_ref(node_attr(k));
348 node_attr(k) = node_attr(p);
349 add_node_attr_ref(node_attr(k));
350 try_couple_nodes(k, vlink(ptmp));
351 couple_nodes(ptmp,k);
352 if (ptmp == q)
353 q = vlink(q);
356 /* if |q| was not a breakpoint at glue and has been reset to |rightskip|
357 then we append |rightskip| after |q| now */
358 if (!glue_break) {
359 /* Put the \.{\\rightskip} glue after node |q|; */
360 halfword r1 = new_glue((right_skip == null ? null : copy_node(right_skip)));
361 glue_ref_count(glue_ptr(r1)) = null;
362 subtype(r1) = right_skip_code+1;
363 try_couple_nodes(r1,vlink(q));
364 delete_attribute_ref(node_attr(r1));
365 node_attr(r1) = node_attr(q);
366 add_node_attr_ref(node_attr(r1));
367 couple_nodes(q,r1);
368 q = r1;
371 /* /Modify the end of the line to reflect the nature of the break and to
372 include \.{\\rightskip}; also set the proper value of |disc_break|; */
374 /* Put the \.{\\leftskip} glue at the left and detach this line; */
375 /* The following code begins with |q| at the end of the list to be
376 justified. It ends with |q| at the beginning of that list, and with
377 |vlink(temp_head)| pointing to the remainder of the paragraph, if any. */
378 r = vlink(q);
379 vlink(q) = null;
381 q = vlink(temp_head);
382 try_couple_nodes(temp_head, r);
383 if (passive_left_box(cur_p) != null && passive_left_box(cur_p) != 0) {
384 /* omega bits: */
385 halfword s;
386 r = copy_node_list(passive_left_box(cur_p));
387 s = vlink(q);
388 couple_nodes(r,q);
389 q = r;
390 if ((cur_line == cur_list.pg_field + 1) && (s != null)) {
391 if (type(s) == hlist_node) {
392 if (list_ptr(s) == null) {
393 q = vlink(q);
394 try_couple_nodes(r,vlink(s));
395 try_couple_nodes(s, r);
400 /*at this point |q| is the leftmost node; all discardable nodes have been discarded */
401 if (protrude_chars > 0) {
402 halfword p;
403 p = q;
404 p = find_protchar_left(p, false); /* no more discardables */
405 w = char_pw(p, left_side);
406 if (w != 0) {
407 k = new_margin_kern(-w, last_leftmost_char, left_side);
408 delete_attribute_ref(node_attr(k));
409 node_attr(k) = node_attr(q);
410 add_node_attr_ref(node_attr(k));
411 couple_nodes(k,q);
412 q = k;
415 if (left_skip != zero_glue) {
416 r = new_glue(copy_node(left_skip));
417 glue_ref_count(glue_ptr(r)) = null;
418 subtype(r) = left_skip_code+1;
419 delete_attribute_ref(node_attr(r));
420 node_attr(r) = node_attr(q);
421 add_node_attr_ref(node_attr(r));
422 couple_nodes(r,q);
423 q = r;
425 /* /Put the \.{\\leftskip} glue at the left and detach this line; */
427 /* Call the packaging subroutine, setting |just_box| to the justified box; */
428 /* Now |q| points to the hlist that represents the current line of the
429 paragraph. We need to compute the appropriate line width, pack the
430 line into a box of this size, and shift the box by the appropriate
431 amount of indentation. */
432 if (cur_line > last_special_line) {
433 cur_width = second_width;
434 cur_indent = second_indent;
435 } else if (par_shape_ptr == null) {
436 cur_width = first_width;
437 cur_indent = first_indent;
438 } else {
439 cur_indent = varmem[(par_shape_ptr + 2 * cur_line)].cint;
440 cur_width = varmem[(par_shape_ptr + 2 * cur_line + 1)].cint;
442 adjust_tail = adjust_head;
443 pre_adjust_tail = pre_adjust_head;
444 if (adjust_spacing > 0) {
445 just_box = hpack(q, cur_width, cal_expand_ratio, paragraph_dir);
446 } else {
447 just_box = hpack(q, cur_width, exactly, paragraph_dir);
449 shift_amount(just_box) = cur_indent;
450 subtype(just_box) = line_list;
451 /* /Call the packaging subroutine, setting |just_box| to the justified box; */
453 if ((vlink(contrib_head) != null))
454 if (!output_active)
455 lua_node_filter_s(buildpage_filter_callback, lua_key_index(pre_box));
456 if (pre_adjust_head != pre_adjust_tail) {
457 append_list(pre_adjust_head, pre_adjust_tail);
458 if (!output_active)
459 lua_node_filter_s(buildpage_filter_callback, lua_key_index(pre_adjust));
461 pre_adjust_tail = null;
462 append_to_vlist(just_box,lua_key_index(post_linebreak));
463 if (!output_active)
464 lua_node_filter_s(buildpage_filter_callback, lua_key_index(box));
465 if (adjust_head != adjust_tail) {
466 append_list(adjust_head, adjust_tail);
467 if (!output_active)
468 lua_node_filter_s(buildpage_filter_callback, lua_key_index(adjust));
470 adjust_tail = null;
472 /* /Append the new box to the current vertical list, followed by the list of
473 special nodes taken out of the box by the packager; */
475 /* Append a penalty node, if a nonzero penalty is appropriate */
476 /* Penalties between the lines of a paragraph come from club and widow lines,
477 from the |inter_line_penalty| parameter, and from lines that end at
478 discretionary breaks. Breaking between lines of a two-line paragraph gets
479 both club-line and widow-line penalties. The local variable |pen| will
480 be set to the sum of all relevant penalties for the current line, except
481 that the final line is never penalized. */
482 if (cur_line + 1 != best_line) {
483 q = inter_line_penalties_ptr;
484 if (q != null) {
485 r = cur_line;
486 if (r > penalty(q))
487 r = penalty(q);
488 pen = penalty(q + r);
489 } else {
490 if (passive_pen_inter(cur_p) != 0) {
491 pen = passive_pen_inter(cur_p);
492 } else {
493 pen = inter_line_penalty;
496 q = club_penalties_ptr;
497 if (q != null) {
498 r = cur_line - cur_list.pg_field; /* prevgraf */
499 if (r > penalty(q))
500 r = penalty(q);
501 pen += penalty(q + r);
502 } else if (cur_line == cur_list.pg_field + 1) { /* prevgraf */
503 pen += club_penalty;
505 q = widow_penalties_ptr;
506 if (q != null) {
507 r = best_line - cur_line - 1;
508 if (r > penalty(q))
509 r = penalty(q);
510 pen += penalty(q + r);
511 } else if (cur_line + 2 == best_line) {
512 pen += widow_penalty;
514 if (disc_break) {
515 if (passive_pen_broken(cur_p) != 0) {
516 pen += passive_pen_broken(cur_p);
517 } else {
518 pen += broken_penalty;
521 if (pen != 0) {
522 r = new_penalty(pen);
523 couple_nodes(cur_list.tail_field, r);
524 cur_list.tail_field = r;
527 /* /Append a penalty node, if a nonzero penalty is appropriate */
529 /* /Justify the line ending at breakpoint |cur_p|, and append it to the
530 current vertical list, together with associated penalties and other
531 insertions; */
532 incr(cur_line);
533 cur_p = next_break(cur_p);
534 if (cur_p != null && !post_disc_break) {
535 /* Prune unwanted nodes at the beginning of the next line; */
536 /* Glue and penalty and kern and math nodes are deleted at the
537 beginning of a line, except in the anomalous case that the
538 node to be deleted is actually one of the chosen
539 breakpoints. Otherwise the pruning done here is designed to
540 match the lookahead computation in |try_break|, where the
541 |break_width| values are computed for non-discretionary
542 breakpoints. */
543 r = temp_head;
545 Normally we have a matching math open and math close node but when we cross a line
546 the open node is removed, including any glue or penalties following it. This is however
547 not that nice for callbacks that rely on symmetry. Of course this only counts for one
548 liners, as we can still have only a begin or end node on a line. The end_of_math lua
549 helper is made robust against this although there you should be aware of the fact that
550 one can end up in the middle of math in callbacks that don't work on whole paragraphs,
551 but at least this branch makes sure that some proper analysis is possible. (todo: check
552 if math glyphs have the subtype marked done).
554 Todo: turn math nodes into glues when mathskip otherwise remove them.
556 while (1) {
557 q = vlink(r);
559 if (q == cur_break(cur_p) || is_char_node(q))
560 break;
561 if (!((type(q) == local_par_node))) {
562 if (non_discardable(q) || (type(q) == kern_node && subtype(q) != explicit_kern && subtype(q) != italic_kern))
563 break;
566 if (type(q) == math_node) {
567 /* begin mathskip code */
568 surround(q) = 0 ;
569 if (glue_ptr(q) != zero_glue) {
570 delete_glue_ref(glue_ptr(q));
571 glue_ptr(q) = zero_glue;
572 add_glue_ref(glue_ptr(q));
574 /* end mathskip code */
576 if (q == cur_break(cur_p)) {
577 break;
578 } else if (is_char_node(q)) {
579 break;
580 } else if (type(q) == local_par_node) {
581 /* weird, in the middle somewhere */
582 } else if (non_discardable(q)) {
583 break;
584 } else if (type(q) == kern_node && subtype(q) != explicit_kern && subtype(q) != italic_kern) {
585 break;
587 r = q;
589 if (r != temp_head) {
590 vlink(r) = null;
591 flush_node_list(vlink(temp_head));
592 try_couple_nodes(temp_head, q);
595 } while (cur_p != null);
596 if ((cur_line != best_line) || (vlink(temp_head) != null))
597 confusion("line breaking");
598 cur_list.pg_field = best_line - 1; /* prevgraf */
599 cur_list.dirs_field = dir_ptr; /* |dir_save| */
600 dir_ptr = null;