Debian Codegraph @

texk/web2c/luatexdir/lua/lnodelib.c

raw
1 /* lnodelib.c
2
3 Copyright 2006-2013 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 /*
23
24 After doing lots of tests with luatex and luajittex, with and without jit,
25 and with and without ffi, we came to the conclusion that userdata prevents
26 a speedup. We also found that the checking of metatables as well as assignment
27 comes with overhead that can't be neglected. This is normally not really a
28 problem but when processing fonts for more complex scripts it's quite some
29 overhead.
30
31 Because the userdata approach has some benefits, we stick to this. We did
32 some experiments with fast access (assuming nodes), but eventually settled for
33 the direct approach. For code that is proven to be okay, one can use the
34 direct variants and operate on nodes more directly. Currently these are
35 numbers but don't rely on that property; treat them as abstractions. An
36 important aspect is that one cannot mix both methods, although with
37 node.direct.tonode and node.direct.todirect one can cast representations.
38
39 So the advice is: use the indexed approach when possible and investigate the
40 direct one when speed might be an issue. For that reason we also provide some
41 get* and set* functions in the top level node namespace. There is a limited set
42 of getters for nodes and a generic getfield to complement them. The direct
43 namespace has a few more.
44
45 Keep in mind that such speed considerations only make sense when we're accessing
46 nodes millions of times (which happens in font processing for instance). Setters
47 are less important as documents have not that many content related nodes and
48 setting many thousands of properties is hardly a burden contrary to millions of
49 consultations. And with millions, we're talking of tens of millions which is not
50 that common.
51
52 Another change is that __index and __newindex are (as expected) exposed to
53 users but do no checking. The getfield and setfield functions do check. In
54 fact, a fast mode can be simulated by fast_getfield = __index but the (measured)
55 benefit on average runs is not that large (some 5% when we also use the other
56 fast ones) which is easily nilled by inefficient coding. The direct variants
57 on the other hand can be significantly faster but with the drawback of lack
58 of userdata features. With respect to speed: keep in mind that measuring
59 a speedup on these functions is not representative for a normal run, where
60 much more happens.
61
62 */
63
64 #include "ptexlib.h"
65 #include "lua/luatex-api.h"
66 #ifdef LuajitTeX
67 #include "lua/lauxlib_bridge.h"
68 #else
69 #include "lauxlib.h"
70 #endif
71
72 /*
73
74 These macros create and access pointers (indices) to keys which is faster. The
75 shortcuts are created as part of the initialization.
76
77 */
78
79 /*
80
81 When the first argument to an accessor is a node, we can use it's metatable
82 entry when we are returning nodes, which saves a lookup.
83
84 */
85
86 #define fast_metatable(n) do { \
87 a = (halfword *) lua_newuserdata(L, sizeof(halfword)); \
88 *a = n; \
89 lua_getmetatable(L,1); \
90 lua_setmetatable(L,-2); \
91 } while (0)
92
93 #define fast_metatable_or_nil(n) do { \
94 if (n) { \
95 a = (halfword *) lua_newuserdata(L, sizeof(halfword)); \
96 *a = n; \
97 lua_getmetatable(L,1); \
98 lua_setmetatable(L,-2); \
99 } else { \
100 lua_pushnil(L); \
101 } \
102 } while (0)
103
104 #define fast_metatable_or_nil_alink(n) do { \
105 if (n) { \
106 alink(n) = null; \
107 a = (halfword *) lua_newuserdata(L, sizeof(halfword)); \
108 *a = n; \
109 lua_getmetatable(L,1); \
110 lua_setmetatable(L,-2); \
111 } else { \
112 lua_pushnil(L); \
113 } \
114 } while (0)
115
116 #define fast_metatable_top(n) do { \
117 a = (halfword *) lua_newuserdata(L, sizeof(halfword)); \
118 *a = n; \
119 lua_getmetatable(L,-2); \
120 lua_setmetatable(L,-2); \
121 } while (0)
122
123 /*
124
125 This is a first step towards abstract direct nodes. When we have Lua 5.3 we
126 need to check all returned values for being integers. This might be another
127 level of abtraction.
128
129 */
130
131 #define nodelib_pushdirect(n) lua_pushinteger(L,n)
132 #define nodelib_popdirect(n) lua_tointeger(L,n)
133
134 #define nodelib_pushdirect_or_nil(n) do { \
135 if (n==null) { \
136 lua_pushnil(L); \
137 } else { \
138 lua_pushinteger(L,n); \
139 } \
140 } while (0)
141
142 #define nodelib_pushdirect_or_nil_alink(n) do { \
143 if (n==null) { \
144 lua_pushnil(L); \
145 } else { \
146 alink(n) = null; \
147 lua_pushinteger(L,n); \
148 } \
149 } while (0)
150
151 #define nodelib_setattr(L, s, n) reassign_attribute(n,nodelib_getlist(L,s))
152
153 #define nodelib_gettoks(L,a) tokenlist_from_lua(L, a)
154
155 #define nodelib_getspec nodelib_getlist
156 #define nodelib_getaction nodelib_getlist
157
158 /* fetching a field from a node .. we can often use the reuse bot-of-stack metatable */
159
160 #define nodelib_pushlist(L,n) { lua_pushinteger(L,n); lua_nodelib_push(L); } /* can be: fast_metatable_or_nil(n) */
161 #define nodelib_pushattr(L,n) { lua_pushinteger(L,n); lua_nodelib_push(L); } /* can be: fast_metatable_or_nil(n) */
162 #define nodelib_pushaction(L,n) { lua_pushinteger(L,n); lua_nodelib_push(L); } /* can be: fast_metatable_or_nil(n) */
163 #define nodelib_pushstring(L,n) { char *ss=makecstring(n); lua_pushstring(L,ss); free(ss); }
164
165 /* find prev, and fix backlinks .. can be a macro instead (only used a few times) */
166
167 #define set_t_to_prev(head,current) \
168 t = head; \
169 while (vlink(t)!=current && t != null) { \
170 if (vlink(t)!=null) \
171 alink(vlink(t)) = t; \
172 t = vlink(t); \
173 }
174
175 #define direct_check_index_range(j,s) \
176 if (j<0 || j > 65535) { \
177 luaL_error(L, "incorrect index value %d for tex.%s()", (int)j, s); \
178 }
179
180 #define NODE_METATABLE "luatex.node"
181
182 #define DEBUG 0
183 #define DEBUG_OUT stdout
184
185 /* maybe these qualify as macros, not functions */
186
187 static halfword *maybe_isnode(lua_State * L, int i)
188 {
189 halfword *p = lua_touserdata(L, i);
190 if (p != NULL) {
191 if (lua_getmetatable(L, i)) {
192 lua_get_metatablelua(luatex_node);
193 if (!lua_rawequal(L, -1, -2))
194 p = NULL;
195 lua_pop(L, 2);
196 }
197 }
198 return p;
199 }
200
201 /* we could make the message a function and just inline the rest (via a macro) */
202
203 halfword *check_isnode(lua_State * L, int i)
204 {
205 halfword *p = maybe_isnode(L, i);
206 if (p != NULL)
207 return p;
208 formatted_error("node lib","lua <node> expected, not an object with type %s", luaL_typename(L, i));
209 return NULL;
210 }
211
212 static int nodelib_setdir_text(lua_State * L, int i, halfword n)
213 {
214 if (lua_type(L, i) == LUA_TSTRING) {
215 const char *s = lua_tostring(L, i);
216 if (s==lua_key_plus(TLT)) {
217 dir_dir(n) = 0 ;
218 subtype(n) = normal_dir;
219 } else if (s==lua_key_minus(TLT)) {
220 dir_dir(n) = 0 ;
221 subtype(n) = cancel_dir;
222 } else if (s==lua_key_plus(TRT)) {
223 dir_dir(n) = 1 ;
224 subtype(n) = normal_dir;
225 } else if (s==lua_key_minus(TRT)) {
226 dir_dir(n) = 1 ;
227 subtype(n) = cancel_dir;
228 } else if (s==lua_key_plus(LTL)) {
229 dir_dir(n) = 2 ;
230 subtype(n) = normal_dir;
231 } else if (s==lua_key_minus(LTL)) {
232 dir_dir(n) = 2 ;
233 subtype(n) = cancel_dir;
234 } else if (s==lua_key_plus(RTT)) {
235 dir_dir(n) = 3 ;
236 subtype(n) = normal_dir;
237 } else if (s==lua_key_minus(RTT)) {
238 dir_dir(n) = 3 ;
239 subtype(n) = cancel_dir;
240 } else {
241 luaL_error(L, "Bad direction specifier %s", s);
242 }
243 } else {
244 luaL_error(L, "Direction specifiers have to be strings");
245 }
246 return 0;
247 }
248
249 static int nodelib_getdir_par(lua_State * L, int n)
250 {
251 if (lua_type(L, n) == LUA_TSTRING) {
252 const char *s = lua_tostring(L, n);
253 if (s==lua_key(TLT))
254 return 0 ;
255 else if (s==lua_key(TRT))
256 return 1 ;
257 else if (s==lua_key(LTL))
258 return 2 ;
259 else if (s==lua_key(RTT))
260 return 3 ;
261 else
262 luaL_error(L, "Bad direction specifier %s", s);
263 } else {
264 luaL_error(L, "Direction specifiers have to be strings");
265 }
266 return 0;
267 }
268
269 static int nodelib_getdirection(lua_State * L, int n)
270 {
271 if (lua_type(L, n) == LUA_TNUMBER) {
272 int i = lua_tointeger(L, n);
273 check_dir_value(i);
274 return i;
275 } else {
276 luaL_error(L, "Direction specifiers have to be numbers");
277 }
278 return 0;
279 }
280
281 int nodelib_getdir(lua_State * L, int n) /* the api public one */
282 {
283 return nodelib_getdir_par(L,n);
284 }
285
286 /*
287
288 This routine finds the numerical value of a string (or number) at
289 lua stack index |n|. If it is not a valid node type, returns -1
290
291 */
292
293 /* s = lua_tostring(L, 2); */
294 /* if (lua_key_eq(s, id)) { */
295
296 static int get_node_type_id_from_name(lua_State * L, int n, node_info * data)
297 {
298 if (data != NULL) {
299 int j;
300 const char *s = lua_tostring(L, n);
301 for (j = 0; data[j].id != -1; j++) {
302 if (s == data[j].name) {
303 return j;
304 }
305 }
306 }
307 return -1;
308 }
309
310 static int get_node_subtype_id_from_name(lua_State * L, int n, subtype_info * data)
311 {
312 if (data != NULL) {
313 int j;
314 const char *s = lua_tostring(L, n);
315 for (j = 0; data[j].id != -1; j++) {
316 if (s == data[j].name) {
317 return j;
318 }
319 }
320 }
321 return -1;
322 }
323
324 static int get_valid_node_type_id(lua_State * L, int n)
325 {
326 int i = -1;
327 int t = lua_type(L, n);
328 if (t == LUA_TSTRING) {
329 i = get_node_type_id_from_name(L,n,node_data);
330 if (i < 0) {
331 luaL_error(L, "invalid node type id: %s", lua_tostring(L, n));
332 }
333 } else if (t == LUA_TNUMBER) {
334 i = lua_tointeger(L,n);
335 if (! known_node_type(i)) {
336 luaL_error(L, "invalid node type id: %d", i);
337 }
338 } else {
339 luaL_error(L, "invalid node type id");
340 }
341 return i;
342 }
343
344 static int get_valid_node_subtype_id(lua_State * L, int n)
345 {
346 int i = -1;
347 int t = lua_type(L, n);
348 if (t == LUA_TSTRING) {
349 i = get_node_type_id_from_name(L,n,whatsit_node_data);
350 if (i<0) {
351 luaL_error(L, "invalid whatsit type id: %s", lua_tostring(L, n));
352 }
353 } else if (t == LUA_TNUMBER) {
354 i = lua_tointeger(L,n);
355 if (! known_whatsit_type(i)) {
356 luaL_error(L, "invalid whatsit type id: %d", i);
357 }
358 } else {
359 luaL_error(L, "invalid whatsit type id");
360 }
361 return i;
362 }
363
364 /* enable fixer ... more fixers can be added in the future */
365
366 static int lua_nodelib_fix_node_lists(lua_State * L)
367 {
368 if (lua_type(L, -1) == LUA_TBOOLEAN) {
369 fix_node_lists = lua_toboolean(L,-1);
370 } else if (lua_type(L, -1) == LUA_TNUMBER) {
371 fix_node_lists = lua_tointeger(L,-1);
372 }
373 return 0;
374 }
375
376
377 /* two simple helpers to speed up and simplify lua code (replaced by getnext and getprev) */
378
379 static int lua_nodelib_next(lua_State * L)
380 {
381 halfword *p = maybe_isnode(L,1);
382 if (p != NULL && *p && vlink(*p)) {
383 lua_nodelib_push_fast(L,vlink(*p));
384 } else {
385 lua_pushnil(L);
386 }
387 return 1;
388 }
389
390 static int lua_nodelib_prev(lua_State * L)
391 {
392 halfword *p = maybe_isnode(L,1);
393 if (p != NULL && *p && alink(*p)) {
394 lua_nodelib_push_fast(L,alink(*p));
395 } else {
396 lua_pushnil(L);
397 }
398 return 1;
399 }
400
401 /*
402
403 Creates a userdata object for a number found at the stack top, if it is
404 representing a node (i.e. an pointer into |varmem|). It replaces the
405 stack entry with the new userdata, or pushes |nil| if the number is |null|,
406 or if the index is definately out of range. This test could be improved.
407
408 */
409
410 void lua_nodelib_push(lua_State * L)
411 {
412 halfword n;
413 halfword *a;
414 n = -1;
415 if (lua_type(L, -1) == LUA_TNUMBER)
416 n = (int) lua_tointeger(L, -1);
417 lua_pop(L, 1);
418 if ((n == null) || (n < 0) || (n > var_mem_max)) {
419 lua_pushnil(L);
420 } else {
421 a = lua_newuserdata(L, sizeof(halfword));
422 *a = n;
423 lua_get_metatablelua(luatex_node);
424 lua_setmetatable(L, -2);
425 }
426 return;
427 }
428
429 void lua_nodelib_push_fast(lua_State * L, halfword n)
430 {
431 halfword *a;
432 if (n) {
433 a = lua_newuserdata(L, sizeof(halfword));
434 *a = n;
435 lua_get_metatablelua(luatex_node);
436 lua_setmetatable(L, -2);
437 } else {
438 lua_pushnil(L);
439 }
440 return;
441 }
442
443 /* getting and setting fields (helpers) */
444
445 int nodelib_getlist(lua_State * L, int n)
446 {
447 if (lua_isuserdata(L, n)) {
448 halfword m = *check_isnode(L, n);
449 return m;
450 } else {
451 return null;
452 }
453 }
454
455 static str_number nodelib_getstring(lua_State * L, int a)
456 {
457 size_t k;
458 const char *s = lua_tolstring(L, a, &k);
459 return maketexlstring(s, k);
460 }
461
462 static int nodelib_cantset(lua_State * L, int n, const char *s)
463 {
464 luaL_error(L,"You cannot set field %s in a node of type %s",s,node_data[type(n)].name);
465 return 0;
466 }
467
468 /* converts type strings to type ids */
469
470 static int lua_nodelib_id(lua_State * L)
471 {
472 if (lua_type(L,1) == LUA_TSTRING) {
473 int i = get_node_type_id_from_name(L, 1, node_data);
474 if (i >= 0) {
475 lua_pushinteger(L, i);
476 } else {
477 lua_pushnil(L);
478 }
479 } else {
480 lua_pushnil(L);
481 }
482 return 1;
483 }
484
485 /* node.direct.getid */
486
487 static int lua_nodelib_direct_getid(lua_State * L)
488 {
489 halfword n = lua_tointeger(L, 1);
490 if (n) {
491 lua_pushinteger(L, type(n));
492 } else {
493 lua_pushnil(L);
494 }
495 return 1;
496 }
497
498 /* node.getid */
499
500 static int lua_nodelib_getid(lua_State * L)
501 {
502 /* [given-node] [...] */
503 halfword *p = lua_touserdata(L, 1);
504 if ( (p == NULL) || (! lua_getmetatable(L,1)) ) {
505 lua_pushnil(L);
506 return 1;
507 }
508 /* [given-node] [mt-given-node] */
509 lua_get_metatablelua(luatex_node);
510 /* [given-node] [mt-given-node] [mt-node] */
511 if (!lua_rawequal(L, -1, -2)) {
512 lua_pushnil(L);
513 } else {
514 lua_pushinteger(L, type(*p));
515 }
516 return 1;
517 }
518
519 /* node.direct.getsubtype */
520 /* node.direct.setsubtype */
521
522 static int lua_nodelib_direct_getsubtype(lua_State * L)
523 {
524 halfword n = lua_tointeger(L, 1);
525 if (n) {
526 lua_pushinteger(L, subtype(n));
527 } else {
528 lua_pushnil(L);
529 }
530 return 1;
531 }
532
533 static int lua_nodelib_direct_setsubtype(lua_State * L)
534 {
535 halfword n = lua_tointeger(L, 1);
536 if ((n) && (lua_type(L,2) == LUA_TNUMBER)) {
537 subtype(n) = (halfword) lua_tointeger(L, 2);
538 }
539 return 0;
540 }
541
542 /* node.getsubtype */
543
544 static int lua_nodelib_getsubtype(lua_State * L)
545 {
546 halfword *p = lua_touserdata(L, 1);
547 if ( (p == NULL) || (! lua_getmetatable(L,1)) ) {
548 lua_pushnil(L);
549 } else {
550 lua_get_metatablelua(luatex_node);
551 if ( (!lua_rawequal(L, -1, -2)) || (! nodetype_has_subtype(*p))) {
552 lua_pushnil(L);
553 } else {
554 lua_pushinteger(L, subtype(*p));
555 }
556 }
557 return 1;
558 }
559
560 /* node.direct.getexpansion */
561 /* node.direct.setexpansion */
562
563 static int lua_nodelib_direct_getexpansion(lua_State * L)
564 {
565 halfword n = lua_tointeger(L, 1);
566 if (n != null) {
567 halfword t = type(n);
568 if (t == glyph_node) {
569 lua_pushinteger(L, ex_glyph(n));
570 return 1;
571 } else if (t == kern_node) {
572 lua_pushinteger(L, ex_kern(n));
573 return 1;
574 }
575 }
576 lua_pushnil(L);
577 return 1;
578 }
579
580 static int lua_nodelib_direct_setexpansion(lua_State * L)
581 {
582 halfword n = lua_tointeger(L, 1);
583 if (n) {
584 halfword t = type(n);
585 halfword e = 0;
586 if (lua_type(L, 2) == LUA_TNUMBER) {
587 e = (halfword) lua_tointeger(L, 2);
588 }
589 if (t == glyph_node) {
590 ex_glyph(n) = e;
591 } else if ( t == kern_node) {
592 ex_kern(n) = e;
593 }
594 }
595 return 0;
596 }
597
598 /* node.direct.getfont */
599 /* node.direct.setfont */
600
601 static int lua_nodelib_direct_getfont(lua_State * L)
602 {
603 halfword n = lua_tointeger(L, 1);
604 if (n != null) {
605 halfword t = type(n);
606 if (t == glyph_node) {
607 lua_pushinteger(L, font(n));
608 } else if ((t == math_char_node) || (t == math_text_char_node)) {
609 lua_pushinteger(L, fam_fnt(math_fam(n), 0));
610 } else if (t == delim_node) {
611 lua_pushinteger(L, fam_fnt(small_fam(n), 0));
612 } else {
613 lua_pushnil(L);
614 }
615 } else {
616 lua_pushnil(L);
617 }
618 return 1;
619 }
620
621 static int lua_nodelib_direct_setfont(lua_State * L)
622 {
623 halfword n = lua_tointeger(L, 1);
624 if ((n) && type(n) == glyph_node) {
625 font(n) = (halfword) lua_tointeger(L,2);
626 /* optional char */
627 if ((lua_type(L, 3) == LUA_TNUMBER)) {
628 character(n) = (halfword) lua_tointeger(L, 3);
629 }
630 }
631 return 0;
632 }
633
634 /* node.getfont */
635
636 static int lua_nodelib_getfont(lua_State * L)
637 {
638 halfword *n = lua_touserdata(L, 1);
639 if ((n == NULL) || (! lua_getmetatable(L,1)) ) {
640 lua_pushnil(L);
641 } else {
642 halfword t = type(*n);
643 if (t == glyph_node) {
644 lua_pushinteger(L, font(*n));
645 } else if ((t == math_char_node) || (t == math_text_char_node)) {
646 lua_pushinteger(L, fam_fnt(math_fam(*n), 0));
647 } else if (t == delim_node) {
648 lua_pushinteger(L, fam_fnt(small_fam(*n), 0));
649 } else {
650 lua_pushnil(L);
651 }
652 }
653 return 1;
654 }
655
656 /* node.direct.getchar */
657 /* node.direct.setchar */
658
659 static int lua_nodelib_direct_getchar(lua_State * L)
660 {
661 halfword n = lua_tointeger(L, 1);
662 if (n) {
663 halfword t = type(n);
664 if (t == glyph_node) {
665 lua_pushinteger(L, character(n));
666 } else if ((t == math_char_node) || (t == math_text_char_node)) {
667 lua_pushinteger(L, math_character(n));
668 } else if (t == delim_node) {
669 /* used in wide fonts */
670 lua_pushinteger(L, small_char(n));
671 } else {
672 lua_pushnil(L);
673 }
674 } else {
675 lua_pushnil(L);
676 }
677 return 1;
678 }
679
680 static int lua_nodelib_direct_getfam(lua_State * L)
681 {
682 halfword n = lua_tointeger(L, 1);
683 if (n) {
684 halfword t = type(n);
685 if ((t == math_char_node) || (t == math_text_char_node)) {
686 lua_pushinteger(L, math_fam(n));
687 } else if (t == delim_node) {
688 lua_pushinteger(L, small_fam(n));
689 } else if (t == fraction_noad) {
690 lua_pushinteger(L, fraction_fam(n));
691 } else if (t == simple_noad) {
692 lua_pushinteger(L, noad_fam(n));
693 } else {
694 lua_pushnil(L);
695 }
696 } else {
697 lua_pushnil(L);
698 }
699 return 1;
700 }
701
702 static int lua_nodelib_direct_setchar(lua_State * L)
703 {
704 halfword n = lua_tointeger(L, 1);
705 if ((n) && (lua_type(L, 2) == LUA_TNUMBER)) {
706 halfword t = type(n);
707 if (t == glyph_node) {
708 character(n) = (halfword) lua_tointeger(L, 2);
709 } else if ((t == math_char_node) || (t == math_text_char_node)) {
710 math_character(n) = (halfword) lua_tointeger(L, 2);
711 } else if (t == delim_node) {
712 /* used in wide fonts */
713 small_char(n) = (halfword) lua_tointeger(L, 2);
714 }
715 }
716 return 0;
717 }
718
719 static int lua_nodelib_direct_setfam(lua_State * L)
720 {
721 halfword n = lua_tointeger(L, 1);
722 if ((n) && (lua_type(L, 2) == LUA_TNUMBER)) {
723 halfword t = type(n);
724 if ((t == math_char_node) || (t == math_text_char_node)) {
725 math_fam(n) = (halfword) lua_tointeger(L, 2);
726 } else if (t == delim_node) {
727 small_fam(n) = (halfword) lua_tointeger(L, 2);
728 } else if (t == fraction_noad) {
729 fraction_fam(n) = (halfword) lua_tointeger(L, 2);
730 } else if (t == simple_noad) {
731 noad_fam(n) = (halfword) lua_tointeger(L, 2);
732 }
733 }
734 return 0;
735 }
736
737 /* node.getchar */
738
739 static int lua_nodelib_getchar(lua_State * L)
740 {
741 halfword *n = lua_touserdata(L, 1);
742 if ((n == NULL) || (! lua_getmetatable(L,1))) {
743 lua_pushnil(L);
744 } else {
745 halfword t = type(*n);
746 if (t == glyph_node) {
747 lua_pushinteger(L, character(*n));
748 } else if ((t == math_char_node) || (t == math_text_char_node)) {
749 lua_pushinteger(L, math_character(*n));
750 } else if (t == delim_node) {
751 /* used in wide fonts */
752 lua_pushinteger(L, small_char(*n));
753 } else {
754 lua_pushnil(L);
755 }
756 }
757 return 1;
758 }
759
760 /* node.direct.getcomponents */
761 /* node.direct.setcomponents */
762
763 static int lua_nodelib_direct_getcomponents(lua_State * L)
764 {
765 halfword n = lua_tointeger(L, 1);
766 if ((n) && (type(n) == glyph_node)) {
767 nodelib_pushdirect_or_nil(lig_ptr(n));
768 } else {
769 lua_pushnil(L);
770 }
771 return 1;
772 }
773
774 static int lua_nodelib_direct_setcomponents(lua_State * L)
775 {
776 halfword n = lua_tointeger(L, 1);
777 if ((n) && (type(n) == glyph_node)) {
778 if (lua_type(L, 2) == LUA_TNUMBER) {
779 lig_ptr(n) = (halfword) lua_tointeger(L, 2);
780 } else {
781 lig_ptr(n) = null;
782 }
783 }
784 return 0;
785 }
786
787 /* node.direct.getlang */
788 /* node.direct.setlang */
789
790 static int lua_nodelib_direct_getlang(lua_State * L)
791 {
792 halfword n = lua_tointeger(L, 1);
793 if ((n) && (type(n) == glyph_node)) {
794 lua_pushinteger(L, char_lang(n));
795 } else {
796 lua_pushnil(L);
797 }
798 return 1;
799 }
800
801 static int lua_nodelib_direct_setlang(lua_State * L)
802 {
803 halfword n = lua_tointeger(L, 1);
804 if ((n) && (type(n) == glyph_node)) {
805 if (lua_type(L, 2) == LUA_TNUMBER) {
806 set_char_lang(n,lua_tointeger(L, 2));
807 } else {
808 /* nothing */
809 }
810 }
811 return 0;
812 }
813
814 /* node.direct.getattributelist */
815 /* node.direct.setattributelist */
816
817 static int lua_nodelib_direct_getattributelist(lua_State * L)
818 {
819 halfword n = lua_tointeger(L, 1);
820 if ((n) && nodetype_has_attributes(type(n)) && node_attr(n) != null) {
821 lua_pushinteger(L, node_attr(n));
822 } else {
823 lua_pushnil(L);
824 }
825 return 1;
826 }
827
828 static int lua_nodelib_direct_setattributelist(lua_State * L)
829 {
830 halfword n = lua_tointeger(L, 1);
831 if ((n) && nodetype_has_attributes(type(n))) {
832 int t = lua_type(L, 2);
833 if (t == LUA_TNUMBER) {
834 halfword a =lua_tointeger(L, 2);
835 if (type(a) == attribute_list_node) {
836 reassign_attribute(n,a);
837 } else if (nodetype_has_attributes(type(a))) {
838 reassign_attribute(n,node_attr(a));
839 } else {
840 reassign_attribute(n,null);
841 }
842 } else if (t == LUA_TBOOLEAN) {
843 if (lua_toboolean(L,2)) {
844 reassign_attribute(n,current_attribute_list());
845 } else {
846 reassign_attribute(n,null);
847 }
848 } else {
849 reassign_attribute(n,null);
850 }
851 return 0;
852 }
853 return 0;
854 }
855 /* node.direct.getpenalty */
856 /* node.direct.setpenalty */
857
858 static int lua_nodelib_direct_getpenalty(lua_State * L)
859 {
860 halfword n = lua_tointeger(L, 1);
861 if (n) {
862 halfword t = type(n);
863 if (t == penalty_node) {
864 lua_pushinteger(L, penalty(n));
865 } else if (t == disc_node) {
866 lua_pushinteger(L, disc_penalty(n));
867 } else {
868 lua_pushnil(L);
869 }
870 } else {
871 lua_pushnil(L);
872 }
873 return 1;
874 }
875
876 static int lua_nodelib_direct_setpenalty(lua_State * L)
877 {
878 halfword n = lua_tointeger(L, 1);
879 if (n) {
880 halfword t = type(n);
881 if (t == penalty_node) {
882 if (lua_type(L, 2) == LUA_TNUMBER) {
883 penalty(n) = (halfword) lua_tointeger(L, 2);
884 } else {
885 penalty(n) = 0;
886 }
887 } else if (t == disc_node) {
888 if (lua_type(L, 2) == LUA_TNUMBER) {
889 disc_penalty(n) = (halfword) lua_tointeger(L, 2);
890 } else {
891 penalty(n) = 0;
892 }
893 }
894 }
895 return 0;
896 }
897
898 /* node.direct.getnucleus */
899 /* node.direct.getsub */
900 /* node.direct.getsup */
901
902 static int lua_nodelib_direct_getnucleus(lua_State * L)
903 {
904 halfword n = lua_tointeger(L, 1);
905 if (n) {
906 halfword t = type(n);
907 if (t == simple_noad || t == accent_noad || t == radical_noad) {
908 nodelib_pushdirect_or_nil(nucleus(n));
909 return 1;
910 }
911 }
912 lua_pushnil(L);
913 return 1;
914 }
915
916 static int lua_nodelib_direct_setnucleus(lua_State * L)
917 {
918 halfword n = lua_tointeger(L, 1);
919 if (n) {
920 halfword t = type(n);
921 if (t == simple_noad || t == accent_noad || t == radical_noad) {
922 if (lua_type(L, 2) == LUA_TNUMBER) {
923 nucleus(n) = lua_tointeger(L,2);
924 } else {
925 nucleus(n) = null;
926 }
927 }
928 }
929 return 0;
930 }
931
932 static int lua_nodelib_direct_getsub(lua_State * L)
933 {
934 halfword n = lua_tointeger(L, 1);
935 if (n) {
936 halfword t = type(n);
937 if (t == simple_noad || t == accent_noad || t == radical_noad) {
938 nodelib_pushdirect_or_nil(subscr(n));
939 return 1;
940 }
941 }
942 lua_pushnil(L);
943 return 1;
944 }
945
946 static int lua_nodelib_direct_setsub(lua_State * L)
947 {
948 halfword n = lua_tointeger(L, 1);
949 if (n) {
950 halfword t = type(n);
951 if (t == simple_noad || t == accent_noad || t == radical_noad) {
952 if (lua_type(L, 2) == LUA_TNUMBER) {
953 subscr(n) = lua_tointeger(L,2);
954 } else {
955 subscr(n) = null;
956 }
957 }
958 }
959 return 0;
960 }
961
962 static int lua_nodelib_direct_getsup(lua_State * L)
963 {
964 halfword n = lua_tointeger(L, 1);
965 if (n) {
966 halfword t = type(n);
967 if (t == simple_noad || t == accent_noad || t == radical_noad) {
968 nodelib_pushdirect_or_nil(supscr(n));
969 return 1;
970 }
971 }
972 lua_pushnil(L);
973 return 1;
974 }
975
976 static int lua_nodelib_direct_setsup(lua_State * L)
977 {
978 halfword n = lua_tointeger(L, 1);
979 if (n) {
980 halfword t = type(n);
981 if (t == simple_noad || t == accent_noad || t == radical_noad) {
982 if (lua_type(L, 2) == LUA_TNUMBER) {
983 supscr(n) = lua_tointeger(L,2);
984 } else {
985 supscr(n) = null;
986 }
987 }
988 }
989 return 0;
990 }
991
992 /* node.direct.getkern (overlaps with getwidth) */
993 /* node.direct.setkern (overlaps with getwidth) */
994
995 static int lua_nodelib_direct_getkern(lua_State * L)
996 {
997 halfword n = lua_tointeger(L, 1);
998 if (n) {
999 halfword t = type(n);
1000 if (t == kern_node) {
1001 lua_pushnumber(L, width(n));
1002 if (lua_toboolean(L,2)) {
1003 lua_pushinteger(L, ex_kern(n));
1004 return 2;
1005 }
1006 } else if (t == margin_kern_node) {
1007 lua_pushinteger(L, width(n));
1008 } else if (t == math_node) {
1009 lua_pushinteger(L, surround(n));
1010 } else {
1011 lua_pushnil(L);
1012 }
1013 } else {
1014 lua_pushnil(L);
1015 }
1016 return 1;
1017 }
1018
1019 static int lua_nodelib_direct_setkern(lua_State * L)
1020 {
1021 halfword n = lua_tointeger(L, 1);
1022 if (n != null) {
1023 halfword t = type(n);
1024 if (t == kern_node || t == margin_kern_node) {
1025 if (lua_type(L, 2) == LUA_TNUMBER) {
1026 width(n) = (halfword) lua_roundnumber(L, 2);
1027 } else {
1028 width(n) = 0;
1029 }
1030 if (lua_type(L, 3) == LUA_TNUMBER) {
1031 subtype(n) = (halfword) lua_tointeger(L, 3);
1032 }
1033 } else if (t == math_node) {
1034 if (lua_type(L, 2) == LUA_TNUMBER) {
1035 surround(n) = (halfword) lua_roundnumber(L, 2);
1036 } else {
1037 surround(n) = 0;
1038 }
1039 }
1040 }
1041 return 0;
1042 }
1043
1044 /* node.direct.getdir */
1045 /* node.direct.setdir */
1046
1047 static int lua_nodelib_direct_getdir(lua_State * L)
1048 {
1049 halfword n = lua_tointeger(L, 1);
1050 if (n) {
1051 halfword t = type(n);
1052 if (t == dir_node) {
1053 lua_push_dir_text(L, dir_dir(n), subtype(n));
1054 } else if (t == hlist_node || t == vlist_node) {
1055 lua_push_dir_par(L, box_dir(n));
1056 } else if (t == rule_node) {
1057 lua_push_dir_par(L, rule_dir(n));
1058 } else if (t == local_par_node) {
1059 lua_push_dir_par(L, local_par_dir(n));
1060 } else {
1061 lua_pushnil(L);
1062 }
1063 } else {
1064 lua_pushnil(L);
1065 }
1066 return 1;
1067 }
1068
1069 static int lua_nodelib_direct_getdirection(lua_State * L)
1070 {
1071 halfword n = lua_tointeger(L, 1);
1072 if (n) {
1073 halfword t = type(n);
1074 if (t == dir_node) {
1075 lua_push_direction(L, dir_dir(n));
1076 lua_pushboolean(L, subtype(n));
1077 return 2;
1078 } else if (t == hlist_node || t == vlist_node) {
1079 lua_push_direction(L, box_dir(n));
1080 } else if (t == rule_node) {
1081 lua_push_direction(L, rule_dir(n));
1082 } else if (t == local_par_node) {
1083 lua_push_direction(L, local_par_dir(n));
1084 } else {
1085 lua_pushnil(L);
1086 }
1087 } else {
1088 lua_pushnil(L);
1089 }
1090 return 1;
1091 }
1092
1093 static int lua_nodelib_direct_setdir(lua_State * L)
1094 {
1095 halfword n = lua_tointeger(L, 1);
1096 if (n) {
1097 halfword t = type(n);
1098 if (t == dir_node) {
1099 nodelib_setdir_text(L, 2, n);
1100 } else if (t == hlist_node || type(n) == vlist_node) {
1101 box_dir(n) = nodelib_getdir_par(L, 2);
1102 } else if (t == rule_node) {
1103 rule_dir(n) = nodelib_getdir_par(L, 2);
1104 } else if (t == local_par_node) {
1105 local_par_dir(n) = nodelib_getdir_par(L, 2);
1106 }
1107 }
1108 return 0;
1109 }
1110
1111 static int lua_nodelib_direct_setdirection(lua_State * L)
1112 {
1113 halfword n = lua_tointeger(L, 1);
1114 if (n) {
1115 halfword t = type(n);
1116 if (t == dir_node) {
1117 dir_dir(n) = nodelib_getdirection(L, 2);
1118 if ((lua_type(L, 3) == LUA_TBOOLEAN)) {
1119 if (lua_toboolean(L, 3)) {
1120 subtype(n) = cancel_dir;
1121 } else {
1122 subtype(n) = normal_dir;
1123 }
1124 }
1125 } else if (t == hlist_node || type(n) == vlist_node) {
1126 box_dir(n) = nodelib_getdirection(L, 2);
1127 } else if (t == rule_node) {
1128 rule_dir(n) = nodelib_getdirection(L, 2);
1129 } else if (t == local_par_node) {
1130 local_par_dir(n) = nodelib_getdirection(L, 2);
1131 }
1132 }
1133 return 0;
1134 }
1135
1136 /* node.direct.getoffsets */
1137 /* node.direct.setoffsets */
1138
1139 static int lua_nodelib_direct_getoffsets(lua_State * L)
1140 {
1141 halfword n = lua_tointeger(L, 1);
1142 if (n) {
1143 if (type(n) == glyph_node) {
1144 lua_pushinteger(L, x_displace(n));
1145 lua_pushinteger(L, y_displace(n));
1146 return 2;
1147 } else if (type(n) == rule_node) {
1148 lua_pushinteger(L, rule_left(n));
1149 lua_pushinteger(L, rule_right(n));
1150 return 2;
1151 }
1152 }
1153 return 0;
1154 }
1155
1156 static int lua_nodelib_direct_setoffsets(lua_State * L)
1157 {
1158 halfword n = lua_tointeger(L, 1);
1159 if (n) {
1160 if (type(n) == glyph_node) {
1161 if (lua_type(L, 2) == LUA_TNUMBER) {
1162 x_displace(n) = (halfword) lua_roundnumber(L, 2);
1163 }
1164 if (lua_type(L, 3) == LUA_TNUMBER) {
1165 y_displace(n) = (halfword) lua_roundnumber(L, 3);
1166 }
1167 } else if (type(n) == rule_node) {
1168 if (lua_type(L, 2) == LUA_TNUMBER) {
1169 rule_left(n) = (halfword) lua_roundnumber(L, 2);
1170 }
1171 if (lua_type(L, 3) == LUA_TNUMBER) {
1172 rule_right(n) = (halfword) lua_roundnumber(L, 3);
1173 }
1174 }
1175 }
1176 return 0;
1177 }
1178
1179 /* node.direct.getdisc */
1180 /* node.direct.setdisc */
1181
1182 static int lua_nodelib_direct_getdisc(lua_State * L)
1183 {
1184 halfword n = lua_tointeger(L, 1);
1185 if ((n) && (type(n) == disc_node)) {
1186 nodelib_pushdirect_or_nil_alink(vlink(pre_break(n)));
1187 nodelib_pushdirect_or_nil_alink(vlink(post_break(n)));
1188 nodelib_pushdirect_or_nil_alink(vlink(no_break(n)));
1189 if (lua_isboolean(L, 2) && lua_toboolean(L, 2)) {
1190 nodelib_pushdirect_or_nil(tlink(pre_break(n)));
1191 nodelib_pushdirect_or_nil(tlink(post_break(n)));
1192 nodelib_pushdirect_or_nil(tlink(no_break(n)));
1193 return 6;
1194 }
1195 return 3;
1196 } else {
1197 return 0;
1198 }
1199 }
1200
1201 static int lua_nodelib_direct_setdisc(lua_State * L)
1202 {
1203 halfword n = lua_tointeger(L, 1);
1204 if (type(n) == disc_node) {
1205 int t = lua_gettop(L) ;
1206 if (t > 1) {
1207 set_disc_field(pre_break(n), lua_tointeger(L,2));
1208 if (t > 2) {
1209 set_disc_field(post_break(n), lua_tointeger(L,3));
1210 if (t > 3) {
1211 set_disc_field(no_break(n), lua_tointeger(L,4));
1212 if (t > 4) {
1213 subtype(n) = (quarterword) lua_tointeger(L,5);
1214 if (t > 5) {
1215 disc_penalty(n) = lua_tointeger(L,6);
1216 }
1217 }
1218 } else {
1219 set_disc_field(no_break(n), null);
1220 }
1221 } else {
1222 set_disc_field(post_break(n), null);
1223 set_disc_field(no_break(n), null);
1224 }
1225 } else {
1226 set_disc_field(pre_break(n), null);
1227 set_disc_field(post_break(n), null);
1228 set_disc_field(no_break(n), null);
1229 }
1230 }
1231 return 0;
1232 }
1233
1234 /* node.getdisc */
1235
1236 static int lua_nodelib_getdisc(lua_State * L)
1237 {
1238 halfword *a;
1239 halfword *n = lua_touserdata(L, 1);
1240 if ((n != NULL) && (type(*n) == disc_node)) {
1241 fast_metatable_or_nil(vlink(pre_break(*n)));
1242 fast_metatable_or_nil(vlink(post_break(*n)));
1243 fast_metatable_or_nil(vlink(no_break(*n)));
1244 if (lua_isboolean(L, 2) && lua_toboolean(L, 2)) {
1245 fast_metatable_or_nil(tlink(pre_break(*n)));
1246 fast_metatable_or_nil(tlink(post_break(*n)));
1247 fast_metatable_or_nil(tlink(no_break(*n)));
1248 return 6;
1249 }
1250 return 3;
1251 }
1252 return 0;
1253 }
1254
1255 /* node.direct.getwhd */
1256 /* node.direct.setwhd */
1257
1258 # define push_list_whd(n) do { \
1259 lua_pushinteger(L, width(n)); \
1260 lua_pushinteger(L, height(n)); \
1261 lua_pushinteger(L, depth(n)); \
1262 } while (0)
1263
1264 # define push_char_whd(n) do { \
1265 lua_pushinteger(L, char_width(font(n),character(n))); \
1266 lua_pushinteger(L, char_height(font(n),character(n))); \
1267 lua_pushinteger(L, char_depth(font(n),character(n))); \
1268 } while (0)
1269
1270 # define push_char_ehd(n) do { \
1271 lua_pushinteger(L, char_width(font(n),character(n))); \
1272 lua_pushinteger(L, char_height(font(n),character(n))); \
1273 lua_pushinteger(L, char_depth(font(n),character(n))); \
1274 lua_pushinteger(L, ex_glyph(n)); \
1275 } while (0)
1276
1277 static int lua_nodelib_direct_getwhd(lua_State * L)
1278 {
1279 halfword n = lua_tointeger(L, 1);
1280 if (n != null) {
1281 halfword t = type(n);
1282 if ((t == hlist_node) || (t == vlist_node) || (t == rule_node) || (t == unset_node)) {
1283 push_list_whd(n);
1284 return 3;
1285 } else if (t == glyph_node) {
1286 if (lua_toboolean(L,2)) {
1287 push_char_ehd(n);
1288 return 4;
1289 } else {
1290 push_char_whd(n);
1291 return 3;
1292 }
1293 } else if (t == glue_node) {
1294 halfword l = leader_ptr(n);
1295 if (l != null) {
1296 t = type(l) ;
1297 if ((t == hlist_node) || (t == vlist_node) || (t == rule_node)) {
1298 push_list_whd(l);
1299 return 3;
1300 }
1301 }
1302 }
1303 }
1304 return 0;
1305 }
1306
1307 static int lua_nodelib_direct_setwhd(lua_State * L)
1308 {
1309 halfword n = lua_tointeger(L, 1);
1310 if (n != null) {
1311 halfword t = type(n);
1312 if (t == glue_node) {
1313 n = leader_ptr(n);
1314 if (n == null) {
1315 return 0;
1316 } else {
1317 t = type(n);
1318 }
1319 }
1320 if ((t == hlist_node) || (t == vlist_node) || (t == rule_node) || (t == unset_node)) {
1321 int t = lua_gettop(L) ;
1322 if (t > 1) {
1323 if ((lua_type(L, 2) == LUA_TNUMBER)) {
1324 width(n) = (halfword) lua_roundnumber(L, 2);
1325 } else {
1326 /* leave as is */
1327 }
1328 if (t > 2) {
1329 if ((lua_type(L, 3) == LUA_TNUMBER)) {
1330 height(n) = (halfword) lua_roundnumber(L, 3);
1331 } else {
1332 /* leave as is */
1333 }
1334 if (t > 3) {
1335 if ((lua_type(L, 4) == LUA_TNUMBER)) {
1336 depth(n) = (halfword) lua_roundnumber(L, 4);
1337 } else {
1338 /* leave as is */
1339 }
1340 }
1341 }
1342 }
1343 }
1344 }
1345 return 0;
1346 }
1347
1348 /* node.getwhd */
1349
1350 static int lua_nodelib_getwhd(lua_State * L)
1351 {
1352 halfword *n = lua_touserdata(L, 1);
1353 if (n != NULL) {
1354 halfword t = type(*n);
1355 if ((t == hlist_node) || (t == vlist_node) || (t == rule_node) || (t == unset_node)) {
1356 push_list_whd(*n);
1357 return 3;
1358 } else if (t == glyph_node) {
1359 if (lua_toboolean(L,2)) {
1360 push_char_ehd(*n);
1361 return 4;
1362 } else {
1363 push_char_whd(*n);
1364 return 3;
1365 }
1366 } else if (t == glue_node) {
1367 halfword l = leader_ptr(*n);
1368 if (l != null) {
1369 t = type(l) ;
1370 if ((t == hlist_node) || (t == vlist_node) || (t == rule_node)) {
1371 push_list_whd(l);
1372 return 3;
1373 }
1374 }
1375 }
1376 }
1377 return 0;
1378 }
1379
1380 /* node.direct.getlist */
1381
1382 static int lua_nodelib_direct_getlist(lua_State * L)
1383 {
1384 halfword n = lua_tointeger(L, 1);
1385 if (n == null) {
1386 lua_pushnil(L);
1387 } else {
1388 halfword t = type(n) ;
1389 if ((t == hlist_node) || (t == vlist_node)) {
1390 nodelib_pushdirect_or_nil_alink(list_ptr(n));
1391 } else if ((t == sub_box_node) || (t == sub_mlist_node)) {
1392 nodelib_pushdirect_or_nil_alink(math_list(n));
1393 } else if (t == ins_node) {
1394 nodelib_pushdirect_or_nil_alink(ins_ptr(n));
1395 } else if (t == adjust_node) {
1396 nodelib_pushdirect_or_nil_alink(adjust_ptr(n));
1397 } else {
1398 lua_pushnil(L);
1399 }
1400 }
1401 return 1;
1402 }
1403
1404 static int lua_nodelib_direct_setlist(lua_State * L)
1405 {
1406 halfword n = lua_tointeger(L, 1);
1407 if (n != null) {
1408 halfword t = type(n) ;
1409 if ((t == hlist_node) || (t == vlist_node)) {
1410 if (lua_type(L,2) == LUA_TNUMBER) {
1411 list_ptr(n) = (halfword) lua_tointeger(L, 2);
1412 } else {
1413 list_ptr(n) = null;
1414 }
1415 } else if ((t == sub_box_node) || (t == sub_mlist_node)) {
1416 if (lua_type(L,2) == LUA_TNUMBER) {
1417 math_list(n) = (halfword) lua_tointeger(L, 2);
1418 } else {
1419 math_list(n) = null;
1420 }
1421 } else if (t == ins_node) {
1422 if (lua_type(L,2) == LUA_TNUMBER) {
1423 ins_ptr(n) = (halfword) lua_tointeger(L, 2);
1424 } else {
1425 ins_ptr(n) = null;
1426 }
1427 } else if (t == adjust_node) {
1428 if (lua_type(L,2) == LUA_TNUMBER) {
1429 adjust_ptr(n) = (halfword) lua_tointeger(L, 2);
1430 } else {
1431 adjust_ptr(n) = null;
1432 }
1433 }
1434 }
1435 return 0;
1436 }
1437
1438 /* node.getlist */
1439
1440 static int lua_nodelib_getlist(lua_State * L)
1441 {
1442 halfword *a;
1443 halfword *n = lua_touserdata(L, 1);
1444 if ((n == NULL) || (! lua_getmetatable(L,1))) {
1445 lua_pushnil(L);
1446 } else if ((type(*n) == hlist_node) || (type(*n) == vlist_node)) {
1447 fast_metatable_or_nil_alink(list_ptr(*n));
1448 } else if ((type(*n) == sub_box_node) || (type(*n) == sub_mlist_node)) {
1449 fast_metatable_or_nil_alink(math_list(*n));
1450 } else if (type(*n) == ins_node) {
1451 fast_metatable_or_nil_alink(ins_ptr(*n));
1452 } else if (type(*n) == adjust_node) {
1453 fast_metatable_or_nil_alink(adjust_ptr(*n));
1454 } else {
1455 lua_pushnil(L);
1456 }
1457 return 1;
1458 }
1459
1460 /* node.direct.getleader */
1461 /* node.direct.setleader */
1462
1463 static int lua_nodelib_direct_getleader(lua_State * L)
1464 {
1465 halfword n = lua_tointeger(L, 1);
1466 if ((n) && (type(n) == glue_node)) {
1467 nodelib_pushdirect_or_nil(leader_ptr(n));
1468 } else {
1469 lua_pushnil(L);
1470 }
1471 return 1;
1472 }
1473
1474 static int lua_nodelib_direct_setleader(lua_State * L)
1475 {
1476 halfword n = lua_tointeger(L, 1);
1477 if ((n) && (type(n) == glue_node)) {
1478 if (lua_type(L,2) == LUA_TNUMBER) {
1479 leader_ptr(n) = (halfword) lua_tointeger(L, 2);
1480 } else {
1481 leader_ptr(n) = null;
1482 }
1483 }
1484 return 0;
1485 }
1486
1487 /* node.getleader */
1488
1489 static int lua_nodelib_getleader(lua_State * L)
1490 {
1491 halfword *a;
1492 halfword *n = lua_touserdata(L, 1);
1493 if ((n == NULL) || (! lua_getmetatable(L,1)) ) {
1494 lua_pushnil(L);
1495 } else if (type(*n) == glue_node) {
1496 fast_metatable_or_nil(leader_ptr(*n));
1497 } else {
1498 lua_pushnil(L);
1499 }
1500 return 1;
1501 }
1502
1503 /* node.direct.getdata */
1504 /* node.direct.setdata */
1505
1506 #define get_user_node_direct_value(L, n) do { \
1507 switch (user_node_type(n)) { \
1508 case 'a': \
1509 nodelib_pushdirect(user_node_value(n)); \
1510 break; \
1511 case 'd': \
1512 lua_pushinteger(L, user_node_value(n)); \
1513 break; \
1514 case 'l': \
1515 if (user_node_value(n) != 0) { \
1516 lua_rawgeti(L, LUA_REGISTRYINDEX, user_node_value(n)); \
1517 } else { \
1518 lua_pushnil(L); \
1519 } \
1520 break; \
1521 case 'n': \
1522 nodelib_pushdirect(user_node_value(n)); \
1523 break; \
1524 case 's': \
1525 nodelib_pushstring(L, user_node_value(n)); \
1526 break; \
1527 case 't': \
1528 tokenlist_to_lua(L, user_node_value(n)); \
1529 break; \
1530 default: \
1531 lua_pushinteger(L, user_node_value(n)); \
1532 break; \
1533 } \
1534 } while (0)
1535
1536 #define set_user_node_direct_value(L,n,i) do { \
1537 switch (user_node_type(n)) { \
1538 case 'a': \
1539 user_node_value(n) = nodelib_getlist(L, i); \
1540 break; \
1541 case 'd': \
1542 user_node_value(n) = (halfword) lua_roundnumber(L, i); \
1543 break; \
1544 case 'l': \
1545 lua_pushvalue(L, i); \
1546 if (user_node_value(n) != 0) { \
1547 luaL_unref(L, LUA_REGISTRYINDEX,user_node_value(n)); \
1548 } \
1549 user_node_value(n) = luaL_ref(L, LUA_REGISTRYINDEX); \
1550 break; \
1551 case 'n': \
1552 user_node_value(n) = nodelib_getlist(L, i); \
1553 break; \
1554 case 's': \
1555 user_node_value(n) = nodelib_getstring(L, i); \
1556 break; \
1557 case 't': \
1558 user_node_value(n) = nodelib_gettoks(L, i); \
1559 break; \
1560 default: \
1561 user_node_value(n) = (halfword) lua_roundnumber(L, i); \
1562 break; \
1563 } \
1564 } while (0)
1565
1566 #define get_pdf_literal_direct_value(L,n) do { \
1567 if (pdf_literal_type(n) == lua_refid_literal) { \
1568 lua_rawgeti(L, LUA_REGISTRYINDEX, pdf_literal_data(n)); \
1569 } else if (pdf_literal_type(n) == lua_refid_literal) { \
1570 tokenlist_to_luastring(L, pdf_literal_data(n)); \
1571 } \
1572 } while (0)
1573
1574 #define set_pdf_literal_direct_normal(L,n,i) do { \
1575 if (ini_version) { \
1576 pdf_literal_data(n) = nodelib_gettoks(L, i); \
1577 pdf_literal_type(n) = normal; \
1578 } else { \
1579 lua_pushvalue(L, i); \
1580 pdf_literal_data(n) = luaL_ref(L, LUA_REGISTRYINDEX); \
1581 pdf_literal_type(n) = lua_refid_literal; \
1582 } \
1583 } while (0)
1584
1585 #define set_pdf_literal_direct_token(L,n,i) do { \
1586 pdf_literal_data(n) = nodelib_gettoks(L, i); \
1587 pdf_literal_type(n) = normal; \
1588 } while (0)
1589
1590 #define cleanup_late_lua(n) do { \
1591 if (late_lua_data(n) != 0) { \
1592 if (late_lua_type(n) == normal) { \
1593 delete_token_ref(late_lua_data(n)); \
1594 } else if (late_lua_type(n) == lua_refid_literal) { \
1595 luaL_unref(L, LUA_REGISTRYINDEX,late_lua_data(n)); \
1596 } \
1597 } \
1598 } while (0)
1599
1600 #define cleanup_late_lua_name(n) do { \
1601 if (late_lua_name(n) != 0) { \
1602 delete_token_ref(late_lua_name(n)); \
1603 } \
1604 } while (0)
1605
1606 #define set_late_lua_direct_normal(L,n,i) do { \
1607 cleanup_late_lua(n) ; \
1608 if (ini_version) { \
1609 late_lua_data(n) = nodelib_gettoks(L, i); \
1610 late_lua_type(n) = normal; \
1611 } else if (lua_type(L, i) == LUA_TNUMBER) { \
1612 late_lua_data(n) = lua_tointeger(L,i); \
1613 late_lua_type(n) = lua_refid_call; \
1614 } else { \
1615 lua_pushvalue(L, i); \
1616 late_lua_data(n) = luaL_ref(L, LUA_REGISTRYINDEX); \
1617 late_lua_type(n) = lua_refid_literal; \
1618 } \
1619 } while (0)
1620
1621 #define set_late_lua_direct_token(L,n,i) do { \
1622 cleanup_late_lua(n) ; \
1623 late_lua_data(n) = nodelib_gettoks(L, i); \
1624 late_lua_type(n) = normal; \
1625 } while (0)
1626
1627 #define get_late_lua_direct_value(L,n) do { \
1628 if (late_lua_type(n) == lua_refid_literal) { \
1629 lua_rawgeti(L, LUA_REGISTRYINDEX, late_lua_data(n)); \
1630 } else if (late_lua_type(n) == lua_refid_call) { \
1631 lua_pushinteger(L, late_lua_data(n)); \
1632 } else if (late_lua_type(n) == normal) { \
1633 tokenlist_to_luastring(L, late_lua_data(n)); \
1634 } \
1635 } while (0)
1636
1637 #define set_special_direct_value(L,n,i) do { \
1638 write_tokens(n) = nodelib_gettoks(L, i); \
1639 } while (0)
1640
1641 #define get_special_direct_value(L,n) do { \
1642 tokenlist_to_luastring(L, write_tokens(n)); \
1643 } while (0)
1644
1645 #define set_write_direct_value(L,n,i) do { \
1646 write_tokens(n) = nodelib_gettoks(L, i); \
1647 } while (0)
1648
1649 #define get_write_direct_value(L,n) do { \
1650 tokenlist_to_lua(L, write_tokens(n)); \
1651 } while (0)
1652
1653 #define get_write_direct_data(L,n) do { \
1654 int l; \
1655 char *s; \
1656 expand_macros_in_tokenlist(write_tokens(n)); \
1657 s = tokenlist_to_cstring(def_ref, 1, &l); \
1658 lua_pushlstring(L, s, (size_t) l); \
1659 free(s); \
1660 flush_list(def_ref); \
1661 } while (0)
1662
1663 #define set_pdf_setmatrix_direct_value(L,n,i) do { \
1664 pdf_setmatrix_data(n) = nodelib_gettoks(L, i); \
1665 } while (0)
1666
1667 #define get_pdf_setmatrix_direct_value(L,n) do { \
1668 tokenlist_to_luastring(L, pdf_setmatrix_data(n)); \
1669 } while (0)
1670
1671 /*tex
1672
1673 These getter and setter get |data| as well as |value| fields. One can
1674 make them equivalent to |getvalue| and |setvalue| if needed.
1675
1676 */
1677
1678 static int lua_nodelib_direct_getdata(lua_State * L)
1679 {
1680 halfword n = lua_tointeger(L, 1);
1681 if (n == null) {
1682 lua_pushnil(L);
1683 } else {
1684 halfword t = type(n) ;
1685 if (t == glyph_node) {
1686 lua_pushinteger(L,glyph_node_data(n));
1687 } else if (t == boundary_node) {
1688 lua_pushinteger(L,boundary_value(n));
1689 } else if (t == whatsit_node) {
1690 halfword s = subtype(n);
1691 if (s == user_defined_node) {
1692 get_user_node_direct_value(L, n);
1693 } else if (s == pdf_literal_node || s == pdf_late_literal_node) {
1694 get_pdf_literal_direct_value(L, n);
1695 /*tex A bonus. */
1696 lua_pushinteger(L,pdf_literal_mode(n));
1697 return 2;
1698 } else if (s == late_lua_node) {
1699 get_late_lua_direct_value(L, n);
1700 } else if (s == pdf_setmatrix_node) {
1701 get_pdf_setmatrix_direct_value(L, n);
1702 } else if (s == special_node || s == late_special_node) {
1703 get_special_direct_value(L, n);
1704 } else if (s == write_node) {
1705 get_write_direct_data(L, n);
1706 } else {
1707 lua_pushnil(L);
1708 }
1709 } else {
1710 lua_pushnil(L);
1711 }
1712 }
1713 return 1;
1714 }
1715
1716 static int lua_nodelib_direct_setdata(lua_State * L) /* data and value */
1717 {
1718 halfword n = lua_tointeger(L, 1);
1719 if (n != null) {
1720 halfword t = type(n) ;
1721 if (t == glyph_node) {
1722 glyph_node_data(n) = lua_tointeger(L,2);
1723 } else if (t == boundary_node) {
1724 boundary_value(n) = lua_tointeger(L,2);
1725 } else if (t == whatsit_node) {
1726 halfword s = subtype(n);
1727 if (s == user_defined_node) {
1728 set_user_node_direct_value(L, n, 2);
1729 } else if (s == pdf_literal_node || s == pdf_late_literal_node) {
1730 if (lua_type(L,2) == LUA_TTABLE) {
1731 set_pdf_literal_direct_token(L, n, 2);
1732 } else {
1733 set_pdf_literal_direct_normal(L, n, 2);
1734 }
1735 if (lua_type(L,3) == LUA_TNUMBER) {
1736 /*tex A bonus. */
1737 pdf_literal_mode(n) = lua_tointeger(L,3);
1738 }
1739 } else if (s == late_lua_node) {
1740 set_late_lua_direct_normal(L, n, 2);
1741 } else if (s == pdf_setmatrix_node) {
1742 set_pdf_setmatrix_direct_value(L, n, 2);
1743 } else if (s == special_node || s == late_special_node) {
1744 set_special_direct_value(L, n, 2);
1745 } else if (s == write_node) {
1746 set_write_direct_value(L,n,2);
1747 }
1748 }
1749 }
1750 return 0;
1751 }
1752
1753 /* node.direct.getnext */
1754 /* node.direct.setnext */
1755
1756 static int lua_nodelib_direct_getnext(lua_State * L)
1757 {
1758 halfword p = lua_tointeger(L, 1);
1759 if (p == null) {
1760 lua_pushnil(L);
1761 } else {
1762 nodelib_pushdirect_or_nil(vlink(p));
1763 }
1764 return 1;
1765 }
1766
1767 static int lua_nodelib_direct_setnext(lua_State * L)
1768 {
1769 halfword n = lua_tointeger(L, 1);
1770 if (n) {
1771 if (lua_type(L, 2) == LUA_TNUMBER) {
1772 vlink(n) = (halfword) lua_tointeger(L, 2);
1773 } else {
1774 vlink(n) = null;
1775 }
1776 }
1777 return 0;
1778 }
1779
1780 /* node.getnext */
1781
1782 static int lua_nodelib_getnext(lua_State * L)
1783 {
1784 halfword *a;
1785 /* [given-node] [...]*/
1786 halfword *p = lua_touserdata(L, 1);
1787 if ( (p == NULL) || (! lua_getmetatable(L,1)) ) {
1788 lua_pushnil(L);
1789 } else {
1790 /* [given-node] [mt-given-node]*/
1791 lua_get_metatablelua(luatex_node);
1792 /* [given-node] [mt-given-node] [mt-node]*/
1793 if (!lua_rawequal(L, -1, -2)) {
1794 lua_pushnil(L);
1795 } else {
1796 fast_metatable_or_nil(vlink(*p));
1797 }
1798 }
1799 return 1; /* just one*/
1800 }
1801
1802 /* node.direct.getprev */
1803 /* node.direct.setprev */
1804
1805 static int lua_nodelib_direct_getprev(lua_State * L)
1806 {
1807 halfword p = lua_tointeger(L, 1);
1808 if (p == null) {
1809 lua_pushnil(L);
1810 } else {
1811 nodelib_pushdirect_or_nil(alink(p));
1812 }
1813 return 1;
1814 }
1815
1816 static int lua_nodelib_direct_setprev(lua_State * L)
1817 {
1818 halfword n = lua_tointeger(L, 1);
1819 if (n) {
1820 if (lua_type(L, 2) == LUA_TNUMBER) {
1821 alink(n) = (halfword) lua_tointeger(L, 2);
1822 } else {
1823 alink(n) = null;
1824 }
1825 }
1826 return 0;
1827 }
1828
1829 /* node.getprev */
1830
1831 static int lua_nodelib_getprev(lua_State * L)
1832 {
1833 halfword *a;
1834 halfword *p = lua_touserdata(L, 1);
1835 if ( (p == NULL) || (! lua_getmetatable(L,1)) ) {
1836 lua_pushnil(L);
1837 } else {
1838 lua_get_metatablelua(luatex_node);
1839 if (!lua_rawequal(L, -1, -2)) {
1840 lua_pushnil(L);
1841 } else {
1842 fast_metatable_or_nil(alink(*p));
1843 }
1844 }
1845 return 1;
1846 }
1847
1848 /* node.direct.getboth */
1849 /* node.direct.setboth */
1850
1851 static int lua_nodelib_direct_getboth(lua_State * L)
1852 {
1853 halfword n = lua_tointeger(L, 1);
1854 if (n) {
1855 nodelib_pushdirect_or_nil(alink(n));
1856 nodelib_pushdirect_or_nil(vlink(n));
1857 } else {
1858 lua_pushnil(L);
1859 lua_pushnil(L);
1860 }
1861 return 2;
1862 }
1863
1864 static int lua_nodelib_direct_setboth(lua_State * L)
1865 {
1866 halfword n = lua_tointeger(L, 1);
1867 if (n) {
1868 if (lua_type(L, 2) == LUA_TNUMBER) {
1869 alink(n) = (halfword) lua_tointeger(L, 2);
1870 } else {
1871 alink(n) = null;
1872 }
1873 if (lua_type(L, 3) == LUA_TNUMBER) {
1874 vlink(n) = (halfword) lua_tointeger(L, 3);
1875 } else {
1876 vlink(n) = null;
1877 }
1878 }
1879 return 0;
1880 }
1881
1882 /* node.getboth */
1883
1884 static int lua_nodelib_getboth(lua_State * L)
1885 {
1886 halfword *a;
1887 halfword *p = lua_touserdata(L, 1);
1888 if ( (p == NULL) || (! lua_getmetatable(L,1)) ) {
1889 lua_pushnil(L);
1890 lua_pushnil(L);
1891 } else {
1892 lua_get_metatablelua(luatex_node);
1893 if (lua_rawequal(L, -1, -2)) {
1894 fast_metatable_or_nil(alink(*p));
1895 fast_metatable_or_nil(vlink(*p));
1896 } else {
1897 lua_pushnil(L);
1898 lua_pushnil(L);
1899 }
1900 }
1901 return 2;
1902 }
1903
1904 /* node.direct.setlink */
1905 /* node.direct.setsplit */
1906
1907 /*
1908 a b b nil c d : prev-a-b-c-next
1909 nil a b b nil c d nil : nil-a-b-c-nil
1910 */
1911
1912 static int lua_nodelib_direct_setlink(lua_State * L)
1913 {
1914 int n = lua_gettop(L);
1915 int i;
1916 halfword h = null; /* head node */
1917 halfword t = null; /* tail node */
1918 halfword c = null; /* current node */
1919 for (i=1;i<=n;i++) {
1920 /*
1921 we don't go for the tail of the current node because we can inject
1922 between existing nodes and the nodes themselves can have old values
1923 for prev and next, so ... only single nodes are looked at!
1924 */
1925 if (lua_type(L, i) == LUA_TNUMBER) {
1926 c = lua_tointeger(L, i);
1927 if (c != t) {
1928 if (t != null) {
1929 vlink(t) = c;
1930 alink(c) = t;
1931 } else if (i > 1) {
1932 /* we assume that the first node is a kind of head */
1933 alink(c) = null;
1934 }
1935 t = c;
1936 if (h == null) {
1937 h = t;
1938 }
1939 } else {
1940 /* we ignore duplicate nodes which can be tails or the previous */
1941 }
1942 } else if (t == null) {
1943 /* we just ignore nil nodes and have no tail yet */
1944 } else {
1945 /* safeguard: a nil in the list can be meant as end so we nil the next of tail */
1946 vlink(t) = null;
1947 }
1948 }
1949 if (h == null) {
1950 /* no head */
1951 lua_pushnil(L);
1952 } else {
1953 /* first valid head */
1954 lua_pushinteger(L,h);
1955 }
1956 return 1;
1957 }
1958
1959 static int lua_nodelib_direct_setsplit(lua_State * L)
1960 {
1961 if (lua_type(L, 1) == LUA_TNUMBER && (lua_type(L, 2) == LUA_TNUMBER)) {
1962 halfword l = lua_tointeger(L, 1);
1963 halfword r = lua_tointeger(L, 2);
1964 if (l != r) {
1965 alink(vlink(l)) = null;
1966 vlink(alink(r)) = null;
1967 }
1968 vlink(l) = null;
1969 alink(r) = null;
1970 }
1971 return 0;
1972 }
1973
1974 /* node.subtype (maybe also return them for other node types now) */
1975
1976 static int lua_nodelib_subtype(lua_State * L)
1977 {
1978 if (lua_type(L,1) == LUA_TSTRING) {
1979 int i = get_node_type_id_from_name(L,1,whatsit_node_data);
1980 if (i >= 0) {
1981 lua_pushinteger(L, i);
1982 } else {
1983 lua_pushnil(L);
1984 }
1985 } else {
1986 lua_pushnil(L);
1987 }
1988 return 1;
1989 }
1990
1991 /* node.type (converts id numbers to type names) */
1992
1993 static int lua_nodelib_type(lua_State * L)
1994 {
1995 if (lua_type(L,1) == LUA_TNUMBER) {
1996 int i = lua_tointeger(L, 1);
1997 if (known_node_type(i)) {
1998 /* lua_pushstring(L, node_data[i].name); */
1999 lua_push_string_by_index(L, node_data[i].lua);
2000 return 1;
2001 }
2002 } else if (maybe_isnode(L, 1) != NULL) {
2003 /* lua_pushstring(L,"node"); */
2004 lua_push_string_by_name(L,node);
2005 return 1;
2006 }
2007 lua_pushnil(L);
2008 return 1;
2009 }
2010
2011 /* node.new (allocate a new node) */
2012
2013 static halfword lua_nodelib_new_node(lua_State * L)
2014 {
2015 int i, j;
2016 int t = lua_type(L, 1);
2017 if (t == LUA_TNUMBER) {
2018 i = lua_tointeger(L,1);
2019 if (! known_node_type(i)) {
2020 i = -1;
2021 }
2022 } else if (t == LUA_TSTRING) {
2023 i = get_node_type_id_from_name(L,1,node_data);
2024 } else {
2025 i = -1;
2026 }
2027 if (i < 0) {
2028 luaL_error(L, "invalid node id for creating new node");
2029 }
2030 t = lua_type(L, 2);
2031 if (i == whatsit_node) {
2032 if (t == LUA_TNUMBER) {
2033 j = lua_tointeger(L,2);
2034 if (! known_whatsit_type(j)) {
2035 j = -1;
2036 }
2037 } else if (t == LUA_TSTRING) {
2038 j = get_node_type_id_from_name(L,2,whatsit_node_data);
2039 } else {
2040 j = -1;
2041 }
2042 if (j < 0) {
2043 luaL_error(L, "creating a whatsit requires the subtype number as a second argument");
2044 }
2045 } else if (t == LUA_TNUMBER) {
2046 j = (int) lua_tointeger(L, 2);
2047 } else if (t == LUA_TSTRING) {
2048 j = get_node_subtype_id_from_name(L,2,node_data[i].subtypes);
2049 } else {
2050 j = 0;
2051 }
2052 return new_node(i, j);
2053 }
2054
2055 static int lua_nodelib_new(lua_State * L)
2056 {
2057 halfword n = lua_nodelib_new_node(L);
2058 lua_nodelib_push_fast(L, n);
2059 return 1;
2060 }
2061
2062 /* node.direct.new */
2063
2064 static int lua_nodelib_direct_new(lua_State * L)
2065 {
2066 halfword n = lua_nodelib_new_node(L);
2067 lua_pushinteger(L,n);
2068 return 1;
2069 }
2070
2071 /* node.free (this function returns the 'next' node, because that may be helpful) */
2072
2073 static int lua_nodelib_free(lua_State * L)
2074 {
2075 halfword n;
2076 halfword p;
2077 if (lua_gettop(L) < 1) {
2078 lua_pushnil(L);
2079 return 1;
2080 } else if (lua_isnil(L, 1)) {
2081 return 1; /* the nil itself */
2082 }
2083 n = *(check_isnode(L, 1));
2084 p = vlink(n);
2085 flush_node(n);
2086 /* can be: lua_nodelib_push_fast(L, p); */
2087 lua_pushinteger(L, p);
2088 lua_nodelib_push(L);
2089 return 1;
2090 }
2091
2092 /* node.direct.free */
2093
2094 static int lua_nodelib_direct_free(lua_State * L)
2095 {
2096 halfword n = lua_tointeger(L,1);
2097 if (n == null) {
2098 lua_pushnil(L);
2099 } else {
2100 halfword p = vlink(n);
2101 flush_node(n);
2102 if (p == 0) {
2103 lua_pushnil(L);
2104 } else {
2105 lua_pushinteger(L,p);
2106 }
2107 }
2108 return 1;
2109 }
2110
2111 /* node.flush_node (no next returned) */
2112
2113 static int lua_nodelib_flush_node(lua_State * L)
2114 {
2115 if ((lua_gettop(L) < 1) || lua_isnil(L, 1)) {
2116 return 0;
2117 } else {
2118 halfword n = *(check_isnode(L, 1));
2119 flush_node(n);
2120 return 0;
2121 }
2122 }
2123
2124 /* node.direct.flush_node */
2125
2126 static int lua_nodelib_direct_flush_node(lua_State * L)
2127 {
2128 halfword n = lua_tointeger(L,1);
2129 if (n == null)
2130 return 0;
2131 flush_node(n);
2132 return 0;
2133 }
2134
2135 /* node.flush_list */
2136
2137 static int lua_nodelib_flush_list(lua_State * L)
2138 {
2139 if ((lua_gettop(L) < 1) || lua_isnil(L, 1)) {
2140 return 0;
2141 } else {
2142 halfword n_ptr = *check_isnode(L, 1);
2143 flush_node_list(n_ptr);
2144 return 0;
2145 }
2146 }
2147
2148 /* node.direct.flush_list */
2149
2150 static int lua_nodelib_direct_flush_list(lua_State * L)
2151 {
2152 halfword n = lua_tointeger(L,1);
2153 if (n == null)
2154 return 0;
2155 flush_node_list(n);
2156 return 0;
2157 }
2158
2159 /* remove a node from a list */
2160
2161 #if DEBUG
2162
2163 static void show_node_links (halfword l, const char * p)
2164 {
2165 halfword t = l;
2166 while (t) {
2167 fprintf(DEBUG_OUT, "%s t = %d, prev = %d, next = %d\n", p, (int)t, (int)alink(t), (int)vlink(t));
2168 t = vlink(t);
2169 }
2170 }
2171
2172 #endif
2173
2174 /* node.remove */
2175
2176 static int lua_nodelib_remove(lua_State * L)
2177 {
2178 halfword head, current, t;
2179 if (lua_gettop(L) < 2)
2180 luaL_error(L, "Not enough arguments for node.remove()");
2181 head = *(check_isnode(L, 1));
2182 if (lua_isnil(L, 2))
2183 return 2; /* the arguments, as they are */
2184 current = *(check_isnode(L, 2));
2185 if (head == current) {
2186 if (alink(current)){
2187 vlink(alink(current)) = vlink(current);
2188 }
2189 if (vlink(current)){
2190 alink( vlink(current)) = alink(current);
2191 }
2192
2193 head = vlink(current);
2194 current = vlink(current);
2195 } else {
2196 t = alink(current);
2197 if (t == null || vlink(t) != current) {
2198 set_t_to_prev(head, current);
2199 if (t == null) /* error! */
2200 luaL_error(L,"Attempt to node.remove() a non-existing node");
2201 }
2202 /* t is now the previous node */
2203 vlink(t) = vlink(current);
2204 if (vlink(current) != null)
2205 alink(vlink(current)) = t;
2206 current = vlink(current);
2207 }
2208 /* can be: lua_nodelib_push_fast(L, head); */
2209 lua_pushinteger(L, head);
2210 lua_nodelib_push(L);
2211 /* can be: lua_nodelib_push_fast(L, current); */
2212 lua_pushinteger(L, current);
2213 lua_nodelib_push(L);
2214 return 2;
2215 }
2216
2217 /* node.direct.remove */
2218
2219 static int lua_nodelib_direct_remove(lua_State * L)
2220 {
2221 halfword current, t;
2222 halfword head = lua_tointeger(L,1);
2223 if (head == null) {
2224 lua_pushnil(L);
2225 lua_pushnil(L);
2226 return 2 ;
2227 }
2228 current = (halfword) lua_tointeger(L,2);
2229 if (current == null) {
2230 lua_pushinteger(L, head);
2231 lua_pushnil(L);
2232 return 2 ;
2233 }
2234 if (head == current) {
2235 if (alink(current)){
2236 vlink( alink(current) ) = vlink(current);
2237 }
2238 if (vlink(current)){
2239 alink( vlink(current) ) = alink(current);
2240 }
2241 head = vlink(current);
2242 current = vlink(current);
2243 } else {
2244 t = alink(current);
2245 if (t == null || vlink(t) != current) {
2246 set_t_to_prev(head, current);
2247 if (t == null) {
2248 luaL_error(L,"Attempt to node.direct.remove() a non-existing node");
2249 }
2250 }
2251 vlink(t) = vlink(current);
2252 if (vlink(current) != null)
2253 alink(vlink(current)) = t;
2254 current = vlink(current);
2255 }
2256 if (head == null) {
2257 lua_pushnil(L);
2258 } else {
2259 lua_pushinteger(L, head);
2260 }
2261 if (current == null) {
2262 lua_pushnil(L);
2263 } else {
2264 lua_pushinteger(L, current);
2265 }
2266 return 2;
2267 }
2268
2269 /* node.insert_before (insert a node in a list) */
2270
2271 static int lua_nodelib_insert_before(lua_State * L)
2272 {
2273 halfword head, current, n, t;
2274 if (lua_gettop(L) < 3) {
2275 luaL_error(L, "Not enough arguments for node.insert_before()");
2276 }
2277 if (lua_isnil(L, 3)) {
2278 lua_pop(L, 1);
2279 return 2;
2280 } else {
2281 n = *(check_isnode(L, 3));
2282 }
2283 if (lua_isnil(L, 1)) { /* no head */
2284 vlink(n) = null;
2285 alink(n) = null;
2286 lua_nodelib_push_fast(L, n);
2287 lua_pushvalue(L, -1);
2288 return 2;
2289 } else {
2290 head = *(check_isnode(L, 1));
2291 }
2292 if (lua_isnil(L, 2)) {
2293 current = tail_of_list(head);
2294 } else {
2295 current = *(check_isnode(L, 2));
2296 }
2297 if (head != current) {
2298 t = alink(current);
2299 if (t == null || vlink(t) != current) {
2300 set_t_to_prev(head, current);
2301 if (t == null) { /* error! */
2302 luaL_error(L, "Attempt to node.insert_before() a non-existing node");
2303 }
2304 }
2305 couple_nodes(t, n);
2306 }
2307 couple_nodes(n, current);
2308 if (head == current) {
2309 lua_nodelib_push_fast(L, n);
2310 } else {
2311 lua_nodelib_push_fast(L, head);
2312 }
2313 lua_nodelib_push_fast(L, n);
2314 return 2;
2315 }
2316
2317 /* node.direct.insert_before */
2318
2319 static int lua_nodelib_direct_insert_before(lua_State * L)
2320 {
2321 halfword head, current;
2322 halfword n = lua_tointeger(L,3);
2323 if (n == null){
2324 /* no node */
2325 lua_pop(L, 1);
2326 return 2 ;
2327 }
2328 head = (halfword) lua_tointeger(L,1);
2329 current = (halfword) lua_tointeger(L,2);
2330 /* no head, ignore current */
2331 if (head == null) {
2332 vlink(n) = null;
2333 alink(n) = null;
2334 lua_pushinteger(L, n);
2335 lua_pushvalue(L, -1);
2336 /* n, n */
2337 return 2;
2338 }
2339 /* no current */
2340 if (current == null)
2341 current = tail_of_list(head);
2342 if (head != current) {
2343 halfword t = alink(current);
2344 if (t == null || vlink(t) != current) {
2345 set_t_to_prev(head, current);
2346 }
2347 couple_nodes(t, n);
2348 }
2349 couple_nodes(n, current); /* nice but incompatible: couple_nodes(tail_of_list(n),current) */
2350 if (head == current) {
2351 lua_pushinteger(L, n);
2352 } else {
2353 lua_pushinteger(L, head);
2354 }
2355 lua_pushinteger(L, n);
2356 return 2;
2357 }
2358
2359 /* node.insert_after */
2360
2361 static int lua_nodelib_insert_after(lua_State * L)
2362 {
2363 halfword head, current, n;
2364 if (lua_gettop(L) < 3) {
2365 luaL_error(L, "Not enough arguments for node.insert_after()");
2366 }
2367 if (lua_isnil(L, 3)) {
2368 lua_pop(L, 1);
2369 return 2;
2370 } else {
2371 n = *(check_isnode(L, 3));
2372 }
2373 if (lua_isnil(L, 1)) { /* no head */
2374 vlink(n) = null;
2375 alink(n) = null;
2376 lua_nodelib_push_fast(L, n);
2377 lua_pushvalue(L, -1);
2378 return 2;
2379 } else {
2380 head = *(check_isnode(L, 1));
2381 }
2382 if (lua_isnil(L, 2)) {
2383 current = head;
2384 while (vlink(current) != null)
2385 current = vlink(current);
2386 } else {
2387 current = *(check_isnode(L, 2));
2388 }
2389 try_couple_nodes(n, vlink(current));
2390 couple_nodes(current, n);
2391
2392 lua_pop(L, 2);
2393 lua_nodelib_push_fast(L, n);
2394 return 2;
2395 }
2396
2397 /* node.direct.insert_after */
2398
2399 static int lua_nodelib_direct_insert_after(lua_State * L)
2400 {
2401 halfword head, current;
2402 /*[head][current][new]*/
2403 halfword n = lua_tointeger(L,3);
2404 if (n == null) {
2405 /* no node */
2406 return 2 ;
2407 }
2408 head = (halfword) lua_tointeger(L,1);
2409 current = (halfword) lua_tointeger(L,2);
2410 if (head == null) {
2411 /* no head, ignore current */
2412 vlink(n) = null;
2413 alink(n) = null;
2414 lua_pushinteger(L,n);
2415 lua_pushvalue(L, -1);
2416 /* n, n */
2417 return 2;
2418 }
2419 if (current == null) {
2420 /* no current */
2421 current = head;
2422 while (vlink(current) != null)
2423 current = vlink(current);
2424 }
2425 try_couple_nodes(n, vlink(current)); /* nice but incompatible: try_couple_nodes(tail_of_list(n), vlink(current)); */
2426 couple_nodes(current, n);
2427 lua_pop(L, 2);
2428 lua_pushinteger(L, n);
2429 return 2;
2430 }
2431
2432 /* node.copy_list */
2433
2434 /*
2435 we need to use an intermediate variable as otherwise target is used in the loop
2436 and subfields get overwritten (or something like that) which results in crashes
2437 and unexpected side effects
2438 */
2439
2440 static int lua_nodelib_copy_list(lua_State * L)
2441 {
2442 halfword n, s = null;
2443 halfword m;
2444 if (lua_isnil(L, 1))
2445 return 1; /* the nil itself */
2446 n = *check_isnode(L, 1);
2447 if ((lua_gettop(L) > 1) && (!lua_isnil(L,2)))
2448 s = *check_isnode(L, 2);
2449 m = do_copy_node_list(n, s);
2450 lua_nodelib_push_fast(L,m);
2451 return 1;
2452 }
2453
2454 /* node.direct.copy_list */
2455
2456 static int lua_nodelib_direct_copy_list(lua_State * L)
2457 {
2458 halfword n = lua_tointeger(L,1);
2459 if (n == null) {
2460 lua_pushnil(L);
2461 } else {
2462 halfword s = lua_tointeger(L,2);
2463 halfword m;
2464 if (s == null) {
2465 m = do_copy_node_list(n,null);
2466 } else {
2467 m = do_copy_node_list(n,s);
2468 }
2469 lua_pushinteger(L,m);
2470 }
2471 return 1;
2472 }
2473
2474 /* node.copy (deep copy) */
2475
2476 static int lua_nodelib_copy(lua_State * L)
2477 {
2478 halfword n;
2479 if (lua_isnil(L, 1))
2480 return 1; /* the nil itself */
2481 n = *check_isnode(L, 1);
2482 n = copy_node(n);
2483 lua_nodelib_push_fast(L, n);
2484 return 1;
2485 }
2486
2487 /* node.direct.copy (deep copy) */
2488
2489 static int lua_nodelib_direct_copy(lua_State * L)
2490 {
2491 if (lua_isnil(L, 1)) {
2492 return 1; /* the nil itself */
2493 } else {
2494 /* beware, a glue node can have number 0 (zeropt) so we cannot test for null) */
2495 halfword n = lua_tointeger(L, 1);
2496 n = copy_node(n);
2497 lua_pushinteger(L, n);
2498 return 1;
2499 }
2500 }
2501
2502
2503 /* node.write (output a node to tex's processor) */
2504
2505 static int lua_nodelib_append(lua_State * L)
2506 {
2507 halfword n;
2508 int i;
2509 int j = lua_gettop(L);
2510 for (i = 1; i <= j; i++) {
2511 n = *check_isnode(L, i);
2512 tail_append(n);
2513 if (nodetype_has_attributes(type(n)) && node_attr(n) == null) {
2514 build_attribute_list(n);
2515 }
2516 while (vlink(n) != null) {
2517 n = vlink(n);
2518 tail_append(n);
2519 if (nodetype_has_attributes(type(n)) && node_attr(n) == null) {
2520 build_attribute_list(n);
2521 }
2522 }
2523 }
2524 return 0;
2525 }
2526
2527 /* node.direct.write */
2528
2529 static int lua_nodelib_direct_append(lua_State * L)
2530 {
2531 halfword m;
2532 int i;
2533 int j = lua_gettop(L);
2534 for (i = 1; i <= j; i++) {
2535 halfword n = lua_tointeger(L,i); /*lua_getnumber(L, i);*/
2536 if (n != null) {
2537 m = n ;
2538 tail_append(m);
2539 if (nodetype_has_attributes(type(n)) && node_attr(n) == null) {
2540 build_attribute_list(n);
2541 }
2542 while (vlink(m) != null) {
2543 m = vlink(m);
2544 tail_append(m);
2545 if (nodetype_has_attributes(type(n)) && node_attr(n) == null) {
2546 build_attribute_list(n);
2547 }
2548 }
2549 }
2550 }
2551 return 0;
2552 }
2553
2554 /* node.last */
2555
2556 static int lua_nodelib_last_node(lua_State * L)
2557 {
2558 halfword m = pop_tail();
2559 /* can be: lua_nodelib_push_fast(L, m); */
2560 lua_pushinteger(L, m);
2561 lua_nodelib_push(L);
2562 return 1;
2563 }
2564
2565 /* node.direct.last */
2566
2567 static int lua_nodelib_direct_last_node(lua_State * L)
2568 {
2569 halfword m = pop_tail();
2570 lua_pushinteger(L, m);
2571 return 1;
2572 }
2573
2574 /* node.hpack (build a hbox) */
2575
2576 static int lua_nodelib_hpack(lua_State * L)
2577 {
2578 halfword p;
2579 const char *s;
2580 int w = 0;
2581 int m = 1;
2582 int d = -1;
2583 halfword n = *(check_isnode(L, 1));
2584 if (lua_gettop(L) > 1) {
2585 w = lua_roundnumber(L, 2);
2586 if (lua_gettop(L) > 2) {
2587 if (lua_type(L, 3) == LUA_TSTRING) {
2588 s = lua_tostring(L, 3);
2589 if (lua_key_eq(s, exactly)) {
2590 m = 0;
2591 } else if (lua_key_eq(s, additional)) {
2592 m = 1;
2593 } else if (lua_key_eq(s, cal_expand_ratio)) {
2594 m = 2;
2595 } else if (lua_key_eq(s, subst_ex_font)) {
2596 m = 3;
2597 }
2598 } else if (lua_type(L, 3) == LUA_TNUMBER) {
2599 m = (int) lua_tointeger(L, 3);
2600 }
2601 if ((m<0) || (m>3)) {
2602 luaL_error(L, "wrong mode in hpack");
2603 }
2604 if (lua_gettop(L) > 3) {
2605 if (lua_type(L, 4) == LUA_TNUMBER) {
2606 d = nodelib_getdirection(L, 4);
2607 } else if (lua_type(L, 4) == LUA_TSTRING) {
2608 d = nodelib_getdir_par(L, 4);
2609 } else {
2610 lua_pushstring(L, "incorrect 4th argument");
2611 }
2612 }
2613 }
2614 }
2615 p = hpack(n, w, m, d);
2616 lua_nodelib_push_fast(L, p);
2617 lua_pushinteger(L, last_badness);
2618 return 2;
2619 }
2620
2621 /* node.direct.hpack */
2622
2623 static int lua_nodelib_direct_hpack(lua_State * L)
2624 {
2625 halfword p;
2626 const char *s;
2627 int w = 0;
2628 int m = 1;
2629 int d = -1;
2630 halfword n = lua_tointeger(L,1);
2631 /* could be macro */
2632 if (lua_gettop(L) > 1) {
2633 w = lua_roundnumber(L, 2);
2634 if (lua_gettop(L) > 2) {
2635 if (lua_type(L, 3) == LUA_TSTRING) {
2636 s = lua_tostring(L, 3);
2637 if (lua_key_eq(s, additional)) {
2638 m = 1;
2639 } else if (lua_key_eq(s, exactly)) {
2640 m = 0;
2641 } else if (lua_key_eq(s, cal_expand_ratio)) {
2642 m = 2;
2643 } else if (lua_key_eq(s, subst_ex_font)) {
2644 m = 3;
2645 } else {
2646 luaL_error(L, "3rd argument should be either additional or exactly");
2647 }
2648 } else if (lua_type(L, 3) == LUA_TNUMBER) {
2649 m = (int) lua_tointeger(L, 3);
2650 } else {
2651 lua_pushstring(L, "incorrect 3rd argument");
2652 }
2653 if (lua_gettop(L) > 3) {
2654 if (lua_type(L, 4) == LUA_TNUMBER) {
2655 d = nodelib_getdirection(L, 4);
2656 } else if (lua_type(L, 4) == LUA_TSTRING) {
2657 d = nodelib_getdir_par(L, 4);
2658 } else {
2659 lua_pushstring(L, "incorrect 4th argument");
2660 }
2661 }
2662 }
2663 }
2664 /* till here */
2665 p = hpack(n, w, m, d);
2666 lua_pushinteger(L, p);
2667 lua_pushinteger(L, last_badness);
2668 return 2;
2669 }
2670
2671 /* node.vpack (build a vbox) */
2672
2673 static int lua_nodelib_vpack(lua_State * L)
2674 {
2675 halfword p;
2676 const char *s;
2677 int w = 0;
2678 int m = 1;
2679 int d = -1;
2680 halfword n = *(check_isnode(L, 1));
2681 if (lua_gettop(L) > 1) {
2682 w = lua_roundnumber(L, 2);
2683 if (lua_gettop(L) > 2) {
2684 if (lua_type(L, 3) == LUA_TSTRING) {
2685 s = lua_tostring(L, 3);
2686 if (lua_key_eq(s, additional)) {
2687 m = 1;
2688 } else if (lua_key_eq(s, exactly)) {
2689 m = 0;
2690 } else {
2691 luaL_error(L, "3rd argument should be either additional or exactly");
2692 }
2693
2694 if (lua_gettop(L) > 3) {
2695 if (lua_type(L, 4) == LUA_TNUMBER) {
2696 d = nodelib_getdirection(L, 4);
2697 } else if (lua_type(L, 4) == LUA_TSTRING) {
2698 d = nodelib_getdir_par(L, 4);
2699 } else {
2700 lua_pushstring(L, "incorrect 4th argument");
2701 }
2702 }
2703 }
2704
2705 else if (lua_type(L, 3) == LUA_TNUMBER) {
2706 m= (int) lua_tointeger(L, 3);
2707 } else {
2708 lua_pushstring(L, "incorrect 3rd argument");
2709 }
2710 }
2711 }
2712 p = vpackage(n, w, m, max_dimen, d);
2713 lua_nodelib_push_fast(L, p);
2714 lua_pushinteger(L, last_badness);
2715 return 2;
2716 }
2717
2718 /* node.direct.vpack */
2719
2720 static int lua_nodelib_direct_vpack(lua_State * L)
2721 {
2722 halfword p;
2723 const char *s;
2724 int w = 0;
2725 int m = 1;
2726 int d = -1;
2727 halfword n = (halfword) lua_tointeger(L,1);
2728 if (lua_gettop(L) > 1) {
2729 w = lua_roundnumber(L, 2);
2730 if (lua_gettop(L) > 2) {
2731 if (lua_type(L, 3) == LUA_TSTRING) {
2732 s = lua_tostring(L, 3);
2733 if (lua_key_eq(s, additional)) {
2734 m = 1;
2735 } else if (lua_key_eq(s, exactly)) {
2736 m = 0;
2737 } else {
2738 luaL_error(L, "3rd argument should be either additional or exactly");
2739 }
2740
2741 if (lua_gettop(L) > 3) {
2742 if (lua_type(L, 4) == LUA_TNUMBER) {
2743 d = nodelib_getdirection(L, 4);
2744 } else if (lua_type(L, 4) == LUA_TSTRING) {
2745 d = nodelib_getdir_par(L, 4);
2746 } else {
2747 lua_pushstring(L, "incorrect 4th argument");
2748 }
2749 }
2750 }
2751
2752 else if (lua_type(L, 3) == LUA_TNUMBER) {
2753 m= (int) lua_tointeger(L, 3);
2754 } else {
2755 lua_pushstring(L, "incorrect 3rd argument");
2756 }
2757 }
2758 }
2759 p = vpackage(n, w, m, max_dimen, d);
2760 lua_pushinteger(L, p);
2761 lua_pushinteger(L, last_badness);
2762 return 2;
2763 }
2764
2765 /* node.dimensions (of a hlist or vlist) */
2766
2767 static int lua_nodelib_dimensions(lua_State * L)
2768 {
2769 int top = lua_gettop(L);
2770 if (top > 0) {
2771 scaled_whd siz;
2772 glue_ratio g_mult = 1.0;
2773 int g_sign = normal;
2774 int g_order = normal;
2775 int i = 1;
2776 int d = -1;
2777 halfword n = null, p = null;
2778 if (lua_type(L, 1) == LUA_TNUMBER) {
2779 if (top < 4) {
2780 lua_pushnil(L);
2781 return 1;
2782 }
2783 i += 3;
2784 g_mult = (glue_ratio) lua_tonumber(L, 1); /* integer or float */
2785 g_sign = (int) lua_tointeger(L, 2);
2786 g_order = (int) lua_tointeger(L, 3);
2787 }
2788 n = *(check_isnode(L, i));
2789 if (lua_gettop(L) > i && !lua_isnil(L, (i + 1))) {
2790 if (lua_type(L, (i + 1)) == LUA_TSTRING) {
2791 d = nodelib_getdir_par(L, (i + 1));
2792 } else {
2793 p = *(check_isnode(L, (i + 1)));
2794 }
2795 }
2796 if (lua_gettop(L) > (i + 1)) {
2797 if (lua_type(L, (i + 2)) == LUA_TNUMBER) {
2798 d = nodelib_getdirection(L, (i + 2));
2799 } else if (lua_type(L, (i + 2)) == LUA_TSTRING) {
2800 d = nodelib_getdir_par(L, (i + 2));
2801 }
2802 }
2803 siz = natural_sizes(n, p, g_mult, g_sign, g_order, d);
2804 lua_pushinteger(L, siz.wd);
2805 lua_pushinteger(L, siz.ht);
2806 lua_pushinteger(L, siz.dp);
2807 return 3;
2808 } else {
2809 luaL_error(L, "missing argument to 'dimensions' (node expected)");
2810 }
2811 return 0; /* not reached */
2812 }
2813
2814 static int lua_nodelib_rangedimensions(lua_State * L) /* parent, first, last */
2815 {
2816 int top = lua_gettop(L);
2817 if (top > 1) {
2818 scaled_whd siz;
2819 halfword l = *(check_isnode(L, 1)); /* parent */
2820 halfword n = *(check_isnode(L, 2)); /* first */
2821 halfword p = null;
2822 if (top > 2) {
2823 p = *(check_isnode(L, 3)); /* last */
2824 }
2825 siz = natural_sizes(n, p, (glue_ratio) glue_set(l), glue_sign(l), glue_order(l), box_dir(l));
2826 lua_pushinteger(L, siz.wd);
2827 lua_pushinteger(L, siz.ht);
2828 lua_pushinteger(L, siz.dp);
2829 return 3;
2830 } else {
2831 luaL_error(L, "missing argument to 'rangedimensions' (2 or more nodes expected)");
2832 }
2833 return 0; /* not reached */
2834 }
2835
2836 /* node.direct.dimensions*/
2837
2838 static int lua_nodelib_direct_dimensions(lua_State * L)
2839 {
2840 int top = lua_gettop(L);
2841 if (top > 0) {
2842 scaled_whd siz;
2843 glue_ratio g_mult = 1.0;
2844 int g_sign = normal;
2845 int g_order = normal;
2846 int i = 1;
2847 int d = -1;
2848 halfword n = null;
2849 halfword p = null;
2850 if (top > 3) {
2851 i += 3;
2852 g_mult = (glue_ratio) lua_tonumber(L, 1); /* integer or float */
2853 g_sign = (int) lua_tointeger(L, 2);
2854 g_order = (int) lua_tointeger(L, 3);
2855 }
2856 n = (halfword) lua_tointeger(L,i);
2857 if (lua_gettop(L) > i && !lua_isnil(L, (i + 1))) {
2858 if (lua_type(L, (i + 1)) == LUA_TSTRING) {
2859 d = nodelib_getdir_par(L, (i + 1));
2860 } else {
2861 p = (halfword) lua_tointeger(L,i+1);
2862 }
2863 }
2864 if (lua_gettop(L) > (i + 1)) {
2865 if (lua_type(L, (i + 2)) == LUA_TNUMBER) {
2866 d = nodelib_getdirection(L, (i + 2));
2867 } else if (lua_type(L, (i + 2)) == LUA_TSTRING) {
2868 d = nodelib_getdir_par(L, (i + 2));
2869 }
2870 }
2871 siz = natural_sizes(n, p, g_mult, g_sign, g_order, d);
2872 lua_pushinteger(L, siz.wd);
2873 lua_pushinteger(L, siz.ht);
2874 lua_pushinteger(L, siz.dp);
2875 return 3;
2876 } else {
2877 luaL_error(L, "missing argument to 'dimensions' (direct node expected)");
2878 }
2879 return 0; /* not reached */
2880 }
2881
2882 static int lua_nodelib_direct_rangedimensions(lua_State * L) /* parent, first, last */
2883 {
2884 int top = lua_gettop(L);
2885 if (top > 1) {
2886 scaled_whd siz;
2887 halfword l = (halfword) lua_tointeger(L,1); /* parent */
2888 halfword n = (halfword) lua_tointeger(L,2); /* first */
2889 halfword p = null;
2890 if (top > 2) {
2891 p = (halfword) lua_tointeger(L,3); /* last */
2892 }
2893 siz = natural_sizes(n, p, (glue_ratio) glue_set(l), glue_sign(l), glue_order(l), box_dir(l));
2894 lua_pushinteger(L, siz.wd);
2895 lua_pushinteger(L, siz.ht);
2896 lua_pushinteger(L, siz.dp);
2897 return 3;
2898 } else {
2899 luaL_error(L, "missing argument to 'rangedimensions' (2 or more direct nodes expected)");
2900 }
2901 return 0; /* not reached */
2902 }
2903
2904 /* node.mlist_to_hlist (create a hlist from a formula) */
2905
2906 static int lua_nodelib_mlist_to_hlist(lua_State * L)
2907 {
2908 int w;
2909 boolean m;
2910 halfword n = *(check_isnode(L, 1));
2911 assign_math_style(L,2,w);
2912 luaL_checkany(L, 3);
2913 m = lua_toboolean(L, 3);
2914 mlist_to_hlist(n, m, w);
2915 alink(vlink(temp_head)) = null;
2916 lua_nodelib_push_fast(L, vlink(temp_head));
2917 return 1;
2918 }
2919
2920 /* node.family_font */
2921
2922 static int lua_nodelib_mfont(lua_State * L)
2923 {
2924 int s;
2925 int f = luaL_checkinteger(L, 1);
2926 if (lua_gettop(L) == 2)
2927 s = lua_tointeger(L, 2); /* this should be a multiple of 256 ! */
2928 else
2929 s = 0;
2930 lua_pushinteger(L, fam_fnt(f,s));
2931 return 1;
2932 }
2933
2934 /*
2935 This function is similar to |get_node_type_id|, for field
2936 identifiers. It has to do some more work, because not all
2937 identifiers are valid for all types of nodes.
2938
2939 We can make this faster if needed but when this needs to
2940 be called often something is wrong with the code.
2941
2942 */
2943
2944 static int get_node_field_id(lua_State * L, int n, int node)
2945 {
2946 int t = type(node);
2947 const char *s = lua_tostring(L, n);
2948
2949 if (s == NULL)
2950 return -2;
2951
2952 if (lua_key_eq(s, next)) {
2953 return 0;
2954 } else if (lua_key_eq(s, id)) {
2955 return 1;
2956 } else if (lua_key_eq(s, subtype)) {
2957 if (nodetype_has_subtype(t)) {
2958 return 2;
2959 }
2960 } else if (lua_key_eq(s, attr)) {
2961 if (nodetype_has_attributes(t)) {
2962 return 3;
2963 }
2964 } else if (lua_key_eq(s, prev)) {
2965 if (nodetype_has_prev(t)) {
2966 return -1;
2967 }
2968 } else {
2969 int j;
2970 field_info *fields ;
2971 if (t == whatsit_node) {
2972 fields = whatsit_node_data[subtype(node)].fields;
2973 } else {
2974 fields = node_data[t].fields;
2975 }
2976 if (lua_key_eq(s, list)) {
2977 s = lua_key(head);
2978 }
2979 if (fields != NULL) {
2980 for (j = 0; fields[j].lua != 0; j++) {
2981 // if (strcmp(s, fields[j]) == 0) {
2982 if (fields[j].name == s) {
2983 return j + 3;
2984 }
2985 }
2986 }
2987 }
2988 return -2;
2989 }
2990
2991 /* node.has_field */
2992
2993 static int lua_nodelib_has_field(lua_State * L)
2994 {
2995 int i = -2;
2996 if (!lua_isnil(L, 1))
2997 i = get_node_field_id(L, 2, *(check_isnode(L, 1)));
2998 lua_pushboolean(L, (i != -2));
2999 return 1;
3000 }
3001
3002 /* node.is_char (node[,font]) */
3003
3004 static int lua_nodelib_is_char(lua_State * L)
3005 {
3006 halfword n = *(check_isnode(L, 1));
3007 if (type(n) != glyph_node) {
3008 lua_pushnil(L); /* no glyph at all */
3009 lua_pushinteger(L,type(n)); /* can save a lookup call */
3010 return 2;
3011 } else if (is_protected_glyph_node(n)) {
3012 lua_pushboolean(L,0); /* a done glyph */
3013 } else if (lua_type(L,2) == LUA_TNUMBER) {
3014 halfword f = lua_tointeger(L, 2);
3015 if (f && f == font(n)) {
3016 lua_pushinteger(L,character(n)); /* a todo glyph in the asked font */
3017 } else {
3018 lua_pushboolean(L,0); /* a todo glyph in another font */
3019 }
3020 } else {
3021 lua_pushinteger(L,character(n)); /* a todo glyph */
3022 }
3023 return 1;
3024 }
3025
3026 static int lua_nodelib_is_glyph(lua_State * L)
3027 {
3028 halfword n = *(check_isnode(L, 1));
3029 if (type(n) != glyph_node) {
3030 lua_pushboolean(L,0);
3031 lua_pushinteger(L,type(n));
3032 } else {
3033 lua_pushinteger(L,character(n));
3034 lua_pushinteger(L,font(n));
3035 }
3036 return 2;
3037 }
3038
3039 /* node.direct.has_field */
3040
3041 static int lua_nodelib_direct_has_field(lua_State * L)
3042 {
3043 int i = -2;
3044 halfword n = lua_tointeger(L, 1);
3045 if (n != null)
3046 i = get_node_field_id(L, 2, n);
3047 lua_pushboolean(L, (i != -2));
3048 return 1;
3049 }
3050
3051 /* fetch the list of valid node types */
3052
3053 static int do_lua_nodelib_types(lua_State * L, node_info * data)
3054 {
3055 int i;
3056 lua_newtable(L);
3057 for (i = 0; data[i].id != -1; i++) {
3058 lua_pushstring(L, data[i].name);
3059 lua_rawseti(L, -2, data[i].id);
3060 }
3061 return 1;
3062 }
3063
3064 /* node.types */
3065
3066 static int lua_nodelib_types(lua_State * L)
3067 {
3068 return do_lua_nodelib_types(L, node_data);
3069 }
3070
3071 /* node.whatsits */
3072
3073 static int lua_nodelib_whatsits(lua_State * L)
3074 {
3075 return do_lua_nodelib_types(L, whatsit_node_data);
3076 }
3077
3078 /* node.fields (fetch the list of valid fields) */
3079
3080 static int lua_nodelib_fields(lua_State * L)
3081 {
3082 int i = -1;
3083 int offset = 2;
3084 field_info *fields;
3085 int t = get_valid_node_type_id(L, 1);
3086 if (t == whatsit_node) {
3087 t = get_valid_node_subtype_id(L, 2);
3088 fields = whatsit_node_data[t].fields;
3089 } else {
3090 fields = node_data[t].fields;
3091 }
3092 lua_checkstack(L, 2);
3093 lua_newtable(L);
3094 lua_push_string_by_name(L,next);
3095 lua_rawseti(L, -2, 0);
3096 lua_push_string_by_name(L,id);
3097 lua_rawseti(L, -2, 1);
3098 if (nodetype_has_subtype(t)) {
3099 lua_push_string_by_name(L,subtype);
3100 lua_rawseti(L, -2, 2);
3101 offset++;
3102 }
3103 if (nodetype_has_prev(t)) {
3104 lua_push_string_by_name(L,prev);
3105 lua_rawseti(L, -2, -1);
3106 }
3107 if (fields != NULL) {
3108 for (i = 0; fields[i].lua != 0; i++) {
3109 // lua_pushstring(L, fields[i]); /* todo */
3110 lua_rawgeti(L, LUA_REGISTRYINDEX, fields[i].lua);
3111 lua_rawseti(L, -2, (i + offset));
3112 }
3113 }
3114 return 1;
3115 }
3116
3117 static int lua_nodelib_values(lua_State * L)
3118 {
3119 int i = -1;
3120 subtype_info *values = NULL;
3121 const char *s ;
3122 int t = lua_type(L,1);
3123 if (t == LUA_TSTRING) {
3124 /*
3125 delimiter options (bit set)
3126 delimiter modes (bit set)
3127 */
3128 s = lua_tostring(L,1);
3129 if (lua_key_eq(s,dir)) values = node_values_dir;
3130 else if (lua_key_eq(s,direction)) values = node_values_dir;
3131 else if (lua_key_eq(s,glue)) values = node_values_fill;
3132 /* backend */
3133 else if (lua_key_eq(s,pdf_literal)) values = node_values_pdf_literal;
3134 else if (lua_key_eq(s,pdf_action)) values = node_values_pdf_action;
3135 else if (lua_key_eq(s,pdf_window)) values = node_values_pdf_window;
3136 else if (lua_key_eq(s,color_stack)) values = node_values_color_stack;
3137 /* extras */
3138 else if (lua_key_eq(s,pagestate)) values = other_values_page_states;
3139 }
3140 if (values != NULL) {
3141 lua_checkstack(L, 2);
3142 lua_newtable(L);
3143 for (i = 0; values[i].id >= 0 ; i++) {
3144 lua_rawgeti(L, LUA_REGISTRYINDEX, values[i].lua);
3145 lua_rawseti(L, -2, values[i].id);
3146 }
3147 } else {
3148 lua_pushnil(L);
3149 }
3150 return 1;
3151 }
3152
3153 static int lua_nodelib_subtypes(lua_State * L)
3154 {
3155 int i = -1;
3156 subtype_info *subtypes = NULL;
3157 const char *s ;
3158 int t = lua_type(L,1);
3159 if (t == LUA_TSTRING) {
3160 /* official accessors */
3161 s = lua_tostring(L,1);
3162 if (lua_key_eq(s,glyph)) subtypes = node_subtypes_glyph;
3163 else if (lua_key_eq(s,glue)) subtypes = node_subtypes_glue;
3164 else if (lua_key_eq(s,dir)) subtypes = node_subtypes_dir;
3165 else if (lua_key_eq(s,boundary)) subtypes = node_subtypes_boundary;
3166 else if (lua_key_eq(s,penalty)) subtypes = node_subtypes_penalty;
3167 else if (lua_key_eq(s,kern)) subtypes = node_subtypes_kern;
3168 else if (lua_key_eq(s,rule)) subtypes = node_subtypes_rule;
3169 else if (lua_key_eq(s,list)
3170 || lua_key_eq(s,hlist)
3171 || lua_key_eq(s,vlist)) subtypes = node_subtypes_list; /* too many but ok as reserved */
3172 else if (lua_key_eq(s,adjust)) subtypes = node_subtypes_adjust;
3173 else if (lua_key_eq(s,disc)) subtypes = node_subtypes_disc;
3174 else if (lua_key_eq(s,fill)) subtypes = node_values_fill;
3175 else if (lua_key_eq(s,leader)) subtypes = node_subtypes_leader;
3176 else if (lua_key_eq(s,marginkern)) subtypes = node_subtypes_marginkern;
3177 else if (lua_key_eq(s,math)) subtypes = node_subtypes_math;
3178 else if (lua_key_eq(s,noad)) subtypes = node_subtypes_noad;
3179 else if (lua_key_eq(s,radical)) subtypes = node_subtypes_radical;
3180 else if (lua_key_eq(s,accent)) subtypes = node_subtypes_accent;
3181 else if (lua_key_eq(s,fence)) subtypes = node_subtypes_fence;
3182 /* backend */
3183 else if (lua_key_eq(s,pdf_destination)) subtypes = node_values_pdf_destination;
3184 else if (lua_key_eq(s,pdf_literal)) subtypes = node_values_pdf_literal;
3185 } else if (t == LUA_TNUMBER) {
3186 /* maybe */
3187 t = lua_tointeger(L,1);
3188 if (t == glyph_node) subtypes = node_subtypes_glyph;
3189 else if (t == glue_node) subtypes = node_subtypes_glue;
3190 else if (t == dir_node) subtypes = node_subtypes_dir;
3191 else if (t == boundary_node) subtypes = node_subtypes_boundary;
3192 else if (t == penalty_node) subtypes = node_subtypes_penalty;
3193 else if (t == kern_node) subtypes = node_subtypes_kern;
3194 else if (t == rule_node) subtypes = node_subtypes_rule;
3195 else if((t == hlist_node)
3196 || (t == vlist_node)) subtypes = node_subtypes_list;
3197 else if (t == adjust_node) subtypes = node_subtypes_adjust;
3198 else if (t == disc_node) subtypes = node_subtypes_disc;
3199 else if (t == glue_spec_node) subtypes = node_values_fill;
3200 else if (t == margin_kern_node) subtypes = node_subtypes_marginkern;
3201 else if (t == math_node) subtypes = node_subtypes_math;
3202 else if (t == simple_noad) subtypes = node_subtypes_noad;
3203 else if (t == radical_noad) subtypes = node_subtypes_radical;
3204 else if (t == accent_noad) subtypes = node_subtypes_accent;
3205 else if (t == fence_noad) subtypes = node_subtypes_fence;
3206 /* backend */
3207 else if (t == pdf_dest_node) subtypes = node_values_pdf_destination;
3208 else if((t == pdf_literal_node)
3209 || (t == pdf_late_literal_node)) subtypes = node_values_pdf_literal;
3210 }
3211 if (subtypes != NULL) {
3212 lua_checkstack(L, 2);
3213 lua_newtable(L);
3214 for (i = 0; subtypes[i].id >= 0 ; i++) {
3215 lua_rawgeti(L, LUA_REGISTRYINDEX, subtypes[i].lua);
3216 lua_rawseti(L, -2, subtypes[i].id);
3217 }
3218 } else {
3219 lua_pushnil(L);
3220 }
3221 return 1;
3222 }
3223
3224 /* node.slide (find the end of a list and add prev links) */
3225
3226 static int lua_nodelib_slide(lua_State * L)
3227 {
3228 halfword n;
3229 if (lua_isnil(L, 1))
3230 return 1; /* the nil itself */
3231 n = *check_isnode(L, 1);
3232 if (n == null)
3233 return 1; /* the old userdata */
3234 /* alink(t) = null; */ /* don't do this, |t|'s |alink| may be a valid pointer */
3235 while (vlink(n) != null) {
3236 alink(vlink(n)) = n;
3237 n = vlink(n);
3238 }
3239 lua_nodelib_push_fast(L, n);
3240 return 1;
3241 }
3242
3243 /* node.direct.slide */
3244
3245 static int lua_nodelib_direct_slide(lua_State * L)
3246 {
3247 halfword n = lua_tointeger(L, 1);
3248 if (n == null) {
3249 lua_pushnil(L);
3250 } else {
3251 while (vlink(n) != null) {
3252 alink(vlink(n)) = n;
3253 n = vlink(n);
3254 }
3255 lua_pushinteger(L, n);
3256 }
3257 return 1;
3258 }
3259
3260 /* node.tail (find the end of a list) */
3261
3262 static int lua_nodelib_tail(lua_State * L)
3263 {
3264 halfword n;
3265 if (lua_isnil(L, 1))
3266 return 1; /* the nil itself */
3267 n = *check_isnode(L, 1);
3268 if (n == null)
3269 return 1; /* the old userdata */
3270 while (vlink(n) != null)
3271 n = vlink(n);
3272 lua_nodelib_push_fast(L, n);
3273 return 1;
3274 }
3275
3276 /* node.direct.tail */
3277
3278 static int lua_nodelib_direct_tail(lua_State * L)
3279 {
3280 halfword n = lua_tointeger(L, 1);
3281 if (n == null) {
3282 lua_pushnil(L);
3283 } else {
3284 while (vlink(n) != null)
3285 n = vlink(n);
3286 lua_pushinteger(L, n);
3287 }
3288 return 1;
3289 }
3290
3291 /* node.end_of_math (skip over math and return last) */
3292
3293 static int lua_nodelib_end_of_math(lua_State * L)
3294 {
3295 halfword n;
3296 if (lua_isnil(L, 1))
3297 return 0;
3298 n = *check_isnode(L, 1);
3299 if (n == null)
3300 return 0;
3301 if (type(n) == math_node && (subtype(n) == 1)) {
3302 lua_nodelib_push_fast(L, n);
3303 return 1;
3304 }
3305 while (vlink(n) != null) {
3306 n = vlink(n);
3307 if (n && (type(n) == math_node) && (subtype(n) == 1)) {
3308 lua_nodelib_push_fast(L, n);
3309 return 1;
3310 }
3311 }
3312 return 0;
3313 }
3314
3315 /* node.direct.end_of_math */
3316
3317 static int lua_nodelib_direct_end_of_math(lua_State * L)
3318 {
3319 halfword n = lua_tointeger(L, 1);
3320 if (n == null)
3321 return 0;
3322 if ((type(n)==math_node && (subtype(n)==1))) {
3323 lua_pushinteger(L, n);
3324 return 1;
3325 }
3326 while (vlink(n) != null) {
3327 n = vlink(n);
3328 if (n && (type(n)==math_node) && (subtype(n)==1)) {
3329 lua_pushinteger(L, n);
3330 return 1;
3331 }
3332 }
3333 return 0;
3334 }
3335
3336
3337 /* node.has_attribute (gets attribute) */
3338
3339 static int lua_nodelib_has_attribute(lua_State * L)
3340 {
3341 int i, val;
3342 halfword n = *check_isnode(L, 1);
3343 if (n != null) {
3344 i = lua_tointeger(L, 2);
3345 val = luaL_optinteger(L, 3, UNUSED_ATTRIBUTE);
3346 if ((val = has_attribute(n, i, val)) > UNUSED_ATTRIBUTE) {
3347 lua_pushinteger(L, val);
3348 return 1;
3349 }
3350 }
3351 lua_pushnil(L);
3352 return 1;
3353 }
3354
3355 /* node.direct.has_attribute */
3356
3357 static int lua_nodelib_direct_has_attribute(lua_State * L)
3358 {
3359 int i, val;
3360 halfword n = lua_tointeger(L, 1);
3361 if (n != null) {
3362 i = lua_tointeger(L, 2);
3363 val = luaL_optinteger(L, 3, UNUSED_ATTRIBUTE);
3364 if ((val = has_attribute(n, i, val)) > UNUSED_ATTRIBUTE) {
3365 lua_pushinteger(L, val);
3366 return 1;
3367 }
3368 }
3369 lua_pushnil(L);
3370 return 1;
3371 }
3372
3373 /* node.get_attribute */
3374
3375 static int lua_nodelib_get_attribute(lua_State * L)
3376 {
3377 halfword p = *check_isnode(L, 1);
3378 if (nodetype_has_attributes(type(p))) {
3379 p = node_attr(p);
3380 if (p != null) {
3381 p = vlink(p);
3382 if (p != null) {
3383 int i = 0;
3384 if (lua_gettop(L) > 1) {
3385 i = lua_tointeger(L, 2);
3386 }
3387 while (p != null) {
3388 if (attribute_id(p) == i) {
3389 int ret = attribute_value(p);
3390 if (ret == UNUSED_ATTRIBUTE) {
3391 break;
3392 } else {
3393 lua_pushinteger(L,ret);
3394 return 1;
3395 }
3396 } else if (attribute_id(p) > i) {
3397 break;
3398 }
3399 p = vlink(p);
3400 }
3401 }
3402 }
3403 }
3404 lua_pushnil(L);
3405 return 1;
3406 }
3407
3408 static int lua_nodelib_find_attribute(lua_State * L) /* returns attr value and node */
3409 {
3410 halfword c = *check_isnode(L, 1);
3411 halfword p ;
3412 int i = lua_tointeger(L, 2);
3413 while (c != null) {
3414 if (nodetype_has_attributes(type(c))) {
3415 p = node_attr(c);
3416 if (p != null) {
3417 p = vlink(p);
3418 while (p != null) {
3419 if (attribute_id(p) == i) {
3420 int ret = attribute_value(p);
3421 if (ret == UNUSED_ATTRIBUTE) {
3422 break;
3423 } else {
3424 lua_pushinteger(L,ret);
3425 lua_nodelib_push_fast(L, c );
3426 return 2;
3427 }
3428 } else if (attribute_id(p) > i) {
3429 break;
3430 }
3431 p = vlink(p);
3432 }
3433 }
3434 }
3435 c = vlink(c);
3436 }
3437 /*
3438 lua_pushnil(L);
3439 lua_pushnil(L);
3440 return 2;
3441 */
3442 return 0;
3443 }
3444
3445 /* node.direct.get_attribute */
3446 /* node.direct.set_attribute */
3447 /* node.direct.unset_attribute */
3448 /* node.direct.find_attribute */
3449
3450 static int lua_nodelib_direct_get_attribute(lua_State * L)
3451 {
3452 register halfword p = lua_tointeger(L, 1);
3453 if (nodetype_has_attributes(type(p))) {
3454 p = node_attr(p);
3455 if (p != null) {
3456 p = vlink(p);
3457 if (p != null) {
3458 int i = 0;
3459 if (lua_gettop(L) > 1) {
3460 i = lua_tointeger(L, 2);
3461 }
3462 while (p != null) {
3463 if (attribute_id(p) == i) {
3464 int ret = attribute_value(p);
3465 if (ret == UNUSED_ATTRIBUTE) {
3466 break;
3467 } else {
3468 lua_pushinteger(L,ret);
3469 return 1;
3470 }
3471 } else if (attribute_id(p) > i) {
3472 break;
3473 }
3474 p = vlink(p);
3475 }
3476 }
3477 }
3478 }
3479 lua_pushnil(L);
3480 return 1;
3481 }
3482
3483 static int lua_nodelib_direct_set_attribute(lua_State * L)
3484 {
3485 int i, val;
3486 halfword n = lua_tointeger(L, 1);
3487 if (n == null)
3488 return 0;
3489 if (lua_gettop(L) == 3) {
3490 i = (int) lua_tointeger(L, 2);
3491 val = (int) lua_tointeger(L, 3);
3492 if (val == UNUSED_ATTRIBUTE) {
3493 (void) unset_attribute(n, i, val);
3494 } else {
3495 set_attribute(n, i, val);
3496 }
3497 } else {
3498 luaL_error(L, "incorrect number of arguments");
3499 }
3500 return 0;
3501 }
3502
3503 static int lua_nodelib_direct_find_attribute(lua_State * L) /* returns attr value and node */
3504 {
3505 halfword c = lua_tointeger(L, 1);
3506 halfword p ;
3507 int i = lua_tointeger(L, 2);
3508 while (c != null) {
3509 if (nodetype_has_attributes(type(c))) {
3510 p = node_attr(c);
3511 if (p != null) {
3512 p = vlink(p);
3513 while (p != null) {
3514 if (attribute_id(p) == i) {
3515 int ret = attribute_value(p);
3516 if (ret == UNUSED_ATTRIBUTE) {
3517 break;
3518 } else {
3519 lua_pushinteger(L,ret);
3520 lua_pushinteger(L,c);
3521 return 2;
3522 }
3523 } else if (attribute_id(p) > i) {
3524 break;
3525 }
3526 p = vlink(p);
3527 }
3528 }
3529 }
3530 c = vlink(c);
3531 }
3532 /*
3533 lua_pushnil(L);
3534 lua_pushnil(L);
3535 return 2;
3536 */
3537 return 0;
3538 }
3539
3540 static int lua_nodelib_direct_unset_attribute(lua_State * L)
3541 {
3542 halfword n = lua_tointeger(L, 1);
3543 if (n == null) {
3544 lua_pushnil(L);
3545 } else if (lua_gettop(L) <= 3) { /* a useless test, we never test for that elsewhere */
3546 int i = (int)luaL_checknumber(L, 2);
3547 int val = (int)luaL_optnumber(L, 3, UNUSED_ATTRIBUTE);
3548 int ret = unset_attribute(n, i, val);
3549 if (ret > UNUSED_ATTRIBUTE) {
3550 lua_pushinteger(L, ret);
3551 } else {
3552 lua_pushnil(L);
3553 }
3554 } else { /* can go */
3555 return luaL_error(L, "incorrect number of arguments");
3556 }
3557 return 1;
3558 }
3559
3560 /* node.set_attribute */
3561 /* node.unset_attribute */
3562
3563 static int lua_nodelib_set_attribute(lua_State * L)
3564 {
3565 if (lua_gettop(L) == 3) {
3566 int i = lua_tointeger(L, 2);
3567 int val = lua_tointeger(L, 3);
3568 halfword n = *check_isnode(L, 1);
3569 if (val == UNUSED_ATTRIBUTE) {
3570 (void) unset_attribute(n, i, val);
3571 } else {
3572 set_attribute(n, i, val);
3573 }
3574 } else {
3575 luaL_error(L, "incorrect number of arguments");
3576 }
3577 return 0;
3578 }
3579
3580 static int lua_nodelib_unset_attribute(lua_State * L)
3581 {
3582 if (lua_gettop(L) <= 3) {
3583 int i = luaL_checknumber(L, 2);
3584 int val = luaL_optnumber(L, 3, UNUSED_ATTRIBUTE);
3585 halfword n = *check_isnode(L, 1);
3586 int ret = unset_attribute(n, i, val);
3587 if (ret > UNUSED_ATTRIBUTE) {
3588 lua_pushinteger(L, ret);
3589 } else {
3590 lua_pushnil(L);
3591 }
3592 return 1;
3593 } else {
3594 return luaL_error(L, "incorrect number of arguments");
3595 }
3596 }
3597
3598 /* node.direct.getwidth */
3599 /* node.direct.setwidth */
3600 /* node.direct.getheight (for consistency) */
3601 /* node.direct.setheight (for consistency) */
3602 /* node.direct.getdepth (for consistency) */
3603 /* node.direct.setdepth (for consistency) */
3604
3605 /* split ifs for clearity .. compiler will optimize */
3606
3607 static int lua_nodelib_direct_getwidth(lua_State * L)
3608 {
3609 halfword n = lua_tointeger(L, 1);
3610 if (n) {
3611 halfword t = type(n);
3612 if (t == hlist_node || t == vlist_node || t == rule_node) {
3613 lua_pushinteger(L,width(n));
3614 } else if (t == glyph_node) {
3615 lua_pushnumber(L, char_width(font(n),character(n)));
3616 if (lua_toboolean(L,2)) {
3617 lua_pushinteger(L, ex_glyph(n));
3618 return 2;
3619 }
3620 } else if (t == glue_node || t == glue_spec_node || t == math_node || t == ins_node) {
3621 lua_pushinteger(L,width(n));
3622 } else if (t == kern_node) {
3623 lua_pushinteger(L, width(n));
3624 if (lua_toboolean(L,2)) {
3625 lua_pushinteger(L, ex_kern(n));
3626 return 2;
3627 }
3628 } else if (t == margin_kern_node) {
3629 lua_pushinteger(L,width(n));
3630 } else if (t == unset_node) {
3631 lua_pushinteger(L,width(n));
3632 } else {
3633 lua_pushnil(L);
3634 }
3635 } else {
3636 lua_pushnil(L);
3637 }
3638 return 1;
3639 }
3640
3641 static int lua_nodelib_direct_setwidth(lua_State * L)
3642 {
3643 halfword n = lua_tointeger(L, 1);
3644 if (n) {
3645 halfword t = type(n);
3646 if (t == hlist_node || t == vlist_node || t == rule_node || t == glue_node || t == glue_spec_node || t == math_node ||
3647 t == kern_node || t == margin_kern_node || t == ins_node || t == unset_node ||
3648 t == fraction_noad || t == radical_noad ) {
3649 if (lua_type(L, 2) == LUA_TNUMBER) {
3650 width(n) = lua_roundnumber(L,2);
3651 } else {
3652 width(n) = 0;
3653 }
3654 }
3655 }
3656 return 0;
3657 }
3658
3659 static int lua_nodelib_direct_getheight(lua_State * L)
3660 {
3661 halfword n = lua_tointeger(L, 1);
3662 if (n) {
3663 halfword t = type(n);
3664 if (t == hlist_node || t == vlist_node || t == rule_node) {
3665 lua_pushinteger(L,height(n));
3666 } else if (t == glyph_node) {
3667 lua_pushinteger(L, char_height(font(n),character(n)));
3668 } else if (t == unset_node || t == ins_node) {
3669 lua_pushinteger(L,height(n));
3670 } else if (t == fence_noad) {
3671 lua_pushinteger(L,delimiterheight(n));
3672 } else {
3673 lua_pushnil(L);
3674 }
3675 } else {
3676 lua_pushnil(L);
3677 }
3678 return 1;
3679 }
3680
3681 static int lua_nodelib_direct_setheight(lua_State * L)
3682 {
3683 halfword n = lua_tointeger(L, 1);
3684 if (n) {
3685 halfword t = type(n);
3686 halfword h = 0;
3687 if (lua_type(L, 2) == LUA_TNUMBER) {
3688 h = lua_roundnumber(L,2);
3689 }
3690 if (t == hlist_node || t == vlist_node || t == rule_node || t == unset_node) {
3691 height(n) = h;
3692 } else if (t == fence_noad) {
3693 delimiterheight(n) = h;
3694 }
3695 }
3696 return 0;
3697 }
3698
3699 static int lua_nodelib_direct_getdepth(lua_State * L)
3700 {
3701 halfword n = lua_tointeger(L, 1);
3702 if (n) {
3703 halfword t = type(n);
3704 if (t == hlist_node || t == vlist_node || t == rule_node) {
3705 lua_pushinteger(L,depth(n));
3706 } else if (t == glyph_node) {
3707 lua_pushinteger(L, char_depth(font(n),character(n)));
3708 } else if (t == unset_node || t == ins_node) {
3709 lua_pushinteger(L,depth(n));
3710 } else if (t == fence_noad) {
3711 lua_pushinteger(L,delimiterdepth(n));
3712 } else {
3713 lua_pushnil(L);
3714 }
3715 } else {
3716 lua_pushnil(L);
3717 }
3718 return 1;
3719 }
3720
3721 static int lua_nodelib_direct_setdepth(lua_State * L)
3722 {
3723 halfword n = lua_tointeger(L, 1);
3724 if (n) {
3725 halfword t = type(n);
3726 halfword d = 0;
3727 if (lua_type(L, 2) == LUA_TNUMBER) {
3728 d = lua_roundnumber(L,2);
3729 }
3730 if (t == hlist_node || t == vlist_node || t == rule_node || t == unset_node) {
3731 depth(n) = d;
3732 } else if (t == fence_noad) {
3733 delimiterdepth(n) = d;
3734 }
3735 }
3736 return 0;
3737 }
3738
3739 /* node.direct.getshift */
3740 /* node.direct.setshift */
3741
3742 static int lua_nodelib_direct_getshift(lua_State * L)
3743 {
3744 halfword n = lua_tointeger(L, 1);
3745 if (n) {
3746 halfword t = type(n);
3747 if (t == hlist_node || t == vlist_node) {
3748 lua_pushinteger(L,shift_amount(n));
3749 return 1;
3750 }
3751 }
3752 lua_pushnil(L);
3753 return 1;
3754 }
3755
3756 static int lua_nodelib_direct_setshift(lua_State * L)
3757 {
3758 halfword n = lua_tointeger(L, 1);
3759 if (n) {
3760 halfword t = type(n);
3761 if (t == hlist_node || t == vlist_node) {
3762 if (lua_type(L, 2) == LUA_TNUMBER) {
3763 shift_amount(n) = lua_roundnumber(L,2);
3764 } else {
3765 shift_amount(n) = 0;
3766 }
3767 }
3768 }
3769 return 0;
3770 }
3771
3772 /* node.direct.getglue */
3773 /* node.direct.setglue */
3774 /* node.direct.is_zero_glue */
3775
3776 static int lua_nodelib_direct_get_glue(lua_State * L)
3777 {
3778 halfword n = lua_tointeger(L, 1);
3779 if (n) {
3780 halfword t = type(n);
3781 if (t == glue_node || t == glue_spec_node || t == math_node || t == ins_node) {
3782 lua_pushinteger(L,width(n));
3783 lua_pushinteger(L,stretch(n));
3784 lua_pushinteger(L,shrink(n));
3785 lua_pushinteger(L,stretch_order(n));
3786 lua_pushinteger(L,shrink_order(n));
3787 return 5;
3788 } else if (t == hlist_node || t == vlist_node) {
3789 lua_pushnumber(L, (double) glue_set(n)); /* float */
3790 lua_pushinteger(L,glue_order(n));
3791 lua_pushinteger(L,glue_sign(n));
3792 return 3;
3793 }
3794 }
3795 return 0;
3796 }
3797
3798 static int lua_nodelib_direct_set_glue(lua_State * L)
3799 {
3800 halfword n = lua_tointeger(L, 1);
3801 if (n) {
3802 int top = lua_gettop(L);
3803 halfword t = type(n);
3804 if (t == glue_node || t == glue_spec_node || t == math_node) {
3805 width(n) = ((top > 1 && lua_type(L, 2) == LUA_TNUMBER)) ? lua_roundnumber(L,2) : 0;
3806 stretch(n) = ((top > 2 && lua_type(L, 3) == LUA_TNUMBER)) ? lua_roundnumber(L,3) : 0;
3807 shrink(n) = ((top > 3 && lua_type(L, 4) == LUA_TNUMBER)) ? lua_roundnumber(L,4) : 0;
3808 stretch_order(n) = ((top > 4 && lua_type(L, 5) == LUA_TNUMBER)) ? lua_tointeger(L,5) : 0;
3809 shrink_order(n) = ((top > 5 && lua_type(L, 6) == LUA_TNUMBER)) ? lua_tointeger(L,6) : 0;
3810 } else if (t == hlist_node || t == vlist_node) {
3811 glue_set(n) = ((top > 1 && lua_type(L, 2) == LUA_TNUMBER)) ? (glue_ratio) lua_tonumber(L,2) : 0;
3812 glue_order(n) = ((top > 2 && lua_type(L, 3) == LUA_TNUMBER)) ? lua_tointeger(L,3) : 0;
3813 glue_sign(n) = ((top > 3 && lua_type(L, 4) == LUA_TNUMBER)) ? lua_tointeger(L,4) : 0;
3814 return 3;
3815 }
3816 }
3817 return 0;
3818 }
3819
3820 static int lua_nodelib_direct_is_zero_glue(lua_State * L)
3821 {
3822 halfword n = lua_tointeger(L, 1);
3823 if (n) {
3824 halfword t = type(n);
3825 if (t == glue_node || t == glue_spec_node || t == math_node || t == ins_node) {
3826 lua_pushboolean(L,(width(n) == 0 && stretch(n) == 0 && shrink(n) == 0));
3827 return 1;
3828 } else if (t == hlist_node || t == vlist_node) {
3829 lua_pushboolean(L,(glue_set(n) == 0 && glue_order(n) == 0 && glue_sign(n) == 0));
3830 return 1;
3831 }
3832 }
3833 return 0;
3834 }
3835
3836 /* node.getglue */
3837 /* node.setglue */
3838 /* node.is_zero_glue */
3839
3840 static int lua_nodelib_get_glue(lua_State * L)
3841 {
3842 halfword n = *check_isnode(L, 1);
3843 if (n) {
3844 halfword t = type(n);
3845 if (t == glue_node || t == glue_spec_node || t == math_node || t == ins_node) {
3846 lua_pushinteger(L,width(n));
3847 lua_pushinteger(L,stretch(n));
3848 lua_pushinteger(L,shrink(n));
3849 lua_pushinteger(L,stretch_order(n));
3850 lua_pushinteger(L,shrink_order(n));
3851 return 5;
3852 } else if (t == hlist_node || t == vlist_node) {
3853 lua_pushnumber(L, (double) glue_set(n)); /* float */
3854 lua_pushinteger(L,glue_order(n));
3855 lua_pushinteger(L,glue_sign(n));
3856 return 3;
3857 }
3858 }
3859 return luaL_error(L, "glue (spec) or list expected");
3860 }
3861
3862 static int lua_nodelib_set_glue(lua_State * L)
3863 {
3864 halfword n = *check_isnode(L, 1);
3865 int top = lua_gettop(L) ;
3866 if (n) {
3867 halfword t = type(n);
3868 if (t == glue_node || t == glue_spec_node || t == math_node) {
3869 width(n) = ((top > 1 && lua_type(L, 2) == LUA_TNUMBER)) ? lua_roundnumber(L,2) : 0;
3870 stretch(n) = ((top > 2 && lua_type(L, 3) == LUA_TNUMBER)) ? lua_roundnumber(L,3) : 0;
3871 shrink(n) = ((top > 3 && lua_type(L, 4) == LUA_TNUMBER)) ? lua_roundnumber(L,4) : 0;
3872 stretch_order(n) = ((top > 4 && lua_type(L, 5) == LUA_TNUMBER)) ? lua_tointeger(L,5) : 0;
3873 shrink_order(n) = ((top > 5 && lua_type(L, 6) == LUA_TNUMBER)) ? lua_tointeger(L,6) : 0;
3874 } else if (t == hlist_node || t == vlist_node) {
3875 glue_set(n) = ((top > 1 && lua_type(L, 2) == LUA_TNUMBER)) ? (glue_ratio) lua_tonumber(L,2) : 0;
3876 glue_order(n) = ((top > 2 && lua_type(L, 3) == LUA_TNUMBER)) ? lua_tointeger(L,3) : 0;
3877 glue_sign(n) = ((top > 3 && lua_type(L, 4) == LUA_TNUMBER)) ? lua_tointeger(L,4) : 0;
3878 return 3;
3879 }
3880 return 0;
3881 }
3882 return luaL_error(L, "glue (spec) or list expected");
3883 }
3884
3885 static int lua_nodelib_is_zero_glue(lua_State * L)
3886 {
3887 halfword n = *check_isnode(L, 1);
3888 if (n != null) {
3889 halfword t = type(n);
3890 if (t == glue_node || t == glue_spec_node || t == math_node || t == ins_node) {
3891 lua_pushboolean(L,(width(n) == 0 && stretch(n) == 0 && shrink(n) == 0));
3892 return 1;
3893 } else if (t == hlist_node || t == vlist_node) {
3894 lua_pushboolean(L,(glue_set(n) == 0 && glue_order(n) == 0 && glue_sign(n) == 0));
3895 return 1;
3896 }
3897 }
3898 return luaL_error(L, "glue (spec) or list expected");
3899 }
3900
3901 /* iteration */
3902
3903 static int nodelib_aux_nil(lua_State * L)
3904 {
3905 lua_pushnil(L);
3906 return 1;
3907 }
3908
3909 /* node.direct.traverse */
3910 /* node.direct.traverse_id */
3911 /* node.direct.traverse_char */
3912 /* node.direct.traverse_glyph */
3913 /* node.direct.traverse_list */
3914
3915 static int nodelib_direct_aux_next(lua_State * L)
3916 {
3917 halfword t;
3918 if (lua_isnil(L, 2)) {
3919 t = lua_tointeger(L,1) ;
3920 lua_settop(L,1);
3921 } else {
3922 t = lua_tointeger(L,2) ;
3923 t = vlink(t);
3924 lua_settop(L,2);
3925 }
3926 if (t == null) {
3927 lua_pushnil(L);
3928 return 1;
3929 } else {
3930 lua_pushinteger(L,t);
3931 lua_pushinteger(L,type(t));
3932 lua_pushinteger(L,subtype(t));
3933 return 3;
3934 }
3935 }
3936
3937 static int lua_nodelib_direct_traverse(lua_State * L)
3938 {
3939 halfword n;
3940 if (lua_isnil(L, 1)) {
3941 lua_pushcclosure(L, nodelib_aux_nil, 0);
3942 return 1;
3943 }
3944 n = (halfword) lua_tointeger(L, 1);
3945 if (n == null) {
3946 lua_pushcclosure(L, nodelib_aux_nil, 0);
3947 return 1;
3948 }
3949 lua_pushcclosure(L, nodelib_direct_aux_next, 0);
3950 lua_pushinteger(L,n);
3951 lua_pushnil(L);
3952 return 3;
3953 }
3954
3955 static int nodelib_direct_aux_next_filtered(lua_State * L)
3956 {
3957 halfword t; /* traverser */
3958 int i = (int) lua_tointeger(L, lua_upvalueindex(1));
3959 if (lua_isnil(L, 2)) { /* first call */
3960 t = lua_tointeger(L,1) ;
3961 lua_settop(L,1);
3962 } else {
3963 t = lua_tointeger(L,2) ;
3964 t = vlink(t);
3965 lua_settop(L,2);
3966 }
3967 while (t != null && type(t) != i) {
3968 t = vlink(t);
3969 }
3970 if (t == null) {
3971 lua_pushnil(L);
3972 return 1;
3973 } else {
3974 lua_pushinteger(L,t);
3975 lua_pushinteger(L,subtype(t));
3976 return 2;
3977 }
3978 }
3979
3980 static int lua_nodelib_direct_traverse_filtered(lua_State * L)
3981 {
3982 halfword n;
3983 if (lua_isnil(L, 2)) {
3984 lua_pushcclosure(L, nodelib_aux_nil, 0);
3985 return 1;
3986 }
3987 n = (halfword) lua_tointeger(L, 2);
3988 if (n == null)
3989 return 0;
3990 lua_pop(L, 1);
3991 lua_pushcclosure(L, nodelib_direct_aux_next_filtered, 1);
3992 lua_pushinteger(L,n);
3993 lua_pushnil(L);
3994 return 3;
3995 }
3996
3997 static int nodelib_direct_aux_next_char(lua_State * L)
3998 {
3999 halfword t; /* traverser */
4000 if (lua_isnil(L, 2)) { /* first call */
4001 t = lua_tointeger(L,1) ;
4002 lua_settop(L,1);
4003 } else {
4004 t = lua_tointeger(L,2) ;
4005 t = vlink(t);
4006 lua_settop(L,2);
4007 }
4008 while ((t != null) && (type(t) != glyph_node || is_protected_glyph_node(t))) {
4009 t = vlink(t);
4010 }
4011 if (t == null) {
4012 lua_pushnil(L);
4013 return 1;
4014 } else {
4015 lua_pushinteger(L,t);
4016 lua_pushinteger(L,character(t));
4017 lua_pushinteger(L,font(t));
4018 return 3;
4019 }
4020 }
4021
4022 static int lua_nodelib_direct_traverse_char(lua_State * L)
4023 {
4024 halfword n;
4025 if (lua_isnil(L, 1)) {
4026 lua_pushcclosure(L, nodelib_aux_nil, 0);
4027 return 1;
4028 }
4029 n = (halfword) lua_tointeger(L, 1);
4030 if (n == null) {
4031 lua_pushcclosure(L, nodelib_aux_nil, 0);
4032 return 1;
4033 }
4034 lua_pushcclosure(L, nodelib_direct_aux_next_char, 0);
4035 lua_pushinteger(L,n);
4036 lua_pushnil(L);
4037 return 3;
4038 }
4039
4040 static int nodelib_direct_aux_next_glyph(lua_State * L)
4041 {
4042 halfword t; /* traverser */
4043 if (lua_isnil(L, 2)) { /* first call */
4044 t = lua_tointeger(L,1) ;
4045 lua_settop(L,1);
4046 } else {
4047 t = lua_tointeger(L,2) ;
4048 t = vlink(t);
4049 lua_settop(L,2);
4050 }
4051 while (t != null && type(t) != glyph_node) {
4052 t = vlink(t);
4053 }
4054 if (t == null) {
4055 lua_pushnil(L);
4056 return 1;
4057 } else {
4058 lua_pushinteger(L,t);
4059 lua_pushinteger(L,character(t));
4060 lua_pushinteger(L,font(t));
4061 return 3;
4062 }
4063 }
4064
4065 static int lua_nodelib_direct_traverse_glyph(lua_State * L)
4066 {
4067 halfword n;
4068 if (lua_isnil(L, 1)) {
4069 lua_pushcclosure(L, nodelib_aux_nil, 0);
4070 return 1;
4071 }
4072 n = (halfword) lua_tointeger(L, 1);
4073 if (n == null) {
4074 lua_pushcclosure(L, nodelib_aux_nil, 0);
4075 return 1;
4076 }
4077 lua_pushcclosure(L, nodelib_direct_aux_next_glyph, 0);
4078 lua_pushinteger(L,n);
4079 lua_pushnil(L);
4080 return 3;
4081 }
4082
4083 static int nodelib_direct_aux_next_list(lua_State * L)
4084 {
4085 halfword t; /* traverser */
4086 if (lua_isnil(L, 2)) { /* first call */
4087 t = lua_tointeger(L,1) ;
4088 lua_settop(L,1);
4089 } else {
4090 t = lua_tointeger(L,2) ;
4091 t = vlink(t);
4092 lua_settop(L,2);
4093 }
4094 while (t != null && type(t) != hlist_node && type(t) != vlist_node) {
4095 t = vlink(t);
4096 }
4097 if (t == null) {
4098 lua_pushnil(L);
4099 return 1;
4100 } else {
4101 lua_pushinteger(L,t);
4102 lua_pushinteger(L,type(t));
4103 lua_pushinteger(L,subtype(t));
4104 nodelib_pushdirect_or_nil(list_ptr(t));
4105 return 4;
4106 }
4107 }
4108
4109 static int lua_nodelib_direct_traverse_list(lua_State * L)
4110 {
4111 halfword n;
4112 if (lua_isnil(L, 1)) {
4113 lua_pushcclosure(L, nodelib_aux_nil, 0);
4114 return 1;
4115 }
4116 n = (halfword) lua_tointeger(L, 1);
4117 if (n == null) {
4118 lua_pushcclosure(L, nodelib_aux_nil, 0);
4119 return 1;
4120 }
4121 lua_pushcclosure(L, nodelib_direct_aux_next_list, 1);
4122 lua_pushinteger(L, n);
4123 lua_pushnil(L);
4124 return 3;
4125 }
4126
4127 /* node.traverse */
4128 /* node.traverse_id */
4129 /* node.traverse_char */
4130 /* node.traverse_glyph */
4131 /* node.traverse_list */
4132
4133 static int nodelib_aux_next(lua_State * L)
4134 {
4135 halfword t;
4136 halfword *a;
4137 if (lua_isnil(L, 2)) {
4138 t = *check_isnode(L, 1);
4139 lua_settop(L,1);
4140 } else {
4141 t = *check_isnode(L, 2);
4142 t = vlink(t);
4143 lua_settop(L,2);
4144 }
4145 if (t == null) {
4146 lua_pushnil(L);
4147 return 1;
4148 } else {
4149 fast_metatable_top(t);
4150 lua_pushinteger(L,type(t));
4151 lua_pushinteger(L,subtype(t));
4152 return 3;
4153 }
4154 }
4155
4156 static int lua_nodelib_traverse(lua_State * L)
4157 {
4158 halfword n;
4159 if (lua_isnil(L, 1)) {
4160 lua_pushcclosure(L, nodelib_aux_nil, 0);
4161 return 1;
4162 }
4163 n = *check_isnode(L, 1);
4164 lua_pushcclosure(L, nodelib_aux_next, 0);
4165 lua_nodelib_push_fast(L, n);
4166 lua_pushnil(L);
4167 return 3;
4168 }
4169
4170 static int nodelib_aux_next_filtered(lua_State * L)
4171 {
4172 halfword t; /* traverser */
4173 halfword *a;
4174 int i = (int) lua_tointeger(L, lua_upvalueindex(1));
4175 if (lua_isnil(L, 2)) { /* first call */
4176 t = *check_isnode(L, 1);
4177 lua_settop(L,1);
4178 } else {
4179 t = *check_isnode(L, 2);
4180 t = vlink(t);
4181 lua_settop(L,2);
4182 }
4183 while (t != null && type(t) != i) {
4184 t = vlink(t);
4185 }
4186 if (t == null) {
4187 lua_pushnil(L);
4188 return 1;
4189 } else {
4190 fast_metatable_top(t);
4191 lua_pushinteger(L,subtype(t));
4192 return 2;
4193 }
4194 }
4195
4196 static int lua_nodelib_traverse_filtered(lua_State * L)
4197 {
4198 halfword n;
4199 if (lua_isnil(L, 2)) {
4200 lua_pushcclosure(L, nodelib_aux_nil, 0);
4201 return 1;
4202 }
4203 n = *check_isnode(L, 2);
4204 lua_pop(L, 1); /* the node, integer remains */
4205 lua_pushcclosure(L, nodelib_aux_next_filtered, 1);
4206 lua_nodelib_push_fast(L, n);
4207 lua_pushnil(L);
4208 return 3;
4209 }
4210
4211 static int nodelib_aux_next_char(lua_State * L)
4212 {
4213 halfword t; /* traverser */
4214 halfword *a;
4215 if (lua_isnil(L, 2)) { /* first call */
4216 t = *check_isnode(L, 1);
4217 lua_settop(L,1);
4218 } else {
4219 t = *check_isnode(L, 2);
4220 t = vlink(t);
4221 lua_settop(L,2);
4222 }
4223 while ((t != null) && (type(t) != glyph_node || is_protected_glyph_node(t))) {
4224 t = vlink(t);
4225 }
4226 if (t == null) {
4227 lua_pushnil(L);
4228 return 1;
4229 } else {
4230 fast_metatable_top(t);
4231 lua_pushinteger(L,character(t));
4232 lua_pushinteger(L,font(t));
4233 return 3;
4234 }
4235 }
4236
4237 static int lua_nodelib_traverse_char(lua_State * L)
4238 {
4239 halfword n;
4240 if (lua_isnil(L, 1)) {
4241 lua_pushcclosure(L, nodelib_aux_nil, 0);
4242 return 1;
4243 }
4244 n = *check_isnode(L, 1);
4245 lua_pushcclosure(L, nodelib_aux_next_char, 0);
4246 lua_nodelib_push_fast(L, n);
4247 lua_pushnil(L);
4248 return 3;
4249 }
4250
4251 static int nodelib_aux_next_glyph(lua_State * L)
4252 {
4253 halfword t; /* traverser */
4254 halfword *a;
4255 if (lua_isnil(L, 2)) { /* first call */
4256 t = *check_isnode(L, 1);
4257 lua_settop(L,1);
4258 } else {
4259 t = *check_isnode(L, 2);
4260 t = vlink(t);
4261 lua_settop(L,2);
4262 }
4263 while (t != null && type(t) != glyph_node) {
4264 t = vlink(t);
4265 }
4266 if (t == null) {
4267 lua_pushnil(L);
4268 return 1;
4269 } else {
4270 fast_metatable_top(t);
4271 lua_pushinteger(L,character(t));
4272 lua_pushinteger(L,font(t));
4273 return 3;
4274 }
4275 }
4276
4277 static int lua_nodelib_traverse_glyph(lua_State * L)
4278 {
4279 halfword n;
4280 if (lua_isnil(L, 1)) {
4281 lua_pushcclosure(L, nodelib_aux_nil, 0);
4282 return 1;
4283 }
4284 n = *check_isnode(L, 1);
4285 lua_pushcclosure(L, nodelib_aux_next_glyph, 0);
4286 lua_nodelib_push_fast(L, n);
4287 lua_pushnil(L);
4288 return 3;
4289 }
4290
4291 static int nodelib_aux_next_list(lua_State * L)
4292 {
4293 halfword t; /* traverser */
4294 halfword *a;
4295 if (lua_isnil(L, 2)) { /* first call */
4296 t = *check_isnode(L, 1);
4297 lua_settop(L,1);
4298 } else {
4299 t = *check_isnode(L, 2);
4300 t = vlink(t);
4301 lua_settop(L,2);
4302 }
4303 while (t != null && type(t) != hlist_node && type(t) != vlist_node) {
4304 t = vlink(t);
4305 }
4306 if (t == null) {
4307 lua_pushnil(L);
4308 return 1;
4309 } else {
4310 fast_metatable_top(t);
4311 lua_pushinteger(L,type(t));
4312 lua_pushinteger(L,subtype(t));
4313 fast_metatable_or_nil(list_ptr(t));
4314 return 4;
4315 }
4316 }
4317
4318 static int lua_nodelib_traverse_list(lua_State * L)
4319 {
4320 halfword n;
4321 if (lua_isnil(L, 1)) {
4322 lua_pushcclosure(L, nodelib_aux_nil, 0);
4323 return 1;
4324 }
4325 n = *check_isnode(L, 1);
4326 lua_pushcclosure(L, nodelib_aux_next_list, 1);
4327 lua_nodelib_push_fast(L, n);
4328 lua_pushnil(L);
4329 return 3;
4330 }
4331
4332 /* counting */
4333
4334 static int do_lua_nodelib_count(lua_State * L, halfword match, int i, halfword first1)
4335 {
4336 int count = 0;
4337 int t = first1;
4338 while (t != match) {
4339 if (i < 0 || type(t) == i)
4340 count++;
4341 t = vlink(t);
4342 }
4343 lua_pushinteger(L, count);
4344 return 1;
4345 }
4346
4347 /* node.direct.length */
4348 /* node.direct.count */
4349
4350 static int lua_nodelib_direct_length(lua_State * L)
4351 {
4352 halfword m;
4353 halfword n = lua_tointeger(L, 1);
4354 if (n == 0) {
4355 lua_pushinteger(L, 0);
4356 return 1;
4357 }
4358 m = (halfword) lua_tointeger(L, 2);
4359 return do_lua_nodelib_count(L, m, -1, n);
4360 }
4361
4362 static int lua_nodelib_direct_count(lua_State * L)
4363 {
4364 return do_lua_nodelib_count(L,
4365 (halfword) lua_tointeger(L, 3), /* m */
4366 (int) lua_tointeger(L, 1), /* i */
4367 (halfword) lua_tointeger(L, 2) /* n */
4368 );
4369 }
4370
4371 /* node.length */
4372 /* node.count */
4373
4374 static int lua_nodelib_length(lua_State * L)
4375 {
4376 halfword n;
4377 halfword m = null;
4378 if (lua_isnil(L, 1)) {
4379 lua_pushinteger(L, 0);
4380 return 1;
4381 }
4382 n = *check_isnode(L, 1);
4383 if (lua_gettop(L) == 2)
4384 m = *check_isnode(L, 2);
4385 return do_lua_nodelib_count(L, m, -1, n);
4386 }
4387
4388 static int lua_nodelib_count(lua_State * L)
4389 {
4390 halfword n;
4391 halfword m = null;
4392 int i = lua_tointeger(L, 1);
4393 if (lua_isnil(L, 2)) {
4394 lua_pushinteger(L, 0);
4395 return 1;
4396 }
4397 n = *check_isnode(L, 2);
4398 if (lua_gettop(L) == 3)
4399 m = *check_isnode(L, 3);
4400 return do_lua_nodelib_count(L, m, i, n);
4401 }
4402
4403 /* node.direct.getfield */
4404
4405 static void lua_nodelib_getfield_whatsit(lua_State * L, int n, const char *s)
4406 {
4407 int t = subtype(n);
4408 if (t == user_defined_node) {
4409 if (lua_key_eq(s, user_id)) {
4410 lua_pushinteger(L, user_node_id(n));
4411 } else if (lua_key_eq(s, type)) {
4412 lua_pushinteger(L, user_node_type(n));
4413 } else if (lua_key_eq(s, value)) {
4414 switch (user_node_type(n)) {
4415 case 'a':
4416 nodelib_pushlist(L, user_node_value(n));
4417 break;
4418 case 'd':
4419 lua_pushinteger(L, user_node_value(n));
4420 break;
4421 case 'l':
4422 if (user_node_value(n) != 0) {
4423 lua_rawgeti(L, LUA_REGISTRYINDEX, user_node_value(n));
4424 } else {
4425 lua_pushnil(L);
4426 }
4427 break;
4428 case 'n':
4429 nodelib_pushlist(L, user_node_value(n));
4430 break;
4431 case 's':
4432 nodelib_pushstring(L, user_node_value(n));
4433 break;
4434 case 't':
4435 tokenlist_to_lua(L, user_node_value(n));
4436 break;
4437 default:
4438 lua_pushinteger(L, user_node_value(n));
4439 break;
4440 }
4441 } else {
4442 lua_pushnil(L);
4443 }
4444 } else if (t == pdf_literal_node || t == pdf_late_literal_node) {
4445 /*tex The |string| key is obsolete. */
4446 if (lua_key_eq(s, mode)) {
4447 lua_pushinteger(L, pdf_literal_mode(n));
4448 } else if (lua_key_eq(s, data) || lua_key_eq(s, token) || lua_key_eq(s, string)) {
4449 get_pdf_literal_direct_value(L, n);
4450 } else {
4451 lua_pushnil(L);
4452 }
4453 } else if (t == late_lua_node) {
4454 /*tex The |string| key is obsolete. */
4455 if (lua_key_eq(s, data) || lua_key_eq(s, token) || lua_key_eq(s, string)) {
4456 get_late_lua_direct_value(L,n);
4457 } else if (lua_key_eq(s, name)) {
4458 tokenlist_to_luastring(L, late_lua_name(n));
4459 } else {
4460 lua_pushnil(L);
4461 }
4462 } else if (t == pdf_annot_node) {
4463 if (lua_key_eq(s, width)) {
4464 lua_pushinteger(L, width(n));
4465 } else if (lua_key_eq(s, depth)) {
4466 lua_pushinteger(L, depth(n));
4467 } else if (lua_key_eq(s, height)) {
4468 lua_pushinteger(L, height(n));
4469 } else if (lua_key_eq(s, objnum)) {
4470 lua_pushinteger(L, pdf_annot_objnum(n));
4471 } else if (lua_key_eq(s, data)) {
4472 tokenlist_to_luastring(L, pdf_annot_data(n));
4473 } else {
4474 lua_pushnil(L);
4475 }
4476 } else if (t == pdf_dest_node) {
4477 if (lua_key_eq(s, width)) {
4478 lua_pushinteger(L, width(n));
4479 } else if (lua_key_eq(s, depth)) {
4480 lua_pushinteger(L, depth(n));
4481 } else if (lua_key_eq(s, height)) {
4482 lua_pushinteger(L, height(n));
4483 } else if (lua_key_eq(s, named_id)) {
4484 lua_pushinteger(L, pdf_dest_named_id(n));
4485 } else if (lua_key_eq(s, dest_id)) {
4486 if (pdf_dest_named_id(n) == 1)
4487 tokenlist_to_luastring(L, pdf_dest_id(n));
4488 else
4489 lua_pushinteger(L, pdf_dest_id(n));
4490 } else if (lua_key_eq(s, dest_type)) {
4491 lua_pushinteger(L, pdf_dest_type(n));
4492 } else if (lua_key_eq(s, xyz_zoom)) {
4493 lua_pushinteger(L, pdf_dest_xyz_zoom(n));
4494 } else if (lua_key_eq(s, objnum)) {
4495 lua_pushinteger(L, pdf_dest_objnum(n));
4496 } else {
4497 lua_pushnil(L);
4498 }
4499 } else if (t == pdf_setmatrix_node) {
4500 if (lua_key_eq(s, data)) {
4501 get_pdf_setmatrix_direct_value(L,n);
4502 } else {
4503 lua_pushnil(L);
4504 }
4505 } else if (t == pdf_colorstack_node) {
4506 if (lua_key_eq(s, stack)) {
4507 lua_pushinteger(L, pdf_colorstack_stack(n));
4508 } else if (lua_key_eq(s, command)) {
4509 lua_pushinteger(L, pdf_colorstack_cmd(n));
4510 } else if (lua_key_eq(s, data)) {
4511 tokenlist_to_luastring(L, pdf_colorstack_data(n));
4512 } else {
4513 lua_pushnil(L);
4514 }
4515 } else if (t == pdf_refobj_node) {
4516 if (lua_key_eq(s, objnum)) {
4517 lua_pushinteger(L, pdf_obj_objnum(n));
4518 } else {
4519 lua_pushnil(L);
4520 }
4521 } else if (t == write_node) {
4522 if (lua_key_eq(s, stream)) {
4523 lua_pushinteger(L, write_stream(n));
4524 } else if (lua_key_eq(s, data)) {
4525 get_write_direct_data(L,n);
4526 } else if (lua_key_eq(s, value)) {
4527 get_write_direct_value(L,n);
4528 } else {
4529 lua_pushnil(L);
4530 }
4531 } else if (t == special_node || t == late_special_node) {
4532 if (lua_key_eq(s, data) || lua_key_eq(s, value)) {
4533 get_special_direct_value(L,n);
4534 } else {
4535 lua_pushnil(L);
4536 }
4537 } else if (t == pdf_start_link_node) {
4538 if (lua_key_eq(s, width)) {
4539 lua_pushinteger(L, width(n));
4540 } else if (lua_key_eq(s, depth)) {
4541 lua_pushinteger(L, depth(n));
4542 } else if (lua_key_eq(s, height)) {
4543 lua_pushinteger(L, height(n));
4544 } else if (lua_key_eq(s, objnum)) {
4545 lua_pushinteger(L, pdf_link_objnum(n));
4546 } else if (lua_key_eq(s, link_attr)) {
4547 tokenlist_to_luastring(L, pdf_link_attr(n));
4548 } else if (lua_key_eq(s, action)) {
4549 nodelib_pushaction(L, pdf_link_action(n));
4550 } else {
4551 lua_pushnil(L);
4552 }
4553 } else if (t == pdf_action_node) {
4554 if (lua_key_eq(s, action_type)) {
4555 lua_pushinteger(L, pdf_action_type(n));
4556 } else if (lua_key_eq(s, named_id)) {
4557 lua_pushinteger(L, pdf_action_named_id(n));
4558 } else if (lua_key_eq(s, action_id)) {
4559 if (pdf_action_named_id(n) & 1) {
4560 tokenlist_to_luastring(L, pdf_action_id(n));
4561 } else {
4562 lua_pushinteger(L, pdf_action_id(n));
4563 }
4564 } else if (lua_key_eq(s, file)) {
4565 tokenlist_to_luastring(L, pdf_action_file(n));
4566 } else if (lua_key_eq(s, new_window)) {
4567 lua_pushinteger(L, pdf_action_new_window(n));
4568 } else if (lua_key_eq(s, data)) {
4569 tokenlist_to_luastring(L, pdf_action_tokens(n));
4570 } else if (lua_key_eq(s, struct_id)) {
4571 if (pdf_action_struct_id(n) == null) {
4572 lua_pushnil(L);
4573 } else if (pdf_action_named_id(n) & 2) {
4574 tokenlist_to_luastring(L, pdf_action_struct_id(n));
4575 } else {
4576 lua_pushinteger(L, pdf_action_struct_id(n));
4577 }
4578 } else {
4579 lua_pushnil(L);
4580 }
4581 } else if ((t == pdf_thread_node) || (t == pdf_start_thread_node)) {
4582 if (lua_key_eq(s, width)) {
4583 lua_pushinteger(L, width(n));
4584 } else if (lua_key_eq(s, depth)) {
4585 lua_pushinteger(L, depth(n));
4586 } else if (lua_key_eq(s, height)) {
4587 lua_pushinteger(L, height(n));
4588 } else if (lua_key_eq(s, named_id)) {
4589 lua_pushinteger(L, pdf_thread_named_id(n));
4590 } else if (lua_key_eq(s, thread_id)) {
4591 if (pdf_thread_named_id(n) == 1) {
4592 tokenlist_to_luastring(L, pdf_thread_id(n));
4593 } else {
4594 lua_pushinteger(L, pdf_thread_id(n));
4595 }
4596 } else if (lua_key_eq(s, thread_attr)) {
4597 tokenlist_to_luastring(L, pdf_thread_attr(n));
4598 } else {
4599 lua_pushnil(L);
4600 }
4601 } else if (t == open_node) {
4602 if (lua_key_eq(s, stream)) {
4603 lua_pushinteger(L, write_stream(n));
4604 } else if (lua_key_eq(s, name)) {
4605 nodelib_pushstring(L, open_name(n));
4606 } else if (lua_key_eq(s, area)) {
4607 nodelib_pushstring(L, open_area(n));
4608 } else if (lua_key_eq(s, ext)) {
4609 nodelib_pushstring(L, open_ext(n));
4610 } else {
4611 lua_pushnil(L);
4612 }
4613 } else if (t == close_node) {
4614 if (lua_key_eq(s, stream)) {
4615 lua_pushinteger(L, write_stream(n));
4616 } else {
4617 lua_pushnil(L);
4618 }
4619 } else {
4620 lua_pushnil(L);
4621 }
4622 }
4623
4624 static int lua_nodelib_fast_getfield(lua_State * L)
4625 {
4626 /*
4627 the order is somewhat determined by the occurance of nodes and
4628 importance of fields
4629 */
4630
4631 halfword *a;
4632 const char *s;
4633
4634 halfword n = *((halfword *) lua_touserdata(L, 1));
4635 int t = type(n);
4636
4637 /*
4638
4639 somenode[9] as interface to attributes ... 30% faster than has_attribute
4640 (1) because there is no lua function overhead, and (2) because we already
4641 know that we deal with a node so no checking is needed. The fast typecheck
4642 is needed (lua_check... is a slow down actually).
4643
4644 */
4645
4646 if (lua_type(L, 2) == LUA_TNUMBER) {
4647
4648 halfword p;
4649 int i;
4650
4651 if (! nodetype_has_attributes(t)) {
4652 lua_pushnil(L);
4653 return 1;
4654 }
4655
4656 p = node_attr(n);
4657 if (p == null || vlink(p) == null) {
4658 lua_pushnil(L);
4659 return 1;
4660 }
4661 i = (int) lua_tointeger(L, 2);
4662 p = vlink(p);
4663 while (p != null) {
4664 if (attribute_id(p) == i) {
4665 if ((int) attribute_value(p) > UNUSED_ATTRIBUTE) {
4666 lua_pushinteger(L, (int) attribute_value(p));
4667 } else {
4668 lua_pushnil(L);
4669 }
4670 return 1;
4671 } else if (attribute_id(p) > i) {
4672 lua_pushnil(L);
4673 return 1;
4674 }
4675 p = vlink(p);
4676 }
4677 lua_pushnil(L);
4678 return 1;
4679 }
4680
4681 s = lua_tostring(L, 2);
4682
4683 if (lua_key_eq(s, id)) {
4684 lua_pushinteger(L, t);
4685 } else if (lua_key_eq(s, next)) {
4686 fast_metatable_or_nil(vlink(n));
4687 } else if (lua_key_eq(s, prev)) {
4688 fast_metatable_or_nil(alink(n));
4689 } else if (lua_key_eq(s, attr)) {
4690 if (! nodetype_has_attributes(t)) {
4691 lua_pushnil(L);
4692 } else {
4693 nodelib_pushattr(L, node_attr(n));
4694 }
4695 } else if (t == glyph_node) {
4696 /* candidates: fontchar (font,char) whd (width,height,depth) */
4697 if (lua_key_eq(s, subtype)) {
4698 lua_pushinteger(L, subtype(n));
4699 } else if (lua_key_eq(s, font)) {
4700 lua_pushinteger(L, font(n));
4701 } else if (lua_key_eq(s, char)) {
4702 lua_pushinteger(L, character(n));
4703 } else if (lua_key_eq(s, xoffset)) {
4704 lua_pushinteger(L, x_displace(n));
4705 } else if (lua_key_eq(s, yoffset)) {
4706 lua_pushinteger(L, y_displace(n));
4707 } else if (lua_key_eq(s, data)) {
4708 lua_pushinteger(L, glyph_node_data(n));
4709 } else if (lua_key_eq(s, width)) {
4710 lua_pushinteger(L, char_width(font(n),character(n)));
4711 } else if (lua_key_eq(s, height)) {
4712 lua_pushinteger(L, char_height(font(n),character(n)));
4713 } else if (lua_key_eq(s, depth)) {
4714 lua_pushinteger(L, char_depth(font(n),character(n)));
4715 } else if (lua_key_eq(s, expansion_factor)) {
4716 lua_pushinteger(L, ex_glyph(n));
4717 } else if (lua_key_eq(s, components)) {
4718 fast_metatable_or_nil(lig_ptr(n));
4719 } else if (lua_key_eq(s, lang)) {
4720 lua_pushinteger(L, char_lang(n));
4721 } else if (lua_key_eq(s, left)) {
4722 lua_pushinteger(L, char_lhmin(n));
4723 } else if (lua_key_eq(s, right)) {
4724 lua_pushinteger(L, char_rhmin(n));
4725 } else if (lua_key_eq(s, uchyph)) {
4726 lua_pushinteger(L, char_uchyph(n));
4727 } else {
4728 lua_pushnil(L);
4729 }
4730 } else if ((t == hlist_node) || (t == vlist_node)) {
4731 /* candidates: whd (width,height,depth) */
4732 if (lua_key_eq(s, subtype)) {
4733 lua_pushinteger(L, subtype(n));
4734 } else if (lua_key_eq(s, list) || lua_key_eq(s, head)) {
4735 fast_metatable_or_nil_alink(list_ptr(n));
4736 } else if (lua_key_eq(s, width)) {
4737 lua_pushinteger(L, width(n));
4738 } else if (lua_key_eq(s, height)) {
4739 lua_pushinteger(L, height(n));
4740 } else if (lua_key_eq(s, depth)) {
4741 lua_pushinteger(L, depth(n));
4742 } else if (lua_key_eq(s, direction)) {
4743 lua_push_direction(L, box_dir(n));
4744 } else if (lua_key_eq(s, dir)) {
4745 lua_push_dir_par(L, box_dir(n));
4746 } else if (lua_key_eq(s, shift)) {
4747 lua_pushinteger(L, shift_amount(n));
4748 } else if (lua_key_eq(s, glue_order)) {
4749 lua_pushinteger(L, glue_order(n));
4750 } else if (lua_key_eq(s, glue_sign)) {
4751 lua_pushinteger(L, glue_sign(n));
4752 } else if (lua_key_eq(s, glue_set)) {
4753 lua_pushnumber(L, (double) glue_set(n)); /* float */
4754 } else {
4755 lua_pushnil(L);
4756 }
4757 } else if (t == disc_node) {
4758 if (lua_key_eq(s, subtype)) {
4759 lua_pushinteger(L, subtype(n));
4760 } else if (lua_key_eq(s, pre)) {
4761 fast_metatable_or_nil_alink(vlink(pre_break(n)));
4762 } else if (lua_key_eq(s, post)) {
4763 fast_metatable_or_nil_alink(vlink(post_break(n)));
4764 } else if (lua_key_eq(s, replace)) {
4765 fast_metatable_or_nil_alink(vlink(no_break(n)));
4766 } else if (lua_key_eq(s, penalty)) {
4767 lua_pushinteger(L, disc_penalty(n));
4768 } else {
4769 lua_pushnil(L);
4770 }
4771 } else if (t == glue_node) {
4772 if (lua_key_eq(s, subtype)) {
4773 lua_pushinteger(L, subtype(n));
4774 } else if (lua_key_eq(s, width)) {
4775 lua_pushinteger(L, width(n));
4776 } else if (lua_key_eq(s, stretch)) {
4777 lua_pushinteger(L, stretch(n));
4778 } else if (lua_key_eq(s, shrink)) {
4779 lua_pushinteger(L, shrink(n));
4780 } else if (lua_key_eq(s, stretch_order)) {
4781 lua_pushinteger(L, stretch_order(n));
4782 } else if (lua_key_eq(s, shrink_order)) {
4783 lua_pushinteger(L, shrink_order(n));
4784 } else if (lua_key_eq(s, leader)) {
4785 fast_metatable_or_nil(leader_ptr(n));
4786 } else {
4787 lua_pushnil(L);
4788 }
4789 } else if (t == kern_node) {
4790 if (lua_key_eq(s, subtype)) {
4791 lua_pushinteger(L, subtype(n));
4792 } else if (lua_key_eq(s, kern)) {
4793 lua_pushinteger(L, width(n));
4794 } else if (lua_key_eq(s, expansion_factor)) {
4795 lua_pushinteger(L, ex_kern(n));
4796 } else {
4797 lua_pushnil(L);
4798 }
4799 } else if (t == penalty_node) {
4800 if (lua_key_eq(s, subtype)) {
4801 lua_pushinteger(L, subtype(n));
4802 } else if (lua_key_eq(s, penalty)) {
4803 lua_pushinteger(L, penalty(n));
4804 } else {
4805 lua_pushnil(L);
4806 }
4807 } else if (t == rule_node) {
4808 /* candidates: whd (width,height,depth) */
4809 if (lua_key_eq(s, subtype)) {
4810 lua_pushinteger(L, subtype(n));
4811 } else if (lua_key_eq(s, width)) {
4812 lua_pushinteger(L, width(n));
4813 } else if (lua_key_eq(s, height)) {
4814 lua_pushinteger(L, height(n));
4815 } else if (lua_key_eq(s, depth)) {
4816 lua_pushinteger(L, depth(n));
4817 } else if (lua_key_eq(s, left)) {
4818 lua_pushinteger(L,rule_left(n));
4819 } else if (lua_key_eq(s, right)) {
4820 lua_pushinteger(L,rule_right(n));
4821 } else if (lua_key_eq(s, direction)) {
4822 lua_push_direction(L, rule_dir(n));
4823 } else if (lua_key_eq(s, dir)) {
4824 lua_push_dir_par(L, rule_dir(n));
4825 } else if (lua_key_eq(s, index)) {
4826 lua_pushinteger(L, rule_index(n));
4827 } else if (lua_key_eq(s, transform)) {
4828 lua_pushinteger(L,rule_transform(n));
4829 } else {
4830 lua_pushnil(L);
4831 }
4832 } else if (t == dir_node) {
4833 if (lua_key_eq(s, direction)) {
4834 lua_push_direction(L, dir_dir(n));
4835 } else if (lua_key_eq(s, dir)) {
4836 lua_push_dir_text(L, dir_dir(n),subtype(n));
4837 } else if (lua_key_eq(s, level)) {
4838 lua_pushinteger(L, dir_level(n));
4839 } else if (lua_key_eq(s, subtype)) { /* can be used for anything */
4840 lua_pushinteger(L, subtype(n));
4841 } else {
4842 lua_pushnil(L);
4843 }
4844 } else if (t == local_par_node) {
4845 if (lua_key_eq(s, pen_inter)) {
4846 lua_pushinteger(L, local_pen_inter(n));
4847 } else if (lua_key_eq(s, pen_broken)) {
4848 lua_pushinteger(L, local_pen_broken(n));
4849 } else if (lua_key_eq(s, direction)) {
4850 lua_push_direction(L, local_par_dir(n));
4851 } else if (lua_key_eq(s, dir)) {
4852 lua_push_dir_par(L, local_par_dir(n));
4853 } else if (lua_key_eq(s, box_left)) {
4854 /* can be: fast_metatable_or_nil(local_box_left(n)) */
4855 nodelib_pushlist(L, local_box_left(n));
4856 } else if (lua_key_eq(s, box_left_width)) {
4857 lua_pushinteger(L, local_box_left_width(n));
4858 } else if (lua_key_eq(s, box_right)) {
4859 /* can be: fast_metatable_or_nil(local_box_right(n)) */
4860 nodelib_pushlist(L, local_box_right(n));
4861 } else if (lua_key_eq(s, box_right_width)) {
4862 lua_pushinteger(L, local_box_right_width(n));
4863 } else {
4864 lua_pushnil(L);
4865 }
4866 } else if (t == glue_spec_node) {
4867 if (lua_key_eq(s, width)) {
4868 lua_pushinteger(L, width(n));
4869 } else if (lua_key_eq(s, stretch)) {
4870 lua_pushinteger(L, stretch(n));
4871 } else if (lua_key_eq(s, shrink)) {
4872 lua_pushinteger(L, shrink(n));
4873 } else if (lua_key_eq(s, stretch_order)) {
4874 lua_pushinteger(L, stretch_order(n));
4875 } else if (lua_key_eq(s, shrink_order)) {
4876 lua_pushinteger(L, shrink_order(n));
4877 } else {
4878 lua_pushnil(L);
4879 }
4880 } else if (t == whatsit_node) {
4881 if (lua_key_eq(s, subtype)) {
4882 lua_pushinteger(L, subtype(n));
4883 } else {
4884 lua_nodelib_getfield_whatsit(L, n, s);
4885 }
4886 } else if (t == simple_noad) {
4887 if (lua_key_eq(s, subtype)) {
4888 lua_pushinteger(L, subtype(n));
4889 } else if (lua_key_eq(s, nucleus)) {
4890 fast_metatable_or_nil(nucleus(n));
4891 } else if (lua_key_eq(s, sub)) {
4892 fast_metatable_or_nil(subscr(n));
4893 } else if (lua_key_eq(s, sup)) {
4894 fast_metatable_or_nil(supscr(n));
4895 } else if (lua_key_eq(s, options)) {
4896 lua_pushinteger(L, noadoptions(n));
4897 } else {
4898 lua_pushnil(L);
4899 }
4900 } else if ((t == math_char_node) || (t == math_text_char_node)) {
4901 /* candidates: famchar (fam,char) */
4902 if (lua_key_eq(s, subtype)) {
4903 lua_pushinteger(L, subtype(n));
4904 } else if (lua_key_eq(s, fam)) {
4905 lua_pushinteger(L, math_fam(n));
4906 } else if (lua_key_eq(s, char)) {
4907 lua_pushinteger(L, math_character(n));
4908 } else if (lua_key_eq(s, font)) {
4909 lua_pushinteger(L, fam_fnt(math_fam(n), 0));
4910 } else {
4911 lua_pushnil(L);
4912 }
4913 } else if (t == mark_node) {
4914 if (lua_key_eq(s, subtype)) {
4915 lua_pushinteger(L, subtype(n));
4916 } else if (lua_key_eq(s, class)) {
4917 lua_pushinteger(L, mark_class(n));
4918 } else if (lua_key_eq(s, mark)) {
4919 tokenlist_to_lua(L, mark_ptr(n));
4920 } else {
4921 lua_pushnil(L);
4922 }
4923 } else if (t == ins_node) {
4924 if (lua_key_eq(s, subtype)) {
4925 lua_pushinteger(L, subtype(n));
4926 } else if (lua_key_eq(s, cost)) {
4927 lua_pushinteger(L, float_cost(n));
4928 } else if (lua_key_eq(s, depth)) {
4929 lua_pushinteger(L, depth(n));
4930 } else if (lua_key_eq(s, height)) {
4931 lua_pushinteger(L, height(n));
4932 } else if ((lua_key_eq(s, list)) || (lua_key_eq(s, head))) { /* already mapped */
4933 fast_metatable_or_nil_alink(ins_ptr(n));
4934 /* glue parameters */
4935 } else if (lua_key_eq(s, width)) {
4936 lua_pushinteger(L, width(n));
4937 } else if (lua_key_eq(s, stretch)) {
4938 lua_pushinteger(L, stretch(n));
4939 } else if (lua_key_eq(s, shrink)) {
4940 lua_pushinteger(L, shrink(n));
4941 } else if (lua_key_eq(s, stretch_order)) {
4942 lua_pushinteger(L, stretch_order(n));
4943 } else if (lua_key_eq(s, shrink_order)) {
4944 lua_pushinteger(L, shrink_order(n));
4945 } else {
4946 lua_pushnil(L);
4947 }
4948 } else if (t == math_node) {
4949 if (lua_key_eq(s, subtype)) {
4950 lua_pushinteger(L, subtype(n));
4951 } else if (lua_key_eq(s, surround)) {
4952 lua_pushinteger(L, surround(n));
4953 /* glue parameters */
4954 } else if (lua_key_eq(s, width)) {
4955 lua_pushinteger(L, width(n));
4956 } else if (lua_key_eq(s, stretch)) {
4957 lua_pushinteger(L, stretch(n));
4958 } else if (lua_key_eq(s, shrink)) {
4959 lua_pushinteger(L, shrink(n));
4960 } else if (lua_key_eq(s, stretch_order)) {
4961 lua_pushinteger(L, stretch_order(n));
4962 } else if (lua_key_eq(s, shrink_order)) {
4963 lua_pushinteger(L, shrink_order(n));
4964 } else {
4965 lua_pushnil(L);
4966 }
4967 } else if (t == fraction_noad) {
4968 if (lua_key_eq(s, subtype)) {
4969 lua_pushinteger(L, subtype(n));
4970 } else if (lua_key_eq(s, width)) {
4971 lua_pushinteger(L, thickness(n));
4972 } else if (lua_key_eq(s, num)) {
4973 fast_metatable_or_nil(numerator(n));
4974 } else if (lua_key_eq(s, denom)) {
4975 fast_metatable_or_nil(denominator(n));
4976 } else if (lua_key_eq(s, left)) {
4977 fast_metatable_or_nil(left_delimiter(n));
4978 } else if (lua_key_eq(s, right)) {
4979 fast_metatable_or_nil(right_delimiter(n));
4980 } else if (lua_key_eq(s, middle)) {
4981 fast_metatable_or_nil(middle_delimiter(n));
4982 } else if (lua_key_eq(s, fam)) {
4983 lua_pushinteger(L, fraction_fam(n));
4984 } else if (lua_key_eq(s, options)) {
4985 lua_pushinteger(L, fractionoptions(n));
4986 } else {
4987 lua_pushnil(L);
4988 }
4989 } else if (t == style_node) {
4990 if (lua_key_eq(s, subtype)) {
4991 lua_pushinteger(L, subtype(n));
4992 } else if (lua_key_eq(s, style)) {
4993 lua_push_math_style_name(L,subtype(n));
4994 } else {
4995 lua_pushnil(L);
4996 }
4997 } else if (t == accent_noad) {
4998 if (lua_key_eq(s, subtype)) {
4999 lua_pushinteger(L, subtype(n));
5000 } else if (lua_key_eq(s, nucleus)) {
5001 fast_metatable_or_nil(nucleus(n));
5002 } else if (lua_key_eq(s, sub)) {
5003 fast_metatable_or_nil(subscr(n));
5004 } else if (lua_key_eq(s, sup)) {
5005 fast_metatable_or_nil(supscr(n));
5006 } else if ((lua_key_eq(s, top_accent))||(lua_key_eq(s, accent))) {
5007 fast_metatable_or_nil(top_accent_chr(n));
5008 } else if (lua_key_eq(s, bot_accent)) {
5009 fast_metatable_or_nil(bot_accent_chr(n));
5010 } else if (lua_key_eq(s, overlay_accent)) {
5011 fast_metatable_or_nil(overlay_accent_chr(n));
5012 } else if (lua_key_eq(s, fraction)) {
5013 lua_pushinteger(L, accentfraction(n));
5014 } else {
5015 lua_pushnil(L);
5016 }
5017 } else if (t == fence_noad) {
5018 if (lua_key_eq(s, subtype)) {
5019 lua_pushinteger(L, subtype(n));
5020 } else if (lua_key_eq(s, delim)) {
5021 fast_metatable_or_nil(delimiter(n));
5022 } else if (lua_key_eq(s, italic)) {
5023 lua_pushinteger(L, delimiteritalic(n));
5024 } else if (lua_key_eq(s, height)) {
5025 lua_pushinteger(L, delimiterheight(n));
5026 } else if (lua_key_eq(s, depth)) {
5027 lua_pushinteger(L, delimiterdepth(n));
5028 } else if (lua_key_eq(s, options)) {
5029 lua_pushinteger(L, delimiteroptions(n));
5030 } else if (lua_key_eq(s, class)) {
5031 lua_pushinteger(L, delimiterclass(n));
5032 } else {
5033 lua_pushnil(L);
5034 }
5035 } else if (t == delim_node) {
5036 if (lua_key_eq(s, subtype)) {
5037 lua_pushinteger(L, subtype(n));
5038 } else if (lua_key_eq(s, small_fam)) {
5039 lua_pushinteger(L, small_fam(n));
5040 } else if (lua_key_eq(s, small_char)) {
5041 lua_pushinteger(L, small_char(n));
5042 } else if (lua_key_eq(s, large_fam)) {
5043 lua_pushinteger(L, large_fam(n));
5044 } else if (lua_key_eq(s, large_char)) {
5045 lua_pushinteger(L, large_char(n));
5046 } else {
5047 lua_pushnil(L);
5048 }
5049 } else if ((t == sub_box_node) || (t == sub_mlist_node)) {
5050 if (lua_key_eq(s, subtype)) {
5051 lua_pushinteger(L, subtype(n));
5052 } else if ((lua_key_eq(s, list)) || (lua_key_eq(s, head))){
5053 fast_metatable_or_nil_alink(math_list(n));
5054 } else {
5055 lua_pushnil(L);
5056 }
5057 } else if (t == radical_noad) {
5058 if (lua_key_eq(s, subtype)) {
5059 lua_pushinteger(L, subtype(n));
5060 } else if (lua_key_eq(s, nucleus)) {
5061 fast_metatable_or_nil(nucleus(n));
5062 } else if (lua_key_eq(s, sub)) {
5063 fast_metatable_or_nil(subscr(n));
5064 } else if (lua_key_eq(s, sup)) {
5065 fast_metatable_or_nil(supscr(n));
5066 } else if (lua_key_eq(s, left)) {
5067 fast_metatable_or_nil(left_delimiter(n));
5068 } else if (lua_key_eq(s, degree)) {
5069 fast_metatable_or_nil(degree(n));
5070 } else if (lua_key_eq(s, width)) {
5071 lua_pushinteger(L, radicalwidth(n));
5072 } else if (lua_key_eq(s, options)) {
5073 lua_pushinteger(L, radicaloptions(n));
5074 } else {
5075 lua_pushnil(L);
5076 }
5077 } else if (t == margin_kern_node) {
5078 if (lua_key_eq(s, subtype)) {
5079 lua_pushinteger(L, subtype(n));
5080 } else if (lua_key_eq(s, width)) {
5081 lua_pushinteger(L, width(n));
5082 } else if (lua_key_eq(s, glyph)) {
5083 fast_metatable_or_nil(margin_char(n));
5084 } else {
5085 lua_pushnil(L);
5086 }
5087 } else if (t == split_up_node) {
5088 if (lua_key_eq(s, subtype)) {
5089 lua_pushinteger(L, subtype(n));
5090 } else if (lua_key_eq(s, last_ins_ptr)) {
5091 fast_metatable_or_nil(last_ins_ptr(n));
5092 } else if (lua_key_eq(s, best_ins_ptr)) {
5093 fast_metatable_or_nil(best_ins_ptr(n));
5094 } else if (lua_key_eq(s, broken_ptr)) {
5095 fast_metatable_or_nil(broken_ptr(n));
5096 } else if (lua_key_eq(s, broken_ins)) {
5097 fast_metatable_or_nil(broken_ins(n));
5098 } else {
5099 lua_pushnil(L);
5100 }
5101 } else if (t == choice_node) {
5102 if (lua_key_eq(s, subtype)) {
5103 lua_pushinteger(L, subtype(n));
5104 } else if (lua_key_eq(s, display)) {
5105 fast_metatable_or_nil(display_mlist(n));
5106 } else if (lua_key_eq(s, text)) {
5107 fast_metatable_or_nil(text_mlist(n));
5108 } else if (lua_key_eq(s, script)) {
5109 fast_metatable_or_nil(script_mlist(n));
5110 } else if (lua_key_eq(s, scriptscript)) {
5111 fast_metatable_or_nil(script_script_mlist(n));
5112 } else {
5113 lua_pushnil(L);
5114 }
5115 } else if (t == inserting_node) {
5116 if (lua_key_eq(s, subtype)) {
5117 lua_pushinteger(L, subtype(n));
5118 } else if (lua_key_eq(s, last_ins_ptr)) {
5119 fast_metatable_or_nil(last_ins_ptr(n));
5120 } else if (lua_key_eq(s, best_ins_ptr)) {
5121 fast_metatable_or_nil(best_ins_ptr(n));
5122 } else {
5123 lua_pushnil(L);
5124 }
5125 } else if (t == attribute_node) {
5126 if (lua_key_eq(s, subtype)) {
5127 lua_pushinteger(L, subtype(n));
5128 } else if (lua_key_eq(s, number)) {
5129 lua_pushinteger(L, attribute_id(n));
5130 } else if (lua_key_eq(s, value)) {
5131 lua_pushinteger(L, attribute_value(n));
5132 } else {
5133 lua_pushnil(L);
5134 }
5135 } else if (t == adjust_node) {
5136 if (lua_key_eq(s, subtype)) {
5137 lua_pushinteger(L, subtype(n));
5138 } else if ((lua_key_eq(s, list)) || (lua_key_eq(s, head))) {
5139 fast_metatable_or_nil_alink(adjust_ptr(n));
5140 } else {
5141 lua_pushnil(L);
5142 }
5143 } else if (t == unset_node) {
5144 if (lua_key_eq(s, subtype)) {
5145 lua_pushinteger(L, subtype(n));
5146 } else if (lua_key_eq(s, width)) {
5147 lua_pushinteger(L, width(n));
5148 } else if (lua_key_eq(s, height)) {
5149 lua_pushinteger(L, height(n));
5150 } else if (lua_key_eq(s, depth)) {
5151 lua_pushinteger(L, depth(n));
5152 } else if (lua_key_eq(s, direction)) {
5153 lua_push_direction(L, box_dir(n));
5154 } else if (lua_key_eq(s, dir)) {
5155 lua_push_dir_par(L, box_dir(n));
5156 } else if (lua_key_eq(s, shrink)) {
5157 lua_pushinteger(L, glue_shrink(n));
5158 } else if (lua_key_eq(s, glue_order)) {
5159 lua_pushinteger(L, glue_order(n));
5160 } else if (lua_key_eq(s, glue_sign)) {
5161 lua_pushinteger(L, glue_sign(n));
5162 } else if (lua_key_eq(s, stretch)) {
5163 lua_pushinteger(L, glue_stretch(n));
5164 } else if (lua_key_eq(s, count)) {
5165 lua_pushinteger(L, span_count(n));
5166 } else if ((lua_key_eq(s, list)) || (lua_key_eq(s, head))){
5167 fast_metatable_or_nil_alink(list_ptr(n));
5168 } else {
5169 lua_pushnil(L);
5170 }
5171 } else if (t == attribute_list_node) {
5172 if (lua_key_eq(s, subtype)) {
5173 lua_pushinteger(L, subtype(n));
5174 } else {
5175 lua_pushnil(L);
5176 }
5177 } else if (t == boundary_node) {
5178 if (lua_key_eq(s, subtype)) {
5179 lua_pushinteger(L, subtype(n));
5180 } else if (lua_key_eq(s, value)) {
5181 lua_pushinteger(L, boundary_value(n));
5182 } else {
5183 lua_pushnil(L);
5184 }
5185 } else {
5186 lua_pushnil(L);
5187 }
5188 return 1;
5189 }
5190
5191 static int lua_nodelib_getfield(lua_State * L)
5192 {
5193 /* [given-node] [...]*/
5194 halfword *p = lua_touserdata(L, 1);
5195 if ( (p == NULL) || (! lua_getmetatable(L,1)) ) {
5196 lua_pushnil(L) ;
5197 return 1;
5198 }
5199 /* [given-node] [mt-given-node]*/
5200 lua_get_metatablelua(luatex_node);
5201 /* [given-node] [mt-given-node] [mt-node]*/
5202 if (!lua_rawequal(L, -1, -2)) {
5203 lua_pushnil(L) ;
5204 return 1;
5205 }
5206 /* prune stack and call getfield */
5207 lua_settop(L,2);
5208 return lua_nodelib_fast_getfield(L);
5209 }
5210
5211 /* node.direct.getfield */
5212
5213 static void lua_nodelib_direct_getfield_whatsit(lua_State * L, int n, const char *s)
5214 {
5215 int t = subtype(n);
5216 if (t == user_defined_node) {
5217 if (lua_key_eq(s, user_id)) {
5218 lua_pushinteger(L, user_node_id(n));
5219 } else if (lua_key_eq(s, type)) {
5220 lua_pushinteger(L, user_node_type(n));
5221 } else if (lua_key_eq(s, value)) {
5222 get_user_node_direct_value(L, n);
5223 } else {
5224 lua_pushnil(L);
5225 }
5226 } else if (t == pdf_literal_node || t == pdf_late_literal_node) {
5227 /*tex The |string| key is obsolete. */
5228 if (lua_key_eq(s, mode)) {
5229 lua_pushinteger(L, pdf_literal_mode(n));
5230 } else if (lua_key_eq(s, data) || lua_key_eq(s, token) || lua_key_eq(s, string)) {
5231 get_pdf_literal_direct_value(L, n);
5232 } else {
5233 lua_pushnil(L);
5234 }
5235 } else if (t == late_lua_node) {
5236 /*tex The |string| key is obsolete. */
5237 if (lua_key_eq(s, data) || lua_key_eq(s, token) || lua_key_eq(s, string)) {
5238 get_late_lua_direct_value(L,n);
5239 } else if (lua_key_eq(s, name)) {
5240 tokenlist_to_luastring(L, late_lua_name(n));
5241 } else {
5242 lua_pushnil(L);
5243 }
5244 } else if (t == pdf_annot_node) {
5245 if (lua_key_eq(s, width)) {
5246 lua_pushinteger(L, width(n));
5247 } else if (lua_key_eq(s, depth)) {
5248 lua_pushinteger(L, depth(n));
5249 } else if (lua_key_eq(s, height)) {
5250 lua_pushinteger(L, height(n));
5251 } else if (lua_key_eq(s, objnum)) {
5252 lua_pushinteger(L, pdf_annot_objnum(n));
5253 } else if (lua_key_eq(s, data)) {
5254 tokenlist_to_luastring(L, pdf_annot_data(n));
5255 } else {
5256 lua_pushnil(L);
5257 }
5258 } else if (t == pdf_dest_node) {
5259 if (lua_key_eq(s, width)) {
5260 lua_pushinteger(L, width(n));
5261 } else if (lua_key_eq(s, depth)) {
5262 lua_pushinteger(L, depth(n));
5263 } else if (lua_key_eq(s, height)) {
5264 lua_pushinteger(L, height(n));
5265 } else if (lua_key_eq(s, named_id)) {
5266 lua_pushinteger(L, pdf_dest_named_id(n));
5267 } else if (lua_key_eq(s, dest_id)) {
5268 if (pdf_dest_named_id(n) == 1)
5269 tokenlist_to_luastring(L, pdf_dest_id(n));
5270 else
5271 lua_pushinteger(L, pdf_dest_id(n));
5272 } else if (lua_key_eq(s, dest_type)) {
5273 lua_pushinteger(L, pdf_dest_type(n));
5274 } else if (lua_key_eq(s, xyz_zoom)) {
5275 lua_pushinteger(L, pdf_dest_xyz_zoom(n));
5276 } else if (lua_key_eq(s, objnum)) {
5277 lua_pushinteger(L, pdf_dest_objnum(n));
5278 } else {
5279 lua_pushnil(L);
5280 }
5281 } else if (t == pdf_setmatrix_node) {
5282 if (lua_key_eq(s, data)) {
5283 get_pdf_setmatrix_direct_value(L,n);
5284 } else {
5285 lua_pushnil(L);
5286 }
5287 } else if (t == pdf_colorstack_node) {
5288 if (lua_key_eq(s, stack)) {
5289 lua_pushinteger(L, pdf_colorstack_stack(n));
5290 } else if (lua_key_eq(s, command)) {
5291 lua_pushinteger(L, pdf_colorstack_cmd(n));
5292 } else if (lua_key_eq(s, data)) {
5293 tokenlist_to_luastring(L, pdf_colorstack_data(n));
5294 } else {
5295 lua_pushnil(L);
5296 }
5297 } else if (t == pdf_refobj_node) {
5298 if (lua_key_eq(s, objnum)) {
5299 lua_pushinteger(L, pdf_obj_objnum(n));
5300 } else {
5301 lua_pushnil(L);
5302 }
5303 } else if (t == write_node) {
5304 if (lua_key_eq(s, stream)) {
5305 lua_pushinteger(L, write_stream(n));
5306 } else if (lua_key_eq(s, data)) {
5307 get_write_direct_data(L,n);
5308 } else if (lua_key_eq(s, value)) {
5309 get_write_direct_value(L,n);
5310 } else {
5311 lua_pushnil(L);
5312 }
5313 } else if (t == special_node || t == late_special_node) {
5314 if (lua_key_eq(s, data) || lua_key_eq(s, value)) {
5315 get_special_direct_value(L,n);
5316 } else {
5317 lua_pushnil(L);
5318 }
5319 } else if (t == pdf_start_link_node) {
5320 if (lua_key_eq(s, width)) {
5321 lua_pushinteger(L, width(n));
5322 } else if (lua_key_eq(s, depth)) {
5323 lua_pushinteger(L, depth(n));
5324 } else if (lua_key_eq(s, height)) {
5325 lua_pushinteger(L, height(n));
5326 } else if (lua_key_eq(s, objnum)) {
5327 lua_pushinteger(L, pdf_link_objnum(n));
5328 } else if (lua_key_eq(s, link_attr)) {
5329 tokenlist_to_luastring(L, pdf_link_attr(n));
5330 } else if (lua_key_eq(s, action)) {
5331 nodelib_pushaction(L, pdf_link_action(n));
5332 } else {
5333 lua_pushnil(L);
5334 }
5335 } else if (t == pdf_action_node) {
5336 if (lua_key_eq(s, action_type)) {
5337 lua_pushinteger(L, pdf_action_type(n));
5338 } else if (lua_key_eq(s, named_id)) {
5339 lua_pushinteger(L, pdf_action_named_id(n));
5340 } else if (lua_key_eq(s, action_id)) {
5341 if (pdf_action_named_id(n) & 1) {
5342 tokenlist_to_luastring(L, pdf_action_id(n));
5343 } else {
5344 lua_pushinteger(L, pdf_action_id(n));
5345 }
5346 } else if (lua_key_eq(s, file)) {
5347 tokenlist_to_luastring(L, pdf_action_file(n));
5348 } else if (lua_key_eq(s, new_window)) {
5349 lua_pushinteger(L, pdf_action_new_window(n));
5350 } else if (lua_key_eq(s, data)) {
5351 tokenlist_to_luastring(L, pdf_action_tokens(n));
5352 } else if (lua_key_eq(s, struct_id)) {
5353 if (pdf_action_struct_id(n) == null) {
5354 lua_pushnil(L);
5355 } else if (pdf_action_named_id(n) & 2) {
5356 tokenlist_to_luastring(L, pdf_action_struct_id(n));
5357 } else {
5358 lua_pushinteger(L, pdf_action_struct_id(n));
5359 }
5360 } else {
5361 lua_pushnil(L);
5362 }
5363 } else if ((t == pdf_thread_node) || (t == pdf_start_thread_node)) {
5364 if (lua_key_eq(s, width)) {
5365 lua_pushinteger(L, width(n));
5366 } else if (lua_key_eq(s, depth)) {
5367 lua_pushinteger(L, depth(n));
5368 } else if (lua_key_eq(s, height)) {
5369 lua_pushinteger(L, height(n));
5370 } else if (lua_key_eq(s, named_id)) {
5371 lua_pushinteger(L, pdf_thread_named_id(n));
5372 } else if (lua_key_eq(s, thread_id)) {
5373 if (pdf_thread_named_id(n) == 1) {
5374 tokenlist_to_luastring(L, pdf_thread_id(n));
5375 } else {
5376 lua_pushinteger(L, pdf_thread_id(n));
5377 }
5378 } else if (lua_key_eq(s, thread_attr)) {
5379 tokenlist_to_luastring(L, pdf_thread_attr(n));
5380 } else {
5381 lua_pushnil(L);
5382 }
5383 } else if (t == open_node) {
5384 if (lua_key_eq(s, stream)) {
5385 lua_pushinteger(L, write_stream(n));
5386 } else if (lua_key_eq(s, name)) {
5387 nodelib_pushstring(L, open_name(n));
5388 } else if (lua_key_eq(s, area)) {
5389 nodelib_pushstring(L, open_area(n));
5390 } else if (lua_key_eq(s, ext)) {
5391 nodelib_pushstring(L, open_ext(n));
5392 } else {
5393 lua_pushnil(L);
5394 }
5395 } else if (t == close_node) {
5396 if (lua_key_eq(s, stream)) {
5397 lua_pushinteger(L, write_stream(n));
5398 } else {
5399 lua_pushnil(L);
5400 }
5401 } else {
5402 lua_pushnil(L);
5403 }
5404 }
5405
5406 static int lua_nodelib_direct_getfield(lua_State * L)
5407 {
5408
5409 const char *s;
5410 halfword n = lua_tointeger(L, 1);
5411 int t = type(n);
5412 if (lua_type(L, 2) == LUA_TNUMBER) {
5413 halfword p;
5414 int i;
5415 if (! nodetype_has_attributes(t)) {
5416 lua_pushnil(L) ;
5417 return 1;
5418 }
5419 p = node_attr(n);
5420 if (p == null || vlink(p) == null) {
5421 lua_pushnil(L) ;
5422 return 1;
5423 }
5424 i = (int) lua_tointeger(L, 2);
5425 p = vlink(p);
5426 while (p != null) {
5427 if (attribute_id(p) == i) {
5428 if ((int) attribute_value(p) > UNUSED_ATTRIBUTE) {
5429 lua_pushinteger(L, (int) attribute_value(p));
5430 } else {
5431 lua_pushnil(L);
5432 }
5433 return 1;
5434 } else if (attribute_id(p) > i) {
5435 lua_pushnil(L) ;
5436 return 1;
5437 }
5438 p = vlink(p);
5439 }
5440 lua_pushnil(L) ;
5441 return 1;
5442 }
5443
5444 s = lua_tostring(L, 2);
5445
5446 if (lua_key_eq(s, id)) {
5447 lua_pushinteger(L, t);
5448 } else if (lua_key_eq(s, next)) {
5449 nodelib_pushdirect_or_nil(vlink(n));
5450 } else if (lua_key_eq(s, prev)) {
5451 nodelib_pushdirect_or_nil(alink(n));
5452 } else if (lua_key_eq(s, subtype)) {
5453 if (t == glue_spec_node) {
5454 lua_pushinteger(L, 0); /* dummy, the only one */
5455 } else {
5456 lua_pushinteger(L, subtype(n));
5457 }
5458 } else if (lua_key_eq(s, attr)) {
5459 if (! nodetype_has_attributes(t)) {
5460 lua_pushnil(L);
5461 } else {
5462 /* nodelib_pushattr(L, node_attr(n)); */
5463 lua_pushinteger(L, node_attr(n));
5464 }
5465 } else if (t == glyph_node) {
5466 if (lua_key_eq(s, font)) {
5467 lua_pushinteger(L, font(n));
5468 } else if (lua_key_eq(s, char)) {
5469 lua_pushinteger(L, character(n));
5470 } else if (lua_key_eq(s, xoffset)) {
5471 lua_pushinteger(L, x_displace(n));
5472 } else if (lua_key_eq(s, yoffset)) {
5473 lua_pushinteger(L, y_displace(n));
5474 } else if (lua_key_eq(s, data)) {
5475 lua_pushinteger(L, glyph_node_data(n));
5476 } else if (lua_key_eq(s, width)) {
5477 lua_pushinteger(L, char_width(font(n),character(n)));
5478 } else if (lua_key_eq(s, height)) {
5479 lua_pushinteger(L, char_height(font(n),character(n)));
5480 } else if (lua_key_eq(s, depth)) {
5481 lua_pushinteger(L, char_depth(font(n),character(n)));
5482 } else if (lua_key_eq(s, expansion_factor)) {
5483 lua_pushinteger(L, ex_glyph(n));
5484 } else if (lua_key_eq(s, components)) {
5485 nodelib_pushdirect_or_nil(lig_ptr(n));
5486 } else if (lua_key_eq(s, lang)) {
5487 lua_pushinteger(L, char_lang(n));
5488 } else if (lua_key_eq(s, left)) {
5489 lua_pushinteger(L, char_lhmin(n));
5490 } else if (lua_key_eq(s, right)) {
5491 lua_pushinteger(L, char_rhmin(n));
5492 } else if (lua_key_eq(s, uchyph)) {
5493 lua_pushinteger(L, char_uchyph(n));
5494 } else {
5495 lua_pushnil(L);
5496 }
5497 } else if ((t == hlist_node) || (t == vlist_node)) {
5498 /* candidates: whd (width,height,depth) */
5499 if (lua_key_eq(s, list) || lua_key_eq(s, head)) {
5500 nodelib_pushdirect_or_nil_alink(list_ptr(n));
5501 } else if (lua_key_eq(s, width)) {
5502 lua_pushinteger(L, width(n));
5503 } else if (lua_key_eq(s, height)) {
5504 lua_pushinteger(L, height(n));
5505 } else if (lua_key_eq(s, depth)) {
5506 lua_pushinteger(L, depth(n));
5507 } else if (lua_key_eq(s, direction)) {
5508 lua_push_direction(L, box_dir(n));
5509 } else if (lua_key_eq(s, dir)) {
5510 lua_push_dir_par(L, box_dir(n));
5511 } else if (lua_key_eq(s, shift)) {
5512 lua_pushinteger(L, shift_amount(n));
5513 } else if (lua_key_eq(s, glue_order)) {
5514 lua_pushinteger(L, glue_order(n));
5515 } else if (lua_key_eq(s, glue_sign)) {
5516 lua_pushinteger(L, glue_sign(n));
5517 } else if (lua_key_eq(s, glue_set)) {
5518 lua_pushnumber(L, (double) glue_set(n)); /* float */
5519 } else {
5520 lua_pushnil(L);
5521 }
5522 } else if (t == disc_node) {
5523 if (lua_key_eq(s, pre)) {
5524 nodelib_pushdirect_or_nil(vlink(pre_break(n)));
5525 } else if (lua_key_eq(s, post)) {
5526 nodelib_pushdirect_or_nil(vlink(post_break(n)));
5527 } else if (lua_key_eq(s, replace)) {
5528 nodelib_pushdirect_or_nil(vlink(no_break(n)));
5529 } else if (lua_key_eq(s, penalty)) {
5530 lua_pushinteger(L, disc_penalty(n));
5531 } else {
5532 lua_pushnil(L);
5533 }
5534 } else if (t == glue_node) {
5535 if (lua_key_eq(s, width)) {
5536 lua_pushinteger(L, width(n));
5537 } else if (lua_key_eq(s, stretch)) {
5538 lua_pushinteger(L, stretch(n));
5539 } else if (lua_key_eq(s, shrink)) {
5540 lua_pushinteger(L, shrink(n));
5541 } else if (lua_key_eq(s, stretch_order)) {
5542 lua_pushinteger(L, stretch_order(n));
5543 } else if (lua_key_eq(s, shrink_order)) {
5544 lua_pushinteger(L, shrink_order(n));
5545 } else if (lua_key_eq(s, leader)) {
5546 nodelib_pushdirect_or_nil(leader_ptr(n));
5547 } else {
5548 lua_pushnil(L);
5549 }
5550 } else if (t == kern_node) {
5551 if (lua_key_eq(s, kern)) {
5552 lua_pushinteger(L, width(n));
5553 } else if (lua_key_eq(s, expansion_factor)) {
5554 lua_pushinteger(L, ex_kern(n));
5555 } else {
5556 lua_pushnil(L);
5557 }
5558 } else if (t == penalty_node) {
5559 if (lua_key_eq(s, penalty)) {
5560 lua_pushinteger(L, penalty(n));
5561 } else {
5562 lua_pushnil(L);
5563 }
5564 } else if (t == rule_node) {
5565 /* candidates: whd (width,height,depth) */
5566 if (lua_key_eq(s, width)) {
5567 lua_pushinteger(L, width(n));
5568 } else if (lua_key_eq(s, height)) {
5569 lua_pushinteger(L, height(n));
5570 } else if (lua_key_eq(s, depth)) {
5571 lua_pushinteger(L, depth(n));
5572 } else if (lua_key_eq(s, left)) {
5573 lua_pushinteger(L,rule_left(n));
5574 } else if (lua_key_eq(s, right)) {
5575 lua_pushinteger(L,rule_right(n));
5576 } else if (lua_key_eq(s, direction)) {
5577 lua_push_direction(L, rule_dir(n));
5578 } else if (lua_key_eq(s, dir)) {
5579 lua_push_dir_par(L, rule_dir(n));
5580 } else if (lua_key_eq(s, index)) {
5581 lua_pushinteger(L, rule_index(n));
5582 } else if (lua_key_eq(s, transform)) {
5583 lua_pushinteger(L,rule_transform(n));
5584 } else {
5585 lua_pushnil(L);
5586 }
5587 } else if (t == dir_node) {
5588 if (lua_key_eq(s, direction)) {
5589 lua_push_direction(L, dir_dir(n));
5590 } else if (lua_key_eq(s, dir)) {
5591 lua_push_dir_text(L, dir_dir(n), subtype(n));
5592 } else if (lua_key_eq(s, level)) {
5593 lua_pushinteger(L, dir_level(n));
5594 } else if (lua_key_eq(s, subtype)) { /* can be used for anything */
5595 lua_pushinteger(L, subtype(n));
5596 } else {
5597 lua_pushnil(L);
5598 }
5599 } else if (t == local_par_node) {
5600 if (lua_key_eq(s, pen_inter)) {
5601 lua_pushinteger(L, local_pen_inter(n));
5602 } else if (lua_key_eq(s, pen_broken)) {
5603 lua_pushinteger(L, local_pen_broken(n));
5604 } else if (lua_key_eq(s, direction)) {
5605 lua_push_direction(L, local_par_dir(n));
5606 } else if (lua_key_eq(s, dir)) {
5607 lua_push_dir_par(L, local_par_dir(n));
5608 } else if (lua_key_eq(s, box_left)) {
5609 nodelib_pushdirect_or_nil(local_box_left(n));
5610 } else if (lua_key_eq(s, box_left_width)) {
5611 lua_pushinteger(L, local_box_left_width(n));
5612 } else if (lua_key_eq(s, box_right)) {
5613 nodelib_pushdirect_or_nil(local_box_right(n));
5614 } else if (lua_key_eq(s, box_right_width)) {
5615 lua_pushinteger(L, local_box_right_width(n));
5616 } else {
5617 lua_pushnil(L);
5618 }
5619 } else if (t == whatsit_node) {
5620 lua_nodelib_direct_getfield_whatsit(L, n, s);
5621 } else if (t == simple_noad) {
5622 if (lua_key_eq(s, nucleus)) {
5623 nodelib_pushdirect_or_nil(nucleus(n));
5624 } else if (lua_key_eq(s, sub)) {
5625 nodelib_pushdirect_or_nil(subscr(n));
5626 } else if (lua_key_eq(s, sup)) {
5627 nodelib_pushdirect_or_nil(supscr(n));
5628 } else if (lua_key_eq(s, options)) {
5629 lua_pushinteger(L, noadoptions(n));
5630 } else {
5631 lua_pushnil(L);
5632 }
5633 } else if ((t == math_char_node) || (t == math_text_char_node)) {
5634 if (lua_key_eq(s, fam)) {
5635 lua_pushinteger(L, math_fam(n));
5636 } else if (lua_key_eq(s, char)) {
5637 lua_pushinteger(L, math_character(n));
5638 } else if (lua_key_eq(s, font)) {
5639 lua_pushinteger(L, fam_fnt(math_fam(n), 0));
5640 } else {
5641 lua_pushnil(L);
5642 }
5643 } else if (t == mark_node) {
5644 if (lua_key_eq(s, class)) {
5645 lua_pushinteger(L, mark_class(n));
5646 } else if (lua_key_eq(s, mark)) {
5647 tokenlist_to_lua(L, mark_ptr(n));
5648 } else {
5649 lua_pushnil(L);
5650 }
5651 } else if (t == ins_node) {
5652 if (lua_key_eq(s, cost)) {
5653 lua_pushinteger(L, float_cost(n));
5654 } else if (lua_key_eq(s, depth)) {
5655 lua_pushinteger(L, depth(n));
5656 } else if (lua_key_eq(s, height)) {
5657 lua_pushinteger(L, height(n));
5658 } else if ((lua_key_eq(s, list)) || (lua_key_eq(s, head))) {
5659 nodelib_pushdirect_or_nil_alink(ins_ptr(n));
5660 /* glue */
5661 } else if (lua_key_eq(s, width)) {
5662 lua_pushinteger(L, width(n));
5663 } else if (lua_key_eq(s, stretch)) {
5664 lua_pushinteger(L, stretch(n));
5665 } else if (lua_key_eq(s, shrink)) {
5666 lua_pushinteger(L, shrink(n));
5667 } else if (lua_key_eq(s, stretch_order)) {
5668 lua_pushinteger(L, stretch_order(n));
5669 } else if (lua_key_eq(s, shrink_order)) {
5670 lua_pushinteger(L, shrink_order(n));
5671 } else {
5672 lua_pushnil(L);
5673 }
5674 } else if (t == math_node) {
5675 if (lua_key_eq(s, surround)) {
5676 lua_pushinteger(L, surround(n));
5677 /* glue */
5678 } else if (lua_key_eq(s, width)) {
5679 lua_pushinteger(L, width(n));
5680 } else if (lua_key_eq(s, stretch)) {
5681 lua_pushinteger(L, stretch(n));
5682 } else if (lua_key_eq(s, shrink)) {
5683 lua_pushinteger(L, shrink(n));
5684 } else if (lua_key_eq(s, stretch_order)) {
5685 lua_pushinteger(L, stretch_order(n));
5686 } else if (lua_key_eq(s, shrink_order)) {
5687 lua_pushinteger(L, shrink_order(n));
5688 } else {
5689 lua_pushnil(L);
5690 }
5691 } else if (t == fraction_noad) {
5692 if (lua_key_eq(s, width)) {
5693 lua_pushinteger(L, thickness(n));
5694 } else if (lua_key_eq(s, num)) {
5695 nodelib_pushdirect_or_nil(numerator(n));
5696 } else if (lua_key_eq(s, denom)) {
5697 nodelib_pushdirect_or_nil(denominator(n));
5698 } else if (lua_key_eq(s, left)) {
5699 nodelib_pushdirect_or_nil(left_delimiter(n));
5700 } else if (lua_key_eq(s, right)) {
5701 nodelib_pushdirect_or_nil(right_delimiter(n));
5702 } else if (lua_key_eq(s, middle)) {
5703 nodelib_pushdirect_or_nil(middle_delimiter(n));
5704 } else if (lua_key_eq(s, fam)) {
5705 lua_pushinteger(L, fraction_fam(n));
5706 } else if (lua_key_eq(s, options)) {
5707 lua_pushinteger(L, fractionoptions(n));
5708 } else {
5709 lua_pushnil(L);
5710 }
5711 } else if (t == style_node) {
5712 if (lua_key_eq(s, style)) {
5713 lua_push_math_style_name(L,subtype(n));
5714 } else {
5715 lua_pushnil(L);
5716 }
5717 } else if (t == accent_noad) {
5718 if (lua_key_eq(s, nucleus)) {
5719 nodelib_pushdirect_or_nil(nucleus(n));
5720 } else if (lua_key_eq(s, sub)) {
5721 nodelib_pushdirect_or_nil(subscr(n));
5722 } else if (lua_key_eq(s, sup)) {
5723 nodelib_pushdirect_or_nil(supscr(n));
5724 } else if ((lua_key_eq(s, top_accent))||(lua_key_eq(s, accent))) {
5725 nodelib_pushdirect_or_nil(top_accent_chr(n));
5726 } else if (lua_key_eq(s, bot_accent)) {
5727 nodelib_pushdirect_or_nil(bot_accent_chr(n));
5728 } else if (lua_key_eq(s, overlay_accent)) {
5729 nodelib_pushdirect_or_nil(overlay_accent_chr(n));
5730 } else if (lua_key_eq(s, fraction)) {
5731 lua_pushinteger(L, accentfraction(n));
5732 } else {
5733 lua_pushnil(L);
5734 }
5735 } else if (t == fence_noad) {
5736 if (lua_key_eq(s, delim)) {
5737 nodelib_pushdirect_or_nil(delimiter(n));
5738 } else if (lua_key_eq(s, italic)) {
5739 lua_pushinteger(L, delimiteritalic(n));
5740 } else if (lua_key_eq(s, height)) {
5741 lua_pushinteger(L, delimiterheight(n));
5742 } else if (lua_key_eq(s, depth)) {
5743 lua_pushinteger(L, delimiterdepth(n));
5744 } else if (lua_key_eq(s, options)) {
5745 lua_pushinteger(L, delimiteroptions(n));
5746 } else if (lua_key_eq(s, class)) {
5747 lua_pushinteger(L, delimiterclass(n));
5748 } else {
5749 lua_pushnil(L);
5750 }
5751 } else if (t == delim_node) {
5752 if (lua_key_eq(s, small_fam)) {
5753 lua_pushinteger(L, small_fam(n));
5754 } else if (lua_key_eq(s, small_char)) {
5755 lua_pushinteger(L, small_char(n));
5756 } else if (lua_key_eq(s, large_fam)) {
5757 lua_pushinteger(L, large_fam(n));
5758 } else if (lua_key_eq(s, large_char)) {
5759 lua_pushinteger(L, large_char(n));
5760 } else {
5761 lua_pushnil(L);
5762 }
5763 } else if ((t == sub_box_node) || (t == sub_mlist_node)) {
5764 if ((lua_key_eq(s, list)) || (lua_key_eq(s, head))){
5765 nodelib_pushdirect_or_nil_alink(math_list(n));
5766 } else {
5767 lua_pushnil(L);
5768 }
5769 } else if (t == radical_noad) {
5770 if (lua_key_eq(s, nucleus)) {
5771 nodelib_pushdirect_or_nil(nucleus(n));
5772 } else if (lua_key_eq(s, sub)) {
5773 nodelib_pushdirect_or_nil(subscr(n));
5774 } else if (lua_key_eq(s, sup)) {
5775 nodelib_pushdirect_or_nil(supscr(n));
5776 } else if (lua_key_eq(s, left)) {
5777 nodelib_pushdirect_or_nil(left_delimiter(n));
5778 } else if (lua_key_eq(s, degree)) {
5779 nodelib_pushdirect_or_nil(degree(n));
5780 } else if (lua_key_eq(s, width)) {
5781 lua_pushinteger(L, radicalwidth(n));
5782 } else if (lua_key_eq(s, options)) {
5783 lua_pushinteger(L, radicaloptions(n));
5784 } else {
5785 lua_pushnil(L);
5786 }
5787 } else if (t == margin_kern_node) {
5788 if (lua_key_eq(s, kern) ||lua_key_eq(s, width)) {
5789 lua_pushinteger(L, width(n));
5790 } else if (lua_key_eq(s, glyph)) {
5791 nodelib_pushdirect_or_nil(margin_char(n));
5792 } else {
5793 lua_pushnil(L);
5794 }
5795 } else if (t == split_up_node) {
5796 if (lua_key_eq(s, last_ins_ptr)) {
5797 nodelib_pushdirect_or_nil(last_ins_ptr(n));
5798 } else if (lua_key_eq(s, best_ins_ptr)) {
5799 nodelib_pushdirect_or_nil(best_ins_ptr(n));
5800 } else if (lua_key_eq(s, broken_ptr)) {
5801 nodelib_pushdirect_or_nil(broken_ptr(n));
5802 } else if (lua_key_eq(s, broken_ins)) {
5803 nodelib_pushdirect_or_nil(broken_ins(n));
5804 } else {
5805 lua_pushnil(L);
5806 }
5807 } else if (t == choice_node) {
5808 if (lua_key_eq(s, display)) {
5809 nodelib_pushdirect_or_nil(display_mlist(n));
5810 } else if (lua_key_eq(s, text)) {
5811 nodelib_pushdirect_or_nil(text_mlist(n));
5812 } else if (lua_key_eq(s, script)) {
5813 nodelib_pushdirect_or_nil(script_mlist(n));
5814 } else if (lua_key_eq(s, scriptscript)) {
5815 nodelib_pushdirect_or_nil(script_script_mlist(n));
5816 } else {
5817 lua_pushnil(L);
5818 }
5819 } else if (t == inserting_node) {
5820 if (lua_key_eq(s, last_ins_ptr)) {
5821 nodelib_pushdirect_or_nil(last_ins_ptr(n));
5822 } else if (lua_key_eq(s, best_ins_ptr)) {
5823 nodelib_pushdirect_or_nil(best_ins_ptr(n));
5824 } else {
5825 lua_pushnil(L);
5826 }
5827 } else if (t == attribute_node) {
5828 if (lua_key_eq(s, number)) {
5829 lua_pushinteger(L, attribute_id(n));
5830 } else if (lua_key_eq(s, value)) {
5831 lua_pushinteger(L, attribute_value(n));
5832 } else {
5833 lua_pushnil(L);
5834 }
5835 } else if (t == adjust_node) {
5836 if ((lua_key_eq(s, list)) || (lua_key_eq(s, head))) {
5837 nodelib_pushdirect_or_nil_alink(adjust_ptr(n));
5838 } else {
5839 lua_pushnil(L);
5840 }
5841 } else if (t == unset_node) {
5842 if (lua_key_eq(s, width)) {
5843 lua_pushinteger(L, width(n));
5844 } else if (lua_key_eq(s, height)) {
5845 lua_pushinteger(L, height(n));
5846 } else if (lua_key_eq(s, depth)) {
5847 lua_pushinteger(L, depth(n));
5848 } else if (lua_key_eq(s, direction)) {
5849 lua_push_direction(L, box_dir(n));
5850 } else if (lua_key_eq(s, dir)) {
5851 lua_push_dir_par(L, box_dir(n));
5852 } else if (lua_key_eq(s, shrink)) {
5853 lua_pushinteger(L, glue_shrink(n));
5854 } else if (lua_key_eq(s, glue_order)) {
5855 lua_pushinteger(L, glue_order(n));
5856 } else if (lua_key_eq(s, glue_sign)) {
5857 lua_pushinteger(L, glue_sign(n));
5858 } else if (lua_key_eq(s, stretch)) {
5859 lua_pushinteger(L, glue_stretch(n));
5860 } else if (lua_key_eq(s, count)) {
5861 lua_pushinteger(L, span_count(n));
5862 } else if ((lua_key_eq(s, list)) || (lua_key_eq(s, head))){
5863 nodelib_pushdirect_or_nil_alink(list_ptr(n));
5864 } else {
5865 lua_pushnil(L);
5866 }
5867 /* } else if (t == attribute_list_node) { */
5868 /* lua_pushnil(L); */
5869 } else if (t == boundary_node) {
5870 if (lua_key_eq(s, subtype)) {
5871 lua_pushinteger(L, subtype(n));
5872 } else if (lua_key_eq(s, value)) {
5873 lua_pushinteger(L, boundary_value(n));
5874 } else {
5875 lua_pushnil(L);
5876 }
5877 } else if (t == glue_spec_node) {
5878 if (lua_key_eq(s, width)) {
5879 lua_pushinteger(L, width(n));
5880 } else if (lua_key_eq(s, stretch)) {
5881 lua_pushinteger(L, stretch(n));
5882 } else if (lua_key_eq(s, shrink)) {
5883 lua_pushinteger(L, shrink(n));
5884 } else if (lua_key_eq(s, stretch_order)) {
5885 lua_pushinteger(L, stretch_order(n));
5886 } else if (lua_key_eq(s, shrink_order)) {
5887 lua_pushinteger(L, shrink_order(n));
5888 } else {
5889 lua_pushnil(L);
5890 }
5891 } else {
5892 lua_pushnil(L);
5893 }
5894 return 1;
5895 }
5896
5897 /* msg could be preallocated and shared */
5898
5899 static void lua_nodelib_do_tostring(lua_State * L, halfword n, const char *tag)
5900 {
5901 char *msg;
5902 char a[7] = { ' ', ' ', ' ', 'n', 'i', 'l', 0 };
5903 char v[7] = { ' ', ' ', ' ', 'n', 'i', 'l', 0 };
5904 msg = xmalloc(256);
5905 if ((alink(n) != null) && (type(n) != attribute_node))
5906 snprintf(a, 7, "%6d", (int) alink(n));
5907 if (vlink(n) != null)
5908 snprintf(v, 7, "%6d", (int) vlink(n));
5909 snprintf(msg, 255, "<%s %s < %6d > %s : %s %d>", tag, a, (int) n, v, node_data[type(n)].name, subtype(n));
5910 lua_pushstring(L, msg);
5911 free(msg);
5912 return ;
5913 }
5914
5915 /* __tostring node.tostring */
5916
5917 static int lua_nodelib_tostring(lua_State * L)
5918 {
5919 halfword n = *check_isnode(L, 1);
5920 lua_nodelib_do_tostring(L, n, "node");
5921 return 1;
5922 }
5923
5924 /* node.direct.tostring */
5925
5926 static int lua_nodelib_direct_tostring(lua_State * L)
5927 {
5928 halfword n = lua_tointeger(L,1);
5929 if (n==0) {
5930 lua_pushnil(L);
5931 } else {
5932 lua_nodelib_do_tostring(L, n, "direct");
5933 }
5934 return 1;
5935 }
5936
5937 /* __eq */
5938
5939 static int lua_nodelib_equal(lua_State * L)
5940 {
5941 halfword n = *((halfword *) lua_touserdata(L, 1));
5942 halfword m = *((halfword *) lua_touserdata(L, 2));
5943 lua_pushboolean(L, (n == m));
5944 return 1;
5945 }
5946
5947 /* node.ligaturing */
5948
5949 static int font_tex_ligaturing(lua_State * L)
5950 {
5951 /* on the stack are two nodes and a direction */
5952 /* we need to deal with prev nodes when a range starts with a ligature */
5953 halfword tmp_head;
5954 halfword h;
5955 halfword t = null;
5956 halfword p ;
5957 if (lua_gettop(L) < 1) {
5958 lua_pushnil(L);
5959 lua_pushboolean(L, 0);
5960 return 2;
5961 }
5962 h = *check_isnode(L, 1);
5963 if (lua_gettop(L) > 1) {
5964 t = *check_isnode(L, 2);
5965 }
5966 tmp_head = new_node(nesting_node, 1);
5967 p = alink(h);
5968 couple_nodes(tmp_head, h);
5969 tlink(tmp_head) = t;
5970 t = handle_ligaturing(tmp_head, t);
5971 if (p != null) {
5972 vlink(p) = vlink(tmp_head) ;
5973 }
5974 alink(vlink(tmp_head)) = p ;
5975 /*
5976 lua_pushinteger(L, vlink(tmp_head));
5977 lua_nodelib_push(L);
5978 */
5979 lua_nodelib_push_fast(L, vlink(tmp_head));
5980 /*
5981 lua_pushinteger(L, t);
5982 lua_nodelib_push(L);
5983 */
5984 lua_nodelib_push_fast(L, t);
5985 lua_pushboolean(L, 1);
5986 flush_node(tmp_head);
5987 return 3;
5988 }
5989
5990 static int font_tex_direct_ligaturing(lua_State * L)
5991 {
5992 /* on the stack are two nodes and a direction */
5993 /* we need to deal with prev nodes when a range starts with a ligature */
5994 halfword tmp_head;
5995 halfword h;
5996 halfword t = null;
5997 halfword p ;
5998 if (lua_gettop(L) < 1) {
5999 lua_pushnil(L);
6000 lua_pushboolean(L, 0);
6001 return 2;
6002 }
6003 h = lua_tointeger(L, 1);
6004 if (lua_gettop(L) > 1) {
6005 t = lua_tointeger(L, 2);
6006 }
6007 tmp_head = new_node(nesting_node, 1);
6008 p = alink(h);
6009 couple_nodes(tmp_head, h);
6010 tlink(tmp_head) = t;
6011 t = handle_ligaturing(tmp_head, t);
6012 if (p != null) {
6013 vlink(p) = vlink(tmp_head) ;
6014 }
6015 alink(vlink(tmp_head)) = p ;
6016 lua_pushinteger(L, vlink(tmp_head));
6017 lua_pushinteger(L, t);
6018 lua_pushboolean(L, 1);
6019 flush_node(tmp_head);
6020 return 3;
6021 }
6022
6023 /* node.kerning */
6024
6025 static int font_tex_kerning(lua_State * L)
6026 {
6027 /* on the stack are two nodes and a direction */
6028
6029 halfword tmp_head;
6030 halfword h;
6031 halfword t = null;
6032 halfword p ;
6033 if (lua_gettop(L) < 1) {
6034 lua_pushnil(L);
6035 lua_pushboolean(L, 0);
6036 return 2;
6037 }
6038 h = *check_isnode(L, 1);
6039 if (lua_gettop(L) > 1) {
6040 t = *check_isnode(L, 2);
6041 }
6042 tmp_head = new_node(nesting_node, 1);
6043 p = alink(h);
6044 couple_nodes(tmp_head, h);
6045 tlink(tmp_head) = t;
6046 t = handle_kerning(tmp_head, t);
6047 if (p != null) {
6048 vlink(p) = vlink(tmp_head) ;
6049 }
6050 alink(vlink(tmp_head)) = p ;
6051 /*
6052 lua_pushinteger(L, vlink(tmp_head));
6053 lua_nodelib_push(L);
6054 */
6055 lua_nodelib_push_fast(L, vlink(tmp_head));
6056 /*
6057 lua_pushinteger(L, t);
6058 lua_nodelib_push(L);
6059 */
6060 lua_nodelib_push_fast(L, t);
6061 lua_pushboolean(L, 1);
6062 flush_node(tmp_head);
6063 return 3;
6064 }
6065
6066 static int font_tex_direct_kerning(lua_State * L)
6067 {
6068 /* on the stack are two nodes and a direction */
6069
6070 halfword tmp_head;
6071 halfword h;
6072 halfword t = null;
6073 halfword p ;
6074 if (lua_gettop(L) < 1) {
6075 lua_pushnil(L);
6076 lua_pushboolean(L, 0);
6077 return 2;
6078 }
6079 h = lua_tointeger(L, 1);
6080 if (lua_gettop(L) > 1) {
6081 t = lua_tointeger(L, 2);
6082 }
6083 tmp_head = new_node(nesting_node, 1);
6084 p = alink(h);
6085 couple_nodes(tmp_head, h);
6086 tlink(tmp_head) = t;
6087 t = handle_kerning(tmp_head, t);
6088 if (p != null) {
6089 vlink(p) = vlink(tmp_head) ;
6090 }
6091 alink(vlink(tmp_head)) = p ;
6092 lua_pushinteger(L, vlink(tmp_head));
6093 if (t == null) {
6094 lua_pushnil(L);
6095 } else {
6096 lua_pushinteger(L, t);
6097 }
6098 lua_pushboolean(L, 1);
6099 flush_node(tmp_head);
6100 return 3;
6101 }
6102
6103 /*tex
6104 It's more consistent to have it here (so we will alias in lang later). Todo:
6105 if no glyph then quit.
6106 */
6107
6108 static int lang_tex_hyphenating(lua_State * L)
6109 {
6110 halfword t = null;
6111 halfword h = *check_isnode(L, 1);
6112 if (lua_isuserdata(L, 2)) {
6113 t = *check_isnode(L, 2);
6114 }
6115 if (t == null) {
6116 t = h;
6117 while (vlink(t) != null) {
6118 t = vlink(t);
6119 }
6120 }
6121 hnj_hyphenation(h, t);
6122 lua_nodelib_push_fast(L, h);
6123 lua_nodelib_push_fast(L, t);
6124 lua_pushboolean(L, 1);
6125 return 3;
6126 }
6127
6128 static int lang_tex_direct_hyphenating(lua_State * L)
6129 {
6130 halfword t = null;
6131 halfword h = lua_tointeger(L, 1);
6132 if (lua_type(L,1) != LUA_TNUMBER) {
6133 t = lua_tointeger(L, 2);
6134 }
6135 if (t == null) {
6136 t = h;
6137 while (vlink(t) != null) {
6138 t = vlink(t);
6139 }
6140 }
6141 hnj_hyphenation(h, t);
6142 lua_pushinteger(L, h);
6143 lua_pushinteger(L, t);
6144 lua_pushboolean(L, 1);
6145 return 3;
6146 }
6147
6148 /* node.protect_glyphs (returns also boolean because that signals callback) */
6149 /* node.unprotect_glyphs (returns also boolean because that signals callback) */
6150
6151 #define protect_one_indeed(n) \
6152 if (n != null) { \
6153 protect_glyph_node(n); \
6154 }
6155
6156 #define protect_all_indeed(n) \
6157 h = n ; \
6158 if (h != null) { \
6159 while (h != null) { \
6160 if (type(h) == glyph_node) { \
6161 protect_glyph_node(h); \
6162 } \
6163 h = vlink(h); \
6164 } \
6165 }
6166
6167 #define protect_node_indeed(n) \
6168 if (type(n) == glyph_node) { \
6169 protect_one_indeed(n); \
6170 } else if (type(n) == disc_node) { \
6171 protect_all_indeed(vlink(no_break(n))); \
6172 protect_all_indeed(vlink(pre_break(n))); \
6173 protect_all_indeed(vlink(post_break(n))); \
6174 }
6175
6176 #define unprotect_one_indeed(n) \
6177 if (n != null) { \
6178 unprotect_glyph_node(n); \
6179 }
6180
6181 #define unprotect_all_indeed(n) \
6182 h = n ; \
6183 if (h != null) { \
6184 while (h != null) { \
6185 if (type(h) == glyph_node) { \
6186 unprotect_glyph_node(h); \
6187 } \
6188 h = vlink(h); \
6189 } \
6190 }
6191
6192 #define unprotect_node_indeed(n) \
6193 if (type(n) == glyph_node) { \
6194 unprotect_one_indeed(n); \
6195 } else if (type(n) == disc_node) { \
6196 unprotect_all_indeed(vlink(no_break(n))); \
6197 unprotect_all_indeed(vlink(pre_break(n))); \
6198 unprotect_all_indeed(vlink(post_break(n))); \
6199 }
6200
6201 static int lua_nodelib_protect_glyph(lua_State * L)
6202 {
6203 halfword n = *check_isnode(L, 1);
6204 halfword h;
6205 protect_node_indeed(n);
6206 return 0;
6207 }
6208
6209 static int lua_nodelib_unprotect_glyph(lua_State * L)
6210 {
6211 halfword n = *check_isnode(L, 1);
6212 halfword h;
6213 unprotect_node_indeed(n);
6214 return 0;
6215 }
6216
6217 static int lua_nodelib_protect_glyphs(lua_State * L)
6218 {
6219 halfword head = *check_isnode(L, 1);
6220 halfword tail = null;
6221 halfword h;
6222 if (lua_gettop(L) > 1) {
6223 tail = *check_isnode(L, 2);
6224 }
6225 while (head != null) {
6226 protect_node_indeed(head);
6227 if (head == tail) {
6228 break;
6229 }
6230 head = vlink(head);
6231 }
6232 return 0;
6233 }
6234
6235 static int lua_nodelib_unprotect_glyphs(lua_State * L)
6236 {
6237 halfword head = *check_isnode(L, 1);
6238 halfword tail = null;
6239 halfword h;
6240 if (lua_gettop(L) > 1) {
6241 tail = *check_isnode(L, 2);
6242 }
6243 while (head != null) {
6244 unprotect_node_indeed(head);
6245 if (head == tail) {
6246 break;
6247 }
6248 head = vlink(head);
6249 }
6250 return 0;
6251 }
6252
6253 /* node.direct.protect_glyphs */
6254 /* node.direct.unprotect_glyphs */
6255
6256 static int lua_nodelib_direct_protect_glyph(lua_State * L)
6257 {
6258 halfword n = (halfword) lua_tointeger(L,1);
6259 halfword h;
6260 protect_node_indeed(n);
6261 return 0;
6262 }
6263
6264 static int lua_nodelib_direct_unprotect_glyph(lua_State * L)
6265 {
6266 halfword n = (halfword) lua_tointeger(L,1);
6267 halfword h;
6268 unprotect_node_indeed(n);
6269 return 0;
6270 }
6271
6272 static int lua_nodelib_direct_protect_glyphs(lua_State * L)
6273 {
6274 halfword head = (halfword) lua_tointeger(L,1);
6275 halfword tail = (halfword) lua_tointeger(L,2);
6276 halfword h;
6277 while (head != null) {
6278 protect_node_indeed(head);
6279 if (head == tail) {
6280 break;
6281 }
6282 head = vlink(head);
6283 }
6284 return 0;
6285 }
6286
6287 static int lua_nodelib_direct_unprotect_glyphs(lua_State * L)
6288 {
6289 halfword head = (halfword) lua_tointeger(L,1);
6290 halfword tail = (halfword) lua_tointeger(L,2);
6291 halfword h;
6292 while (head != null) {
6293 unprotect_node_indeed(head);
6294 if (head == tail) {
6295 break;
6296 }
6297 head = vlink(head);
6298 }
6299 return 0;
6300 }
6301
6302 /* node.first_glyph */
6303
6304 static int lua_nodelib_first_glyph(lua_State * L)
6305 {
6306 /* on the stack are two nodes and a direction */
6307 halfword h, savetail = null, t = null;
6308 if (lua_gettop(L) < 1) {
6309 lua_pushnil(L);
6310 lua_pushboolean(L, 0);
6311 return 2;
6312 }
6313 h = *(check_isnode(L, 1));
6314 if (lua_gettop(L) > 1) {
6315 t = *(check_isnode(L, 2));
6316 savetail = vlink(t);
6317 vlink(t) = null;
6318 }
6319 while (h != null && (type(h) != glyph_node || !is_simple_character(h))) {
6320 h = vlink(h);
6321 }
6322 if (savetail != null) {
6323 vlink(t) = savetail;
6324 }
6325 lua_pushinteger(L, h);
6326 lua_nodelib_push(L);
6327 lua_pushboolean(L, (h == null ? 0 : 1));
6328 return 2;
6329 }
6330
6331 /* node.direct.first_glyph */
6332
6333 static int lua_nodelib_direct_first_glyph(lua_State * L)
6334 {
6335 halfword h,savetail,t;
6336 savetail = null;
6337 t = null;
6338 h = (halfword) lua_tointeger(L,1);
6339 if (h == null) {
6340 lua_pushnil(L);
6341 return 1;
6342 }
6343 t = (halfword) lua_tointeger(L,2);
6344 if (t != null) {
6345 savetail = vlink(t);
6346 vlink(t) = null;
6347 }
6348 while (h != null && (type(h) != glyph_node || !is_simple_character(h)))
6349 h = vlink(h);
6350 if (savetail != null)
6351 vlink(t) = savetail;
6352 lua_pushinteger(L, h);
6353 return 1; /* no need to also push a boolean if we have nil */
6354 }
6355
6356 /* new, fast and dumb ones: only signals that something needs to be processed */
6357
6358 /* node.has_glyph */
6359
6360 static int lua_nodelib_has_glyph(lua_State * L)
6361 {
6362 halfword *a;
6363 halfword h = (halfword) *(check_isnode(L,1)) ;
6364 while (h != null) {
6365 if ( (type(h) == glyph_node) || (type(h) == disc_node)) {
6366 fast_metatable(h);
6367 return 1;
6368 } else {
6369 h = vlink(h);
6370 }
6371 }
6372 lua_pushnil(L);
6373 return 1;
6374 }
6375
6376 /* node.direct.has_glyph */
6377
6378 static int lua_nodelib_direct_has_glyph(lua_State * L)
6379 {
6380 halfword h = (halfword) lua_tointeger(L,1) ;
6381 while (h != null) {
6382 if ((type(h) == glyph_node) || (type(h) == disc_node)) {
6383 nodelib_pushdirect(h);
6384 return 1;
6385 } else {
6386 h = vlink(h);
6387 }
6388 }
6389 lua_pushnil(L);
6390 return 1;
6391 }
6392
6393 /* this is too simplistic, but it helps Hans to get going */
6394
6395 /*
6396
6397 static halfword do_ligature_n(halfword prev, halfword stop, halfword lig)
6398 {
6399 vlink(lig) = vlink(stop);
6400 vlink(stop) = null;
6401 lig_ptr(lig) = vlink(prev);
6402 vlink(prev) = lig;
6403 return lig;
6404 }
6405
6406 */
6407
6408 /* node.do_ligature_n(node prev, node last, node lig) */
6409
6410 /*
6411
6412 static int lua_nodelib_do_ligature_n(lua_State * L)
6413 {
6414 halfword p;
6415 halfword n = *check_isnode(L, 1);
6416 halfword m = *check_isnode(L, 2);
6417 halfword o = *check_isnode(L, 3);
6418 if (alink(n) == null || vlink(alink(n)) != n) {
6419 halfword tmp_head = new_node(temp_node, 0);
6420 couple_nodes(tmp_head, n);
6421 p = do_ligature_n(tmp_head, m, o);
6422 flush_node(tmp_head);
6423 } else {
6424 p = do_ligature_n(alink(n), m, o);
6425 }
6426 lua_pushinteger(L, p);
6427 lua_nodelib_push(L);
6428 return 1;
6429 }
6430
6431 */
6432
6433 /* node.direct.do_ligature_n(node prev, node last, node lig) */
6434
6435 /*
6436
6437 static int lua_nodelib_direct_do_ligature_n(lua_State * L)
6438 {
6439 halfword p;
6440 halfword n = (halfword) lua_tointeger(L, 1);
6441 halfword m = (halfword) lua_tointeger(L, 2);
6442 halfword o = (halfword) lua_tointeger(L, 3);
6443 if ((n == null) || (m == null) || (o == null)) {
6444 lua_pushnil(L);
6445 } else {
6446 if (alink(n) == null || vlink(alink(n)) != n) {
6447 halfword tmp_head = new_node(temp_node, 0);
6448 couple_nodes(tmp_head, n);
6449 p = do_ligature_n(tmp_head, m, o);
6450 flush_node(tmp_head);
6451 } else {
6452 p = do_ligature_n(alink(n), m, o);
6453 }
6454 lua_pushinteger(L, p);
6455 }
6456 return 1;
6457 }
6458
6459 */
6460
6461 /* node.usedlist */
6462
6463 static int lua_nodelib_usedlist(lua_State * L)
6464 {
6465 lua_pushinteger(L, list_node_mem_usage());
6466 lua_nodelib_push(L);
6467 return 1;
6468 }
6469
6470 /* node.direct.usedlist */
6471
6472 static int lua_nodelib_direct_usedlist(lua_State * L)
6473 {
6474 lua_pushinteger(L, list_node_mem_usage());
6475 return 1;
6476 }
6477
6478 /* node.protrusion_skipable(node m) */
6479
6480 static int lua_nodelib_cp_skipable(lua_State * L)
6481 {
6482 halfword n = *check_isnode(L, 1);
6483 lua_pushboolean(L, cp_skipable(n));
6484 return 1;
6485 }
6486
6487 /* node.direct.protrusion_skipable(node m) */
6488
6489 static int lua_nodelib_direct_cp_skipable(lua_State * L)
6490 {
6491 halfword n = lua_tointeger(L, 1);
6492 if (n == null) {
6493 lua_pushnil(L);
6494 } else {
6495 lua_pushboolean(L, cp_skipable(n));
6496 }
6497 return 1;
6498 }
6499
6500
6501 /* node.currentattr(node m) */
6502
6503 static int lua_nodelib_currentattr(lua_State * L)
6504 {
6505 int u = lua_gettop(L);
6506 if (u == null) {
6507 /* query */
6508 halfword n ;
6509 /* current_attribute_list() return attr_list_cache */
6510 /* or null (attr_list_cache can also be null) */
6511 n = current_attribute_list();
6512 if (n) {
6513 lua_pushinteger(L, n);
6514 lua_nodelib_push(L);
6515 }
6516 else
6517 lua_pushnil(L);
6518 return 1;
6519 } else {
6520 /* assign */
6521 normal_warning("node lib","assignment via node.current_attr(<list>) is not supported (yet)");
6522 return 0;
6523 }
6524 }
6525
6526
6527 /* node.direct.currentattr(node m) */
6528
6529 static int lua_nodelib_direct_currentattr(lua_State * L)
6530 {
6531 /* current_attribute_list() return attr_list_cache */
6532 /* or null (attr_list_cache can also be null) */
6533 halfword n = current_attribute_list();
6534 if (n)
6535 lua_pushinteger(L, n);
6536 else
6537 lua_pushnil(L);
6538 return 1;
6539 }
6540
6541
6542 /* node.direct.todirect */
6543
6544 static int lua_nodelib_direct_todirect(lua_State * L)
6545 {
6546 if (lua_type(L,1) != LUA_TNUMBER) {
6547 /* assume node, no further testing, used in known situations */
6548 void *n ;
6549 n = lua_touserdata(L, 1);
6550 if (n == null) {
6551 lua_pushnil(L);
6552 } else {
6553 lua_pushinteger(L, *((halfword *)n) );
6554 }
6555 } /* else assume direct and returns argument */
6556 return 1;
6557 }
6558
6559
6560 /* node.direct.tonode */
6561
6562 static int lua_nodelib_direct_tonode(lua_State * L)
6563 {
6564 halfword *a;
6565 halfword n = lua_tointeger(L, 1);
6566 if (n != null) {
6567 a = (halfword *) lua_newuserdata(L, sizeof(halfword));
6568 *a=n;
6569 lua_get_metatablelua(luatex_node);
6570 lua_setmetatable(L,-2);
6571 } /* else assume node and return argument */
6572 return 1;
6573 }
6574
6575 /* node.setfield */
6576
6577 static int lua_nodelib_setfield_whatsit(lua_State * L, int n, const char *s)
6578 {
6579 int t = subtype(n);
6580
6581 if (t == pdf_literal_node || t == pdf_late_literal_node) {
6582 /*tex The |string| key is obsolete. */
6583 if (lua_key_eq(s, mode)) {
6584 pdf_literal_mode(n) = (quarterword) lua_tointeger(L, 3);
6585 } else if (lua_key_eq(s, data) || lua_key_eq(s, string)) {
6586 set_pdf_literal_direct_normal(L, n, 3);
6587 } else if (lua_key_eq(s, token)) {
6588 set_pdf_literal_direct_token(L, n, 3);
6589 } else {
6590 return nodelib_cantset(L, n, s);
6591 }
6592 } else if (t == late_lua_node) {
6593 /*tex The |string| key is obsolete. */
6594 if (lua_key_eq(s, data) || lua_key_eq(s, string)) {
6595 set_late_lua_direct_normal(L, n, 3);
6596 } else if (lua_key_eq(s, token)) {
6597 set_late_lua_direct_token(L, n, 3);
6598 } else if (lua_key_eq(s, name)) {
6599 cleanup_late_lua_name(n) ;
6600 late_lua_name(n) = nodelib_gettoks(L, 3);
6601 } else {
6602 return nodelib_cantset(L, n, s);
6603 }
6604 /* done */
6605 } else if (t == user_defined_node) {
6606 if (lua_key_eq(s, user_id)) {
6607 user_node_id(n) = (halfword) lua_tointeger(L, 3);
6608 } else if (lua_key_eq(s, type)) {
6609 user_node_type(n) = (halfword) lua_tointeger(L, 3);
6610 } else if (lua_key_eq(s, value)) {
6611 switch (user_node_type(n)) {
6612 case 'a':
6613 user_node_value(n) = nodelib_getlist(L, 3);
6614 break;
6615 case 'd':
6616 user_node_value(n) = (halfword) lua_roundnumber(L, 3);
6617 break;
6618 case 'l':
6619 lua_pushvalue(L, 3);
6620 if (user_node_value(n) != 0) {
6621 luaL_unref(L, LUA_REGISTRYINDEX,user_node_value(n));
6622 }
6623 user_node_value(n) = luaL_ref(L, LUA_REGISTRYINDEX);
6624 break;
6625 case 'n':
6626 user_node_value(n) = nodelib_getlist(L, 3);
6627 break;
6628 case 's':
6629 user_node_value(n) = nodelib_getstring(L, 3);
6630 break;
6631 case 't':
6632 user_node_value(n) = nodelib_gettoks(L, 3);
6633 break;
6634 default:
6635 user_node_value(n) = (halfword) lua_roundnumber(L, 3);
6636 break;
6637 }
6638 } else {
6639 return nodelib_cantset(L, n, s);
6640 }
6641 } else if (t == pdf_annot_node) {
6642 if (lua_key_eq(s, width)) {
6643 width(n) = (halfword) lua_roundnumber(L, 3);
6644 } else if (lua_key_eq(s, depth)) {
6645 depth(n) = (halfword) lua_roundnumber(L, 3);
6646 } else if (lua_key_eq(s, height)) {
6647 height(n) = (halfword) lua_roundnumber(L, 3);
6648 } else if (lua_key_eq(s, objnum)) {
6649 pdf_annot_objnum(n) = (halfword) lua_tointeger(L, 3);
6650 } else if (lua_key_eq(s, data)) {
6651 pdf_annot_data(n) = nodelib_gettoks(L, 3);
6652 } else {
6653 return nodelib_cantset(L, n, s);
6654 }
6655 } else if (t == pdf_dest_node) {
6656 if (lua_key_eq(s, width)) {
6657 width(n) = (halfword) lua_roundnumber(L, 3);
6658 } else if (lua_key_eq(s, depth)) {
6659 depth(n) = (halfword) lua_roundnumber(L, 3);
6660 } else if (lua_key_eq(s, height)) {
6661 height(n) = (halfword) lua_roundnumber(L, 3);
6662 } else if (lua_key_eq(s, named_id)) {
6663 pdf_dest_named_id(n) = (quarterword) lua_tointeger(L, 3);
6664 } else if (lua_key_eq(s, dest_id)) {
6665 if (pdf_dest_named_id(n) == 1) {
6666 pdf_dest_id(n) = nodelib_gettoks(L, 3);
6667 } else {
6668 pdf_dest_id(n) = (halfword) lua_tointeger(L, 3);
6669 }
6670 } else if (lua_key_eq(s, dest_type)) {
6671 pdf_dest_type(n) = (quarterword) lua_tointeger(L, 3);
6672 } else if (lua_key_eq(s, xyz_zoom)) {
6673 pdf_dest_xyz_zoom(n) = (halfword) lua_tointeger(L, 3);
6674 } else if (lua_key_eq(s, objnum)) {
6675 pdf_dest_objnum(n) = (halfword) lua_tointeger(L, 3);
6676 } else {
6677 return nodelib_cantset(L, n, s);
6678 }
6679 } else if (t == pdf_setmatrix_node) {
6680 if (lua_key_eq(s, data)) {
6681 set_pdf_setmatrix_direct_value(L,n,3);
6682 } else {
6683 return nodelib_cantset(L, n, s);
6684 }
6685 } else if (t == pdf_refobj_node) {
6686 if (lua_key_eq(s, objnum)) {
6687 pdf_obj_objnum(n) = (halfword) lua_tointeger(L, 3);
6688 } else {
6689 return nodelib_cantset(L, n, s);
6690 }
6691 } else if (t == pdf_start_link_node) {
6692 if (lua_key_eq(s, width)) {
6693 width(n) = (halfword) lua_roundnumber(L, 3);
6694 } else if (lua_key_eq(s, depth)) {
6695 depth(n) = (halfword) lua_roundnumber(L, 3);
6696 } else if (lua_key_eq(s, height)) {
6697 height(n) = (halfword) lua_roundnumber(L, 3);
6698 } else if (lua_key_eq(s, objnum)) {
6699 pdf_link_objnum(n) = (halfword) lua_tointeger(L, 3);
6700 } else if (lua_key_eq(s, link_attr)) {
6701 pdf_link_attr(n) = nodelib_gettoks(L, 3);
6702 } else if (lua_key_eq(s, action)) {
6703 pdf_link_action(n) = nodelib_getaction(L, 3);
6704 } else {
6705 return nodelib_cantset(L, n, s);
6706 }
6707 } else if (t == write_node) {
6708 if (lua_key_eq(s, stream)) {
6709 write_stream(n) = (halfword) lua_tointeger(L, 3);
6710 } else if (lua_key_eq(s, data) || lua_key_eq(s, value)) {
6711 set_write_direct_value(L,n,3);
6712 } else {
6713 return nodelib_cantset(L, n, s);
6714 }
6715 } else if (t == pdf_colorstack_node) {
6716 if (lua_key_eq(s, stack)) {
6717 pdf_colorstack_stack(n) = (halfword) lua_tointeger(L, 3);
6718 } else if (lua_key_eq(s, command)) {
6719 pdf_colorstack_cmd(n) = (halfword) lua_tointeger(L, 3);
6720 } else if (lua_key_eq(s, data)) {
6721 pdf_colorstack_data(n) = nodelib_gettoks(L, 3);
6722 } else {
6723 return nodelib_cantset(L, n, s);
6724 }
6725 } else if (t == pdf_action_node) {
6726 if (lua_key_eq(s, action_type)) {
6727 pdf_action_type(n) = (quarterword) lua_tointeger(L, 3);
6728 } else if (lua_key_eq(s, named_id)) {
6729 pdf_action_named_id(n) = (quarterword) lua_tointeger(L, 3);
6730 } else if (lua_key_eq(s, action_id)) {
6731 if (pdf_action_named_id(n) & 1) {
6732 pdf_action_id(n) = nodelib_gettoks(L, 3);
6733 } else {
6734 pdf_action_id(n) = (halfword) lua_tointeger(L, 3);
6735 }
6736 } else if (lua_key_eq(s, file)) {
6737 pdf_action_file(n) = nodelib_gettoks(L, 3);
6738 } else if (lua_key_eq(s, new_window)) {
6739 pdf_action_new_window(n) = (halfword) lua_tointeger(L, 3);
6740 } else if (lua_key_eq(s, data)) {
6741 pdf_action_tokens(n) = nodelib_gettoks(L, 3);
6742 } else if (lua_key_eq(s, struct_id)) {
6743 if (lua_isnil(L, 3)) {
6744 pdf_action_struct_id(n) = null;
6745 } else if (pdf_action_named_id(n) & 2) {
6746 pdf_action_struct_id(n) = nodelib_gettoks(L, 3);
6747 } else {
6748 pdf_action_struct_id(n) = (halfword) lua_tointeger(L, 3);
6749 }
6750 } else {
6751 return nodelib_cantset(L, n, s);
6752 }
6753 } else if ((t == pdf_thread_node) || (t == pdf_start_thread_node)) {
6754 if (lua_key_eq(s, width)) {
6755 width(n) = (halfword) lua_roundnumber(L, 3);
6756 } else if (lua_key_eq(s, depth)) {
6757 depth(n) = (halfword) lua_roundnumber(L, 3);
6758 } else if (lua_key_eq(s, height)) {
6759 height(n) = (halfword) lua_roundnumber(L, 3);
6760 } else if (lua_key_eq(s, named_id)) {
6761 pdf_thread_named_id(n) = (quarterword) lua_tointeger(L, 3);
6762 } else if (lua_key_eq(s, thread_id)) {
6763 if (pdf_thread_named_id(n) == 1) {
6764 pdf_thread_id(n) = nodelib_gettoks(L, 3);
6765 } else {
6766 pdf_thread_id(n) = (halfword) lua_tointeger(L, 3);
6767 }
6768 } else if (lua_key_eq(s, thread_attr)) {
6769 pdf_thread_attr(n) = nodelib_gettoks(L, 3);
6770 } else {
6771 return nodelib_cantset(L, n, s);
6772 }
6773 } else if (t == special_node || t == late_special_node) {
6774 if (lua_key_eq(s, data) || lua_key_eq(s, value)) {
6775 set_special_direct_value(L,n,3);
6776 } else {
6777 return nodelib_cantset(L, n, s);
6778 }
6779 } else if (t == open_node) {
6780 if (lua_key_eq(s, stream)) {
6781 write_stream(n) = (halfword) lua_tointeger(L, 3);
6782 } else if (lua_key_eq(s, name)) {
6783 open_name(n) = nodelib_getstring(L, 3);
6784 } else if (lua_key_eq(s, area)) {
6785 open_area(n) = nodelib_getstring(L, 3);
6786 } else if (lua_key_eq(s, ext)) {
6787 open_ext(n) = nodelib_getstring(L, 3);
6788 } else {
6789 return nodelib_cantset(L, n, s);
6790 }
6791 } else if (t == close_node) {
6792 if (lua_key_eq(s, stream)) {
6793 write_stream(n) = (halfword) lua_tointeger(L, 3);
6794 } else {
6795 return nodelib_cantset(L, n, s);
6796 }
6797 } else if (t == pdf_link_state_node) {
6798 if (lua_key_eq(s, value)) {
6799 pdf_link_state(n) = (halfword) lua_tointeger(L, 3);
6800 } else {
6801 return nodelib_cantset(L, n, s);
6802 }
6803 } else if ((t == pdf_end_link_node) || (t == pdf_end_thread_node) || (t == save_pos_node) ||
6804 (t == pdf_save_node) || (t == pdf_restore_node)) {
6805 return nodelib_cantset(L, n, s);
6806 } else {
6807 /* do nothing */
6808 }
6809 return 0;
6810 }
6811
6812 static int lua_nodelib_fast_setfield(lua_State * L)
6813 {
6814 const char *s;
6815 halfword n = *((halfword *) lua_touserdata(L, 1));
6816 int t = type(n);
6817
6818 if (lua_type(L, 2) == LUA_TNUMBER) {
6819 if (lua_gettop(L) == 3) {
6820 int i = lua_tointeger(L, 2);
6821 int val = lua_tointeger(L, 3);
6822 if (val == UNUSED_ATTRIBUTE) {
6823 (void) unset_attribute(n, i, val);
6824 } else {
6825 set_attribute(n, i, val);
6826 }
6827 } else {
6828 luaL_error(L, "incorrect number of arguments");
6829 }
6830 return 0;
6831 }
6832
6833 s = lua_tostring(L, 2);
6834
6835 /*if (lua_key_eq(s, id)) {
6836 type(n) = (quarteword) lua_tointeger(L, 3);
6837 }* else */
6838 if (lua_key_eq(s, next)) {
6839 halfword x = nodelib_getlist(L, 3);
6840 if (x>0 && type(x) == glue_spec_node) {
6841 return luaL_error(L, "You can't assign a %s node to a next field\n", node_data[type(x)].name);
6842 }
6843 vlink(n) = x;
6844 } else if (lua_key_eq(s, prev)) {
6845 halfword x = nodelib_getlist(L, 3);
6846 if (x>0 && type(x) == glue_spec_node) {
6847 return luaL_error(L, "You can't assign a %s node to a prev field\n", node_data[type(x)].name);
6848 }
6849 alink(n) = x;
6850 } else if (lua_key_eq(s, attr)) {
6851 if (nodetype_has_attributes(type(n))) {
6852 nodelib_setattr(L, 3, n);
6853 }
6854 } else if (t == glyph_node) {
6855 if (lua_key_eq(s, subtype)) {
6856 subtype(n) = (quarterword) lua_tointeger(L, 3);
6857 } else if (lua_key_eq(s, font)) {
6858 font(n) = (halfword) lua_tointeger(L, 3);
6859 } else if (lua_key_eq(s, char)) {
6860 character(n) = (halfword) lua_tointeger(L, 3);
6861 } else if (lua_key_eq(s, xoffset)) {
6862 x_displace(n) = (halfword) lua_roundnumber(L, 3);
6863 } else if (lua_key_eq(s, yoffset)) {
6864 y_displace(n) = (halfword) lua_roundnumber(L, 3);
6865 } else if (lua_key_eq(s, data)) {
6866 glyph_node_data(n) = (halfword) lua_tointeger(L, 3);;
6867 } else if (lua_key_eq(s, width)) {
6868 /* not yet */
6869 } else if (lua_key_eq(s, height)) {
6870 /* not yet */
6871 } else if (lua_key_eq(s, depth)) {
6872 /* not yet */
6873 } else if (lua_key_eq(s, expansion_factor)) {
6874 ex_glyph(n) = (halfword) lua_roundnumber(L, 3);
6875 } else if (lua_key_eq(s, components)) {
6876 lig_ptr(n) = nodelib_getlist(L, 3);
6877 } else if (lua_key_eq(s, lang)) {
6878 set_char_lang(n, (halfword) lua_tointeger(L, 3));
6879 } else if (lua_key_eq(s, left)) {
6880 set_char_lhmin(n, (halfword) lua_tointeger(L, 3));
6881 } else if (lua_key_eq(s, right)) {
6882 set_char_rhmin(n, (halfword) lua_tointeger(L, 3));
6883 } else if (lua_key_eq(s, uchyph)) {
6884 set_char_uchyph(n, (halfword) lua_tointeger(L, 3));
6885 } else {
6886 return nodelib_cantset(L, n, s);
6887 }
6888 } else if ((t == hlist_node) || (t == vlist_node)) {
6889 if (lua_key_eq(s, subtype)) {
6890 subtype(n) = (quarterword) lua_tointeger(L, 3);
6891 } else if (lua_key_eq(s, list) || lua_key_eq(s, head)) {
6892 list_ptr(n) = nodelib_getlist(L, 3);
6893 } else if (lua_key_eq(s, width)) {
6894 width(n) = (halfword) lua_roundnumber(L, 3);
6895 } else if (lua_key_eq(s, height)) {
6896 height(n) = (halfword) lua_roundnumber(L, 3);
6897 } else if (lua_key_eq(s, depth)) {
6898 depth(n) = (halfword) lua_roundnumber(L, 3);
6899 } else if (lua_key_eq(s, direction)) {
6900 box_dir(n) = nodelib_getdirection(L, 3);
6901 } else if (lua_key_eq(s, dir)) {
6902 box_dir(n) = nodelib_getdir_par(L, 3);
6903 } else if (lua_key_eq(s, shift)) {
6904 shift_amount(n) = (halfword) lua_roundnumber(L, 3);
6905 } else if (lua_key_eq(s, glue_order)) {
6906 glue_order(n) = (quarterword) lua_tointeger(L, 3);
6907 } else if (lua_key_eq(s, glue_sign)) {
6908 glue_sign(n) = (quarterword) lua_tointeger(L, 3);
6909 } else if (lua_key_eq(s, glue_set)) {
6910 glue_set(n) = (glue_ratio) lua_tonumber(L, 3); /* integer or float */
6911 } else {
6912 return nodelib_cantset(L, n, s);
6913 }
6914 } else if (t == disc_node) {
6915 if (lua_key_eq(s, subtype)) {
6916 subtype(n) = (quarterword) lua_tointeger(L, 3);
6917 } else if (lua_key_eq(s, pre)) {
6918 set_disc_field(pre_break(n), nodelib_getlist(L, 3));
6919 } else if (lua_key_eq(s, post)) {
6920 set_disc_field(post_break(n), nodelib_getlist(L, 3));
6921 } else if (lua_key_eq(s, replace)) {
6922 set_disc_field(no_break(n), nodelib_getlist(L, 3));
6923 } else if (lua_key_eq(s, penalty)) {
6924 disc_penalty(n) = (quarterword) lua_tointeger(L, 3);
6925 } else {
6926 return nodelib_cantset(L, n, s);
6927 }
6928 } else if (t == glue_node) {
6929 if (lua_key_eq(s, subtype)) {
6930 subtype(n) = (quarterword) lua_tointeger(L, 3);
6931 } else if (lua_key_eq(s, width)) {
6932 width(n) = (halfword) lua_roundnumber(L, 3);
6933 } else if (lua_key_eq(s, stretch)) {
6934 stretch(n) = (halfword) lua_roundnumber(L, 3);
6935 } else if (lua_key_eq(s, shrink)) {
6936 shrink(n) = (halfword) lua_roundnumber(L, 3);
6937 } else if (lua_key_eq(s, stretch_order)) {
6938 stretch_order(n) = (quarterword) lua_tointeger(L, 3);
6939 } else if (lua_key_eq(s, shrink_order)) {
6940 shrink_order(n) = (quarterword) lua_tointeger(L, 3);
6941 } else if (lua_key_eq(s, leader)) {
6942 leader_ptr(n) = nodelib_getlist(L, 3);
6943 } else {
6944 return nodelib_cantset(L, n, s);
6945 }
6946 } else if (t == kern_node) {
6947 if (lua_key_eq(s, subtype)) {
6948 subtype(n) = (quarterword) lua_tointeger(L, 3);
6949 } else if (lua_key_eq(s, kern)) {
6950 width(n) = (halfword) lua_roundnumber(L, 3);
6951 } else if (lua_key_eq(s, expansion_factor)) {
6952 ex_kern(n) = (halfword) lua_roundnumber(L, 3);
6953 } else {
6954 return nodelib_cantset(L, n, s);
6955 }
6956 } else if (t == penalty_node) {
6957 if (lua_key_eq(s, subtype)) {
6958 /* dummy subtype */
6959 } else if (lua_key_eq(s, penalty)) {
6960 penalty(n) = (halfword) lua_tointeger(L, 3);
6961 } else {
6962 return nodelib_cantset(L, n, s);
6963 }
6964 } else if (t == rule_node) {
6965 if (lua_key_eq(s, subtype)) {
6966 subtype(n) = (quarterword) lua_tointeger(L, 3);
6967 } else if (lua_key_eq(s, width)) {
6968 width(n) = (halfword) lua_roundnumber(L, 3);
6969 } else if (lua_key_eq(s, height)) {
6970 height(n) = (halfword) lua_roundnumber(L, 3);
6971 } else if (lua_key_eq(s, depth)) {
6972 depth(n) = (halfword) lua_roundnumber(L, 3);
6973 } else if (lua_key_eq(s, left)) {
6974 rule_left(n) = (halfword) lua_roundnumber(L, 3);
6975 } else if (lua_key_eq(s, right)) {
6976 rule_right(n) = (halfword) lua_roundnumber(L, 3);
6977 } else if (lua_key_eq(s, direction)) {
6978 rule_dir(n) = nodelib_getdirection(L, 3);
6979 } else if (lua_key_eq(s, dir)) {
6980 rule_dir(n) = nodelib_getdir_par(L, 3);
6981 } else if (lua_key_eq(s, index)) {
6982 rule_index(n) = (halfword) lua_tointeger(L, 3);
6983 } else if (lua_key_eq(s, transform)) {
6984 rule_transform(n) = (halfword) lua_roundnumber(L, 3); /* can also be user value */
6985 } else {
6986 return nodelib_cantset(L, n, s);
6987 }
6988 } else if (t == dir_node) {
6989 if (lua_key_eq(s, direction)) {
6990 dir_dir(n) = nodelib_getdirection(L, 3);
6991 } else if (lua_key_eq(s, dir)) {
6992 nodelib_setdir_text(L, 3, n);
6993 } else if (lua_key_eq(s, level)) {
6994 dir_level(n) = (halfword) lua_tointeger(L, 3);
6995 } else {
6996 return nodelib_cantset(L, n, s);
6997 }
6998 } else if (t == local_par_node) {
6999 if (lua_key_eq(s, pen_inter)) {
7000 local_pen_inter(n) = (halfword) lua_tointeger(L, 3);
7001 } else if (lua_key_eq(s, pen_broken)) {
7002 local_pen_broken(n) = (halfword) lua_tointeger(L, 3);
7003 } else if (lua_key_eq(s, direction)) {
7004 local_par_dir(n) = nodelib_getdirection(L, 3);
7005 } else if (lua_key_eq(s, dir)) {
7006 local_par_dir(n) = nodelib_getdir_par(L, 3);
7007 } else if (lua_key_eq(s, box_left)) {
7008 local_box_left(n) = nodelib_getlist(L, 3);
7009 } else if (lua_key_eq(s, box_left_width)) {
7010 local_box_left_width(n) = (halfword) lua_roundnumber(L, 3);
7011 } else if (lua_key_eq(s, box_right)) {
7012 local_box_right(n) = nodelib_getlist(L, 3);
7013 } else if (lua_key_eq(s, box_right_width)) {
7014 local_box_right_width(n) = (halfword) lua_roundnumber(L, 3);
7015 } else {
7016 return nodelib_cantset(L, n, s);
7017 }
7018 } else if (t == whatsit_node) {
7019 if (lua_key_eq(s, subtype)) {
7020 subtype(n) = (quarterword) lua_tointeger(L, 3);
7021 } else {
7022 lua_nodelib_setfield_whatsit(L, n, s);
7023 }
7024 } else if (t == simple_noad) {
7025 if (lua_key_eq(s, subtype)) {
7026 subtype(n) = (quarterword) lua_tointeger(L, 3);
7027 } else if (lua_key_eq(s, nucleus)) {
7028 nucleus(n) = nodelib_getlist(L, 3);
7029 } else if (lua_key_eq(s, sub)) {
7030 subscr(n) = nodelib_getlist(L, 3);
7031 } else if (lua_key_eq(s, sup)) {
7032 supscr(n) = nodelib_getlist(L, 3);
7033 } else if (lua_key_eq(s, options)) {
7034 noadoptions(n) = (quarterword) lua_tointeger(L, 3);
7035 } else {
7036 return nodelib_cantset(L, n, s);
7037 }
7038 } else if ((t == math_char_node) || (t == math_text_char_node)) {
7039 if (lua_key_eq(s, subtype)) {
7040 subtype(n) = (quarterword) lua_tointeger(L, 3);
7041 } else if (lua_key_eq(s, fam)) {
7042 math_fam(n) = (halfword) lua_tointeger(L, 3);
7043 } else if (lua_key_eq(s, char)) {
7044 math_character(n) = (halfword) lua_tointeger(L, 3);
7045 } else {
7046 return nodelib_cantset(L, n, s);
7047 }
7048 } else if (t == mark_node) {
7049 if (lua_key_eq(s, subtype)) {
7050 subtype(n) = (quarterword) lua_tointeger(L, 3);
7051 } else if (lua_key_eq(s, class)) {
7052 mark_class(n) = (halfword) lua_tointeger(L, 3);
7053 } else if (lua_key_eq(s, mark)) {
7054 mark_ptr(n) = nodelib_gettoks(L, 3);
7055 } else {
7056 return nodelib_cantset(L, n, s);
7057 }
7058 } else if (t == ins_node) {
7059 if (lua_key_eq(s, subtype)) {
7060 subtype(n) = (quarterword) lua_tointeger(L, 3);
7061 } else if (lua_key_eq(s, cost)) {
7062 float_cost(n) = (halfword) lua_tointeger(L, 3);
7063 } else if (lua_key_eq(s, depth)) {
7064 depth(n) = (halfword) lua_roundnumber(L, 3);
7065 } else if (lua_key_eq(s, height)) {
7066 height(n) = (halfword) lua_roundnumber(L, 3);
7067 } else if ((lua_key_eq(s, list)) || (lua_key_eq(s, head))) {
7068 ins_ptr(n) = nodelib_getlist(L, 3);
7069 /* glue */
7070 } else if (lua_key_eq(s, width)) {
7071 width(n) = (halfword) lua_roundnumber(L, 3);
7072 } else if (lua_key_eq(s, stretch)) {
7073 stretch(n) = (halfword) lua_roundnumber(L, 3);
7074 } else if (lua_key_eq(s, shrink)) {
7075 shrink(n) = (halfword) lua_roundnumber(L, 3);
7076 } else if (lua_key_eq(s, stretch_order)) {
7077 stretch_order(n) = (quarterword) lua_tointeger(L, 3);
7078 } else if (lua_key_eq(s, shrink_order)) {
7079 shrink_order(n) = (quarterword) lua_tointeger(L, 3);
7080 } else {
7081 return nodelib_cantset(L, n, s);
7082 }
7083 } else if (t == math_node) {
7084 if (lua_key_eq(s, subtype)) {
7085 subtype(n) = (quarterword) lua_tointeger(L, 3);
7086 } else if (lua_key_eq(s, surround)) {
7087 surround(n) = (halfword) lua_roundnumber(L, 3);
7088 /* glue */
7089 } else if (lua_key_eq(s, width)) {
7090 width(n) = (halfword) lua_roundnumber(L, 3);
7091 } else if (lua_key_eq(s, stretch)) {
7092 stretch(n) = (halfword) lua_roundnumber(L, 3);
7093 } else if (lua_key_eq(s, shrink)) {
7094 shrink(n) = (halfword) lua_roundnumber(L, 3);
7095 } else if (lua_key_eq(s, stretch_order)) {
7096 stretch_order(n) = (quarterword) lua_tointeger(L, 3);
7097 } else if (lua_key_eq(s, shrink_order)) {
7098 shrink_order(n) = (quarterword) lua_tointeger(L, 3);
7099 } else {
7100 return nodelib_cantset(L, n, s);
7101 }
7102 } else if (t == fraction_noad) {
7103 if (lua_key_eq(s, subtype)) {
7104 subtype(n) = (quarterword) lua_tointeger(L, 3);
7105 } else if (lua_key_eq(s, width)) {
7106 thickness(n) = (halfword) lua_roundnumber(L, 3);
7107 } else if (lua_key_eq(s, num)) {
7108 numerator(n) = nodelib_getlist(L, 3);
7109 } else if (lua_key_eq(s, denom)) {
7110 denominator(n) = nodelib_getlist(L, 3);
7111 } else if (lua_key_eq(s, left)) {
7112 left_delimiter(n) = nodelib_getlist(L, 3);
7113 } else if (lua_key_eq(s, right)) {
7114 right_delimiter(n) = nodelib_getlist(L, 3);
7115 } else if (lua_key_eq(s, middle)) {
7116 middle_delimiter(n) = nodelib_getlist(L, 3);
7117 } else if (lua_key_eq(s, fam)) {
7118 fraction_fam(n) = (halfword) lua_tointeger(L, 3);
7119 } else if (lua_key_eq(s, options)) {
7120 fractionoptions(n) = (halfword) lua_tointeger(L, 3);
7121 } else {
7122 return nodelib_cantset(L, n, s);
7123 }
7124 } else if (t == style_node) {
7125 if (lua_key_eq(s, subtype)) {
7126 /* dummy subtype */
7127 } else if (lua_key_eq(s, style)) {
7128 assign_math_style(L,3,subtype(n));
7129 } else {
7130 /* return nodelib_cantset(L, n, s); */
7131 }
7132 } else if (t == accent_noad) {
7133 if (lua_key_eq(s, subtype)) {
7134 subtype(n) = (quarterword) lua_tointeger(L, 3);
7135 } else if (lua_key_eq(s, nucleus)) {
7136 nucleus(n) = nodelib_getlist(L, 3);
7137 } else if (lua_key_eq(s, sub)) {
7138 subscr(n) = nodelib_getlist(L, 3);
7139 } else if (lua_key_eq(s, sup)) {
7140 supscr(n) = nodelib_getlist(L, 3);
7141 } else if ((lua_key_eq(s, top_accent))||(lua_key_eq(s, accent))) {
7142 top_accent_chr(n) = nodelib_getlist(L, 3);
7143 } else if (lua_key_eq(s, bot_accent)) {
7144 bot_accent_chr(n) = nodelib_getlist(L, 3);
7145 } else if (lua_key_eq(s, overlay_accent)) {
7146 overlay_accent_chr(n) = nodelib_getlist(L, 3);
7147 } else if (lua_key_eq(s, fraction)) {
7148 accentfraction(n) = (halfword) lua_roundnumber(L,3);
7149 } else {
7150 return nodelib_cantset(L, n, s);
7151 }
7152 } else if (t == fence_noad) {
7153 if (lua_key_eq(s, subtype)) {
7154 subtype(n) = (quarterword) lua_tointeger(L, 3);
7155 } else if (lua_key_eq(s, delim)) {
7156 delimiter(n) = nodelib_getlist(L, 3);
7157 } else if (lua_key_eq(s, italic)) {
7158 delimiteritalic(n) = (halfword) lua_roundnumber(L,3);
7159 } else if (lua_key_eq(s, height)) {
7160 delimiterheight(n) = (halfword) lua_roundnumber(L,3);
7161 } else if (lua_key_eq(s, depth)) {
7162 delimiterdepth(n) = (halfword) lua_roundnumber(L,3);
7163 } else if (lua_key_eq(s, options)) {
7164 delimiteroptions(n) = (halfword) lua_tointeger(L,3);
7165 } else if (lua_key_eq(s, class)) {
7166 delimiterclass(n) = (halfword) lua_tointeger(L,3);
7167 } else {
7168 return nodelib_cantset(L, n, s);
7169 }
7170 } else if (t == delim_node) {
7171 if (lua_key_eq(s, subtype)) {
7172 subtype(n) = (quarterword) lua_tointeger(L, 3);
7173 } else if (lua_key_eq(s, small_fam)) {
7174 small_fam(n) = (halfword) lua_tointeger(L, 3);
7175 } else if (lua_key_eq(s, small_char)) {
7176 small_char(n) = (halfword) lua_tointeger(L, 3);
7177 } else if (lua_key_eq(s, large_fam)) {
7178 large_fam(n) = (halfword) lua_tointeger(L, 3);
7179 } else if (lua_key_eq(s, large_char)) {
7180 large_char(n) = (halfword) lua_tointeger(L, 3);
7181 } else {
7182 return nodelib_cantset(L, n, s);
7183 }
7184 } else if ((t == sub_box_node) || (t == sub_mlist_node)) {
7185 if (lua_key_eq(s, subtype)) {
7186 subtype(n) = (quarterword) lua_tointeger(L, 3);
7187 } else if ((lua_key_eq(s, list)) || (lua_key_eq(s, head))){
7188 math_list(n) = nodelib_getlist(L, 3);
7189 } else {
7190 return nodelib_cantset(L, n, s);
7191 }
7192 } else if (t == radical_noad) {
7193 if (lua_key_eq(s, subtype)) {
7194 subtype(n) = (quarterword) lua_tointeger(L, 3);
7195 } else if (lua_key_eq(s, nucleus)) {
7196 nucleus(n) = nodelib_getlist(L, 3);
7197 } else if (lua_key_eq(s, sub)) {
7198 subscr(n) = nodelib_getlist(L, 3);
7199 } else if (lua_key_eq(s, sup)) {
7200 supscr(n) = nodelib_getlist(L, 3);
7201 } else if (lua_key_eq(s, left)) {
7202 left_delimiter(n) = nodelib_getlist(L, 3);
7203 } else if (lua_key_eq(s, degree)) {
7204 degree(n) = nodelib_getlist(L, 3);
7205 } else if (lua_key_eq(s, width)) {
7206 radicalwidth(n) = lua_roundnumber(L, 3);
7207 } else if (lua_key_eq(s, options)) {
7208 radicaloptions(n) = (halfword) lua_tointeger(L,3);
7209 } else {
7210 return nodelib_cantset(L, n, s);
7211 }
7212 } else if (t == margin_kern_node) {
7213 if (lua_key_eq(s, subtype)) {
7214 subtype(n) = (quarterword) lua_tointeger(L, 3);
7215 } else if (lua_key_eq(s, width) || lua_key_eq(s, kern)) {
7216 width(n) = (halfword) lua_roundnumber(L, 3);
7217 } else if (lua_key_eq(s, glyph)) {
7218 margin_char(n) = nodelib_getlist(L, 3);
7219 } else {
7220 return nodelib_cantset(L, n, s);
7221 }
7222 } else if (t == split_up_node) {
7223 if (lua_key_eq(s, subtype)) {
7224 subtype(n) = (quarterword) lua_tointeger(L, 3);
7225 } else if (lua_key_eq(s, last_ins_ptr)) {
7226 last_ins_ptr(n) = nodelib_getlist(L, 3);
7227 } else if (lua_key_eq(s, best_ins_ptr)) {
7228 best_ins_ptr(n) = nodelib_getlist(L, 3);
7229 } else if (lua_key_eq(s, broken_ptr)) {
7230 broken_ptr(n) = nodelib_getlist(L, 3);
7231 } else if (lua_key_eq(s, broken_ins)) {
7232 broken_ins(n) = nodelib_getlist(L, 3);
7233 } else {
7234 return nodelib_cantset(L, n, s);
7235 }
7236 } else if (t == choice_node) {
7237 if (lua_key_eq(s, subtype)) {
7238 subtype(n) = (quarterword) lua_tointeger(L, 3);
7239 } else if (lua_key_eq(s, display)) {
7240 display_mlist(n) = nodelib_getlist(L, 3);
7241 } else if (lua_key_eq(s, text)) {
7242 text_mlist(n) = nodelib_getlist(L, 3);
7243 } else if (lua_key_eq(s, script)) {
7244 script_mlist(n) = nodelib_getlist(L, 3);
7245 } else if (lua_key_eq(s, scriptscript)) {
7246 script_script_mlist(n) = nodelib_getlist(L, 3);
7247 } else {
7248 return nodelib_cantset(L, n, s);
7249 }
7250 } else if (t == inserting_node) {
7251 if (lua_key_eq(s, subtype)) {
7252 subtype(n) = (quarterword) lua_tointeger(L, 3);
7253 } else if (lua_key_eq(s, last_ins_ptr)) {
7254 last_ins_ptr(n) = nodelib_getlist(L, 3);
7255 } else if (lua_key_eq(s, best_ins_ptr)) {
7256 best_ins_ptr(n) = nodelib_getlist(L, 3);
7257 } else {
7258 return nodelib_cantset(L, n, s);
7259 }
7260 } else if (t == attribute_node) {
7261 if (lua_key_eq(s, subtype)) {
7262 /* dummy subtype */
7263 } else if (lua_key_eq(s, number)) {
7264 attribute_id(n) = (halfword) lua_tointeger(L, 3);
7265 } else if (lua_key_eq(s, value)) {
7266 attribute_value(n) = (halfword) lua_tointeger(L, 3);
7267 } else {
7268 return nodelib_cantset(L, n, s);
7269 }
7270 } else if (t == adjust_node) {
7271 if (lua_key_eq(s, subtype)) {
7272 subtype(n) = (quarterword) lua_tointeger(L, 3);
7273 } else if ((lua_key_eq(s, list)) || (lua_key_eq(s, head))) {
7274 adjust_ptr(n) = nodelib_getlist(L, 3);
7275 } else {
7276 return nodelib_cantset(L, n, s);
7277 }
7278 } else if (t == unset_node) {
7279 if (lua_key_eq(s, subtype)) {
7280 /* dummy subtype */
7281 } else if (lua_key_eq(s, width)) {
7282 width(n) = (halfword) lua_roundnumber(L, 3);
7283 } else if (lua_key_eq(s, height)) {
7284 height(n) = (halfword) lua_roundnumber(L, 3);
7285 } else if (lua_key_eq(s, depth)) {
7286 depth(n) = (halfword) lua_roundnumber(L, 3);
7287 } else if (lua_key_eq(s, direction)) {
7288 box_dir(n) = nodelib_getdirection(L, 3);
7289 } else if (lua_key_eq(s, dir)) {
7290 box_dir(n) = nodelib_getdir_par(L, 3);
7291 } else if (lua_key_eq(s, shrink)) {
7292 glue_shrink(n) = (halfword) lua_roundnumber(L, 3);
7293 } else if (lua_key_eq(s, glue_order)) {
7294 glue_order(n) = (quarterword) lua_tointeger(L, 3);
7295 } else if (lua_key_eq(s, glue_sign)) {
7296 glue_sign(n) = (quarterword) lua_tointeger(L, 3);
7297 } else if (lua_key_eq(s, stretch)) {
7298 glue_stretch(n) = (halfword) lua_roundnumber(L, 3);
7299 } else if (lua_key_eq(s, count)) {
7300 span_count(n) = (quarterword) lua_tointeger(L, 3);
7301 } else if ((lua_key_eq(s, list)) || (lua_key_eq(s, head))){
7302 list_ptr(n) = nodelib_getlist(L, 3);
7303 } else {
7304 return nodelib_cantset(L, n, s);
7305 }
7306 } else if (t == attribute_list_node) {
7307 if (lua_key_eq(s, subtype)) {
7308 /* dummy subtype */
7309 } else {
7310 return nodelib_cantset(L, n, s);
7311 }
7312 } else if (t == boundary_node) {
7313 if (lua_key_eq(s, subtype)) {
7314 subtype(n) = (quarterword) lua_tointeger(L, 3);
7315 } else if (lua_key_eq(s, value)) {
7316 boundary_value(n) = lua_tointeger(L, 3);
7317 } else {
7318 return nodelib_cantset(L, n, s);
7319 }
7320 } else if (t == glue_spec_node) {
7321 if (lua_key_eq(s, width)) {
7322 width(n) = (halfword) lua_roundnumber(L, 3);
7323 } else if (lua_key_eq(s, stretch)) {
7324 stretch(n) = (halfword) lua_roundnumber(L, 3);
7325 } else if (lua_key_eq(s, shrink)) {
7326 shrink(n) = (halfword) lua_roundnumber(L, 3);
7327 } else if (lua_key_eq(s, stretch_order)) {
7328 stretch_order(n) = (quarterword) lua_tointeger(L, 3);
7329 } else if (lua_key_eq(s, shrink_order)) {
7330 shrink_order(n) = (quarterword) lua_tointeger(L, 3);
7331 } else {
7332 return nodelib_cantset(L, n, s);
7333 }
7334 } else {
7335 return luaL_error(L, "You can't assign to this %s node (%d)\n", node_data[t].name, n);
7336 }
7337 return 0;
7338 }
7339
7340 static int lua_nodelib_setfield(lua_State * L)
7341 {
7342 /* [given-node] [...]*/
7343 halfword *p = lua_touserdata(L, 1);
7344 if ( (p == NULL) || (! lua_getmetatable(L,1)) ) {
7345 return 0;
7346 }
7347 /* [given-node] [mt-given-node]*/
7348 lua_get_metatablelua(luatex_node);
7349 /* [given-node] [mt-given-node] [mt-node]*/
7350 if ( (!lua_rawequal(L, -1, -2)) ) {
7351 return 0;
7352 }
7353 /* prune stack and call getfield */
7354 lua_settop(L,3);
7355 return lua_nodelib_fast_setfield(L);
7356 }
7357
7358 /* node.direct.setfield */
7359
7360 static int lua_nodelib_direct_setfield_whatsit(lua_State * L, int n, const char *s)
7361 {
7362 int t = subtype(n);
7363 if (t == pdf_literal_node || t == pdf_late_literal_node) {
7364 /*tex The |string| key is obsolete. */
7365 if (lua_key_eq(s, mode)) {
7366 pdf_literal_mode(n) = (quarterword) lua_tointeger(L, 3);
7367 } else if (lua_key_eq(s, data) || lua_key_eq(s, string)) {
7368 set_pdf_literal_direct_normal(L, n, 3);
7369 } else if (lua_key_eq(s, token)) {
7370 set_pdf_literal_direct_token(L, n, 3);
7371 } else {
7372 return nodelib_cantset(L, n, s);
7373 }
7374 } else if (t == late_lua_node) {
7375 /*tex The |string| key is obsolete. */
7376 if (lua_key_eq(s, data) || lua_key_eq(s, string)) {
7377 set_late_lua_direct_normal(L, n, 3);
7378 } else if (lua_key_eq(s, token)) {
7379 set_late_lua_direct_token(L, n, 3);
7380 } else if (lua_key_eq(s, name)) {
7381 cleanup_late_lua_name(n) ;
7382 late_lua_name(n) = nodelib_gettoks(L, 3);
7383 } else {
7384 return nodelib_cantset(L, n, s);
7385 }
7386 } else if (t == user_defined_node) {
7387 if (lua_key_eq(s, user_id)) {
7388 user_node_id(n) = (halfword) lua_tointeger(L, 3);
7389 } else if (lua_key_eq(s, type)) {
7390 user_node_type(n) = (halfword) lua_tointeger(L, 3);
7391 } else if (lua_key_eq(s, value)) {
7392 set_user_node_direct_value(L, n, 3);
7393 } else {
7394 return nodelib_cantset(L, n, s);
7395 }
7396 } else if (t == pdf_annot_node) {
7397 if (lua_key_eq(s, width)) {
7398 width(n) = (halfword) lua_roundnumber(L, 3);
7399 } else if (lua_key_eq(s, depth)) {
7400 depth(n) = (halfword) lua_roundnumber(L, 3);
7401 } else if (lua_key_eq(s, height)) {
7402 height(n) = (halfword) lua_roundnumber(L, 3);
7403 } else if (lua_key_eq(s, objnum)) {
7404 pdf_annot_objnum(n) = (halfword) lua_tointeger(L, 3);
7405 } else if (lua_key_eq(s, data)) {
7406 pdf_annot_data(n) = nodelib_gettoks(L, 3);
7407 } else {
7408 return nodelib_cantset(L, n, s);
7409 }
7410 } else if (t == pdf_dest_node) {
7411 if (lua_key_eq(s, width)) {
7412 width(n) = (halfword) lua_roundnumber(L, 3);
7413 } else if (lua_key_eq(s, depth)) {
7414 depth(n) = (halfword) lua_roundnumber(L, 3);
7415 } else if (lua_key_eq(s, height)) {
7416 height(n) = (halfword) lua_roundnumber(L, 3);
7417 } else if (lua_key_eq(s, named_id)) {
7418 pdf_dest_named_id(n) = (quarterword) lua_tointeger(L, 3);
7419 } else if (lua_key_eq(s, dest_id)) {
7420 if (pdf_dest_named_id(n) == 1) {
7421 pdf_dest_id(n) = nodelib_gettoks(L, 3);
7422 } else {
7423 pdf_dest_id(n) = (halfword) lua_tointeger(L, 3);
7424 }
7425 } else if (lua_key_eq(s, dest_type)) {
7426 pdf_dest_type(n) = (quarterword) lua_tointeger(L, 3);
7427 } else if (lua_key_eq(s, xyz_zoom)) {
7428 pdf_dest_xyz_zoom(n) = (halfword) lua_tointeger(L, 3);
7429 } else if (lua_key_eq(s, objnum)) {
7430 pdf_dest_objnum(n) = (halfword) lua_tointeger(L, 3);
7431 } else {
7432 return nodelib_cantset(L, n, s);
7433 }
7434 } else if (t == pdf_setmatrix_node) {
7435 if (lua_key_eq(s, data)) {
7436 set_pdf_setmatrix_direct_value(L, n, 3);
7437 } else {
7438 return nodelib_cantset(L, n, s);
7439 }
7440 } else if (t == pdf_refobj_node) {
7441 if (lua_key_eq(s, objnum)) {
7442 pdf_obj_objnum(n) = (halfword) lua_tointeger(L, 3);
7443 } else {
7444 return nodelib_cantset(L, n, s);
7445 }
7446 } else if (t == pdf_start_link_node) {
7447 if (lua_key_eq(s, width)) {
7448 width(n) = (halfword) lua_roundnumber(L, 3);
7449 } else if (lua_key_eq(s, depth)) {
7450 depth(n) = (halfword) lua_roundnumber(L, 3);
7451 } else if (lua_key_eq(s, height)) {
7452 height(n) = (halfword) lua_roundnumber(L, 3);
7453 } else if (lua_key_eq(s, objnum)) {
7454 pdf_link_objnum(n) = (halfword) lua_tointeger(L, 3);
7455 } else if (lua_key_eq(s, link_attr)) {
7456 pdf_link_attr(n) = nodelib_gettoks(L, 3);
7457 } else if (lua_key_eq(s, action)) {
7458 pdf_link_action(n) = nodelib_popdirect(n); /*nodelib_getaction(L, 3);*/
7459 } else {
7460 return nodelib_cantset(L, n, s);
7461 }
7462 } else if (t == write_node) {
7463 if (lua_key_eq(s, stream)) {
7464 write_stream(n) = (halfword) lua_tointeger(L, 3);
7465 } else if (lua_key_eq(s, data) || lua_key_eq(s, value)) {
7466 set_write_direct_value(L,n,3);
7467 } else {
7468 return nodelib_cantset(L, n, s);
7469 }
7470 } else if (t == pdf_colorstack_node) {
7471 if (lua_key_eq(s, stack)) {
7472 pdf_colorstack_stack(n) = (halfword) lua_tointeger(L, 3);
7473 } else if (lua_key_eq(s, command)) {
7474 pdf_colorstack_cmd(n) = (halfword) lua_tointeger(L, 3);
7475 } else if (lua_key_eq(s, data)) {
7476 pdf_colorstack_data(n) = nodelib_gettoks(L, 3);
7477 } else {
7478 return nodelib_cantset(L, n, s);
7479 }
7480 } else if (t == pdf_action_node) {
7481 if (lua_key_eq(s, action_type)) {
7482 pdf_action_type(n) = (quarterword) lua_tointeger(L, 3);
7483 } else if (lua_key_eq(s, named_id)) {
7484 pdf_action_named_id(n) = (quarterword) lua_tointeger(L, 3);
7485 } else if (lua_key_eq(s, action_id)) {
7486 if (pdf_action_named_id(n) & 1) {
7487 pdf_action_id(n) = nodelib_gettoks(L, 3);
7488 } else {
7489 pdf_action_id(n) = (halfword) lua_tointeger(L, 3);
7490 }
7491 } else if (lua_key_eq(s, file)) {
7492 pdf_action_file(n) = nodelib_gettoks(L, 3);
7493 } else if (lua_key_eq(s, new_window)) {
7494 pdf_action_new_window(n) = (halfword) lua_tointeger(L, 3);
7495 } else if (lua_key_eq(s, data)) {
7496 pdf_action_tokens(n) = nodelib_gettoks(L, 3);
7497 } else if (lua_key_eq(s, struct_id)) {
7498 if (lua_isnil(L, 3)) {
7499 pdf_action_struct_id(n) = null;
7500 } else if (pdf_action_named_id(n) & 2) {
7501 pdf_action_struct_id(n) = nodelib_gettoks(L, 3);
7502 } else {
7503 pdf_action_struct_id(n) = (halfword) lua_tointeger(L, 3);
7504 }
7505 } else {
7506 return nodelib_cantset(L, n, s);
7507 }
7508 } else if ((t == pdf_thread_node) || (t == pdf_start_thread_node)) {
7509 if (lua_key_eq(s, width)) {
7510 width(n) = (halfword) lua_roundnumber(L, 3);
7511 } else if (lua_key_eq(s, depth)) {
7512 depth(n) = (halfword) lua_roundnumber(L, 3);
7513 } else if (lua_key_eq(s, height)) {
7514 height(n) = (halfword) lua_roundnumber(L, 3);
7515 } else if (lua_key_eq(s, named_id)) {
7516 pdf_thread_named_id(n) = (quarterword) lua_tointeger(L, 3);
7517 } else if (lua_key_eq(s, thread_id)) {
7518 if (pdf_thread_named_id(n) == 1) {
7519 pdf_thread_id(n) = nodelib_gettoks(L, 3);
7520 } else {
7521 pdf_thread_id(n) = (halfword) lua_tointeger(L, 3);
7522 }
7523 } else if (lua_key_eq(s, thread_attr)) {
7524 pdf_thread_attr(n) = nodelib_gettoks(L, 3);
7525 } else {
7526 return nodelib_cantset(L, n, s);
7527 }
7528 } else if (t == special_node || t == late_special_node) {
7529 if (lua_key_eq(s, data) || lua_key_eq(s, value)) {
7530 set_special_direct_value(L, n, 3);
7531 } else {
7532 return nodelib_cantset(L, n, s);
7533 }
7534 } else if (t == open_node) {
7535 if (lua_key_eq(s, stream)) {
7536 write_stream(n) = (halfword) lua_tointeger(L, 3);
7537 } else if (lua_key_eq(s, name)) {
7538 open_name(n) = nodelib_getstring(L, 3);
7539 } else if (lua_key_eq(s, area)) {
7540 open_area(n) = nodelib_getstring(L, 3);
7541 } else if (lua_key_eq(s, ext)) {
7542 open_ext(n) = nodelib_getstring(L, 3);
7543 } else {
7544 return nodelib_cantset(L, n, s);
7545 }
7546 } else if (t == close_node) {
7547 if (lua_key_eq(s, stream)) {
7548 write_stream(n) = (halfword) lua_tointeger(L, 3);
7549 } else {
7550 return nodelib_cantset(L, n, s);
7551 }
7552 } else if ((t == pdf_end_link_node) || (t == pdf_end_thread_node) || (t == save_pos_node) ||
7553 (t == pdf_save_node) || (t == pdf_restore_node)) {
7554 return nodelib_cantset(L, n, s);
7555 } else {
7556 /* do nothing */
7557 }
7558 return 0;
7559 }
7560
7561 /* direct.is_char */
7562 /* direct.is_glyph */
7563
7564 static int lua_nodelib_direct_is_char(lua_State * L)
7565 {
7566 halfword n = lua_tointeger(L, 1);
7567 if (type(n) != glyph_node) {
7568 lua_pushnil(L); /* no glyph at all */
7569 lua_pushinteger(L,type(n)); /* can save a lookup call */
7570 return 2;
7571 } else if (is_protected_glyph_node(n)) {
7572 lua_pushboolean(L,0); /* a done glyph */
7573 } else if (lua_type(L,2) == LUA_TNUMBER) {
7574 halfword f = lua_tointeger(L, 2);
7575 if (f && f == font(n)) {
7576 lua_pushinteger(L,character(n)); /* a todo glyph in the asked font */
7577 } else {
7578 lua_pushboolean(L,0); /* a todo glyph in another font */
7579 }
7580 } else {
7581 lua_pushinteger(L,character(n)); /* a todo glyph */
7582 }
7583 return 1;
7584 }
7585
7586 static int lua_nodelib_direct_is_glyph(lua_State * L)
7587 {
7588 halfword n = lua_tointeger(L, 1);
7589 if (type(n) != glyph_node) {
7590 lua_pushboolean(L,0);
7591 lua_pushinteger(L,type(n));
7592 } else {
7593 lua_pushinteger(L,character(n));
7594 lua_pushinteger(L,font(n));
7595 }
7596 return 2;
7597 }
7598
7599 /* uses_font */
7600 /* direct.uses_font */
7601
7602 #define uses_font_disc(what,p,n) \
7603 p = vlink(what(n)); \
7604 while (p) { \
7605 if ((type(p) == glyph_node) && (font(p) == f)) { \
7606 lua_pushboolean(L,1); \
7607 return 1; \
7608 } \
7609 p = vlink(p); \
7610 }
7611
7612
7613 static int lua_nodelib_direct_uses_font(lua_State * L)
7614 {
7615 halfword n = lua_tointeger(L,1);
7616 halfword f = lua_tointeger(L,2);
7617 halfword p;
7618 if (type(n) == glyph_node) {
7619 lua_pushboolean(L,font(n) == f);
7620 return 1;
7621 } else if (type(n) == disc_node) {
7622 uses_font_disc(pre_break,p,n);
7623 uses_font_disc(post_break,p,n);
7624 uses_font_disc(no_break,p,n);
7625 }
7626 /* todo: other node types */
7627 lua_pushboolean(L,0);
7628 return 1;
7629 }
7630
7631 static int lua_nodelib_uses_font(lua_State * L)
7632 {
7633 halfword n = *check_isnode(L, 1);
7634 halfword f = lua_tointeger(L,2);
7635 halfword p;
7636 if (type(n) == glyph_node) {
7637 lua_pushboolean(L,font(n) == f);
7638 return 1;
7639 } else if (type(n) == disc_node) {
7640 uses_font_disc(pre_break,p,n);
7641 uses_font_disc(post_break,p,n);
7642 uses_font_disc(no_break,p,n);
7643 }
7644 /* todo: other node types */
7645 lua_pushboolean(L,0);
7646 return 1;
7647 }
7648
7649
7650 /* direct.setfield */
7651
7652 static int lua_nodelib_direct_setfield(lua_State * L)
7653 {
7654 const char *s;
7655
7656 halfword n = lua_tointeger(L, 1);
7657 int t = type(n);
7658
7659 if (lua_type(L, 2) == LUA_TNUMBER) {
7660 if (lua_gettop(L) == 3) {
7661 int i = lua_tointeger(L, 2);
7662 int val = lua_tointeger(L, 3);
7663 if (val == UNUSED_ATTRIBUTE) {
7664 (void) unset_attribute(n, i, val);
7665 } else {
7666 set_attribute(n, i, val);
7667 }
7668 } else {
7669 luaL_error(L, "incorrect number of arguments");
7670 }
7671 return 0;
7672 }
7673
7674 s = lua_tostring(L, 2);
7675
7676 /*if (lua_key_eq(s, id)) {
7677 type(n) = (quarteword) lua_tointeger(L, 3);
7678 } else*/
7679 if (lua_key_eq(s, next)) {
7680 halfword x = nodelib_popdirect(3);
7681 if (x>0 && type(x) == glue_spec_node) {
7682 return luaL_error(L, "You can't assign a %s node to a next field\n", node_data[type(x)].name);
7683 }
7684 vlink(n) = x;
7685 } else if (lua_key_eq(s, prev)) {
7686 halfword x = nodelib_popdirect(3);
7687 if (x>0 && type(x) == glue_spec_node) {
7688 return luaL_error(L, "You can't assign a %s node to a prev field\n", node_data[type(x)].name);
7689 }
7690 alink(n) = x;
7691 } else if (lua_key_eq(s, attr)) {
7692 if (nodetype_has_attributes(type(n))) {
7693 /* nodelib_setattr(L, 3, n); */
7694 reassign_attribute(n,lua_tointeger(L, 3));
7695 }
7696 } else if (t == glyph_node) {
7697 if (lua_key_eq(s, subtype)) {
7698 subtype(n) = (quarterword) lua_tointeger(L, 3);
7699 } else if (lua_key_eq(s, font)) {
7700 font(n) = (halfword) lua_tointeger(L, 3);
7701 } else if (lua_key_eq(s, char)) {
7702 character(n) = (halfword) lua_tointeger(L, 3);
7703 } else if (lua_key_eq(s, xoffset)) {
7704 x_displace(n) = (halfword) lua_roundnumber(L, 3);
7705 } else if (lua_key_eq(s, yoffset)) {
7706 y_displace(n) = (halfword) lua_roundnumber(L, 3);
7707 } else if (lua_key_eq(s, data)) {
7708 glyph_node_data(n) = (halfword) lua_tointeger(L, 3);
7709 } else if (lua_key_eq(s, expansion_factor)) {
7710 ex_glyph(n) = (halfword) lua_roundnumber(L, 3);
7711 } else if (lua_key_eq(s, components)) {
7712 lig_ptr(n) = nodelib_popdirect(3);
7713 } else if (lua_key_eq(s, lang)) {
7714 set_char_lang(n, (halfword) lua_tointeger(L, 3));
7715 } else if (lua_key_eq(s, left)) {
7716 set_char_lhmin(n, (halfword) lua_tointeger(L, 3));
7717 } else if (lua_key_eq(s, right)) {
7718 set_char_rhmin(n, (halfword) lua_tointeger(L, 3));
7719 } else if (lua_key_eq(s, uchyph)) {
7720 set_char_uchyph(n, (halfword) lua_tointeger(L, 3));
7721 } else if (lua_key_eq(s, width)) {
7722 /* not yet */
7723 } else if (lua_key_eq(s, height)) {
7724 /* not yet */
7725 } else if (lua_key_eq(s, depth)) {
7726 /* not yet */
7727 } else {
7728 return nodelib_cantset(L, n, s);
7729 }
7730 } else if ((t == hlist_node) || (t == vlist_node)) {
7731 if (lua_key_eq(s, subtype)) {
7732 subtype(n) = (quarterword) lua_tointeger(L, 3);
7733 } else if (lua_key_eq(s, list) || lua_key_eq(s, head)) {
7734 list_ptr(n) = nodelib_popdirect(3);
7735 } else if (lua_key_eq(s, width)) {
7736 width(n) = (halfword) lua_roundnumber(L, 3);
7737 } else if (lua_key_eq(s, height)) {
7738 height(n) = (halfword) lua_roundnumber(L, 3);
7739 } else if (lua_key_eq(s, depth)) {
7740 depth(n) = (halfword) lua_roundnumber(L, 3);
7741 } else if (lua_key_eq(s, direction)) {
7742 box_dir(n) = nodelib_getdirection(L, 3);
7743 } else if (lua_key_eq(s, dir)) {
7744 box_dir(n) = nodelib_getdir_par(L, 3);
7745 } else if (lua_key_eq(s, shift)) {
7746 shift_amount(n) = (halfword) lua_roundnumber(L, 3);
7747 } else if (lua_key_eq(s, glue_order)) {
7748 glue_order(n) = (quarterword) lua_tointeger(L, 3);
7749 } else if (lua_key_eq(s, glue_sign)) {
7750 glue_sign(n) = (quarterword) lua_tointeger(L, 3);
7751 } else if (lua_key_eq(s, glue_set)) {
7752 glue_set(n) = (glue_ratio) lua_tonumber(L, 3); /* integer or float */
7753 } else {
7754 return nodelib_cantset(L, n, s);
7755 }
7756 } else if (t == disc_node) {
7757 if (lua_key_eq(s, subtype)) {
7758 subtype(n) = (quarterword) lua_tointeger(L, 3);
7759 } else if (lua_key_eq(s, pre)) {
7760 set_disc_field(pre_break(n), nodelib_popdirect(3));
7761 } else if (lua_key_eq(s, post)) {
7762 set_disc_field(post_break(n), nodelib_popdirect(3));
7763 } else if (lua_key_eq(s, replace)) {
7764 set_disc_field(no_break(n), nodelib_popdirect(3));
7765 } else if (lua_key_eq(s, penalty)) {
7766 disc_penalty(n) = (quarterword) lua_tointeger(L, 3);
7767 } else {
7768 return nodelib_cantset(L, n, s);
7769 }
7770 } else if (t == glue_node) {
7771 if (lua_key_eq(s, subtype)) {
7772 subtype(n) = (quarterword) lua_tointeger(L, 3);
7773 } else if (lua_key_eq(s, width)) {
7774 width(n) = (halfword) lua_roundnumber(L, 3);
7775 } else if (lua_key_eq(s, stretch)) {
7776 stretch(n) = (halfword) lua_roundnumber(L, 3);
7777 } else if (lua_key_eq(s, shrink)) {
7778 shrink(n) = (halfword) lua_roundnumber(L, 3);
7779 } else if (lua_key_eq(s, stretch_order)) {
7780 stretch_order(n) = (quarterword) lua_tointeger(L, 3);
7781 } else if (lua_key_eq(s, shrink_order)) {
7782 shrink_order(n) = (quarterword) lua_tointeger(L, 3);
7783 } else if (lua_key_eq(s, leader)) {
7784 leader_ptr(n) = nodelib_popdirect(3);
7785 } else {
7786 return nodelib_cantset(L, n, s);
7787 }
7788 } else if (t == kern_node) {
7789 if (lua_key_eq(s, subtype)) {
7790 subtype(n) = (quarterword) lua_tointeger(L, 3);
7791 } else if (lua_key_eq(s, kern)) {
7792 width(n) = (halfword) lua_roundnumber(L, 3);
7793 } else if (lua_key_eq(s, expansion_factor)) {
7794 ex_kern(n) = (halfword) lua_roundnumber(L, 3);
7795 } else {
7796 return nodelib_cantset(L, n, s);
7797 }
7798 } else if (t == penalty_node) {
7799 if (lua_key_eq(s, subtype)) {
7800 /* dummy subtype */
7801 } else if (lua_key_eq(s, penalty)) {
7802 penalty(n) = (halfword) lua_tointeger(L, 3);
7803 } else {
7804 return nodelib_cantset(L, n, s);
7805 }
7806 } else if (t == rule_node) {
7807 if (lua_key_eq(s, subtype)) {
7808 subtype(n) = (quarterword) lua_tointeger(L, 3);
7809 } else if (lua_key_eq(s, width)) {
7810 width(n) = (halfword) lua_roundnumber(L, 3);
7811 } else if (lua_key_eq(s, height)) {
7812 height(n) = (halfword) lua_roundnumber(L, 3);
7813 } else if (lua_key_eq(s, depth)) {
7814 depth(n) = (halfword) lua_roundnumber(L, 3);
7815 } else if (lua_key_eq(s, left)) {
7816 rule_left(n) = (halfword) lua_roundnumber(L, 3);
7817 } else if (lua_key_eq(s, right)) {
7818 rule_right(n) = (halfword) lua_roundnumber(L, 3);
7819 } else if (lua_key_eq(s, direction)) {
7820 rule_dir(n) = nodelib_getdirection(L, 3);
7821 } else if (lua_key_eq(s, dir)) {
7822 rule_dir(n) = nodelib_getdir_par(L, 3);
7823 } else if (lua_key_eq(s, index)) {
7824 rule_index(n) = (halfword) lua_tointeger(L, 3);
7825 } else if (lua_key_eq(s, transform)) {
7826 rule_transform(n) = (halfword) lua_roundnumber(L, 3); /* can also be user value */
7827 } else {
7828 return nodelib_cantset(L, n, s);
7829 }
7830 } else if (t == dir_node) {
7831 if (lua_key_eq(s, direction)) {
7832 dir_dir(n) = nodelib_getdirection(L, 3);
7833 } else if (lua_key_eq(s, dir)) {
7834 nodelib_setdir_text(L, 3, n);
7835 } else if (lua_key_eq(s, level)) {
7836 dir_level(n) = (halfword) lua_tointeger(L, 3);
7837 } else {
7838 return nodelib_cantset(L, n, s);
7839 }
7840 } else if (t == whatsit_node) {
7841 if (lua_key_eq(s, subtype)) {
7842 subtype(n) = (quarterword) lua_tointeger(L, 3);
7843 } else {
7844 lua_nodelib_direct_setfield_whatsit(L, n, s);
7845 }
7846 } else if (t == local_par_node) {
7847 if (lua_key_eq(s, pen_inter)) {
7848 local_pen_inter(n) = (halfword) lua_tointeger(L, 3);
7849 } else if (lua_key_eq(s, pen_broken)) {
7850 local_pen_broken(n) = (halfword) lua_tointeger(L, 3);
7851 } else if (lua_key_eq(s, direction)) {
7852 local_par_dir(n) = nodelib_getdirection(L, 3);
7853 } else if (lua_key_eq(s, dir)) {
7854 local_par_dir(n) = nodelib_getdir_par(L, 3);
7855 } else if (lua_key_eq(s, box_left)) {
7856 local_box_left(n) = nodelib_getlist(L, 3);
7857 } else if (lua_key_eq(s, box_left_width)) {
7858 local_box_left_width(n) = (halfword) lua_roundnumber(L, 3);
7859 } else if (lua_key_eq(s, box_right)) {
7860 local_box_right(n) = nodelib_getlist(L, 3);
7861 } else if (lua_key_eq(s, box_right_width)) {
7862 local_box_right_width(n) = (halfword) lua_roundnumber(L, 3);
7863 } else {
7864 return nodelib_cantset(L, n, s);
7865 }
7866 } else if (t == simple_noad) {
7867 if (lua_key_eq(s, subtype)) {
7868 subtype(n) = (quarterword) lua_tointeger(L, 3);
7869 } else if (lua_key_eq(s, nucleus)) {
7870 nucleus(n) = nodelib_popdirect(3);
7871 } else if (lua_key_eq(s, sub)) {
7872 subscr(n) = nodelib_popdirect(3);
7873 } else if (lua_key_eq(s, sup)) {
7874 supscr(n) = nodelib_popdirect(3);
7875 } else if (lua_key_eq(s, options)) {
7876 noadoptions(n) = (quarterword) lua_tointeger(L, 3);
7877 } else {
7878 return nodelib_cantset(L, n, s);
7879 }
7880 } else if ((t == math_char_node) || (t == math_text_char_node)) {
7881 if (lua_key_eq(s, subtype)) {
7882 subtype(n) = (quarterword) lua_tointeger(L, 3);
7883 } else if (lua_key_eq(s, fam)) {
7884 math_fam(n) = (halfword) lua_tointeger(L, 3);
7885 } else if (lua_key_eq(s, char)) {
7886 math_character(n) = (halfword) lua_tointeger(L, 3);
7887 } else {
7888 return nodelib_cantset(L, n, s);
7889 }
7890 } else if (t == mark_node) {
7891 if (lua_key_eq(s, subtype)) {
7892 subtype(n) = (quarterword) lua_tointeger(L, 3);
7893 } else if (lua_key_eq(s, class)) {
7894 mark_class(n) = (halfword) lua_tointeger(L, 3);
7895 } else if (lua_key_eq(s, mark)) {
7896 mark_ptr(n) = nodelib_gettoks(L, 3);
7897 } else {
7898 return nodelib_cantset(L, n, s);
7899 }
7900 } else if (t == ins_node) {
7901 if (lua_key_eq(s, subtype)) {
7902 subtype(n) = (quarterword) lua_tointeger(L, 3);
7903 } else if (lua_key_eq(s, cost)) {
7904 float_cost(n) = (halfword) lua_tointeger(L, 3);
7905 } else if (lua_key_eq(s, depth)) {
7906 depth(n) = (halfword) lua_roundnumber(L, 3);
7907 } else if (lua_key_eq(s, height)) {
7908 height(n) = (halfword) lua_roundnumber(L, 3);
7909 } else if ((lua_key_eq(s, list)) || (lua_key_eq(s, head))) {
7910 ins_ptr(n) = nodelib_popdirect(3);
7911 /* glue */
7912 } else if (lua_key_eq(s, width)) {
7913 width(n) = (halfword) lua_roundnumber(L, 3);
7914 } else if (lua_key_eq(s, stretch)) {
7915 stretch(n) = (halfword) lua_roundnumber(L, 3);
7916 } else if (lua_key_eq(s, shrink)) {
7917 shrink(n) = (halfword) lua_roundnumber(L, 3);
7918 } else if (lua_key_eq(s, stretch_order)) {
7919 stretch_order(n) = (quarterword) lua_tointeger(L, 3);
7920 } else if (lua_key_eq(s, shrink_order)) {
7921 shrink_order(n) = (quarterword) lua_tointeger(L, 3);
7922 } else {
7923 return nodelib_cantset(L, n, s);
7924 }
7925 } else if (t == math_node) {
7926 if (lua_key_eq(s, subtype)) {
7927 subtype(n) = (quarterword) lua_tointeger(L, 3);
7928 } else if (lua_key_eq(s, surround)) {
7929 surround(n) = (halfword) lua_roundnumber(L, 3);
7930 /* glue */
7931 } else if (lua_key_eq(s, width)) {
7932 width(n) = (halfword) lua_roundnumber(L, 3);
7933 } else if (lua_key_eq(s, stretch)) {
7934 stretch(n) = (halfword) lua_roundnumber(L, 3);
7935 } else if (lua_key_eq(s, shrink)) {
7936 shrink(n) = (halfword) lua_roundnumber(L, 3);
7937 } else if (lua_key_eq(s, stretch_order)) {
7938 stretch_order(n) = (quarterword) lua_tointeger(L, 3);
7939 } else if (lua_key_eq(s, shrink_order)) {
7940 shrink_order(n) = (quarterword) lua_tointeger(L, 3);
7941 } else {
7942 return nodelib_cantset(L, n, s);
7943 }
7944 } else if (t == fraction_noad) {
7945 if (lua_key_eq(s, subtype)) {
7946 subtype(n) = (quarterword) lua_tointeger(L, 3);
7947 } else if (lua_key_eq(s, width)) {
7948 thickness(n) = (halfword) lua_roundnumber(L, 3);
7949 } else if (lua_key_eq(s, num)) {
7950 numerator(n) = nodelib_popdirect(3);
7951 } else if (lua_key_eq(s, denom)) {
7952 denominator(n) = nodelib_popdirect(3);
7953 } else if (lua_key_eq(s, left)) {
7954 left_delimiter(n) = nodelib_popdirect(3);
7955 } else if (lua_key_eq(s, right)) {
7956 right_delimiter(n) = nodelib_popdirect(3);
7957 } else if (lua_key_eq(s, middle)) {
7958 middle_delimiter(n) = nodelib_popdirect(3);
7959 } else if (lua_key_eq(s, fam)) {
7960 fraction_fam(n) = (halfword) lua_tointeger(L, 3);
7961 } else if (lua_key_eq(s, options)) {
7962 fractionoptions(n) = (halfword) lua_tointeger(L, 3);
7963 } else {
7964 return nodelib_cantset(L, n, s);
7965 }
7966 } else if (t == style_node) {
7967 if (lua_key_eq(s, subtype)) {
7968 /* dummy subtype */
7969 } else if (lua_key_eq(s, style)) {
7970 assign_math_style(L,2,subtype(n));
7971 } else {
7972 /* return nodelib_cantset(L, n, s); */
7973 }
7974 } else if (t == accent_noad) {
7975 if (lua_key_eq(s, subtype)) {
7976 subtype(n) = (quarterword) lua_tointeger(L, 3);
7977 } else if (lua_key_eq(s, nucleus)) {
7978 nucleus(n) = nodelib_popdirect(3);
7979 } else if (lua_key_eq(s, sub)) {
7980 subscr(n) = nodelib_popdirect(3);
7981 } else if (lua_key_eq(s, sup)) {
7982 supscr(n) = nodelib_popdirect(3);
7983 } else if ((lua_key_eq(s, top_accent))||(lua_key_eq(s, accent))) {
7984 top_accent_chr(n) = nodelib_popdirect(3);
7985 } else if (lua_key_eq(s, bot_accent)) {
7986 bot_accent_chr(n) = nodelib_popdirect(3);
7987 } else if (lua_key_eq(s, overlay_accent)) {
7988 overlay_accent_chr(n) = nodelib_popdirect(3);
7989 } else if (lua_key_eq(s, fraction)) {
7990 accentfraction(n) = (halfword) lua_roundnumber(L, 3);
7991 } else {
7992 return nodelib_cantset(L, n, s);
7993 }
7994 } else if (t == fence_noad) {
7995 if (lua_key_eq(s, subtype)) {
7996 subtype(n) = (quarterword) lua_tointeger(L, 3);
7997 } else if (lua_key_eq(s, delim)) {
7998 delimiter(n) = nodelib_popdirect(3);
7999 } else if (lua_key_eq(s, italic)) {
8000 delimiteritalic(n) = (halfword) lua_roundnumber(L,3);
8001 } else if (lua_key_eq(s, height)) {
8002 delimiterheight(n) = (halfword) lua_roundnumber(L,3);
8003 } else if (lua_key_eq(s, depth)) {
8004 delimiterdepth(n) = (halfword) lua_roundnumber(L,3);
8005 } else if (lua_key_eq(s, options)) {
8006 delimiteroptions(n) = (halfword) lua_tointeger(L,3);
8007 } else if (lua_key_eq(s, class)) {
8008 delimiterclass(n) = (halfword) lua_tointeger(L,3);
8009 } else {
8010 return nodelib_cantset(L, n, s);
8011 }
8012 } else if (t == delim_node) {
8013 if (lua_key_eq(s, subtype)) {
8014 subtype(n) = (quarterword) lua_tointeger(L, 3);
8015 } else if (lua_key_eq(s, small_fam)) {
8016 small_fam(n) = (halfword) lua_tointeger(L, 3);
8017 } else if (lua_key_eq(s, small_char)) {
8018 small_char(n) = (halfword) lua_tointeger(L, 3);
8019 } else if (lua_key_eq(s, large_fam)) {
8020 large_fam(n) = (halfword) lua_tointeger(L, 3);
8021 } else if (lua_key_eq(s, large_char)) {
8022 large_char(n) = (halfword) lua_tointeger(L, 3);
8023 } else {
8024 return nodelib_cantset(L, n, s);
8025 }
8026 } else if ((t == sub_box_node) || (t == sub_mlist_node)) {
8027 if (lua_key_eq(s, subtype)) {
8028 subtype(n) = (quarterword) lua_tointeger(L, 3);
8029 } else if ((lua_key_eq(s, list)) || (lua_key_eq(s, head))){
8030 math_list(n) = nodelib_popdirect(3);
8031 } else {
8032 return nodelib_cantset(L, n, s);
8033 }
8034 } else if (t == radical_noad) {
8035 if (lua_key_eq(s, subtype)) {
8036 subtype(n) = (quarterword) lua_tointeger(L, 3);
8037 } else if (lua_key_eq(s, nucleus)) {
8038 nucleus(n) = nodelib_popdirect(3);
8039 } else if (lua_key_eq(s, sub)) {
8040 subscr(n) = nodelib_popdirect(3);
8041 } else if (lua_key_eq(s, sup)) {
8042 supscr(n) = nodelib_popdirect(3);
8043 } else if (lua_key_eq(s, left)) {
8044 left_delimiter(n) = nodelib_popdirect(3);
8045 } else if (lua_key_eq(s, degree)) {
8046 degree(n) = nodelib_popdirect(3);
8047 } else if (lua_key_eq(s, width)) {
8048 radicalwidth(n) = lua_roundnumber(L, 3);
8049 } else if (lua_key_eq(s, options)) {
8050 radicaloptions(n) = (halfword) lua_tointeger(L,3);
8051 } else {
8052 return nodelib_cantset(L, n, s);
8053 }
8054 } else if (t == margin_kern_node) {
8055 if (lua_key_eq(s, subtype)) {
8056 subtype(n) = (quarterword) lua_tointeger(L, 3);
8057 } else if (lua_key_eq(s, width) || lua_key_eq(s, width)) {
8058 width(n) = (halfword) lua_roundnumber(L, 3);
8059 } else if (lua_key_eq(s, glyph)) {
8060 margin_char(n) = nodelib_popdirect(3);
8061 } else {
8062 return nodelib_cantset(L, n, s);
8063 }
8064 } else if (t == split_up_node) {
8065 if (lua_key_eq(s, subtype)) {
8066 subtype(n) = (quarterword) lua_tointeger(L, 3);
8067 } else if (lua_key_eq(s, last_ins_ptr)) {
8068 last_ins_ptr(n) = nodelib_popdirect(3);
8069 } else if (lua_key_eq(s, best_ins_ptr)) {
8070 best_ins_ptr(n) = nodelib_popdirect(3);
8071 } else if (lua_key_eq(s, broken_ptr)) {
8072 broken_ptr(n) = nodelib_popdirect(3);
8073 } else if (lua_key_eq(s, broken_ins)) {
8074 broken_ins(n) = nodelib_popdirect(3);
8075 } else {
8076 return nodelib_cantset(L, n, s);
8077 }
8078 } else if (t == choice_node) {
8079 if (lua_key_eq(s, subtype)) {
8080 subtype(n) = (quarterword) lua_tointeger(L, 3);
8081 } else if (lua_key_eq(s, display)) {
8082 display_mlist(n) = nodelib_popdirect(3);
8083 } else if (lua_key_eq(s, text)) {
8084 text_mlist(n) = nodelib_popdirect(3);
8085 } else if (lua_key_eq(s, script)) {
8086 script_mlist(n) = nodelib_popdirect(3);
8087 } else if (lua_key_eq(s, scriptscript)) {
8088 script_script_mlist(n) = nodelib_popdirect(3);
8089 } else {
8090 return nodelib_cantset(L, n, s);
8091 }
8092 } else if (t == inserting_node) {
8093 if (lua_key_eq(s, subtype)) {
8094 subtype(n) = (quarterword) lua_tointeger(L, 3);
8095 } else if (lua_key_eq(s, last_ins_ptr)) {
8096 last_ins_ptr(n) = nodelib_popdirect(3);
8097 } else if (lua_key_eq(s, best_ins_ptr)) {
8098 best_ins_ptr(n) = nodelib_popdirect(3);
8099 } else {
8100 return nodelib_cantset(L, n, s);
8101 }
8102 } else if (t == attribute_node) {
8103 if (lua_key_eq(s, subtype)) {
8104 /* dummy subtype */
8105 } else if (lua_key_eq(s, number)) {
8106 attribute_id(n) = (halfword) lua_tointeger(L, 3);
8107 } else if (lua_key_eq(s, value)) {
8108 attribute_value(n) = (halfword) lua_tointeger(L, 3);
8109 } else {
8110 return nodelib_cantset(L, n, s);
8111 }
8112 } else if (t == adjust_node) {
8113 if (lua_key_eq(s, subtype)) {
8114 subtype(n) = (quarterword) lua_tointeger(L, 3);
8115 } else if ((lua_key_eq(s, list)) || (lua_key_eq(s, head))) {
8116 adjust_ptr(n) = nodelib_popdirect(3);
8117 } else {
8118 return nodelib_cantset(L, n, s);
8119 }
8120 } else if (t == unset_node) {
8121 if (lua_key_eq(s, subtype)) {
8122 /* dummy subtype */
8123 } else if (lua_key_eq(s, width)) {
8124 width(n) = (halfword) lua_roundnumber(L, 3);
8125 } else if (lua_key_eq(s, height)) {
8126 height(n) = (halfword) lua_roundnumber(L, 3);
8127 } else if (lua_key_eq(s, depth)) {
8128 depth(n) = (halfword) lua_roundnumber(L, 3);
8129 } else if (lua_key_eq(s, direction)) {
8130 box_dir(n) = nodelib_getdirection(L, 3);
8131 } else if (lua_key_eq(s, dir)) {
8132 box_dir(n) = nodelib_getdir_par(L, 3);
8133 } else if (lua_key_eq(s, shrink)) {
8134 glue_shrink(n) = (halfword) lua_roundnumber(L, 3);
8135 } else if (lua_key_eq(s, glue_order)) {
8136 glue_order(n) = (quarterword) lua_tointeger(L, 3);
8137 } else if (lua_key_eq(s, glue_sign)) {
8138 glue_sign(n) = (quarterword) lua_tointeger(L, 3);
8139 } else if (lua_key_eq(s, stretch)) {
8140 glue_stretch(n) = (halfword) lua_roundnumber(L, 3);
8141 } else if (lua_key_eq(s, count)) {
8142 span_count(n) = (quarterword) lua_tointeger(L, 3);
8143 } else if ((lua_key_eq(s, list)) || (lua_key_eq(s, head))){
8144 list_ptr(n) = nodelib_popdirect(3);
8145 } else {
8146 return nodelib_cantset(L, n, s);
8147 }
8148 } else if (t == attribute_list_node) {
8149 if (lua_key_eq(s, subtype)) {
8150 /* dummy subtype */
8151 } else {
8152 return nodelib_cantset(L, n, s);
8153 }
8154 } else if (t == boundary_node) {
8155 if (lua_key_eq(s, subtype)) {
8156 subtype(n) = (quarterword) lua_tointeger(L, 3);
8157 } else if (lua_key_eq(s, value)) {
8158 boundary_value(n) = lua_tointeger(L, 3);
8159 } else {
8160 return nodelib_cantset(L, n, s);
8161 }
8162 } else if (t == glue_spec_node) {
8163 if (lua_key_eq(s, width)) {
8164 width(n) = (halfword) lua_roundnumber(L, 3);
8165 } else if (lua_key_eq(s, stretch)) {
8166 stretch(n) = (halfword) lua_roundnumber(L, 3);
8167 } else if (lua_key_eq(s, shrink)) {
8168 shrink(n) = (halfword) lua_roundnumber(L, 3);
8169 } else if (lua_key_eq(s, stretch_order)) {
8170 stretch_order(n) = (quarterword) lua_tointeger(L, 3);
8171 } else if (lua_key_eq(s, shrink_order)) {
8172 shrink_order(n) = (quarterword) lua_tointeger(L, 3);
8173 } else {
8174 return nodelib_cantset(L, n, s);
8175 }
8176 } else {
8177 return luaL_error(L, "You can't assign to this %s node (%d)\n", node_data[t].name, n);
8178 }
8179 return 0;
8180 }
8181
8182 /* boxes */
8183
8184 static int direct_get_box_id(lua_State * L, int i)
8185 {
8186 const char *s;
8187 int cur_cs1, cur_cmd1;
8188 size_t k = 0;
8189 int j = -1;
8190 switch (lua_type(L, i)) {
8191 case LUA_TSTRING:
8192 s = lua_tolstring(L, i, &k);
8193 cur_cs1 = string_lookup(s, k);
8194 cur_cmd1 = eq_type(cur_cs1);
8195 if (cur_cmd1 == char_given_cmd ||
8196 cur_cmd1 == math_given_cmd) {
8197 j = equiv(cur_cs1);
8198 }
8199 break;
8200 case LUA_TNUMBER:
8201 j = lua_tointeger(L, (i));
8202 break;
8203 default:
8204 luaL_error(L, "argument must be a string or a number");
8205 j = -1; /* not a valid box id */
8206 }
8207 return j;
8208 }
8209
8210 /* node.getbox = tex.getbox */
8211 /* node.setbox = tex.setbox */
8212
8213 /* node.direct.getbox */
8214 /* node.direct.setbox */
8215
8216 static int lua_nodelib_direct_getbox(lua_State * L)
8217 {
8218 int t;
8219 int k = direct_get_box_id(L, -1);
8220 direct_check_index_range(k, "getbox");
8221 t = get_tex_box_register(k);
8222 if (t == null) {
8223 lua_pushnil(L);
8224 } else {
8225 lua_pushinteger(L, t);
8226 }
8227 return 1;
8228 }
8229
8230 static int lua_nodelib_direct_setbox(lua_State * L)
8231 {
8232 int isglobal = 0;
8233 int j, k, err, t;
8234 int save_global_defs ;
8235 int n = lua_gettop(L);
8236 if (n == 3 && (lua_type(L, 1) == LUA_TSTRING)) {
8237 const char *s = lua_tostring(L, 1);
8238 if (lua_key_eq(s, global))
8239 isglobal = 1;
8240 }
8241 t = lua_type(L, -1);
8242 k = direct_get_box_id(L, -2);
8243 direct_check_index_range(k, "setbox");
8244 if (t == LUA_TBOOLEAN) {
8245 j = lua_toboolean(L, -1);
8246 if (j == 0) {
8247 j = null;
8248 } else {
8249 return 0;
8250 }
8251 } else if (t == LUA_TNIL) {
8252 j = null;
8253 } else {
8254 j = nodelib_popdirect(-1);
8255 if (j != null && type(j) != hlist_node && type(j) != vlist_node) {
8256 luaL_error(L, "setbox: incompatible node type (%s)\n",get_node_name(type(j), subtype(j)));
8257 return 0;
8258 }
8259 }
8260 save_global_defs = global_defs_par;
8261 if (isglobal) {
8262 global_defs_par = 1;
8263 }
8264 err = set_tex_box_register(k, j);
8265 global_defs_par = save_global_defs;
8266 if (err) {
8267 luaL_error(L, "incorrect value");
8268 }
8269 return 0;
8270 }
8271
8272 /* node.is_node(n) */
8273
8274 /*
8275
8276 static int lua_nodelib_is_node(lua_State * L)
8277 {
8278 if (maybe_isnode(L,1) == NULL)
8279 lua_pushboolean(L,0);
8280 else
8281 lua_pushboolean(L,1);
8282 return 1;
8283 }
8284
8285 */
8286
8287 static int lua_nodelib_is_node(lua_State * L)
8288 {
8289 halfword *p = maybe_isnode(L, 1);
8290 if (p == NULL)
8291 lua_pushboolean(L,0);
8292 else
8293 lua_pushinteger(L, *((halfword *)p));
8294 return 1;
8295 }
8296
8297 /* node.direct.is_direct(n) (handy for mixed usage testing) */
8298
8299 static int lua_nodelib_direct_is_direct(lua_State * L)
8300 { /*
8301 quick and dirty and faster than not node.is_node, this helper
8302 returns false or <direct>
8303 */
8304 /*
8305 if (lua_type(L,1) == LUA_TNUMBER)
8306 lua_pushboolean(L,1);
8307 else
8308 lua_pushboolean(L,0);
8309 */
8310 if (lua_type(L,1) != LUA_TNUMBER)
8311 lua_pushboolean(L,0);
8312 /* else return direct */
8313 return 1;
8314 }
8315
8316 /* node.direct.is_node(n) (handy for mixed usage testing) */
8317
8318 static int lua_nodelib_direct_is_node(lua_State * L)
8319 { /*
8320 quick and dirty, only checks userdata, node.is_node is slower but
8321 more exact, this helper returns false or <node>
8322 */
8323 /*
8324 halfword *n = lua_touserdata(L, 1);
8325 if (n != NULL) {
8326 lua_pushboolean(L,1);
8327 } else {
8328 lua_pushboolean(L,0);
8329 }
8330 */
8331 if (maybe_isnode(L,1) == NULL)
8332 lua_pushboolean(L,0);
8333 /* else assume and return node */
8334 return 1;
8335 }
8336
8337 /*
8338 Beware, enabling and disabling can result in an inconsistent properties table
8339 but it might make sense sometimes. Of course by default we have disabled this
8340 mechanism. And, one can always sweep the table empty.
8341 */
8342
8343 static int lua_nodelib_properties_set_mode(lua_State * L)
8344 { /* <boolean> */
8345 if (lua_isboolean(L,1)) {
8346 lua_properties_enabled = lua_toboolean(L,1);
8347 }
8348 if (lua_isboolean(L,2)) {
8349 lua_properties_use_metatable = lua_toboolean(L,2);
8350 }
8351 return 0;
8352 }
8353
8354 /* We used to have variants in assigned defaults but they made no sense. */
8355
8356 static int lua_nodelib_properties_flush_table(lua_State * L)
8357 { /* <node|direct> <number> */
8358 lua_get_metatablelua(node_properties);
8359 lua_pushnil(L); /* initializes lua_next */
8360 while (lua_next(L,-2) != 0) {
8361 lua_pushvalue(L,-2);
8362 lua_pushnil(L);
8363 lua_settable(L,-5);
8364 lua_pop(L,1);
8365 }
8366 return 1;
8367 }
8368
8369 /* maybe we should allocate a proper index 0..var_mem_max but not now */
8370
8371 static int lua_nodelib_get_property(lua_State * L)
8372 { /* <node> */
8373 halfword n = *((halfword *) lua_touserdata(L, 1));
8374 if (n == null) {
8375 lua_pushnil(L);
8376 } else {
8377 lua_get_metatablelua(node_properties);
8378 lua_rawgeti(L,-1,n);
8379 }
8380 return 1;
8381 }
8382
8383 static int lua_nodelib_direct_get_property(lua_State * L)
8384 { /* <direct> */
8385 halfword n = lua_tointeger(L, 1);
8386 if (n == null) {
8387 lua_pushnil(L);
8388 } else {
8389 lua_get_metatablelua(node_properties);
8390 lua_rawgeti(L,-1,n);
8391 }
8392 return 1;
8393 }
8394
8395 static int lua_nodelib_set_property(lua_State * L)
8396 {
8397 /* <node> <value> */
8398 halfword n = *((halfword *) lua_touserdata(L, 1));
8399 if (n != null) {
8400 lua_settop(L,2);
8401 lua_get_metatablelua(node_properties);
8402 /* <node> <value> <propertytable> */
8403 lua_replace(L,-3);
8404 /* <propertytable> <value> */
8405 lua_rawseti(L,-2,n);
8406 }
8407 return 0;
8408 }
8409
8410 static int lua_nodelib_direct_set_property(lua_State * L)
8411 {
8412 /* <direct> <value> */
8413 halfword n = lua_tointeger(L, 1);
8414 if (n != null) {
8415 lua_settop(L,2);
8416 lua_get_metatablelua(node_properties);
8417 /* <node> <value> <propertytable> */
8418 lua_replace(L,1);
8419 /* <propertytable> <value> */
8420 lua_rawseti(L,1,n);
8421 }
8422 return 0;
8423 }
8424
8425 static int lua_nodelib_direct_properties_get_table(lua_State * L)
8426 { /* <node|direct> */
8427 lua_get_metatablelua(node_properties);
8428 return 1;
8429 }
8430
8431 static int lua_nodelib_properties_get_table(lua_State * L)
8432 { /* <node|direct> */
8433 lua_get_metatablelua(node_properties_indirect);
8434 return 1;
8435 }
8436
8437 /* bonus */
8438
8439 static int lua_nodelib_get_property_t(lua_State * L)
8440 { /* <table> <node> */
8441 halfword n = *((halfword *) lua_touserdata(L, 2));
8442 if (n != null) {
8443 lua_get_metatablelua(node_properties);
8444 lua_rawgeti(L, -1, n);
8445 } else {
8446 lua_pushnil(L);
8447 }
8448 return 1;
8449 }
8450
8451 static int lua_nodelib_set_property_t(lua_State * L)
8452 {
8453 /* <table> <node> <value> */
8454 halfword n = *((halfword *) lua_touserdata(L, 2));
8455 if (n != null) {
8456 lua_get_metatablelua(node_properties);
8457 lua_insert(L, -2);
8458 lua_rawseti(L, -2, n);
8459 }
8460 return 0;
8461 }
8462
8463 /* extra helpers */
8464
8465 static int lua_nodelib_effective_glue(lua_State * L)
8466 {
8467 halfword *glue;
8468 halfword *parent;
8469 glue = lua_touserdata(L, 1);
8470 if ((glue == NULL) || (type(*glue) != glue_node)) {
8471 lua_pushnil(L) ;
8472 } else {
8473 parent = lua_touserdata(L, 2);
8474 if ((parent != NULL) && ((type(*parent) == hlist_node) || (type(*parent) == vlist_node))) {
8475 double w = width(*glue) ;
8476 if ((int) glue_sign(*parent) == 1) {
8477 if (stretch_order(*glue) == glue_order(*parent)) {
8478 w += stretch(*glue) * (double) glue_set(*parent);
8479 }
8480 } else if (glue_sign(*parent) == 2) {
8481 if (shrink_order(*glue) == glue_order(*parent)) {
8482 w -= shrink(*glue) * (double) glue_set(*parent);
8483 }
8484 }
8485 if (lua_toboolean(L,3)) {
8486 lua_pushinteger(L,round(w));
8487 } else {
8488 lua_pushnumber(L,w);
8489 }
8490 } else {
8491 lua_pushinteger(L,width(*glue));
8492 }
8493 }
8494 return 1;
8495 }
8496
8497 static int lua_nodelib_direct_effective_glue(lua_State * L)
8498 {
8499 halfword glue = lua_tointeger(L, 1);
8500 if ((glue == null) || (type(glue) != glue_node)) {
8501 lua_pushnil(L) ;
8502 } else {
8503 halfword parent = lua_tointeger(L, 2);
8504 if ((parent != null) && ((type(parent) == hlist_node) || (type(parent) == vlist_node))) {
8505 double w = (double) width(glue) ;
8506 if ((int)glue_sign(parent) == 1) {
8507 if (stretch_order(glue) == glue_order(parent)) {
8508 w += stretch(glue) * (double) glue_set(parent);
8509 }
8510 } else if (glue_sign(parent) == 2) {
8511 if (shrink_order(glue) == glue_order(parent)) {
8512 w -= shrink(glue) * (double) glue_set(parent);
8513 }
8514 }
8515 if (lua_toboolean(L,3)) {
8516 lua_pushinteger(L,round(w));
8517 } else {
8518 lua_pushnumber(L,w);
8519 }
8520 } else {
8521 lua_pushinteger(L,width(glue));
8522 }
8523 }
8524 return 1;
8525 }
8526
8527 /*
8528 Disc nodes are kind of special in the sense that their head is not the head as we
8529 see it, but a special node that has status info of which head and tail are part.
8530 Normally when proper set/get functions are used this status node is all right but
8531 if a macro package permits arbitrary messing around, then it can at some point
8532 call the following cleaner, just before linebreaking kicks in. This one is not
8533 called automatically because if significantly slows down the line break routing.
8534
8535 */
8536
8537 static void check_disc(halfword p)
8538 {
8539 if (p != null) {
8540 if (vlink(p) != null) {
8541 tlink(p) = tail_of_list(vlink(p));
8542 } else {
8543 tlink(p) = null;
8544 }
8545 }
8546 }
8547
8548 static int lua_nodelib_direct_check_discretionaries(lua_State * L) {
8549 halfword c = lua_tointeger(L, 1);
8550 while (c != null) {
8551 if (type(c) == disc_node) {
8552 check_disc(no_break(c));
8553 check_disc(pre_break(c));
8554 check_disc(post_break(c));
8555 }
8556 c = vlink(c) ;
8557 }
8558 return 0;
8559 }
8560
8561 static int lua_nodelib_direct_check_discretionary(lua_State * L) {
8562 halfword c = lua_tointeger(L, 1);
8563 if (c != null && type(c) == disc_node) {
8564 check_disc(no_break(c));
8565 check_disc(pre_break(c));
8566 check_disc(post_break(c));
8567 }
8568 return 0;
8569 }
8570
8571 static int lua_nodelib_direct_flatten_discretionaries(lua_State * L)
8572 {
8573 halfword head = lua_tointeger(L, 1);
8574 halfword current = head;
8575 halfword next;
8576 halfword d, n, h, t;
8577 int c = 0;
8578 while (current != null) {
8579 next = vlink(current);
8580 if (type(current) == disc_node) {
8581 ++c;
8582 d = current;
8583 n = no_break(d);
8584 h = vlink(n);
8585 t = tlink(n);
8586 if (h != null) {
8587 try_couple_nodes(t,next);
8588 if (current == head) {
8589 head = h;
8590 } else {
8591 try_couple_nodes(alink(current),h);
8592 }
8593 vlink(n) = null ;
8594 /*tlink(n) = null; */
8595 } else {
8596 if (current == head) {
8597 head = next;
8598 } else {
8599 try_couple_nodes(alink(current),next);
8600 }
8601 }
8602 flush_node(d);
8603 }
8604 current = next;
8605 }
8606 nodelib_pushdirect_or_nil(head);
8607 lua_pushinteger(L,c);
8608 return 2;
8609 }
8610
8611 static int lua_nodelib_check_discretionaries(lua_State * L) {
8612 halfword c = *check_isnode(L, 1);
8613 while (c != null) {
8614 if (type(c) == disc_node) {
8615 check_disc(no_break(c));
8616 check_disc(pre_break(c));
8617 check_disc(post_break(c));
8618 }
8619 c = vlink(c) ;
8620 }
8621 return 0;
8622 }
8623
8624 static int lua_nodelib_check_discretionary(lua_State * L) {
8625 halfword c = *check_isnode(L, 1);
8626 if (c != null && type(c) == disc_node) {
8627 check_disc(no_break(c));
8628 check_disc(pre_break(c));
8629 check_disc(post_break(c));
8630 }
8631 return 0;
8632 }
8633
8634 static int lua_nodelib_flatten_discretionaries(lua_State * L)
8635 {
8636 halfword head = *check_isnode(L, 1);
8637 halfword current = head;
8638 halfword next;
8639 halfword d, n, h, t;
8640 halfword *a;
8641 int c = 0;
8642 while (current != null) {
8643 next = vlink(current);
8644 if (type(current) == disc_node) {
8645 ++c;
8646 d = current;
8647 n = no_break(d);
8648 h = vlink(n);
8649 t = tlink(n);
8650 if (h != null) {
8651 try_couple_nodes(t,next);
8652 if (current == head) {
8653 head = h;
8654 } else {
8655 try_couple_nodes(alink(current),h);
8656 }
8657 vlink(n) = null ;
8658 /*tlink(n) = null; */
8659 } else {
8660 if (current == head) {
8661 head = next;
8662 } else {
8663 try_couple_nodes(alink(current),next);
8664 }
8665 }
8666 flush_node(d);
8667 }
8668 current = next;
8669 }
8670 fast_metatable_or_nil(head);
8671 lua_pushinteger(L,c);
8672 return 2;
8673 }
8674
8675 /* synctex but not */
8676
8677 static int lua_nodelib_direct_set_synctex_fields(lua_State * L)
8678 {
8679 halfword n = lua_tointeger(L, 1);
8680 halfword tag = lua_tointeger(L, 2);
8681 halfword line = lua_tointeger(L, 3);
8682 if (n != null) {
8683 switch (type(n)) {
8684 case glyph_node:
8685 if (tag) synctex_tag_glyph(n) = tag;
8686 if (line) synctex_line_glyph(n) = line;
8687 break;
8688 case glue_node:
8689 if (tag) synctex_tag_glue(n) = tag;
8690 if (line) synctex_line_glue(n) = line;
8691 break;
8692 case kern_node:
8693 if (tag) synctex_tag_kern(n) = tag;
8694 if (line) synctex_line_kern(n) = line;
8695 break;
8696 case hlist_node:
8697 case vlist_node:
8698 case unset_node:
8699 if (tag) synctex_tag_box(n) = tag;
8700 if (line) synctex_line_box(n) = line;
8701 break;
8702 case rule_node:
8703 if (tag) synctex_tag_rule(n) = tag;
8704 if (line) synctex_line_rule(n) = line;
8705 break;
8706 case math_node:
8707 if (tag) synctex_tag_math(n) = tag;
8708 if (line) synctex_line_math(n) = line;
8709 break;
8710 }
8711 }
8712 return 0;
8713 }
8714
8715 static int lua_nodelib_direct_get_synctex_fields(lua_State * L)
8716 {
8717 halfword n = lua_tointeger(L, 1);
8718 if (n != null) {
8719 switch (type(n)) {
8720 case glyph_node:
8721 lua_pushinteger(L,synctex_tag_glyph(n));
8722 lua_pushinteger(L,synctex_line_glyph(n));
8723 break;
8724 case glue_node:
8725 lua_pushinteger(L,synctex_tag_glue(n));
8726 lua_pushinteger(L,synctex_line_glue(n));
8727 break;
8728 case kern_node:
8729 lua_pushinteger(L,synctex_tag_kern(n));
8730 lua_pushinteger(L,synctex_line_kern(n));
8731 break;
8732 case hlist_node:
8733 case vlist_node:
8734 case unset_node:
8735 lua_pushinteger(L,synctex_tag_box(n));
8736 lua_pushinteger(L,synctex_line_box(n));
8737 break;
8738 case rule_node:
8739 lua_pushinteger(L,synctex_tag_rule(n));
8740 lua_pushinteger(L,synctex_line_rule(n));
8741 break;
8742 case math_node:
8743 lua_pushinteger(L,synctex_tag_math(n));
8744 lua_pushinteger(L,synctex_line_math(n));
8745 break;
8746 default:
8747 return 0;
8748 break;
8749 }
8750 return 2;
8751 }
8752 return 0;
8753 }
8754
8755 /* helper, assumes one node, returns node and delta .. to be tested */
8756
8757 static int lua_nodelib_prepend_prevdepth(lua_State * L)
8758 {
8759 halfword *a;
8760 halfword p;
8761 halfword prevdepth;
8762 boolean mirrored;
8763 halfword n = *check_isnode(L, 1);
8764 if (!(type(n) == hlist_node || type(n) == vlist_node)) {
8765 lua_pushnil(L);
8766 return 1;
8767 }
8768 prevdepth = lua_tointeger(L,2);
8769 mirrored = (type(n) == hlist_node) && is_mirrored(box_dir(n)) ;
8770 if (prevdepth > ignore_depth) {
8771 halfword d;
8772 if (mirrored) {
8773 d = width(baseline_skip_par) - prevdepth - depth(n);
8774 } else {
8775 d = width(baseline_skip_par) - prevdepth - height(n);
8776 }
8777 if (d < line_skip_limit_par) {
8778 p = new_param_glue(line_skip_code);
8779 } else {
8780 p = new_skip_param(baseline_skip_code);
8781 width(p) = d;
8782 }
8783 couple_nodes(p,n);
8784 fast_metatable_or_nil(p); /* glue */
8785 } else {
8786 fast_metatable_or_nil(n); /* node */
8787 }
8788 if (mirrored) {
8789 prevdepth = height(n);
8790 } else {
8791 prevdepth = depth(n);
8792 }
8793 lua_pushinteger(L,prevdepth); /* new prevdepth */
8794 return 2;
8795 }
8796
8797 static int lua_nodelib_direct_prepend_prevdepth(lua_State * L)
8798 {
8799 halfword p;
8800 halfword prevdepth;
8801 boolean mirrored;
8802 halfword n = lua_tointeger(L, 1);
8803 if (type(n) == hlist_node || type(n) == vlist_node) {
8804 lua_pushnil(L);
8805 return 1;
8806 }
8807 prevdepth = lua_tointeger(L,2);
8808 mirrored = (type(n) == hlist_node) && is_mirrored(box_dir(n)) ;
8809 if (prevdepth > ignore_depth) {
8810 halfword d;
8811 if (mirrored) {
8812 d = width(baseline_skip_par) - prevdepth - depth(n);
8813 } else {
8814 d = width(baseline_skip_par) - prevdepth - height(n);
8815 }
8816 if (d < line_skip_limit_par) {
8817 p = new_param_glue(line_skip_code);
8818 } else {
8819 p = new_skip_param(baseline_skip_code);
8820 width(p) = d;
8821 }
8822 couple_nodes(p,n);
8823 lua_pushinteger(L,p); /* glue */
8824 } else {
8825 lua_pushinteger(L,n); /* node */
8826 }
8827 if (mirrored) {
8828 prevdepth = height(n);
8829 } else {
8830 prevdepth = depth(n);
8831 }
8832 lua_pushinteger(L,prevdepth); /* new prevdepth */
8833 return 2;
8834 }
8835
8836 static int lua_nodelib_make_extensible(lua_State * L)
8837 {
8838 int top = lua_gettop(L);
8839 if (top >= 3) {
8840 halfword fnt = lua_tointeger(L,1);
8841 halfword chr = lua_tointeger(L,2);
8842 halfword size = lua_tointeger(L,3);
8843 halfword overlap = 65536 ;
8844 halfword attlist = null;
8845 halfword b = null;
8846 int horizontal = 0;
8847 if (top >= 4) {
8848 overlap = lua_tointeger(L,4);
8849 }
8850 if (top >= 5) {
8851 horizontal = lua_toboolean(L,5);
8852 }
8853 if (top >= 6) {
8854 attlist = *check_isnode(L, 6);
8855 }
8856 b = make_extensible(fnt,chr,size,overlap,horizontal,attlist);
8857 nodelib_pushlist(L,b);
8858 } else {
8859 lua_pushnil(L);
8860 }
8861 return 1;
8862 }
8863
8864 /* done */
8865
8866 static const struct luaL_Reg nodelib_p[] = {
8867 {"__index", lua_nodelib_get_property_t},
8868 {"__newindex", lua_nodelib_set_property_t},
8869 {NULL, NULL} /* sentinel */
8870 };
8871
8872 static void lua_new_properties_table(lua_State * L)
8873 {
8874 lua_pushstring(L,"node.properties");
8875 lua_newtable(L);
8876 lua_settable(L,LUA_REGISTRYINDEX);
8877
8878 lua_pushstring(L,"node.properties.indirect");
8879 lua_newtable(L);
8880 luaL_newmetatable(L,"node.properties.indirect.meta");
8881 luaL_openlib(L, NULL, nodelib_p, 0);
8882 lua_setmetatable(L,-2);
8883 lua_settable(L,LUA_REGISTRYINDEX);
8884 }
8885
8886 /* node.direct.* */
8887
8888 static const struct luaL_Reg direct_nodelib_f[] = {
8889 {"copy", lua_nodelib_direct_copy},
8890 {"copy_list", lua_nodelib_direct_copy_list},
8891 {"count", lua_nodelib_direct_count},
8892 {"current_attr", lua_nodelib_direct_currentattr},
8893 {"dimensions", lua_nodelib_direct_dimensions},
8894 {"rangedimensions", lua_nodelib_direct_rangedimensions},
8895 /* {"do_ligature_n", lua_nodelib_direct_do_ligature_n}, */
8896 {"end_of_math", lua_nodelib_direct_end_of_math},
8897 /* {"family_font", lua_nodelib_mfont}, */ /* no node argument */
8898 /* {"fields", lua_nodelib_fields}, */ /* no node argument */
8899 {"first_glyph", lua_nodelib_direct_first_glyph},
8900 {"flush_list", lua_nodelib_direct_flush_list},
8901 {"flush_node", lua_nodelib_direct_flush_node},
8902 {"free", lua_nodelib_direct_free},
8903 {"getbox", lua_nodelib_direct_getbox},
8904 {"getchar", lua_nodelib_direct_getchar},
8905 {"getcomponents", lua_nodelib_direct_getcomponents},
8906 {"getlang", lua_nodelib_direct_getlang},
8907 {"getkern", lua_nodelib_direct_getkern},
8908 {"getpenalty", lua_nodelib_direct_getpenalty},
8909 {"getdir", lua_nodelib_direct_getdir},
8910 {"getoffsets", lua_nodelib_direct_getoffsets},
8911 {"getdisc", lua_nodelib_direct_getdisc},
8912 {"getwhd", lua_nodelib_direct_getwhd},
8913 {"getwidth", lua_nodelib_direct_getwidth},
8914 {"getheight", lua_nodelib_direct_getheight},
8915 {"getdepth", lua_nodelib_direct_getdepth},
8916 {"getshift", lua_nodelib_direct_getshift},
8917 {"getfield", lua_nodelib_direct_getfield},
8918 {"getfont", lua_nodelib_direct_getfont},
8919 {"getexpansion", lua_nodelib_direct_getexpansion},
8920 {"getfam", lua_nodelib_direct_getfam},
8921 {"getid", lua_nodelib_direct_getid},
8922 {"getnext", lua_nodelib_direct_getnext},
8923 {"getprev", lua_nodelib_direct_getprev},
8924 {"getboth", lua_nodelib_direct_getboth},
8925 {"getlist", lua_nodelib_direct_getlist},
8926 {"getleader", lua_nodelib_direct_getleader},
8927 {"getdata", lua_nodelib_direct_getdata},
8928 {"getsubtype", lua_nodelib_direct_getsubtype},
8929 {"getattributelist", lua_nodelib_direct_getattributelist},
8930 {"getnucleus", lua_nodelib_direct_getnucleus},
8931 {"getsub", lua_nodelib_direct_getsub},
8932 {"getsup", lua_nodelib_direct_getsup},
8933 {"getdirection", lua_nodelib_direct_getdirection},
8934 {"has_glyph", lua_nodelib_direct_has_glyph},
8935 {"has_attribute", lua_nodelib_direct_has_attribute},
8936 {"get_attribute", lua_nodelib_direct_get_attribute},
8937 {"find_attribute", lua_nodelib_direct_find_attribute},
8938 {"has_field", lua_nodelib_direct_has_field},
8939 {"is_char", lua_nodelib_direct_is_char},
8940 {"is_glyph", lua_nodelib_direct_is_glyph},
8941 {"uses_font", lua_nodelib_direct_uses_font},
8942 {"hpack", lua_nodelib_direct_hpack},
8943 {"hyphenating", lang_tex_direct_hyphenating},
8944 /* {"id", lua_nodelib_id}, */ /* no node argument */
8945 {"insert_after", lua_nodelib_direct_insert_after},
8946 {"insert_before", lua_nodelib_direct_insert_before},
8947 {"is_direct", lua_nodelib_direct_is_direct},
8948 {"is_node", lua_nodelib_direct_is_node},
8949 {"kerning", font_tex_direct_kerning},
8950 {"last_node", lua_nodelib_direct_last_node},
8951 {"length", lua_nodelib_direct_length},
8952 {"ligaturing", font_tex_direct_ligaturing},
8953 /* {"mlist_to_hlist", lua_nodelib_mlist_to_hset_properties_modelist}, */
8954 {"new", lua_nodelib_direct_new},
8955 {"tostring", lua_nodelib_direct_tostring},
8956 {"protect_glyphs", lua_nodelib_direct_protect_glyphs},
8957 {"protect_glyph", lua_nodelib_direct_protect_glyph},
8958 {"protrusion_skippable", lua_nodelib_direct_cp_skipable},
8959 {"remove", lua_nodelib_direct_remove},
8960 {"set_attribute", lua_nodelib_direct_set_attribute},
8961 {"setbox", lua_nodelib_direct_setbox},
8962 {"setfield", lua_nodelib_direct_setfield},
8963 {"setchar", lua_nodelib_direct_setchar},
8964 {"setfont", lua_nodelib_direct_setfont},
8965 {"setexpansion", lua_nodelib_direct_setexpansion},
8966 {"setfam", lua_nodelib_direct_setfam},
8967 {"setcomponents", lua_nodelib_direct_setcomponents},
8968 {"setlang", lua_nodelib_direct_setlang},
8969 {"setkern", lua_nodelib_direct_setkern},
8970 {"setpenalty", lua_nodelib_direct_setpenalty},
8971 {"setdir", lua_nodelib_direct_setdir},
8972 {"setdirection", lua_nodelib_direct_setdirection},
8973 {"setoffsets", lua_nodelib_direct_setoffsets},
8974 {"setdisc", lua_nodelib_direct_setdisc},
8975 {"setwhd", lua_nodelib_direct_setwhd},
8976 {"setwidth", lua_nodelib_direct_setwidth},
8977 {"setheight", lua_nodelib_direct_setheight},
8978 {"setdepth", lua_nodelib_direct_setdepth},
8979 {"setshift", lua_nodelib_direct_setshift},
8980 {"setnext", lua_nodelib_direct_setnext},
8981 {"setprev", lua_nodelib_direct_setprev},
8982 {"setboth", lua_nodelib_direct_setboth},
8983 {"setlink", lua_nodelib_direct_setlink},
8984 {"setsplit", lua_nodelib_direct_setsplit},
8985 {"setlist", lua_nodelib_direct_setlist},
8986 {"setleader", lua_nodelib_direct_setleader},
8987 {"setdata", lua_nodelib_direct_setdata},
8988 {"setsubtype", lua_nodelib_direct_setsubtype},
8989 {"setattributelist", lua_nodelib_direct_setattributelist},
8990 {"setnucleus", lua_nodelib_direct_setnucleus},
8991 {"setsub", lua_nodelib_direct_setsub},
8992 {"setsup", lua_nodelib_direct_setsup},
8993 {"slide", lua_nodelib_direct_slide},
8994 /* {"subtype", lua_nodelib_subtype}, */ /* no node argument */
8995 {"tail", lua_nodelib_direct_tail},
8996 {"todirect", lua_nodelib_direct_todirect},
8997 {"tonode", lua_nodelib_direct_tonode},
8998 {"traverse", lua_nodelib_direct_traverse},
8999 {"traverse_id", lua_nodelib_direct_traverse_filtered},
9000 {"traverse_char", lua_nodelib_direct_traverse_char},
9001 {"traverse_glyph", lua_nodelib_direct_traverse_glyph},
9002 {"traverse_list", lua_nodelib_direct_traverse_list},
9003 /* {"type", lua_nodelib_type}, */ /* no node argument */
9004 /* {"types", lua_nodelib_types}, */ /* no node argument */
9005 {"unprotect_glyphs", lua_nodelib_direct_unprotect_glyphs},
9006 {"unprotect_glyph", lua_nodelib_direct_unprotect_glyph},
9007 {"unset_attribute", lua_nodelib_direct_unset_attribute},
9008 {"setglue",lua_nodelib_direct_set_glue},
9009 {"getglue",lua_nodelib_direct_get_glue},
9010 {"is_zero_glue",lua_nodelib_direct_is_zero_glue},
9011 {"usedlist", lua_nodelib_direct_usedlist},
9012 {"vpack", lua_nodelib_direct_vpack},
9013 /* {"whatsits", lua_nodelib_whatsits}, */ /* no node argument */
9014 {"write", lua_nodelib_direct_append},
9015 {"set_properties_mode",lua_nodelib_properties_set_mode},
9016 {"flush_properties_table",lua_nodelib_properties_flush_table},
9017 {"get_properties_table",lua_nodelib_direct_properties_get_table},
9018 {"getproperty", lua_nodelib_direct_get_property},
9019 {"setproperty", lua_nodelib_direct_set_property},
9020 {"effective_glue", lua_nodelib_direct_effective_glue},
9021 {"check_discretionary", lua_nodelib_direct_check_discretionary},
9022 {"check_discretionaries", lua_nodelib_direct_check_discretionaries},
9023 {"flatten_discretionaries",lua_nodelib_direct_flatten_discretionaries},
9024 {"prepend_prevdepth",lua_nodelib_direct_prepend_prevdepth},
9025 /* done */
9026 {"set_synctex_fields", lua_nodelib_direct_set_synctex_fields},
9027 {"get_synctex_fields", lua_nodelib_direct_get_synctex_fields},
9028 /* done */
9029 {NULL, NULL} /* sentinel */
9030 };
9031
9032 /* node.* */
9033
9034 static const struct luaL_Reg nodelib_f[] = {
9035 {"copy", lua_nodelib_copy},
9036 {"copy_list", lua_nodelib_copy_list},
9037 {"count", lua_nodelib_count},
9038 {"current_attr", lua_nodelib_currentattr},
9039 {"dimensions", lua_nodelib_dimensions},
9040 {"rangedimensions", lua_nodelib_rangedimensions},
9041 /* {"do_ligature_n", lua_nodelib_do_ligature_n}, */
9042 {"end_of_math", lua_nodelib_end_of_math},
9043 {"family_font", lua_nodelib_mfont},
9044 {"fields", lua_nodelib_fields},
9045 {"subtypes", lua_nodelib_subtypes},
9046 {"values", lua_nodelib_values},
9047 {"first_glyph", lua_nodelib_first_glyph},
9048 {"flush_list", lua_nodelib_flush_list},
9049 {"flush_node", lua_nodelib_flush_node},
9050 {"free", lua_nodelib_free},
9051 /* {"getbox", lua_nodelib_getbox}, */ /* tex.getbox */
9052 {"getnext", lua_nodelib_getnext},
9053 {"getprev", lua_nodelib_getprev},
9054 {"getboth", lua_nodelib_getboth},
9055 {"getdisc", lua_nodelib_getdisc},
9056 {"getwhd", lua_nodelib_getwhd},
9057 {"getlist", lua_nodelib_getlist},
9058 {"getleader", lua_nodelib_getleader},
9059 {"getid", lua_nodelib_getid},
9060 {"getsubtype", lua_nodelib_getsubtype},
9061 {"getfont", lua_nodelib_getfont},
9062 {"getchar", lua_nodelib_getchar},
9063 {"getfield", lua_nodelib_getfield},
9064 {"setfield", lua_nodelib_setfield},
9065 {"has_glyph", lua_nodelib_has_glyph},
9066 {"has_attribute", lua_nodelib_has_attribute},
9067 {"get_attribute", lua_nodelib_get_attribute},
9068 {"find_attribute", lua_nodelib_find_attribute},
9069 {"has_field", lua_nodelib_has_field},
9070 {"is_char", lua_nodelib_is_char},
9071 {"is_glyph", lua_nodelib_is_glyph},
9072 {"uses_font", lua_nodelib_uses_font},
9073 {"hpack", lua_nodelib_hpack},
9074 {"hyphenating", lang_tex_hyphenating},
9075 {"id", lua_nodelib_id},
9076 {"insert_after", lua_nodelib_insert_after},
9077 {"insert_before", lua_nodelib_insert_before},
9078 {"is_node", lua_nodelib_is_node},
9079 {"kerning", font_tex_kerning},
9080 {"last_node", lua_nodelib_last_node},
9081 {"length", lua_nodelib_length},
9082 {"ligaturing", font_tex_ligaturing},
9083 {"mlist_to_hlist", lua_nodelib_mlist_to_hlist},
9084 {"new", lua_nodelib_new},
9085 {"next", lua_nodelib_next}, /* getnext is somewhat more efficient, and get* fits better in direct compatibility */
9086 {"prev", lua_nodelib_prev}, /* getprev is somewhat more efficient, and get* fits better in direct compatibility */
9087 {"tostring", lua_nodelib_tostring},
9088 {"protect_glyphs", lua_nodelib_protect_glyphs},
9089 {"protect_glyph", lua_nodelib_protect_glyph},
9090 {"protrusion_skippable", lua_nodelib_cp_skipable},
9091 {"remove", lua_nodelib_remove},
9092 /* {"setbox", lua_nodelib_setbox}, */ /* tex.setbox */
9093 {"set_attribute", lua_nodelib_set_attribute},
9094 {"slide", lua_nodelib_slide},
9095 {"subtype", lua_nodelib_subtype},
9096 {"tail", lua_nodelib_tail},
9097 {"traverse", lua_nodelib_traverse},
9098 {"traverse_id", lua_nodelib_traverse_filtered},
9099 {"traverse_char", lua_nodelib_traverse_char},
9100 {"traverse_glyph", lua_nodelib_traverse_glyph},
9101 {"traverse_list", lua_nodelib_traverse_list},
9102 {"type", lua_nodelib_type},
9103 {"types", lua_nodelib_types},
9104 {"unprotect_glyphs", lua_nodelib_unprotect_glyphs},
9105 {"unprotect_glyph", lua_nodelib_unprotect_glyph},
9106 {"unset_attribute", lua_nodelib_unset_attribute},
9107 {"setglue", lua_nodelib_set_glue},
9108 {"getglue", lua_nodelib_get_glue},
9109 {"is_zero_glue", lua_nodelib_is_zero_glue},
9110 {"usedlist", lua_nodelib_usedlist},
9111 {"vpack", lua_nodelib_vpack},
9112 {"whatsits", lua_nodelib_whatsits},
9113 {"write", lua_nodelib_append},
9114 /* experiment */
9115 /* {"attributes_to_table",lua_nodelib_attributes_to_table}, */
9116 /* experiment */
9117 {"set_properties_mode", lua_nodelib_properties_set_mode},
9118 {"flush_properties_table", lua_nodelib_properties_flush_table},
9119 {"get_properties_table", lua_nodelib_properties_get_table},
9120 {"getproperty", lua_nodelib_get_property},
9121 {"setproperty", lua_nodelib_set_property},
9122 {"effective_glue", lua_nodelib_effective_glue},
9123 {"check_discretionary", lua_nodelib_check_discretionary},
9124 {"check_discretionaries", lua_nodelib_check_discretionaries},
9125 {"flatten_discretionaries", lua_nodelib_flatten_discretionaries},
9126 {"prepend_prevdepth", lua_nodelib_prepend_prevdepth},
9127 {"make_extensible", lua_nodelib_make_extensible},
9128 /* done */
9129 {"fix_node_lists", lua_nodelib_fix_node_lists},
9130 {NULL, NULL} /* sentinel */
9131 };
9132
9133 static const struct luaL_Reg nodelib_m[] = {
9134 {"__index", lua_nodelib_fast_getfield},
9135 {"__newindex", lua_nodelib_fast_setfield},
9136 {"__tostring", lua_nodelib_tostring},
9137 {"__eq", lua_nodelib_equal},
9138 {NULL, NULL} /* sentinel */
9139 };
9140
9141 int luaopen_node(lua_State * L)
9142 {
9143
9144 lua_new_properties_table(L);
9145
9146 /* the main metatable of node userdata */
9147 luaL_newmetatable(L, NODE_METATABLE);
9148 /* node.* */
9149 luaL_openlib(L, NULL, nodelib_m, 0);
9150 luaL_openlib(L, "node", nodelib_f, 0);
9151 /* node.direct */
9152 lua_pushstring(L,"direct");
9153 lua_newtable(L);
9154 luaL_openlib(L, NULL, direct_nodelib_f, 0);
9155 lua_rawset(L,-3);
9156 return 1;
9157 }
9158
9159 void nodelist_to_lua(lua_State * L, int n)
9160 {
9161 lua_pushinteger(L, n);
9162 lua_nodelib_push(L);
9163 }
9164
9165 int nodelist_from_lua(lua_State * L, int n)
9166 {
9167 if (lua_isnil(L, n)) {
9168 return null;
9169 } else {
9170 halfword list = *check_isnode(L, n);
9171 return (list ? list : null);
9172 }
9173 }