Debian Codegraph @

texk/web2c/luatexdir/lua/luanode.c

raw
1 /*
2
3 luanode.w
4
5 Copyright 2006-2008 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 #include "lua/luatex-api.h"
26
27 void lua_node_filter_s(int filterid, int extrainfo)
28 {
29 int i;
30 int callback_id = callback_defined(filterid);
31 int s_top = lua_gettop(Luas);
32 if (callback_id <= 0) {
33 lua_settop(Luas, s_top);
34 return;
35 }
36 if (!get_callback(Luas, callback_id)) {
37 lua_settop(Luas, s_top);
38 return;
39 }
40 lua_push_string_by_index(Luas,extrainfo);
41 if ((i=lua_pcall(Luas, 1, 0, 0)) != 0) {
42 formatted_warning("node filter","error: %s", lua_tostring(Luas, -1));
43 lua_settop(Luas, s_top);
44 luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
45 return;
46 }
47 lua_settop(Luas, s_top);
48 return;
49 }
50
51 void lua_node_filter(int filterid, int extrainfo, halfword head_node, halfword * tail_node)
52 {
53 int i;
54 halfword start_node, start_done, last_node;
55 int s_top = lua_gettop(Luas);
56 int callback_id = callback_defined(filterid);
57 if (head_node == null || callback_id <= 0) {
58 lua_settop(Luas, s_top);
59 return;
60 }
61 /*tex We start after head. */
62 start_node = vlink(head_node);
63 if (start_node == null || !get_callback(Luas, callback_id)) {
64 lua_settop(Luas, s_top);
65 return;
66 }
67 /*tex We make sure we have no prev */
68 alink(start_node) = null ;
69 /*tex the action */
70 nodelist_to_lua(Luas, start_node);
71 lua_push_group_code(Luas,extrainfo);
72 if ((i=lua_pcall(Luas, 2, 1, 0)) != 0) {
73 formatted_warning("node filter", "error: %s\n", lua_tostring(Luas, -1));
74 lua_settop(Luas, s_top);
75 luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
76 return;
77 }
78 /*tex the result */
79 if (lua_isboolean(Luas, -1)) {
80 if (lua_toboolean(Luas, -1) != 1) {
81 /*tex discard */
82 flush_node_list(start_node);
83 vlink(head_node) = null;
84 } else {
85 /*tex keep */
86 }
87 } else {
88 /*tex append to old head */
89 start_done = nodelist_from_lua(Luas,-1);
90 try_couple_nodes(head_node,start_done);
91 }
92 /*tex redundant as we set top anyway */
93 lua_pop(Luas, 2);
94 /*tex find tail in order to update tail */
95 start_node = vlink(head_node);
96 if (start_node != null) {
97 /*tex maybe just always slide (harmless and fast) */
98 if (fix_node_lists) {
99 /*tex slides and returns last node */
100 *tail_node = fix_node_list(start_node);
101 } else {
102 last_node = vlink(start_node);
103 while (last_node != null) {
104 start_node = last_node;
105 last_node = vlink(start_node);
106 }
107 /*tex we're at the end now */
108 *tail_node = start_node;
109 }
110 } else {
111 /*tex we're already at the end */
112 *tail_node = head_node;
113 }
114 /*tex clean up */
115 lua_settop(Luas, s_top);
116 return;
117 }
118
119 int lua_linebreak_callback(int is_broken, halfword head_node, halfword * new_head)
120 {
121 int a, i;
122 register halfword *p;
123 int ret = 0;
124 int s_top = lua_gettop(Luas);
125 int callback_id = callback_defined(linebreak_filter_callback);
126 if (head_node == null || vlink(head_node) == null || callback_id <= 0) {
127 lua_settop(Luas, s_top);
128 return ret;
129 }
130 if (!get_callback(Luas, callback_id)) {
131 lua_settop(Luas, s_top);
132 return ret;
133 }
134 alink(vlink(head_node)) = null ;
135 nodelist_to_lua(Luas, vlink(head_node));
136 lua_pushboolean(Luas, is_broken);
137 if ((i=lua_pcall(Luas, 2, 1, 0)) != 0) {
138 formatted_warning("linebreak", "error: %s", lua_tostring(Luas, -1));
139 lua_settop(Luas, s_top);
140 luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
141 return ret;
142 }
143 /*tex This should prevent a stack overflow (see r6689), */
144 /*tex but as side effect it discards the ouput */
145 /*tex of the linebreak_filter, see [Dev-luatex] linebreak_filter */
146 /*tex lua_settop(Luas, s_top);*/
147 p = lua_touserdata(Luas, -1);
148 if (p != NULL) {
149 a = nodelist_from_lua(Luas,-1);
150 try_couple_nodes(*new_head,a);
151 ret = 1;
152 }
153 /*tex re-inserted this line */
154 lua_settop(Luas, s_top);
155 return ret;
156 }
157
158 int lua_appendtovlist_callback(halfword box, int location, halfword prev_depth,
159 boolean is_mirrored, halfword * result, int * next_depth, boolean * prev_set)
160 {
161 register halfword *p;
162 int i;
163 int s_top = lua_gettop(Luas);
164 int callback_id = callback_defined(append_to_vlist_filter_callback);
165 if (box == null || callback_id <= 0) {
166 lua_settop(Luas, s_top);
167 return 0;
168 }
169 if (!get_callback(Luas, callback_id)) {
170 lua_settop(Luas, s_top);
171 return 0;
172 }
173 nodelist_to_lua(Luas, box);
174 lua_push_string_by_index(Luas,location);
175 lua_pushinteger(Luas, (int) prev_depth);
176 lua_pushboolean(Luas, is_mirrored);
177 if ((i=lua_pcall(Luas, 4, 2, 0)) != 0) {
178 formatted_warning("append to vlist","error: %s", lua_tostring(Luas, -1));
179 lua_settop(Luas, s_top);
180 luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
181 return 0;
182 }
183 if (lua_type(Luas, -2) == LUA_TUSERDATA) {
184 p = check_isnode(Luas, -2);
185 *result = *p;
186 } else if (lua_type(Luas, -2) == LUA_TNIL) {
187 *result = null;
188 } else {
189 normal_warning("append to vlist","error: node or nil expected");
190 }
191 if (lua_type(Luas, -1) == LUA_TNUMBER) {
192 *next_depth = lua_roundnumber(Luas, -1);
193 *prev_set = 1;
194 }
195 lua_settop(Luas, s_top);
196 return 1;
197 }
198
199 halfword lua_hpack_filter(halfword head_node, scaled size, int pack_type, int extrainfo,
200 int pack_direction, halfword attr)
201 {
202 int i;
203 halfword ret;
204 int s_top = lua_gettop(Luas);
205 int callback_id = callback_defined(hpack_filter_callback);
206 if (head_node == null || callback_id <= 0) {
207 lua_settop(Luas, s_top);
208 return head_node;
209 }
210 if (!get_callback(Luas, callback_id)) {
211 lua_settop(Luas, s_top);
212 return head_node;
213 }
214 alink(head_node) = null ;
215 nodelist_to_lua(Luas, head_node);
216 lua_push_group_code(Luas,extrainfo);
217 lua_pushinteger(Luas, size);
218 lua_push_pack_type(Luas, pack_type);
219 if (pack_direction >= 0) {
220 lua_push_dir_par(Luas, pack_direction);
221 } else {
222 lua_pushnil(Luas);
223 }
224 if (attr != null) {
225 nodelist_to_lua(Luas, attr);
226 } else {
227 lua_pushnil(Luas);
228 }
229 if ((i=lua_pcall(Luas, 6, 1, 0)) != 0) {
230 formatted_warning("hpack filter", "error: %s\n", lua_tostring(Luas, -1));
231 lua_settop(Luas, s_top);
232 luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
233 return head_node;
234 }
235 ret = head_node;
236 if (lua_isboolean(Luas, -1)) {
237 if (lua_toboolean(Luas, -1) != 1) {
238 flush_node_list(head_node);
239 ret = null;
240 }
241 } else {
242 ret = nodelist_from_lua(Luas,-1);
243 }
244 lua_settop(Luas, s_top);
245 if (fix_node_lists)
246 fix_node_list(ret);
247 return ret;
248 }
249
250 halfword lua_vpack_filter(halfword head_node, scaled size, int pack_type, scaled maxd,
251 int extrainfo, int pack_direction, halfword attr)
252 {
253 halfword ret;
254 int i;
255 int callback_id;
256 int s_top = lua_gettop(Luas);
257 if (head_node == null) {
258 lua_settop(Luas, s_top);
259 return head_node;
260 }
261 if (extrainfo == 8) {
262 callback_id = callback_defined(pre_output_filter_callback);
263 } else {
264 callback_id = callback_defined(vpack_filter_callback);
265 }
266 if (callback_id <= 0) {
267 lua_settop(Luas, s_top);
268 return head_node;
269 }
270 if (!get_callback(Luas, callback_id)) {
271 lua_settop(Luas, s_top);
272 return head_node;
273 }
274 alink(head_node) = null ;
275 nodelist_to_lua(Luas, head_node);
276 lua_push_group_code(Luas, extrainfo);
277 lua_pushinteger(Luas, size);
278 lua_push_pack_type(Luas, pack_type);
279 lua_pushinteger(Luas, maxd);
280 if (pack_direction >= 0) {
281 lua_push_dir_par(Luas, pack_direction);
282 } else {
283 lua_pushnil(Luas);
284 }
285 if (attr != null) {
286 nodelist_to_lua(Luas, attr);
287 } else {
288 lua_pushnil(Luas);
289 }
290 if ((i=lua_pcall(Luas, 7, 1, 0)) != 0) {
291 formatted_warning("vpack filter", "error: %s", lua_tostring(Luas, -1));
292 lua_settop(Luas, s_top);
293 luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
294 return head_node;
295 }
296 ret = head_node;
297 if (lua_isboolean(Luas, -1)) {
298 if (lua_toboolean(Luas, -1) != 1) {
299 flush_node_list(head_node);
300 ret = null;
301 }
302 } else {
303 ret = nodelist_from_lua(Luas,-1);
304 }
305 lua_settop(Luas, s_top);
306 if (fix_node_lists)
307 fix_node_list(ret);
308 return ret;
309 }
310
311 /*tex
312
313 This is a quick hack to fix \ETEX's \.{\\lastnodetype} now that there are many
314 more visible node types.
315
316 */
317
318 int visible_last_node_type(int n)
319 {
320 int i = type(n);
321 if (i != glyph_node) {
322 return get_etex_code(i);
323 } else if (is_ligature(n)) {
324 /*tex old ligature value */
325 return 7;
326 } else {
327 /*tex old character value */
328 return 0;
329 }
330 }
331
332 void lua_pdf_literal(PDF pdf, int i, int noline)
333 {
334 const char *s = NULL;
335 size_t l = 0;
336 lua_rawgeti(Luas, LUA_REGISTRYINDEX, i);
337 s = lua_tolstring(Luas, -1, &l);
338 if (noline) {
339 pdf_check_space(pdf);
340 pdf_out_block(pdf, s, l);
341 pdf_set_space(pdf);
342 } else {
343 pdf_out_block(pdf, s, l);
344 pdf_out(pdf, 10);
345 }
346 lua_pop(Luas, 1);
347 }
348
349 void copy_pdf_literal(pointer r, pointer p)
350 {
351 int t = pdf_literal_type(p);
352 pdf_literal_type(r) = t;
353 pdf_literal_mode(r) = pdf_literal_mode(p);
354 if (t == normal) {
355 pdf_literal_data(r) = pdf_literal_data(p);
356 add_token_ref(pdf_literal_data(p));
357 } else if (t == lua_refid_literal) {
358 lua_rawgeti(Luas, LUA_REGISTRYINDEX, pdf_literal_data(p));
359 pdf_literal_data(r) = luaL_ref(Luas, LUA_REGISTRYINDEX);
360 } else {
361 /* maybe something user, we don't support a call here but best keep it sane anyway. */
362 pdf_literal_data(r) = pdf_literal_data(p);
363 }
364 }
365
366 void copy_late_lua(pointer r, pointer p)
367 {
368 int t = late_lua_type(p);
369 late_lua_type(r) = t;
370 if (late_lua_name(p) > 0)
371 add_token_ref(late_lua_name(p));
372 if (t == normal) {
373 late_lua_data(r) = late_lua_data(p);
374 add_token_ref(late_lua_data(p));
375 } else if (t == lua_refid_literal) {
376 lua_rawgeti(Luas, LUA_REGISTRYINDEX, late_lua_data(p));
377 late_lua_data(r) = luaL_ref(Luas, LUA_REGISTRYINDEX);
378 }
379 }
380
381 void copy_user_lua(pointer r, pointer p)
382 {
383 if (user_node_value(p) != 0) {
384 lua_rawgeti(Luas, LUA_REGISTRYINDEX, user_node_value(p));
385 user_node_value(r) = luaL_ref(Luas, LUA_REGISTRYINDEX);
386 }
387 }
388
389 void free_pdf_literal(pointer p)
390 {
391 int t = pdf_literal_type(p);
392 if (t == normal) {
393 delete_token_ref(pdf_literal_data(p));
394 } else if (t == lua_refid_literal) {
395 luaL_unref(Luas, LUA_REGISTRYINDEX, pdf_literal_data(p));
396 }
397 }
398
399 void free_late_lua(pointer p)
400 {
401 int t = late_lua_type(p);
402 if (late_lua_name(p) > 0)
403 delete_token_ref(late_lua_name(p));
404 if (t == normal) {
405 delete_token_ref(late_lua_data(p));
406 } else if (t == lua_refid_literal) {
407 luaL_unref(Luas, LUA_REGISTRYINDEX, late_lua_data(p));
408 }
409 }
410
411 void free_user_lua(pointer p)
412 {
413 if (user_node_value(p) != 0) {
414 luaL_unref(Luas, LUA_REGISTRYINDEX, user_node_value(p));
415 }
416 }
417
418 void show_pdf_literal(pointer p)
419 {
420 int t = pdf_literal_type(p);
421 if (subtype(p)==pdf_late_literal_node)
422 tprint_esc("pdflateliteral");
423 else
424 tprint_esc("pdfliteral");
425 switch (pdf_literal_mode(p)) {
426 case set_origin:
427 tprint(" origin");
428 break;
429 case direct_page:
430 tprint(" page");
431 break;
432 case direct_always:
433 tprint(" direct");
434 break;
435 case direct_raw:
436 tprint(" raw");
437 break;
438 default:
439 tprint(" <invalid mode>");
440 break;
441 }
442 if (t == normal) {
443 print_mark(pdf_literal_data(p));
444 } else if (t == lua_refid_literal) {
445 tprint(" <lua data reference ");
446 print_int(pdf_literal_data(p));
447 tprint(">");
448 } else {
449 tprint(" <invalid data>");
450 }
451 }
452
453 void show_late_lua(pointer p)
454 {
455 int t = late_lua_type(p);
456 tprint_esc("latelua");
457 print_int(late_lua_reg(p));
458 if (t == normal) {
459 print_mark(late_lua_data(p));
460 } else if (t == lua_refid_literal) {
461 tprint(" <function reference ");
462 print_int(late_lua_data(p));
463 tprint(">");
464 } else if (t == lua_refid_call) {
465 tprint(" <functioncall reference ");
466 print_int(late_lua_data(p));
467 tprint(">");
468 } else {
469 tprint(" <invalid data>");
470 }
471 }