Debian Codegraph @

texk/web2c/luatexdir/pdf/pdfthread.c

raw
1 /*
2
3 Copyright 2009-2012 Taco Hoekwater <taco@@luatex.org>
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 /*tex
25
26 Threads are handled in similar way as link annotations.
27
28 */
29
30 void append_bead(PDF pdf, halfword p)
31 {
32 int a, b, c, t;
33 if (global_shipping_mode == SHIPPING_FORM)
34 normal_error("pdf backend", "threads cannot be inside an xform");
35 t = pdf_get_obj(pdf, obj_type_thread, pdf_thread_id(p), pdf_thread_named_id(p));
36 b = pdf_create_obj(pdf, obj_type_others, 0);
37 obj_bead_ptr(pdf, b) = pdf_get_mem(pdf, pdfmem_bead_size);
38 set_obj_bead_page(pdf, b, pdf->last_page);
39 set_obj_bead_data(pdf, b, p);
40 if (pdf_thread_attr(p) != null)
41 set_obj_bead_attr(pdf, b, tokens_to_string(pdf_thread_attr(p)));
42 else
43 set_obj_bead_attr(pdf, b, 0);
44 if (obj_thread_first(pdf, t) == 0) {
45 obj_thread_first(pdf, t) = b;
46 set_obj_bead_next(pdf, b, b);
47 set_obj_bead_prev(pdf, b, b);
48 } else {
49 a = obj_thread_first(pdf, t);
50 c = obj_bead_prev(pdf, a);
51 set_obj_bead_prev(pdf, b, c);
52 set_obj_bead_next(pdf, b, a);
53 set_obj_bead_prev(pdf, a, b);
54 set_obj_bead_next(pdf, c, b);
55 }
56 addto_page_resources(pdf, obj_type_bead, b);
57 }
58
59 void do_thread(PDF pdf, halfword p, halfword parent_box, scaledpos cur)
60 {
61 scaled_whd alt_rule;
62 if ((type(p) == hlist_node) && (subtype(p) == pdf_start_thread_node))
63 normal_error("pdf backend", "'startthread' ended up in hlist");
64 if (doing_leaders)
65 return;
66 if (subtype(p) == pdf_start_thread_node) {
67 pdf->thread.wd = width(p);
68 pdf->thread.ht = height(p);
69 pdf->thread.dp = depth(p);
70 pdf->last_thread_id = pdf_thread_id(p);
71 pdf->last_thread_named_id = (pdf_thread_named_id(p) > 0);
72 if (pdf->last_thread_named_id)
73 add_token_ref(pdf_thread_id(p));
74 pdf->thread_level = cur_s;
75 }
76 alt_rule.wd = width(p);
77 alt_rule.ht = height(p);
78 alt_rule.dp = depth(p);
79 pdf_ann_margin(p) = pdf_thread_margin;
80 set_rect_dimens(pdf, p, parent_box, cur, alt_rule);
81 append_bead(pdf, p);
82 pdf->last_thread = p;
83 }
84
85 void append_thread(PDF pdf, halfword parent_box, scaledpos cur)
86 {
87 scaled_whd alt_rule;
88 halfword p = new_node(whatsit_node, pdf_thread_data_node);
89 width(p) = pdf->thread.wd;
90 height(p) = pdf->thread.ht;
91 depth(p) = pdf->thread.dp;
92 pdf_thread_attr(p) = null;
93 pdf_thread_id(p) = pdf->last_thread_id;
94 if (pdf->last_thread_named_id) {
95 add_token_ref(pdf_thread_id(p));
96 pdf_thread_named_id(p) = 1;
97 } else {
98 pdf_thread_named_id(p) = 0;
99 }
100 alt_rule.wd = width(p);
101 alt_rule.ht = height(p);
102 alt_rule.dp = depth(p);
103 set_rect_dimens(pdf, p, parent_box, cur, alt_rule);
104 append_bead(pdf, p);
105 pdf->last_thread = p;
106 }
107
108 void end_thread(PDF pdf, halfword p)
109 {
110 scaledpos pos = pdf->posstruct->pos;
111 if (type(p) == hlist_node)
112 normal_error("pdf backend", "'endthread' ended up in hlist");
113 if (pdf->thread_level != cur_s)
114 normal_error("pdf backend", "'endthread' ended up in different nesting level than 'startthread'");
115 if (is_running(pdf->thread.dp) && (pdf->last_thread != null)) {
116 switch (pdf->posstruct->dir) {
117 case dir_TLT:
118 case dir_TRT:
119 pdf_ann_bottom(pdf->last_thread) = pos.v;
120 break;
121 case dir_LTL:
122 pdf_ann_right(pdf->last_thread) = pos.h;
123 break;
124 case dir_RTT:
125 pdf_ann_left(pdf->last_thread) = pos.h;
126 break;
127 default:
128 formatted_warning("pdf backend","forcing bad dir %i to TLT in end tread",pdf->posstruct->dir);
129 }
130 }
131 if (pdf->last_thread_named_id)
132 delete_token_ref(pdf->last_thread_id);
133 pdf->last_thread = null;
134 }
135
136 /*tex The following function are needed for outputing article thread. */
137
138 void thread_title(PDF pdf, int t)
139 {
140 pdf_add_name(pdf, "Title");
141 pdf_out(pdf, '(');
142 if (obj_info(pdf, t) < 0)
143 pdf_print(pdf, -obj_info(pdf, t));
144 else
145 pdf_print_int(pdf, obj_info(pdf, t));
146 pdf_out(pdf, ')');
147 }
148
149 void pdf_fix_thread(PDF pdf, int t)
150 {
151 halfword a;
152 if (obj_info(pdf, t) < 0) {
153 char *ss = makecstring(-obj_info(pdf, t));
154 formatted_warning("pdf backend", "unknown thread destination name '%s'",ss);
155 } else {
156 formatted_warning("pdf backend", "unknown thread destination num '%d'",obj_info(pdf, t));
157 }
158 a = pdf_create_obj(pdf, obj_type_others, 0);
159 pdf_begin_obj(pdf, a, OBJSTM_ALWAYS);
160 pdf_begin_dict(pdf);
161 pdf_dict_add_ref(pdf, "T", t);
162 pdf_dict_add_ref(pdf, "V", a);
163 pdf_dict_add_ref(pdf, "N", a);
164 pdf_dict_add_ref(pdf, "P", pdf->last_page);
165 pdf_add_name(pdf, "R");
166 pdf_begin_array(pdf);
167 pdf_add_int(pdf, 0);
168 pdf_add_int(pdf, 0);
169 pdf_add_bp(pdf, page_width_par);
170 pdf_add_bp(pdf, page_height_par);
171 pdf_end_array(pdf);
172 pdf_end_dict(pdf);
173 pdf_end_obj(pdf);
174 pdf_begin_obj(pdf, t, OBJSTM_ALWAYS);
175 pdf_begin_dict(pdf);
176 pdf_add_name(pdf, "I");
177 pdf_begin_dict(pdf);
178 thread_title(pdf, t);
179 pdf_end_dict(pdf);
180 pdf_dict_add_ref(pdf, "F", a);
181 pdf_end_dict(pdf);
182 pdf_end_obj(pdf);
183 }
184
185 void out_thread(PDF pdf, int t)
186 {
187 halfword a, b;
188 int last_attr;
189 if (obj_thread_first(pdf, t) == 0) {
190 pdf_fix_thread(pdf, t);
191 return;
192 }
193 pdf_begin_obj(pdf, t, OBJSTM_ALWAYS);
194 pdf_begin_dict(pdf);
195 a = obj_thread_first(pdf, t);
196 b = a;
197 last_attr = 0;
198 do {
199 if (obj_bead_attr(pdf, a) != 0)
200 last_attr = obj_bead_attr(pdf, a);
201 a = obj_bead_next(pdf, a);
202 } while (a != b);
203 if (last_attr != 0) {
204 pdf_print_ln(pdf, last_attr);
205 } else {
206 pdf_add_name(pdf, "I");
207 pdf_begin_dict(pdf);
208 thread_title(pdf, t);
209 pdf_end_dict(pdf);
210 }
211 pdf_dict_add_ref(pdf, "F", a);
212 pdf_end_dict(pdf);
213 pdf_end_obj(pdf);
214 do {
215 pdf_begin_obj(pdf, a, OBJSTM_ALWAYS);
216 pdf_begin_dict(pdf);
217 if (a == b)
218 pdf_dict_add_ref(pdf, "T", t);
219 pdf_dict_add_ref(pdf, "V", obj_bead_prev(pdf, a));
220 pdf_dict_add_ref(pdf, "N", obj_bead_next(pdf, a));
221 pdf_dict_add_ref(pdf, "P", obj_bead_page(pdf, a));
222 pdf_dict_add_ref(pdf, "R", obj_bead_rect(pdf, a));
223 pdf_end_dict(pdf);
224 pdf_end_obj(pdf);
225 a = obj_bead_next(pdf, a);
226 } while (a != b);
227 }
228
229 void scan_thread_id(void)
230 {
231 if (scan_keyword("num")) {
232 scan_int();
233 if (cur_val <= 0)
234 normal_error("pdf backend", "num identifier must be positive");
235 if (cur_val > max_halfword)
236 normal_error("pdf backend", "number too big");
237 set_pdf_thread_id(cur_list.tail_field, cur_val);
238 set_pdf_thread_named_id(cur_list.tail_field, 0);
239 } else if (scan_keyword("name")) {
240 scan_toks(false, true);
241 set_pdf_thread_id(cur_list.tail_field, def_ref);
242 set_pdf_thread_named_id(cur_list.tail_field, 1);
243 } else {
244 normal_error("pdf backend", "identifier type missing");
245 }
246 }
247
248 void check_running_thread(PDF pdf, halfword this_box, scaledpos cur)
249 {
250 if ((pdf->last_thread != null) && is_running(pdf->thread.dp)
251 && (pdf->thread_level == cur_s))
252 append_thread(pdf, this_box, cur);
253 }
254
255 void print_bead_rectangles(PDF pdf)
256 {
257 halfword i;
258 pdf_object_list *k;
259 int l;
260 if ((k = get_page_resources_list(pdf, obj_type_bead)) != NULL) {
261 while (k != NULL) {
262 l = pdf_create_obj(pdf, obj_type_others, 0);
263 pdf_begin_obj(pdf, l, OBJSTM_ALWAYS);
264 pdf_begin_array(pdf);
265 /*tex A pointer to a whatsit or whatsit-like node: */
266 i = obj_bead_data(pdf, k->info);
267 pdf_add_rect_spec(pdf, i);
268 if (subtype(i) == pdf_thread_data_node)
269 flush_node(i);
270 pdf_end_array(pdf);
271 pdf_end_obj(pdf);
272 /*tex Rewrite |obj_bead_data|: */
273 set_obj_bead_rect(pdf, k->info, l);
274 k = k->link;
275 }
276 }
277 }