fixed a bug in openMemStream (LS); deleted unused close_lua_node (HH)
[luatex.git] / source / texk / web2c / luatexdir / tex / postlinebreak.w
blob423164ef3d370d0b299c424e830bb64e6b963207
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| */
159 r = cur_break(cur_p);
160 q = null;
161 disc_break = false;
162 post_disc_break = false;
163 glue_break = false;
165 if (r == null) {
166 for (r = temp_head; vlink(r) != null; r = vlink(r));
167 if (r == final_par_glue) {
168 /* This should almost always be true... */
169 /* TODO assert ? */
170 q = r;
171 /* |q| refers to the last node of the line (and paragraph) */
172 r = alink(r);
174 /* |r| refers to the node after which the dir nodes should be closed */
175 } else if (type(r) == glue_node) {
176 delete_glue_ref(glue_ptr(r));
177 glue_ptr(r) = right_skip;
178 subtype(r) = right_skip_code + 1;
179 incr(glue_ref_count(right_skip));
180 glue_break = true;
181 /* |q| refers to the last node of the line */
182 q = r;
183 r = alink(r);
184 assert(vlink(r) == q);
185 /* |r| refers to the node after which the dir nodes should be closed */
186 } else if (type(r) == disc_node) {
187 halfword a = alink(r);
188 halfword v = vlink(r);
189 assert(a != null);
190 assert(v != null);
191 switch (subtype(r)) {
192 case select_disc:
193 if (vlink_pre_break(r) != null) {
194 flush_node_list(vlink_pre_break(r));
195 vlink_pre_break(r) = null;
196 tlink_pre_break(r) = null;
198 if (vlink_no_break(r) != null) {
199 couple_nodes(a, vlink_no_break(r));
200 couple_nodes(tlink_no_break(r), r);
201 vlink_no_break(r) = null;
202 tlink_no_break(r) = null;
205 assert(type(a) == disc_node && subtype(a) == init_disc);
206 flush_node_list(vlink_no_break(a));
207 vlink_no_break(a) = null;
208 tlink_no_break(a) = null;
209 flush_node_list(vlink_pre_break(a));
210 vlink_pre_break(a) = null;
211 tlink_pre_break(a) = null;
212 flush_node_list(vlink_post_break(a));
213 vlink_post_break(a) = null;
214 tlink_post_break(a) = null;
216 break;
217 case init_disc:
218 assert(type(v) == disc_node && subtype(v) == select_disc);
219 subtype(v) = syllable_disc; /* not special any more */
220 flush_node_list(vlink_no_break(v));
221 vlink_no_break(v) = vlink_post_break(r);
222 tlink_no_break(v) = tlink_post_break(r);
223 vlink_post_break(r) = null;
224 tlink_post_break(r) = null;
225 default:
226 if (vlink_no_break(r) != null) {
227 flush_node_list(vlink_no_break(r));
228 vlink_no_break(r) = null;
229 tlink_no_break(r) = null;
231 if (vlink_pre_break(r) != null) {
232 couple_nodes(a, vlink_pre_break(r));
233 couple_nodes(tlink_pre_break(r), r);
234 vlink_pre_break(r) = null;
235 tlink_pre_break(r) = null;
238 if (vlink_post_break(r) != null) {
239 couple_nodes(r, vlink_post_break(r));
240 couple_nodes(tlink_post_break(r), v);
241 vlink_post_break(r) = null;
242 tlink_post_break(r) = null;
243 post_disc_break = true;
245 disc_break = true;
246 } else if (type(r) == kern_node) {
247 width(r) = 0;
248 } else if (type(r) == math_node) {
249 surround(r) = 0;
252 /* DIR: Adjust the dir stack based on dir nodes in this line; */
253 /* TODO what about the previousparagraph ??? */
254 if (have_directional) {
255 halfword e;
256 halfword p;
257 for (e = vlink(temp_head); e != null && e != cur_break(cur_p);
258 e = vlink(e)) {
259 if (type(e) != whatsit_node || subtype(e) != dir_node)
260 continue;
261 if (dir_dir(e) >= 0) {
262 dir_ptr = do_push_dir_node(dir_ptr, e);
263 } else if (dir_ptr != null
264 && dir_dir(dir_ptr) == (dir_dir(e) + 64)) {
265 dir_ptr = do_pop_dir_node(dir_ptr);
268 assert(e == cur_break(cur_p));
270 /* DIR: Insert dir nodes at the end of the current line; */
271 e = vlink(r);
272 for (p = dir_ptr; p != null; p = vlink(p)) {
273 halfword s = new_dir(dir_dir(p) - 64);
274 delete_attribute_ref(node_attr(s));
275 node_attr(s) = node_attr(r);
276 add_node_attr_ref(node_attr(s));
277 couple_nodes(r, s);
278 try_couple_nodes(s, e);
279 r = s;
282 if (passive_right_box(cur_p) != null) {
283 /* TODO: CHECK has |s| below a |alink| ? */
284 halfword s = copy_node_list(passive_right_box(cur_p));
285 halfword e = vlink(r);
286 couple_nodes(r, s);
287 try_couple_nodes(s, e);
288 r = s;
290 if (q == null) {
291 q = r;
293 /* Now [q] refers to the last node on the line */
295 /* FIXME from this point on we no longer keep alink() valid */
297 /* at this point |q| is the rightmost breakpoint; the only exception is
298 the case of a discretionary break with non-empty |pre_break|, then |q|
299 has been changed to the last node of the |pre_break| list */
300 /* If the par ends with a \break command, the last line is utterly empty.
301 That is the case of |q==temp_head| */
302 if (q != temp_head && protrude_chars > 0) {
303 halfword p, ptmp;
304 if (disc_break && (is_char_node(q) || (type(q) != disc_node))) {
305 p = q; /* |q| has been reset to the last node of |pre_break| */
306 ptmp = p;
307 } else {
308 p = alink(q); /* get |vlink(p) = q| */
309 assert(vlink(p) == q);
310 ptmp = p;
312 p = find_protchar_right(vlink(temp_head), p);
313 w = char_pw(p, right_side);
314 if (w != 0) { /* we have found a marginal kern, append it after |ptmp| */
315 k = new_margin_kern(-w, last_rightmost_char, right_side);
316 delete_attribute_ref(node_attr(k));
317 node_attr(k) = node_attr(p);
318 add_node_attr_ref(node_attr(k));
319 try_couple_nodes(k, vlink(ptmp));
320 couple_nodes(ptmp,k);
321 if (ptmp == q)
322 q = vlink(q);
325 /* if |q| was not a breakpoint at glue and has been reset to |rightskip|
326 then we append |rightskip| after |q| now */
327 if (!glue_break) {
328 /* Put the \.{\\rightskip} glue after node |q|; */
329 halfword r1 = new_glue((right_skip == null ? null : copy_node(right_skip)));
330 glue_ref_count(glue_ptr(r1)) = null;
331 subtype(r1) = right_skip_code+1;
332 try_couple_nodes(r1,vlink(q));
333 delete_attribute_ref(node_attr(r1));
334 node_attr(r1) = node_attr(q);
335 add_node_attr_ref(node_attr(r1));
336 couple_nodes(q,r1);
337 q = r1;
340 /* /Modify the end of the line to reflect the nature of the break and to
341 include \.{\\rightskip}; also set the proper value of |disc_break|; */
343 /* Put the \.{\\leftskip} glue at the left and detach this line; */
344 /* The following code begins with |q| at the end of the list to be
345 justified. It ends with |q| at the beginning of that list, and with
346 |vlink(temp_head)| pointing to the remainder of the paragraph, if any. */
347 r = vlink(q);
348 vlink(q) = null;
350 q = vlink(temp_head);
351 try_couple_nodes(temp_head, r);
352 if (passive_left_box(cur_p) != null && passive_left_box(cur_p) != 0) {
353 /* omega bits: */
354 halfword s;
355 r = copy_node_list(passive_left_box(cur_p));
356 s = vlink(q);
357 couple_nodes(r,q);
358 q = r;
359 if ((cur_line == cur_list.pg_field + 1) && (s != null)) {
360 if (type(s) == hlist_node) {
361 if (list_ptr(s) == null) {
362 q = vlink(q);
363 try_couple_nodes(r,vlink(s));
364 try_couple_nodes(s, r);
369 /*at this point |q| is the leftmost node; all discardable nodes have been discarded */
370 if (protrude_chars > 0) {
371 halfword p;
372 p = q;
373 p = find_protchar_left(p, false); /* no more discardables */
374 w = char_pw(p, left_side);
375 if (w != 0) {
376 k = new_margin_kern(-w, last_leftmost_char, left_side);
377 delete_attribute_ref(node_attr(k));
378 node_attr(k) = node_attr(q);
379 add_node_attr_ref(node_attr(k));
380 couple_nodes(k,q);
381 q = k;
384 if (left_skip != zero_glue) {
385 r = new_glue(copy_node(left_skip));
386 glue_ref_count(glue_ptr(r)) = null;
387 subtype(r) = left_skip_code+1;
388 delete_attribute_ref(node_attr(r));
389 node_attr(r) = node_attr(q);
390 add_node_attr_ref(node_attr(r));
391 couple_nodes(r,q);
392 q = r;
394 /* /Put the \.{\\leftskip} glue at the left and detach this line; */
396 /* Call the packaging subroutine, setting |just_box| to the justified box; */
397 /* Now |q| points to the hlist that represents the current line of the
398 paragraph. We need to compute the appropriate line width, pack the
399 line into a box of this size, and shift the box by the appropriate
400 amount of indentation. */
401 if (cur_line > last_special_line) {
402 cur_width = second_width;
403 cur_indent = second_indent;
404 } else if (par_shape_ptr == null) {
405 cur_width = first_width;
406 cur_indent = first_indent;
407 } else {
408 cur_indent = varmem[(par_shape_ptr + 2 * cur_line)].cint;
409 cur_width = varmem[(par_shape_ptr + 2 * cur_line + 1)].cint;
411 adjust_tail = adjust_head;
412 pre_adjust_tail = pre_adjust_head;
413 if (adjust_spacing > 0) {
414 just_box = hpack(q, cur_width, cal_expand_ratio, paragraph_dir);
415 } else {
416 just_box = hpack(q, cur_width, exactly, paragraph_dir);
418 shift_amount(just_box) = cur_indent;
419 subtype(just_box) = HLIST_SUBTYPE_LINE;
420 /* /Call the packaging subroutine, setting |just_box| to the justified box; */
422 if ((vlink(contrib_head) != null))
423 if (!output_active)
424 lua_node_filter_s(buildpage_filter_callback, lua_key_index(pre_box));
425 if (pre_adjust_head != pre_adjust_tail) {
426 append_list(pre_adjust_head, pre_adjust_tail);
427 if (!output_active)
428 lua_node_filter_s(buildpage_filter_callback, lua_key_index(pre_adjust));
430 pre_adjust_tail = null;
431 append_to_vlist(just_box);
432 if (!output_active)
433 lua_node_filter_s(buildpage_filter_callback, lua_key_index(box));
434 if (adjust_head != adjust_tail) {
435 append_list(adjust_head, adjust_tail);
436 if (!output_active)
437 lua_node_filter_s(buildpage_filter_callback, lua_key_index(adjust));
439 adjust_tail = null;
441 /* /Append the new box to the current vertical list, followed by the list of
442 special nodes taken out of the box by the packager; */
444 /* Append a penalty node, if a nonzero penalty is appropriate */
445 /* Penalties between the lines of a paragraph come from club and widow lines,
446 from the |inter_line_penalty| parameter, and from lines that end at
447 discretionary breaks. Breaking between lines of a two-line paragraph gets
448 both club-line and widow-line penalties. The local variable |pen| will
449 be set to the sum of all relevant penalties for the current line, except
450 that the final line is never penalized. */
451 if (cur_line + 1 != best_line) {
452 q = inter_line_penalties_ptr;
453 if (q != null) {
454 r = cur_line;
455 if (r > penalty(q))
456 r = penalty(q);
457 pen = penalty(q + r);
458 } else {
459 if (passive_pen_inter(cur_p) != 0) {
460 pen = passive_pen_inter(cur_p);
461 } else {
462 pen = inter_line_penalty;
465 q = club_penalties_ptr;
466 if (q != null) {
467 r = cur_line - cur_list.pg_field; /* prevgraf */
468 if (r > penalty(q))
469 r = penalty(q);
470 pen += penalty(q + r);
471 } else if (cur_line == cur_list.pg_field + 1) { /* prevgraf */
472 pen += club_penalty;
474 q = widow_penalties_ptr;
475 if (q != null) {
476 r = best_line - cur_line - 1;
477 if (r > penalty(q))
478 r = penalty(q);
479 pen += penalty(q + r);
480 } else if (cur_line + 2 == best_line) {
481 pen += widow_penalty;
483 if (disc_break) {
484 if (passive_pen_broken(cur_p) != 0) {
485 pen += passive_pen_broken(cur_p);
486 } else {
487 pen += broken_penalty;
490 if (pen != 0) {
491 r = new_penalty(pen);
492 couple_nodes(cur_list.tail_field, r);
493 cur_list.tail_field = r;
496 /* /Append a penalty node, if a nonzero penalty is appropriate */
498 /* /Justify the line ending at breakpoint |cur_p|, and append it to the
499 current vertical list, together with associated penalties and other
500 insertions; */
501 incr(cur_line);
502 cur_p = next_break(cur_p);
503 if (cur_p != null && !post_disc_break) {
504 /* Prune unwanted nodes at the beginning of the next line; */
505 /* Glue and penalty and kern and math nodes are deleted at the
506 beginning of a line, except in the anomalous case that the
507 node to be deleted is actually one of the chosen
508 breakpoints. Otherwise the pruning done here is designed to
509 match the lookahead computation in |try_break|, where the
510 |break_width| values are computed for non-discretionary
511 breakpoints. */
512 r = temp_head;
513 if (experimental_code[1]) {
514 /* hh-ls: This is a first step to improving symmetry and consistency in the node
515 list. This is normally no issue in tex, but in callbacks it matters. */
517 /* Normally we have a matching math open and math close node but when we cross a line
518 the open node is removed, including any glue or penalties following it. This is however
519 not that nice for callbacks that rely on symmetry. Of course this only counts for one
520 liners, as we can still have only a begin or end node on a line. The end_of_math lua
521 helper is made robust against this although there you should be aware of the fact that
522 one can end up in the middle of math in callbacks that don't work on whole paragraphs,
523 but at least this branch makes sure that some proper analysis is possible. (todo: check
524 if math glyphs have the subtype marked done). */
526 halfword m = null ;
527 halfword mp, mn, rn ;
528 while (1) {
529 q = vlink(r);
530 if (! q) {
531 /* unlikely */
532 break;
533 } else if (q == cur_break(cur_p)) {
534 /* quit */
535 break;
536 } else if (type(q) == glyph_node) {
537 /* quit: is > math_code */
538 break;
539 } else if (type(q) == math_node) {
540 /* we want to keep symmetry */
541 surround(q) = 0 ;
542 // fprintf(stdout,"KEEP MATH NODE\n");
543 m = q ;
544 } else if (type(q) == kern_node && subtype(q) != explicit) {
545 /* quit: so we keep \kern but also auto kerns */
546 break;
547 } if (non_discardable(q)) {
548 /* quit: < math_node */
549 break;
550 } else {
551 /* skip: glue, penalty, (font)kern, noads, temp stuff, all kind of left-overs */
553 r = q;
555 if (m != null) {
556 if (r == m) {
557 /* [a] [b] [m=r] => [a] [b=r] */
558 r = alink(m) ;
559 } else {
560 /* [a] [b] [m] [c] [r] [rn] => [a] [b] [c] [r] [m] [rn] */
561 mp = alink(m) ;
562 mn = vlink(m) ;
563 rn = vlink(r) ;
564 vlink(r) = m ;
565 alink(m) = r ;
566 if (rn) {
567 alink(rn) = m ;
568 vlink(m) = rn ;
570 vlink(mp) = mn ;
571 alink(mn) = mp ;
574 } else {
575 while (1) {
576 q = vlink(r);
577 if (q == cur_break(cur_p) || is_char_node(q))
578 break;
579 if (!((type(q) == whatsit_node) && (subtype(q) == local_par_node))) {
580 if (non_discardable(q) || (type(q) == kern_node && subtype(q) != explicit))
581 break;
583 r = q;
586 if (r != temp_head) {
587 vlink(r) = null;
588 flush_node_list(vlink(temp_head));
589 try_couple_nodes(temp_head, q);
592 } while (cur_p != null);
593 if ((cur_line != best_line) || (vlink(temp_head) != null))
594 confusion("line breaking");
595 cur_list.pg_field = best_line - 1; /* prevgraf */
596 cur_list.dirs_field = dir_ptr; /* |dir_save| */
597 dir_ptr = null;