summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/luatexdir/lua/luanode.c
blob: a0c97e37dc84f33db62feacce426962431c07a04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*

luanode.w

Copyright 2006-2008 Taco Hoekwater <taco@@luatex.org>

This file is part of LuaTeX.

LuaTeX is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.

LuaTeX is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU General Public License along
with LuaTeX; if not, see <http://www.gnu.org/licenses/>.

*/

#include "ptexlib.h"
#include "lua/luatex-api.h"

void lua_node_filter_s(int filterid, int extrainfo)
{
    int i;
    int callback_id = callback_defined(filterid);
    int s_top = lua_gettop(Luas);
    if (callback_id <= 0) {
        lua_settop(Luas, s_top);
        return;
    }
    if (!get_callback(Luas, callback_id)) {
        lua_settop(Luas, s_top);
        return;
    }
    lua_push_string_by_index(Luas,extrainfo);
    if ((i=lua_pcall(Luas, 1, 0, 0)) != 0) {
        formatted_warning("node filter","error: %s", lua_tostring(Luas, -1));
        lua_settop(Luas, s_top);
        luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
        return;
    }
    lua_settop(Luas, s_top);
    return;
}

void lua_node_filter(int filterid, int extrainfo, halfword head_node, halfword * tail_node)
{
    int i;
    halfword start_node, start_done, last_node;
    int s_top = lua_gettop(Luas);
    int callback_id = callback_defined(filterid);
    if (head_node == null || callback_id <= 0) {
        lua_settop(Luas, s_top);
        return;
    }
    /*tex We start after head. */
    start_node = vlink(head_node);
    if (start_node == null || !get_callback(Luas, callback_id)) {
        lua_settop(Luas, s_top);
        return;
    }
    /*tex We make sure we have no prev */
    alink(start_node) = null ;
    /*tex the action */
    nodelist_to_lua(Luas, start_node);
    lua_push_group_code(Luas,extrainfo);
    if ((i=lua_pcall(Luas, 2, 1, 0)) != 0) {
        formatted_warning("node filter", "error: %s\n", lua_tostring(Luas, -1));
        lua_settop(Luas, s_top);
        luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
        return;
    }
    /*tex the result */
    if (lua_isboolean(Luas, -1)) {
        if (lua_toboolean(Luas, -1) != 1) {
            /*tex discard */
            flush_node_list(start_node);
            vlink(head_node) = null;
        } else {
            /*tex keep */
        }
    } else {
        /*tex append to old head */
        start_done = nodelist_from_lua(Luas,-1);
        try_couple_nodes(head_node,start_done);
    }
    /*tex redundant as we set top anyway */
    lua_pop(Luas, 2);
    /*tex find tail in order to update tail */
    start_node = vlink(head_node);
    if (start_node != null) {
        /*tex maybe just always slide (harmless and fast) */
        if (fix_node_lists) {
            /*tex slides and returns last node */
            *tail_node = fix_node_list(start_node);
        } else {
            last_node = vlink(start_node);
            while (last_node != null) {
                start_node = last_node;
                last_node = vlink(start_node);
            }
            /*tex we're at the end now */
            *tail_node = start_node;
        }
    } else {
        /*tex we're already at the end */
        *tail_node = head_node;
    }
    /*tex clean up */
    lua_settop(Luas, s_top);
    return;
}

int lua_linebreak_callback(int is_broken, halfword head_node, halfword * new_head)
{
    int a, i;
    register halfword *p;
    int ret = 0;
    int s_top = lua_gettop(Luas);
    int callback_id = callback_defined(linebreak_filter_callback);
    if (head_node == null || vlink(head_node) == null || callback_id <= 0) {
        lua_settop(Luas, s_top);
        return ret;
    }
    if (!get_callback(Luas, callback_id)) {
        lua_settop(Luas, s_top);
        return ret;
    }
    alink(vlink(head_node)) = null ;
    nodelist_to_lua(Luas, vlink(head_node));
    lua_pushboolean(Luas, is_broken);
    if ((i=lua_pcall(Luas, 2, 1, 0)) != 0) {
        formatted_warning("linebreak", "error: %s", lua_tostring(Luas, -1));
        lua_settop(Luas, s_top);
        luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
        return ret;
    }
    /*tex This should prevent a stack overflow (see r6689), */
    /*tex but as side effect it discards the ouput */
    /*tex of the linebreak_filter, see [Dev-luatex] linebreak_filter */
    /*tex lua_settop(Luas, s_top);*/
    p = lua_touserdata(Luas, -1);
    if (p != NULL) {
        a = nodelist_from_lua(Luas,-1);
        try_couple_nodes(*new_head,a);
        ret = 1;
    }
    /*tex re-inserted this line */
    lua_settop(Luas, s_top);
    return ret;
}

int lua_appendtovlist_callback(halfword box, int location, halfword prev_depth,
    boolean is_mirrored, halfword * result, int * next_depth, boolean * prev_set)
{
    register halfword *p;
    int i;
    int s_top = lua_gettop(Luas);
    int callback_id = callback_defined(append_to_vlist_filter_callback);
    if (box == null || callback_id <= 0) {
        lua_settop(Luas, s_top);
        return 0;
    }
    if (!get_callback(Luas, callback_id)) {
        lua_settop(Luas, s_top);
        return 0;
    }
    nodelist_to_lua(Luas, box);
    lua_push_string_by_index(Luas,location);
    lua_pushinteger(Luas, (int) prev_depth);
    lua_pushboolean(Luas, is_mirrored);
    if ((i=lua_pcall(Luas, 4, 2, 0)) != 0) {
        formatted_warning("append to vlist","error: %s", lua_tostring(Luas, -1));
        lua_settop(Luas, s_top);
        luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
        return 0;
    }
    if (lua_type(Luas,-1) == LUA_TNUMBER) {
        *next_depth = lua_roundnumber(Luas,-1);
        *prev_set = true;
        if (lua_type(Luas, -2) != LUA_TNIL) {
            p = check_isnode(Luas, -2);
            *result = *p;
        }
    } else if (lua_type(Luas, -1) != LUA_TNIL) {
        p = check_isnode(Luas, -1);
        *result = *p;
    }
    return 1;
}

halfword lua_hpack_filter(halfword head_node, scaled size, int pack_type, int extrainfo,
    int pack_direction, halfword attr)
{
    int i;
    halfword ret;
    int s_top = lua_gettop(Luas);
    int callback_id = callback_defined(hpack_filter_callback);
    if (head_node == null || callback_id <= 0) {
        lua_settop(Luas, s_top);
        return head_node;
    }
    if (!get_callback(Luas, callback_id)) {
        lua_settop(Luas, s_top);
        return head_node;
    }
    alink(head_node) = null ;
    nodelist_to_lua(Luas, head_node);
    lua_push_group_code(Luas,extrainfo);
    lua_pushinteger(Luas, size);
    lua_push_pack_type(Luas, pack_type);
    if (pack_direction >= 0) {
        lua_push_dir_par(Luas, pack_direction);
    } else {
        lua_pushnil(Luas);
    }
    if (attr != null) {
        nodelist_to_lua(Luas, attr);
    } else {
        lua_pushnil(Luas);
    }
    if ((i=lua_pcall(Luas, 6, 1, 0)) != 0) {
        formatted_warning("hpack filter", "error: %s\n", lua_tostring(Luas, -1));
        lua_settop(Luas, s_top);
        luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
        return head_node;
    }
    ret = head_node;
    if (lua_isboolean(Luas, -1)) {
        if (lua_toboolean(Luas, -1) != 1) {
            flush_node_list(head_node);
            ret = null;
        }
    } else {
        ret = nodelist_from_lua(Luas,-1);
    }
    lua_settop(Luas, s_top);
    if (fix_node_lists)
        fix_node_list(ret);
    return ret;
}

halfword lua_vpack_filter(halfword head_node, scaled size, int pack_type, scaled maxd,
    int extrainfo, int pack_direction, halfword attr)
{
    halfword ret;
    int i;
    int callback_id;
    int s_top = lua_gettop(Luas);
    if (head_node == null) {
        lua_settop(Luas, s_top);
        return head_node;
    }
    if  (extrainfo == 8)  {
        callback_id = callback_defined(pre_output_filter_callback);
    } else {
        callback_id = callback_defined(vpack_filter_callback);
    }
    if (callback_id <= 0) {
        lua_settop(Luas, s_top);
        return head_node;
    }
    if (!get_callback(Luas, callback_id)) {
        lua_settop(Luas, s_top);
        return head_node;
    }
    alink(head_node) = null ;
    nodelist_to_lua(Luas, head_node);
    lua_push_group_code(Luas, extrainfo);
    lua_pushinteger(Luas, size);
    lua_push_pack_type(Luas, pack_type);
    lua_pushinteger(Luas, maxd);
    if (pack_direction >= 0) {
         lua_push_dir_par(Luas, pack_direction);
    } else {
        lua_pushnil(Luas);
    }
    if (attr != null) {
        nodelist_to_lua(Luas, attr);
    } else {
        lua_pushnil(Luas);
    }
    if ((i=lua_pcall(Luas, 7, 1, 0)) != 0) {
        formatted_warning("vpack filter", "error: %s", lua_tostring(Luas, -1));
        lua_settop(Luas, s_top);
        luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
        return head_node;
    }
    ret = head_node;
    if (lua_isboolean(Luas, -1)) {
        if (lua_toboolean(Luas, -1) != 1) {
            flush_node_list(head_node);
            ret = null;
        }
    } else {
        ret = nodelist_from_lua(Luas,-1);
    }
    lua_settop(Luas, s_top);
    if (fix_node_lists)
        fix_node_list(ret);
    return ret;
}

/*tex

    This is a quick hack to fix \ETEX's \.{\\lastnodetype} now that there are many
    more visible node types.

*/

int visible_last_node_type(int n)
{
    int i = type(n);
    if (i != glyph_node) {
        return get_etex_code(i);
    } else if (is_ligature(n)) {
        /*tex old ligature value */
        return 7;
    } else {
        /*tex old character value */
        return 0;
    }
}

void lua_pdf_literal(PDF pdf, int i, int noline)
{
    const char *s = NULL;
    size_t l = 0;
    lua_rawgeti(Luas, LUA_REGISTRYINDEX, i);
    s = lua_tolstring(Luas, -1, &l);
    if (noline) {
        pdf_check_space(pdf);
        pdf_out_block(pdf, s, l);
        pdf_set_space(pdf);
    } else {
        pdf_out_block(pdf, s, l);
        pdf_out(pdf, 10);
    }
    lua_pop(Luas, 1);
}

void copy_pdf_literal(pointer r, pointer p)
{
    int t = pdf_literal_type(p);
    pdf_literal_type(r) = t;
    pdf_literal_mode(r) = pdf_literal_mode(p);
    if (t == normal) {
        pdf_literal_data(r) = pdf_literal_data(p);
        add_token_ref(pdf_literal_data(p));
    } else if (t == lua_refid_literal) {
        lua_rawgeti(Luas, LUA_REGISTRYINDEX, pdf_literal_data(p));
        pdf_literal_data(r) = luaL_ref(Luas, LUA_REGISTRYINDEX);
    } else {
        /* maybe something user, we don't support a call here but best keep it sane anyway. */
        pdf_literal_data(r) = pdf_literal_data(p);
    }
}

void copy_late_lua(pointer r, pointer p)
{
    int t = late_lua_type(p);
    late_lua_type(r) = t;
    if (late_lua_name(p) > 0)
        add_token_ref(late_lua_name(p));
    if (t == normal) {
        late_lua_data(r) = late_lua_data(p);
        add_token_ref(late_lua_data(p));
    } else if (t == lua_refid_literal) {
        lua_rawgeti(Luas, LUA_REGISTRYINDEX, late_lua_data(p));
        late_lua_data(r) = luaL_ref(Luas, LUA_REGISTRYINDEX);
    }
}

void copy_user_lua(pointer r, pointer p)
{
    if (user_node_value(p) != 0) {
        lua_rawgeti(Luas, LUA_REGISTRYINDEX, user_node_value(p));
        user_node_value(r) = luaL_ref(Luas, LUA_REGISTRYINDEX);
    }
}

void free_pdf_literal(pointer p)
{
    int t = pdf_literal_type(p);
    if (t == normal) {
        delete_token_ref(pdf_literal_data(p));
    } else if (t == lua_refid_literal) {
        luaL_unref(Luas, LUA_REGISTRYINDEX, pdf_literal_data(p));
    }
}

void free_late_lua(pointer p)
{
    int t = late_lua_type(p);
    if (late_lua_name(p) > 0)
        delete_token_ref(late_lua_name(p));
    if (t == normal) {
        delete_token_ref(late_lua_data(p));
    } else if (t == lua_refid_literal) {
        luaL_unref(Luas, LUA_REGISTRYINDEX, late_lua_data(p));
    }
}

void free_user_lua(pointer p)
{
    if (user_node_value(p) != 0) {
        luaL_unref(Luas, LUA_REGISTRYINDEX, user_node_value(p));
    }
}

void show_pdf_literal(pointer p)
{
    int t = pdf_literal_type(p);
    tprint_esc("pdfliteral");
    switch (pdf_literal_mode(p)) {
        case set_origin:
            tprint(" origin");
            break;
        case direct_page:
            tprint(" page");
            break;
        case direct_always:
            tprint(" direct");
            break;
        case direct_raw:
            tprint(" raw");
            break;
        default:
            tprint(" <invalid mode>");
            break;
    }
    if (t == normal) {
        print_mark(pdf_literal_data(p));
    } else if (t == lua_refid_literal) {
        tprint(" <lua data reference ");
        print_int(pdf_literal_data(p));
        tprint(">");
    } else {
        tprint(" <invalid data>");
    }
}

void show_late_lua(pointer p)
{
    int t = late_lua_type(p);
    tprint_esc("latelua");
    print_int(late_lua_reg(p));
    if (t == normal) {
        print_mark(late_lua_data(p));
    } else if (t == lua_refid_literal) {
        tprint(" <function reference ");
        print_int(late_lua_data(p));
        tprint(">");
    } else if (t == lua_refid_call) {
        tprint(" <functioncall reference ");
        print_int(late_lua_data(p));
        tprint(">");
    } else {
        tprint(" <invalid data>");
    }
}