Debian Codegraph @

texk/web2c/luatexdir/pdf/pdflistout.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 #define kern_width(q) width(q) + ex_kern(q)
25
26 #define billion 1000000000.0
27
28 #define vet_glue(A) do { \
29 glue_temp=A; \
30 if (glue_temp>billion) \
31 glue_temp=billion; \
32 else if (glue_temp<-billion) \
33 glue_temp=-billion; \
34 } while (0)
35
36 /*tex
37
38 This code scans forward to the ending |dir_node| while keeping track of the
39 needed width in |w|. When it finds the node that will end this segment, it
40 stores the accumulated with in the |dir_dvi_h| field of that end node, so
41 that when that node is found later in the processing, the correct glue
42 correction can be applied (obsolete).
43
44 */
45
46 static scaled simple_advance_width(halfword p)
47 {
48 halfword q = p;
49 scaled w = 0;
50 while ((q != null) && (vlink(q) != null)) {
51 q = vlink(q);
52 switch (type(q)) {
53 case glyph_node:
54 w += glyph_width(q);
55 break;
56 case hlist_node:
57 case vlist_node:
58 case rule_node:
59 case margin_kern_node:
60 w += width(q);
61 break;
62 case kern_node:
63 /*tex Officially we should check the subtype. */
64 w += kern_width(q);
65 break;
66 case disc_node:
67 /* (HH): The frontend should append already. */
68 if (vlink(no_break(q)) != null)
69 w += simple_advance_width(no_break(q));
70 default:
71 break;
72 }
73 }
74 return w;
75 }
76
77 static halfword calculate_width_to_enddir(halfword p, real cur_glue, scaled cur_g, halfword this_box)
78 {
79 int dir_nest = 1;
80 halfword q = p, enddir_ptr = p;
81 scaled w = 0;
82 /*tex The glue value before rounding: */
83 real glue_temp;
84 int g_sign = glue_sign(this_box);
85 int g_order = glue_order(this_box);
86 while ((q != null) && (vlink(q) != null)) {
87 q = vlink(q);
88 if (is_char_node(q))
89 w += pack_width(box_dir(this_box), dir_TRT, q, true);
90 else {
91 switch (type(q)) {
92 case hlist_node:
93 case vlist_node:
94 w += pack_width(box_dir(this_box), box_dir(q), q, false);
95 break;
96 case rule_node:
97 case margin_kern_node:
98 w += width(q);
99 break;
100 case kern_node:
101 /*tex Officially we should check the subtype. */
102 w += kern_width(q);
103 break;
104 case math_node:
105 /*tex Begin of |mathskip| code. */
106 if (glue_is_zero(q)) {
107 w += surround(q);
108 break;
109 } else {
110 /*tex Fall through |mathskip|. */
111 }
112 /*tex End of |mathskip| code. */
113 case glue_node:
114 w += width(q) - cur_g;
115 if (g_sign != normal) {
116 if (g_sign == stretching) {
117 if (stretch_order(q) == g_order) {
118 cur_glue = cur_glue + stretch(q);
119 vet_glue(float_cast(glue_set(this_box)) * cur_glue);
120 cur_g = float_round(glue_temp);
121 }
122 } else if (shrink_order(q) == g_order) {
123 cur_glue = cur_glue - shrink(q);
124 vet_glue(float_cast(glue_set(this_box)) * cur_glue);
125 cur_g = float_round(glue_temp);
126 }
127 }
128 w += cur_g;
129 break;
130 case disc_node:
131 /* (HH): The frontend should append already. */
132 if (vlink(no_break(q)) != null)
133 w += simple_advance_width(no_break(q));
134 break;
135 case dir_node:
136 if (subtype(q) == normal_dir)
137 dir_nest++;
138 else
139 dir_nest--;
140 if (dir_nest == 0) {
141 enddir_ptr = q;
142 dir_cur_h(enddir_ptr) = w;
143 q = null;
144 }
145 break;
146 default:
147 break;
148 }
149 }
150 }
151 if (enddir_ptr == p)
152 /*tex No enddir found, just transport |w| by |begindir|. */
153 dir_cur_h(enddir_ptr) = w;
154 return enddir_ptr;
155 }
156
157 /*tex
158 With |\pdfvariable linking = 1| a few alternative code paths witll be used that try to compensate
159 for multiline side effects as well as left- and right skip (no other skips yet). After days of
160 looking for better ways I settled on this hackery approach. After all for > 15 years we had the
161 old handling and so a flag makes sense. I (HH) don't use this code myself so it's a it of a
162 gamble wrt usage scenarios.
163 */
164
165 halfword calculate_width_to_endlink(halfword p, halfword this_box, halfword *leftskip, halfword *rightskip)
166 {
167 halfword q = p;
168 scaled w = 0;
169 /*tex The glue value before rounding: */
170 real glue_temp;
171 real cur_glue = 0.0;
172 int level = 0;
173 /*tex rounded equivalent of |cur_glue| times the glue ratio */
174 scaled cur_g = 0;
175 int g_sign = glue_sign(this_box);
176 int g_order = glue_order(this_box);
177 *leftskip = 0;
178 while (q != null) {
179 if (is_char_node(q))
180 w += pack_width(box_dir(this_box), dir_TRT, q, true);
181 else {
182 switch (type(q)) {
183 case hlist_node:
184 case vlist_node:
185 w += pack_width(box_dir(this_box), box_dir(q), q, false);
186 break;
187 case rule_node:
188 case margin_kern_node:
189 w += width(q);
190 break;
191 case kern_node:
192 /*tex Officially we should check the subtype. */
193 w += kern_width(q);
194 break;
195 case math_node:
196 /*tex Begin of |mathskip| code. */
197 if (glue_is_zero(q)) {
198 w += surround(q);
199 break;
200 } else {
201 /*tex Fall through |mathskip|. */
202 }
203 /*tex End of |mathskip| code. */
204 case glue_node:
205 /* this is a terrible hack related to multiline links with directions */
206 {
207 halfword ww = w;
208
209 w += width(q) - cur_g;
210 if (g_sign != normal) {
211 if (g_sign == stretching) {
212 if (stretch_order(q) == g_order) {
213 cur_glue = cur_glue + stretch(q);
214 vet_glue(float_cast(glue_set(this_box)) * cur_glue);
215 cur_g = float_round(glue_temp);
216 }
217 } else if (shrink_order(q) == g_order) {
218 cur_glue = cur_glue - shrink(q);
219 vet_glue(float_cast(glue_set(this_box)) * cur_glue);
220 cur_g = float_round(glue_temp);
221 }
222 }
223 w += cur_g;
224 /* also experiment */
225 if (type(q) == glue_node) {
226 if (subtype(q) == left_skip_glue) {
227 *leftskip = w - ww;
228 w = ww;
229 } else if (subtype(q) == right_skip_glue) {
230 *rightskip = w - ww;
231 w = ww;
232 }
233 }
234 }
235 break;
236 case disc_node:
237 /* (HH): The frontend should append already. */
238 if (vlink(no_break(q)) != null)
239 w += simple_advance_width(no_break(q));
240 break;
241 case whatsit_node:
242 if (subtype(q) == pdf_end_link_node) {
243 if (level == 0) {
244 return w;
245 } else {
246 --level;
247 }
248 } else if (subtype(q) == pdf_start_link_node) {
249 ++level;
250 }
251 break;
252 default:
253 break;
254 }
255 }
256 q = vlink(q);
257 }
258 return w;
259 }
260
261 /*tex
262
263 The |out_what| procedure takes care of outputting the whatsit nodes for
264 |vlist_out| and |hlist_out|, which are similar for either case.
265
266 We don't implement \.{\\write} inside of leaders. (The reason is that the
267 number of times a leader box appears might be different in different
268 implementations, due to machine-dependent rounding in the glue calculations.)
269
270 */
271
272 static void handle_backend_whatsit(PDF pdf, halfword p, halfword parent_box, scaledpos cur)
273 {
274 if (output_mode_used == OMODE_PDF) {
275 switch (subtype(p)) {
276 case pdf_literal_node:
277 case pdf_late_literal_node:
278 case pdf_save_node:
279 case pdf_restore_node:
280 case pdf_setmatrix_node:
281 case pdf_colorstack_node:
282 case pdf_refobj_node:
283 case pdf_end_link_node:
284 case pdf_end_thread_node:
285 case pdf_link_state_node:
286 backend_out_whatsit[BACKEND_WHATSIT_INDEX(subtype(p))](pdf, p);
287 break;
288 case pdf_annot_node:
289 case pdf_start_link_node:
290 case pdf_dest_node:
291 case pdf_start_thread_node:
292 case pdf_thread_node:
293 backend_out_whatsit_do[BACKEND_WHATSIT_DO_INDEX(subtype(p))](pdf, p, parent_box, cur);
294 break;
295 default:
296 /*tex We ignore bad ones. */
297 break;
298 }
299 }
300 }
301
302 void hlist_out(PDF pdf, halfword this_box, int rule_callback_id)
303 {
304 /*tex the position structure local within this function */
305 posstructure localpos;
306 /*tex the list origin pos. on the page, provided by the caller */
307 posstructure *refpos;
308 /*tex A few status variables. */
309 scaledpos cur = { 0, 0 }, tmpcur, basepoint;
310 /*tex rule dimensions */
311 scaledpos size = { 0, 0 };
312 scaled effective_horizontal;
313 /*tex what |cur.h| should pop to */
314 scaled save_h;
315 /*tex right edge of sub-box or leader space */
316 scaled edge;
317 /*tex temporary pointer to enddir node */
318 halfword enddir_ptr;
319 /*tex applicable order of infinity for glue */
320 int g_order;
321 /*tex selects type of glue */
322 int g_sign;
323 /*tex glue variables */
324 int lq, lr;
325 /*tex current position in the hlist */
326 halfword p, q;
327 /*tex the leader box being replicated */
328 halfword leader_box;
329 /*tex width of leader box being replicated */
330 scaled leader_wd;
331 /*tex extra space between leader boxes */
332 scaled lx;
333 /*tex were we doing leaders? */
334 boolean outer_doing_leaders;
335 /*tex glue value before rounding */
336 real glue_temp;
337 /*tex glue seen so far */
338 real cur_glue = 0.0;
339 /*tex rounded equivalent of |cur_glue| times the glue ratio */
340 scaled cur_g = 0;
341 scaled_whd rule, ci;
342 /*tex index to scan |pdf_link_stack| */
343 int i;
344 /*tex DVI! \.{DVI} byte location upon entry */
345 int saved_loc = 0;
346 /*tex safeguard */
347 int dir_done = 0;
348 /*tex DVI! what |dvi| should pop to */
349 scaledpos saved_pos = { 0, 0 };
350 int synctex = synctex_par ;
351 scaled rleft, rright;
352 g_order = glue_order(this_box);
353 g_sign = glue_sign(this_box);
354 p = list_ptr(this_box);
355 refpos = pdf->posstruct;
356 /*tex use local structure for recursion */
357 pdf->posstruct = &localpos;
358 localpos.pos = refpos->pos;
359 localpos.dir = box_dir(this_box);
360 cur_s++;
361 backend_out_control_list[BACKEND_INDEX(backend_control_push_list)](pdf,&saved_pos,&saved_loc);
362 for (i = 1; i <= pdf->link_stack_ptr; i++) {
363 if (pdf->link_state == 1) {
364 /*
365 We ignore this link. This is a compatibility-with-pdftex feature needed for latex. It
366 is suboptimal in the sense that when the whatsit is set, the next box is influenced,
367 so there there can be side effects when used in the middle of a line, when using
368 vadjust, etc. But we can assume that the macro package knows when and where this
369 mechanism is triggered, so a more sophisticated solution is not needed (and would be
370 confusing in its own anyway.)
371
372 I would not be surprised of we have some leak here but it's harmless.
373 */
374 } else if (pdf->link_stack[i].nesting_level == cur_s) {
375 /* hm, cur is zero */
376 append_link(pdf, this_box, cur, (small_number) i);
377 }
378 }
379 if (synctex) {
380 synctexhlist(this_box);
381 }
382 while (p != null) {
383 if (is_char_node(p)) {
384 do {
385 if (x_displace(p) != 0 || y_displace(p) != 0) {
386 tmpcur.h = cur.h + x_displace(p);
387 tmpcur.v = cur.v - y_displace(p);
388 synch_pos_with_cur(pdf->posstruct, refpos, tmpcur);
389 }
390 ci = output_one_char(pdf, p);
391 if (textdir_parallel(localpos.dir, dir_TLT)) {
392 cur.h += ci.wd;
393 } else {
394 cur.h += ci.ht + ci.dp;
395 }
396 synch_pos_with_cur(pdf->posstruct, refpos, cur);
397 p = vlink(p);
398 } while (is_char_node(p));
399 if (synctex)
400 synctexcurrent();
401 } else {
402 /*tex
403 Output the non-|char_node| |p| for |hlist_out| and move to the
404 next node.
405 */
406 switch (type(p)) {
407 case hlist_node:
408 case vlist_node:
409 if (textdir_parallel(box_dir(p), localpos.dir)) {
410 effective_horizontal = width(p);
411 basepoint.v = 0;
412 if (textdir_opposite(box_dir(p), localpos.dir))
413 basepoint.h = width(p);
414 else
415 basepoint.h = 0;
416 } else {
417 effective_horizontal = height(p) + depth(p);
418 if (!is_mirrored(box_dir(p))) {
419 if (partextdir_eq(box_dir(p), localpos.dir))
420 basepoint.h = height(p);
421 else
422 basepoint.h = depth(p);
423 } else {
424 if (partextdir_eq(box_dir(p), localpos.dir))
425 basepoint.h = depth(p);
426 else
427 basepoint.h = height(p);
428 }
429 if (is_rotated(localpos.dir)) {
430 if (partextdir_eq(localpos.dir, box_dir(p))) {
431 /*tex up */
432 basepoint.v = -width(p) / 2;
433 } else {
434 /*tex down */
435 basepoint.v = width(p) / 2;
436 }
437 } else if (is_mirrored(localpos.dir)) {
438 if (partextdir_eq(localpos.dir, box_dir(p))) {
439 basepoint.v = 0;
440 } else {
441 /*tex down */
442 basepoint.v = width(p);
443 }
444 } else {
445 if (partextdir_eq(localpos.dir, box_dir(p))) {
446 /*tex up */
447 basepoint.v = -width(p);
448 } else {
449 basepoint.v = 0;
450 }
451 }
452 }
453 if (!is_mirrored(localpos.dir)) {
454 /*tex Shift the box down. */
455 basepoint.v = basepoint.v + shift_amount(p);
456 } else {
457 /*tex Shift the box up. */
458 basepoint.v = basepoint.v - shift_amount(p);
459 }
460 if (list_ptr(p) == null) {
461 if (synctex) {
462 if (type(p) == vlist_node)
463 synctexvoidvlist(p, this_box);
464 else
465 synctexvoidhlist(p, this_box);
466 }
467 } else {
468 tmpcur.h = cur.h + basepoint.h;
469 tmpcur.v = basepoint.v;
470 synch_pos_with_cur(pdf->posstruct, refpos, tmpcur);
471 if (type(p) == vlist_node)
472 vlist_out(pdf, p, rule_callback_id);
473 else
474 hlist_out(pdf, p, rule_callback_id);
475 }
476 cur.h += effective_horizontal;
477 break;
478 case disc_node:
479 /*tex (HH): the frontend should append already */
480 if (vlink(no_break(p)) != null) {
481 if (subtype(p) != select_disc) {
482 /*tex This could be a |tlink|. */
483 q = tail_of_list(vlink(no_break(p)));
484 vlink(q) = vlink(p);
485 q = vlink(no_break(p));
486 vlink(no_break(p)) = null;
487 vlink(p) = q;
488 }
489 }
490 break;
491 case math_node:
492 if (synctex) {
493 synctexmath(p, this_box);
494 }
495 /*tex Begin |mathskip| code. */
496 if (glue_is_zero(p)) {
497 cur.h += surround(p);
498 break;
499 } else {
500 /*tex Fall through |mathskip|. */
501 /* No break; we have to go on to glue_node case.*/
502 }
503 /*tex End |mathskip| code. */
504 case glue_node:
505 {
506 /*tex
507 Move right or output leaders, we use real
508 multiplication.
509 */
510 rule.wd = width(p) - cur_g;
511 if (g_sign != normal) {
512 if (g_sign == stretching) {
513 if (stretch_order(p) == g_order) {
514 cur_glue = cur_glue + stretch(p);
515 vet_glue(float_cast(glue_set(this_box)) * cur_glue);
516 cur_g = float_round(glue_temp);
517 }
518 } else if (shrink_order(p) == g_order) {
519 cur_glue = cur_glue - shrink(p);
520 vet_glue(float_cast(glue_set(this_box)) * cur_glue);
521 cur_g = float_round(glue_temp);
522 }
523 }
524 rule.wd = rule.wd + cur_g;
525 if (subtype(p) >= a_leaders) {
526 /*tex
527 Output leaders in an hlist, |goto fin_rule| if a rule
528 or to |next_p| if done.
529 */
530 leader_box = leader_ptr(p);
531 if (type(leader_box) == rule_node) {
532 rule.ht = height(leader_box);
533 rule.dp = depth(leader_box);
534 rleft = 0;
535 rright = 0;
536 goto FIN_RULE;
537 }
538 if (textdir_parallel(box_dir(leader_box), localpos.dir))
539 leader_wd = width(leader_box);
540 else
541 leader_wd = height(leader_box) + depth(leader_box);
542 if ((leader_wd > 0) && (rule.wd > 0)) {
543 /*tex Compensate for floating-point rounding. */
544 rule.wd = rule.wd + 10;
545 edge = cur.h + rule.wd;
546 lx = 0;
547 /*tex
548 Let |cur.h| be the position of the first box, and
549 set |leader_wd+lx| to the spacing between
550 corresponding parts of boxes.
551 */
552 if (subtype(p) == g_leaders) {
553 save_h = cur.h;
554 switch (localpos.dir) {
555 case dir_TLT:
556 cur.h += refpos->pos.h - shipbox_refpos.h;
557 cur.h = leader_wd * (cur.h / leader_wd);
558 cur.h -= refpos->pos.h - shipbox_refpos.h;
559 break;
560 case dir_TRT:
561 cur.h = refpos->pos.h - shipbox_refpos.h - cur.h;
562 cur.h = leader_wd * (cur.h / leader_wd);
563 cur.h = refpos->pos.h - shipbox_refpos.h - cur.h;
564 break;
565 case dir_LTL:
566 case dir_RTT:
567 cur.h = refpos->pos.v - shipbox_refpos.v - cur.h;
568 cur.h = leader_wd * (cur.h / leader_wd);
569 cur.h = refpos->pos.v - shipbox_refpos.v - cur.h;
570 break;
571 default:
572 formatted_warning("pdf backend","forcing bad dir %i to TLT in hlist case 1",localpos.dir);
573 localpos.dir = dir_TLT;
574 cur.h += refpos->pos.h - shipbox_refpos.h;
575 cur.h = leader_wd * (cur.h / leader_wd);
576 cur.h -= refpos->pos.h - shipbox_refpos.h;
577 break;
578 }
579 if (cur.h < save_h)
580 cur.h += leader_wd;
581 } else if (subtype(p) == a_leaders) {
582 save_h = cur.h;
583 cur.h = leader_wd * (cur.h / leader_wd);
584 if (cur.h < save_h)
585 cur.h += leader_wd;
586 } else {
587 /*tex The number of box copies: */
588 lq = rule.wd / leader_wd;
589 /*tex The remaining space: */
590 lr = rule.wd % leader_wd;
591 if (subtype(p) == c_leaders) {
592 cur.h += lr / 2;
593 } else {
594 lx = lr / (lq + 1);
595 cur.h += (lr - (lq - 1) * lx) / 2;
596 }
597 }
598 while (cur.h + leader_wd <= edge) {
599 /*tex
600 Output a leader box at |cur.h|, then advance
601 |cur.h| by |leader_wd+lx|.
602 */
603 if (pardir_parallel(box_dir(leader_box), localpos.dir)) {
604 basepoint.v = 0;
605 if (textdir_opposite(box_dir(leader_box), localpos.dir))
606 basepoint.h = width(leader_box);
607 else
608 basepoint.h = 0;
609 } else {
610 if (!is_mirrored(box_dir(leader_box))) {
611 if (partextdir_eq(box_dir(leader_box), localpos.dir))
612 basepoint.h = height(leader_box);
613 else
614 basepoint.h = depth(leader_box);
615 } else {
616 if (partextdir_eq(box_dir(leader_box), localpos.dir))
617 basepoint.h = depth(leader_box);
618 else
619 basepoint.h = height(leader_box);
620 }
621 if (partextdir_eq(localpos.dir, box_dir(leader_box)))
622 basepoint.v = -(width(leader_box) / 2);
623 else
624 basepoint.v = (width(leader_box) / 2);
625 }
626 if (!is_mirrored(localpos.dir))
627 basepoint.v = basepoint.v + shift_amount(leader_box); /* shift the box down */
628 else
629 basepoint.v = basepoint.v - shift_amount(leader_box); /* shift the box up */
630 tmpcur.h = cur.h + basepoint.h;
631 tmpcur.v = basepoint.v;
632 synch_pos_with_cur(pdf->posstruct, refpos, tmpcur);
633 outer_doing_leaders = doing_leaders;
634 doing_leaders = true;
635 if (type(leader_box) == vlist_node)
636 vlist_out(pdf, leader_box, rule_callback_id);
637 else
638 hlist_out(pdf, leader_box, rule_callback_id);
639 doing_leaders = outer_doing_leaders;
640 cur.h += leader_wd + lx;
641 }
642 cur.h = edge - 10;
643 goto NEXTP;
644 }
645 }
646 }
647 goto MOVE_PAST;
648 break;
649 case kern_node:
650 if (synctex)
651 synctexkern(p, this_box);
652 /*tex officially we should check the subtype */
653 cur.h += kern_width(p);
654 break;
655 case rule_node:
656 if (rule_dir(p) < 0)
657 rule_dir(p) = localpos.dir;
658 if (pardir_parallel(rule_dir(p), localpos.dir)) {
659 rule.ht = height(p);
660 rule.dp = depth(p);
661 rule.wd = width(p);
662 } else {
663 rule.ht = width(p) / 2;
664 rule.dp = width(p) / 2;
665 rule.wd = height(p) + depth(p);
666 }
667 rleft = rule_left(p);
668 rright = rule_right(p);
669 goto FIN_RULE;
670 break;
671 case dir_node:
672 /*tex
673 Output a reflection instruction if the direction has changed.
674 */
675 if (subtype(p) == normal_dir) {
676 /*tex
677 Calculate the needed width to the matching |enddir|, return the
678 |enddir| node, with width info.
679 */
680 enddir_ptr = calculate_width_to_enddir(p, cur_glue, cur_g, this_box);
681 if (textdir_parallel(dir_dir(p), localpos.dir)) {
682 dir_cur_h(enddir_ptr) += cur.h;
683 if (textdir_opposite(dir_dir(p), localpos.dir))
684 cur.h = dir_cur_h(enddir_ptr);
685 } else
686 dir_cur_h(enddir_ptr) = cur.h;
687 if (enddir_ptr != p) {
688 /*tex Only if it is an |enddir|. */
689 dir_cur_v(enddir_ptr) = cur.v;
690 dir_refpos_h(enddir_ptr) = refpos->pos.h;
691 dir_refpos_v(enddir_ptr) = refpos->pos.v;
692 /*tex Negative: mark it as |enddir|. */
693 dir_dir(enddir_ptr) = localpos.dir;
694 }
695 /*tex fake a nested |hlist_out|. */
696 synch_pos_with_cur(pdf->posstruct, refpos, cur);
697 refpos->pos = pdf->posstruct->pos;
698 localpos.dir = dir_dir(p);
699 cur.h = 0;
700 cur.v = 0;
701 dir_done = 1;
702 } else if (dir_done) {
703 refpos->pos.h = dir_refpos_h(p);
704 refpos->pos.v = dir_refpos_v(p);
705 localpos.dir = dir_dir(p);
706 cur.h = dir_cur_h(p);
707 cur.v = dir_cur_v(p);
708 } else {
709 /*tex maybe issue a warning */
710 }
711 break;
712 case whatsit_node:
713 /*tex Output the whatsit node |p| in |hlist_out|. */
714 if (subtype(p) <= last_common_whatsit) {
715 switch (subtype(p)) {
716 case save_pos_node:
717 last_position = pdf->posstruct->pos;
718 /*
719 pos_info.curpos = pdf->posstruct->pos;
720 pos_info.boxpos.pos = refpos->pos;
721 pos_info.boxpos.dir = localpos.dir;
722 pos_info.boxdim.wd = width(this_box);
723 pos_info.boxdim.ht = height(this_box);
724 pos_info.boxdim.dp = depth(this_box);
725 */
726 break;
727 case user_defined_node:
728 break;
729 case open_node:
730 case write_node:
731 case close_node:
732 wrapup_leader(p);
733 break;
734 case special_node:
735 case late_special_node:
736 /*tex |pdf_special(pdf, p)| */
737 case late_lua_node:
738 /*tex |late_lua(pdf, p)| */
739 backend_out_whatsit[BACKEND_WHATSIT_INDEX(subtype(p))] (pdf, p);
740 break;
741 default:
742 break;
743 }
744 } else {
745 handle_backend_whatsit(pdf, p, this_box, cur);
746 }
747 break;
748 case margin_kern_node:
749 cur.h += width(p);
750 break;
751 default:
752 break;
753 }
754 goto NEXTP;
755 FIN_RULE:
756 /*tex Output a rule in an hlist. */
757 if (is_running(rule.ht)) {
758 rule.ht = height(this_box);
759 }
760 if (rleft != 0) {
761 rule.ht -= rleft;
762 pos_down(-rleft);
763 }
764 if (is_running(rule.dp)) {
765 rule.dp = depth(this_box);
766 }
767 if (rright != 0) {
768 rule.dp -= rright;
769 }
770 /*tex We don't output empty rules. */
771 if ((rule.ht + rule.dp) > 0 && rule.wd > 0) {
772 switch (localpos.dir) {
773 case dir_TLT:
774 size.h = rule.wd;
775 size.v = rule.ht + rule.dp;
776 pos_down(rule.dp);
777 break;
778 case dir_TRT:
779 size.h = rule.wd;
780 size.v = rule.ht + rule.dp;
781 pos_left(size.h);
782 pos_down(rule.dp);
783 break;
784 case dir_LTL:
785 size.h = rule.ht + rule.dp;
786 size.v = rule.wd;
787 pos_left(rule.ht);
788 pos_down(size.v);
789 break;
790 case dir_RTT:
791 size.h = rule.ht + rule.dp;
792 size.v = rule.wd;
793 pos_left(rule.dp);
794 pos_down(size.v);
795 break;
796 default:
797 formatted_warning("pdf backend","forcing bad dir %i to TLT in hlist case 2",localpos.dir);
798 localpos.dir = dir_TLT;
799 size.h = rule.wd;
800 size.v = rule.ht + rule.dp;
801 pos_down(rule.dp);
802 break;
803 }
804 if (type(p) == glue_node) {
805 q = leader_ptr(p);
806 if ((q) && (type(q) == rule_node)) {
807 backend_out_node_rule[BACKEND_INDEX(rule_node)] (pdf, q, size, rule_callback_id);
808 }
809 } else {
810 backend_out_node_rule[BACKEND_INDEX(rule_node)] (pdf, p, size, rule_callback_id);
811 }
812 }
813 MOVE_PAST:
814 cur.h += rule.wd;
815 if (synctex) {
816 synch_pos_with_cur(pdf->posstruct, refpos, cur);
817 if( type(p) == math_node && !glue_is_zero(p) ) {
818 normal_warning("listout","synctex doesn't support math node with not zero glue");
819 } else {
820 synctexhorizontalruleorglue(p, this_box);
821 }
822 }
823 NEXTP:
824 p = vlink(p);
825 synch_pos_with_cur(pdf->posstruct, refpos, cur);
826 }
827 }
828 if (synctex) {
829 synctextsilh(this_box);
830 }
831 backend_out_control_list[BACKEND_INDEX(backend_control_pop_list)](pdf,&saved_pos,&saved_loc);
832 cur_s--;
833 pdf->posstruct = refpos;
834 }
835
836 void vlist_out(PDF pdf, halfword this_box, int rule_callback_id)
837 {
838 /*tex The position structure local within this function: */
839 posstructure localpos;
840 /*tex The list origin pos. on the page, provided by the caller: */
841 posstructure *refpos;
842 /*tex a few status variables */
843 scaledpos cur, tmpcur, basepoint;
844 /*tex rule dimensions */
845 scaledpos size = { 0, 0 };
846 scaled effective_vertical;
847 /*tex what |cur.v| should pop to */
848 scaled save_v;
849 /*tex the top coordinate for this box */
850 scaled top_edge;
851 /*tex bottom boundary of leader space */
852 scaled edge;
853 /*tex applicable order of infinity for glue */
854 glue_ord g_order;
855 /*tex selects type of glue */
856 int g_sign;
857 /*tex glue variables */
858 int lq, lr;
859 /*tex current position in the vlist */
860 halfword p;
861 /*tex temp */
862 halfword q;
863 /*tex the leader box being replicated */
864 halfword leader_box;
865 /*tex height of leader box being replicated */
866 scaled leader_ht;
867 /*tex extra space between leader boxes */
868 scaled lx;
869 /*tex were we doing leaders? */
870 boolean outer_doing_leaders;
871 /*tex glue value before rounding */
872 real glue_temp;
873 /*tex glue seen so far */
874 real cur_glue = 0.0;
875 /*tex rounded equivalent of |cur_glue| times the glue ratio */
876 scaled cur_g = 0;
877 scaled_whd rule;
878 /*tex \DVI\ byte location upon entry */
879 int saved_loc = 0;
880 /*tex \DVI\ what |dvi| should pop to */
881 scaledpos saved_pos = { 0, 0 };
882 int synctex = synctex_par ;
883 int rleft, rright;
884 g_order = (glue_ord) glue_order(this_box);
885 g_sign = glue_sign(this_box);
886 p = list_ptr(this_box);
887 cur.h = 0;
888 cur.v = -height(this_box);
889 top_edge = cur.v;
890 refpos = pdf->posstruct;
891 /*tex Use local structure for recursion. */
892 pdf->posstruct = &localpos;
893 localpos.dir = box_dir(this_box);
894 synch_pos_with_cur(pdf->posstruct, refpos, cur);
895 cur_s++;
896 backend_out_control_list[BACKEND_INDEX(backend_control_push_list)](pdf,&saved_pos,&saved_loc);
897 if (synctex) {
898 synctexvlist(this_box);
899 }
900 /*tex Create thread for the current vbox if needed. */
901 check_running_thread(pdf, this_box, cur);
902 while (p != null) {
903 switch (type(p)) {
904 case hlist_node:
905 case vlist_node:
906 /*tex Output a box in a vlist. */
907 if (pardir_parallel(box_dir(p), localpos.dir)) {
908 effective_vertical = height(p) + depth(p);
909 if ((type(p) == hlist_node) && is_mirrored(box_dir(p)))
910 basepoint.v = depth(p);
911 else
912 basepoint.v = height(p);
913 if (textdir_opposite(box_dir(p), localpos.dir))
914 basepoint.h = width(p);
915 else
916 basepoint.h = 0;
917 } else {
918 effective_vertical = width(p);
919 if (!is_mirrored(box_dir(p))) {
920 if (partextdir_eq(box_dir(p), localpos.dir))
921 basepoint.h = height(p);
922 else
923 basepoint.h = depth(p);
924 } else {
925 if (partextdir_eq(box_dir(p), localpos.dir))
926 basepoint.h = depth(p);
927 else
928 basepoint.h = height(p);
929 }
930 if (partextdir_eq(localpos.dir, box_dir(p)))
931 basepoint.v = 0;
932 else
933 basepoint.v = width(p);
934 }
935 /*tex Shift the box right. */
936 basepoint.h = basepoint.h + shift_amount(p);
937 if (list_ptr(p) == null) {
938 cur.v += effective_vertical;
939 if (synctex) {
940 synch_pos_with_cur(pdf->posstruct, refpos, cur);
941 if (type(p) == vlist_node)
942 synctexvoidvlist(p, this_box);
943 else
944 synctexvoidhlist(p, this_box);
945 }
946 } else {
947 tmpcur.h = basepoint.h;
948 tmpcur.v = cur.v + basepoint.v;
949 synch_pos_with_cur(pdf->posstruct, refpos, tmpcur);
950 if (type(p) == vlist_node)
951 vlist_out(pdf, p, rule_callback_id);
952 else
953 hlist_out(pdf, p, rule_callback_id);
954 cur.v += effective_vertical;
955 }
956 break;
957 case rule_node:
958 if (rule_dir(p) < 0)
959 rule_dir(p) = localpos.dir;
960 if (pardir_parallel(rule_dir(p), localpos.dir)) {
961 rule.ht = height(p);
962 rule.dp = depth(p);
963 rule.wd = width(p);
964 } else {
965 rule.ht = width(p) / 2;
966 rule.dp = width(p) / 2;
967 rule.wd = height(p) + depth(p);
968 }
969 rleft = rule_left(p);
970 rright = rule_right(p);
971 goto FIN_RULE;
972 break;
973 case glue_node:
974 {
975 /*tex
976 Move down or output leaders, we use real multiplication.
977 */
978 rule.ht = width(p) - cur_g;
979 if (g_sign != normal) {
980 if (g_sign == stretching) {
981 if (stretch_order(p) == g_order) {
982 cur_glue = cur_glue + stretch(p);
983 vet_glue(float_cast(glue_set(this_box)) * cur_glue);
984 cur_g = float_round(glue_temp);
985 }
986 } else if (shrink_order(p) == g_order) {
987 cur_glue = cur_glue - shrink(p);
988 vet_glue(float_cast(glue_set(this_box)) * cur_glue);
989 cur_g = float_round(glue_temp);
990 }
991 }
992 rule.ht = rule.ht + cur_g;
993 if (subtype(p) >= a_leaders) {
994 /*tex
995 Output leaders in a vlist, |goto fin_rulefin_rule| if a
996 rule or to |next_p| if done.
997 */
998 leader_box = leader_ptr(p);
999 if (type(leader_box) == rule_node) {
1000 rule.wd = width(leader_box);
1001 rule.dp = 0;
1002 rleft = 0;
1003 rright = 0;
1004 goto FIN_RULE;
1005 }
1006 leader_ht = height(leader_box) + depth(leader_box);
1007 if ((leader_ht > 0) && (rule.ht > 0)) {
1008 /*tex Compensate for floating-point rounding: */
1009 rule.ht = rule.ht + 10;
1010 edge = cur.v + rule.ht;
1011 lx = 0;
1012 /*tex
1013 Let |cur.v| be the position of the first box, and set
1014 |leader_ht+lx| to the spacing between corresponding
1015 parts of boxes.
1016 */
1017 if (subtype(p) == g_leaders) {
1018 save_v = cur.v;
1019 switch (localpos.dir) {
1020 case dir_TLT:
1021 case dir_TRT:
1022 cur.v = refpos->pos.v - shipbox_refpos.v - cur.v;
1023 cur.v = leader_ht * (cur.v / leader_ht);
1024 cur.v = refpos->pos.v - shipbox_refpos.v - cur.v;
1025 break;
1026 case dir_LTL:
1027 cur.v += refpos->pos.h - shipbox_refpos.h;
1028 cur.v = leader_ht * (cur.v / leader_ht);
1029 cur.v -= refpos->pos.h - shipbox_refpos.h;
1030 break;
1031 case dir_RTT:
1032 cur.v = refpos->pos.h - shipbox_refpos.h - cur.v;
1033 cur.v = leader_ht * (cur.v / leader_ht);
1034 cur.v = refpos->pos.h - shipbox_refpos.h - cur.v;
1035 break;
1036 default:
1037 formatted_warning("pdf backend","forcing bad dir %i to TLT in vlist case 1",localpos.dir);
1038 localpos.dir = dir_TLT;
1039 cur.v += refpos->pos.h - shipbox_refpos.h;
1040 cur.v = leader_ht * (cur.v / leader_ht);
1041 cur.v -= refpos->pos.h - shipbox_refpos.h;
1042 break;
1043 }
1044 if (cur.v < save_v)
1045 cur.v += leader_ht;
1046 } else if (subtype(p) == a_leaders) {
1047 save_v = cur.v;
1048 cur.v = top_edge + leader_ht * ((cur.v - top_edge) / leader_ht);
1049 if (cur.v < save_v)
1050 cur.v += leader_ht;
1051 } else {
1052 /*tex The number of box copies. */
1053 lq = rule.ht / leader_ht;
1054 /*tex The remaining space. */
1055 lr = rule.ht % leader_ht;
1056 if (subtype(p) == c_leaders) {
1057 cur.v += lr / 2;
1058 } else {
1059 lx = lr / (lq + 1);
1060 cur.v += (lr - (lq - 1) * lx) / 2;
1061 }
1062 }
1063 while (cur.v + leader_ht <= edge) {
1064 /*tex
1065 Output a leader box at |cur.v|, then advance
1066 |cur.v| by |leader_ht+lx|.
1067 */
1068 tmpcur.h = shift_amount(leader_box);
1069 tmpcur.v = cur.v + height(leader_box);
1070 synch_pos_with_cur(pdf->posstruct, refpos, tmpcur);
1071 outer_doing_leaders = doing_leaders;
1072 doing_leaders = true;
1073 if (type(leader_box) == vlist_node)
1074 vlist_out(pdf, leader_box, rule_callback_id);
1075 else
1076 hlist_out(pdf, leader_box, rule_callback_id);
1077 doing_leaders = outer_doing_leaders;
1078 cur.v += leader_ht + lx;
1079 }
1080 cur.v = edge - 10;
1081 goto NEXTP;
1082 }
1083 }
1084 }
1085 goto MOVE_PAST;
1086 break;
1087 case kern_node:
1088 cur.v += width(p);
1089 break;
1090 case whatsit_node:
1091 /*tex Output the whatsit node |p| in |vlist_out|. */
1092 if (subtype(p) <= last_common_whatsit) {
1093 switch (subtype(p)) {
1094 case save_pos_node:
1095 last_position = pdf->posstruct->pos;
1096 /*
1097 pos_info.curpos = pdf->posstruct->pos;
1098 pos_info.boxpos.pos = refpos->pos;
1099 pos_info.boxpos.dir = localpos.dir;
1100 pos_info.boxdim.wd = width(this_box);
1101 pos_info.boxdim.ht = height(this_box);
1102 pos_info.boxdim.dp = depth(this_box);
1103 */
1104 break;
1105 case user_defined_node:
1106 break;
1107 case open_node:
1108 case write_node:
1109 case close_node:
1110 wrapup_leader(p);
1111 break;
1112 case special_node:
1113 case late_special_node:
1114 /*tex |pdf_special(pdf, p)|; */
1115 case late_lua_node:
1116 /*tex |late_lua(pdf, p)|; */
1117 backend_out_whatsit[BACKEND_WHATSIT_INDEX(subtype(p))] (pdf, p);
1118 break;
1119 default:
1120 break;
1121 }
1122 } else {
1123 handle_backend_whatsit(pdf, p, this_box, cur);
1124 }
1125 break;
1126 case glyph_node:
1127 case disc_node:
1128 /*tex This can't happen unless one messes up in \LUA. */
1129 confusion("vlistout");
1130 break;
1131 default:
1132 break;
1133 }
1134 goto NEXTP;
1135 FIN_RULE:
1136 /*tex Output a rule in a vlist and |goto next_p|. */
1137 if (is_running(rule.wd)) {
1138 rule.wd = width(this_box);
1139 }
1140 if (rleft != 0) {
1141 rule.wd -= rleft;
1142 pos_left(-rleft);
1143 }
1144 if (rright != 0) {
1145 rule.wd -= rright;
1146 }
1147 /*tex We don't output empty rules. */
1148 if ((rule.ht + rule.dp) > 0 && rule.wd > 0) {
1149 switch (localpos.dir) {
1150 case dir_TLT:
1151 size.h = rule.wd;
1152 size.v = rule.ht + rule.dp;
1153 pos_down(size.v);
1154 break;
1155 case dir_TRT:
1156 size.h = rule.wd;
1157 size.v = rule.ht + rule.dp;
1158 pos_left(size.h);
1159 pos_down(size.v);
1160 break;
1161 case dir_LTL:
1162 size.h = rule.ht + rule.dp;
1163 size.v = rule.wd;
1164 pos_down(size.v);
1165 break;
1166 case dir_RTT:
1167 size.h = rule.ht + rule.dp;
1168 size.v = rule.wd;
1169 pos_left(size.h);
1170 pos_down(size.v);
1171 break;
1172 default:
1173 formatted_warning("pdf backend","forcing bad dir %i to TLT in vlist case 2",localpos.dir);
1174 localpos.dir = dir_TLT;
1175 size.h = rule.wd;
1176 size.v = rule.ht + rule.dp;
1177 pos_down(size.v);
1178 break;
1179 }
1180 if (type(p) == glue_node) {
1181 q = leader_ptr(p);
1182 if ((q) && (type(q) == rule_node)) {
1183 backend_out_node_rule[BACKEND_INDEX(rule_node)] (pdf, q, size, rule_callback_id);
1184 }
1185 } else {
1186 backend_out_node_rule[BACKEND_INDEX(rule_node)] (pdf, p, size, rule_callback_id);
1187 }
1188 }
1189 cur.v += rule.ht + rule.dp;
1190 goto NEXTP;
1191 MOVE_PAST:
1192 cur.v += rule.ht;
1193 NEXTP:
1194 p = vlink(p);
1195 synch_pos_with_cur(pdf->posstruct, refpos, cur);
1196 }
1197 if (synctex) {
1198 synctextsilv(this_box);
1199 }
1200 backend_out_control_list[BACKEND_INDEX(backend_control_pop_list)](pdf,&saved_pos,&saved_loc);
1201 cur_s--;
1202 pdf->posstruct = refpos;
1203 }