Debian Codegraph @

texk/web2c/luatexdir/tex/postlinebreak.c

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