3 % Copyright
2006-2008 Taco Hoekwater
<taco@@luatex.org
>
5 % This file is part of LuaTeX.
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.
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.
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
/>.
20 /* hh-ls
: we make sure that lua never sees prev of head but also that when
21 nodes are removedor inserted
, temp nodes don't interfere
*/
24 static const char _svn_version
[] =
29 #include
"lua/luatex-api.h"
32 static const char
*group_code_names
[] = {
58 const char
*pack_type_name
[] = { "exactly", "additional" };
63 lua_node_filter_s
(int filterid
, int extrainfo
)
66 int callback_id
= callback_defined
(filterid
);
67 int s_top
= lua_gettop
(L
);
68 if
(callback_id
<= 0) {
72 if
(!get_callback
(L
, callback_id
)) {
76 lua_push_string_by_index
(L
,extrainfo
); /* arg
1 */
77 if
(lua_pcall
(L
, 1, 0, 0) != 0) {
78 fprintf
(stdout
, "error: %s\n", lua_tostring
(L
, -1));
90 lua_node_filter
(int filterid
, int extrainfo
, halfword head_node
, halfword
* tail_node
)
95 int s_top
= lua_gettop
(L
);
96 int callback_id
= callback_defined
(filterid
);
97 if
(head_node
== null || vlink
(head_node
) == null || callback_id
<= 0) {
101 if
(!get_callback
(L
, callback_id
)) {
102 lua_settop
(L
, s_top
);
105 alink
(vlink
(head_node
)) = null
; /* hh-ls
*/
106 nodelist_to_lua
(L
, vlink
(head_node
)); /* arg
1 */
107 lua_push_group_code
(L
,extrainfo
); /* arg
2 */
108 if
(lua_pcall
(L
, 2, 1, 0) != 0) { /* no arg
, 1 result
*/
109 fprintf
(stdout
, "error: %s\n", lua_tostring
(L
, -1));
110 lua_settop
(L
, s_top
);
114 if
(lua_isboolean
(L
, -1)) {
115 if
(lua_toboolean
(L
, -1) != 1) {
116 flush_node_list
(vlink
(head_node
));
117 vlink
(head_node
) = null
;
120 a
= nodelist_from_lua
(L
);
121 try_couple_nodes
(head_node
,a
);
123 lua_pop
(L
, 2); /* result and callback container table
*/
125 fix_node_list
(head_node
);
126 ret
= vlink
(head_node
);
128 while
(vlink
(ret
) != null
)
132 *tail_node
= head_node
;
134 lua_settop
(L
, s_top
);
140 lua_linebreak_callback
(int is_broken
, halfword head_node
, halfword
* new_head
)
143 register halfword
*p
;
144 int ret
= 0; /* failure
*/
146 int s_top
= lua_gettop
(L
);
147 int callback_id
= callback_defined
(linebreak_filter_callback
);
148 if
(head_node
== null || vlink
(head_node
) == null || callback_id
<= 0) {
149 lua_settop
(L
, s_top
);
152 if
(!get_callback
(L
, callback_id
)) {
153 lua_settop
(L
, s_top
);
156 alink
(vlink
(head_node
)) = null
; /* hh-ls
*/
157 nodelist_to_lua
(L
, vlink
(head_node
)); /* arg
1 */
158 lua_pushboolean
(L
, is_broken
); /* arg
2 */
159 if
(lua_pcall
(L
, 2, 1, 0) != 0) { /* no arg
, 1 result
*/
160 fprintf
(stdout
, "error: %s\n", lua_tostring
(L
, -1));
161 lua_settop
(L
, s_top
);
166 p
= lua_touserdata
(L
, -1);
168 a
= nodelist_from_lua
(L
);
169 try_couple_nodes
(*new_head
,a
);
172 lua_settop
(L
, s_top
);
180 lua_hpack_filter
(halfword head_node
, scaled size
, int pack_type
, int extrainfo
,
185 int s_top
= lua_gettop
(L
);
186 int callback_id
= callback_defined
(hpack_filter_callback
);
187 if
(head_node
== null || callback_id
<= 0) {
188 lua_settop
(L
, s_top
);
191 if
(!get_callback
(L
, callback_id
)) {
192 lua_settop
(L
, s_top
);
195 alink
(head_node
) = null
; /* hh-ls
*/
196 nodelist_to_lua
(L
, head_node
);
197 lua_push_group_code
(L
,extrainfo
);
198 lua_pushnumber
(L
, size
);
199 lua_push_pack_type
(L
,pack_type
);
200 if
(pack_direction
>= 0)
201 lua_push_dir_par
(L
, pack_direction
);
204 if
(lua_pcall
(L
, 5, 1, 0) != 0) { /* no arg
, 1 result
*/
205 fprintf
(stdout
, "error: %s\n", lua_tostring
(L
, -1));
206 lua_settop
(L
, s_top
);
211 if
(lua_isboolean
(L
, -1)) {
212 if
(lua_toboolean
(L
, -1) != 1) {
213 flush_node_list
(head_node
);
217 ret
= nodelist_from_lua
(L
);
219 lua_settop
(L
, s_top
);
221 lua_gc
(L
,LUA_GCSTEP
, LUA_GC_STEP_SIZE
);
230 lua_vpack_filter
(halfword head_node
, scaled size
, int pack_type
, scaled maxd
,
231 int extrainfo
, int pack_direction
)
236 int s_top
= lua_gettop
(L
);
237 if
(head_node
== null
) {
238 lua_settop
(L
, s_top
);
241 if
(extrainfo
== 8) { /* output
*/
242 callback_id
= callback_defined
(pre_output_filter_callback
);
244 callback_id
= callback_defined
(vpack_filter_callback
);
246 if
(callback_id
<= 0) {
247 lua_settop
(L
, s_top
);
250 if
(!get_callback
(L
, callback_id
)) {
251 lua_settop
(L
, s_top
);
254 alink
(head_node
) = null
; /* hh-ls
*/
255 nodelist_to_lua
(L
, head_node
);
256 lua_push_group_code
(L
,extrainfo
);
257 lua_pushnumber
(L
, size
);
258 lua_push_pack_type
(L
,pack_type
);
259 lua_pushnumber
(L
, maxd
);
260 if
(pack_direction
>= 0)
261 lua_push_dir_par
(L
, pack_direction
);
264 if
(lua_pcall
(L
, 6, 1, 0) != 0) { /* no arg
, 1 result
*/
265 fprintf
(stdout
, "error: %s\n", lua_tostring
(L
, -1));
266 lua_settop
(L
, s_top
);
271 if
(lua_isboolean
(L
, -1)) {
272 if
(lua_toboolean
(L
, -1) != 1) {
273 flush_node_list
(head_node
);
277 ret
= nodelist_from_lua
(L
);
279 lua_settop
(L
, s_top
);
281 lua_gc
(L
,LUA_GCSTEP
, LUA_GC_STEP_SIZE
);
289 @ This is a quick hack to fix etex's \.
{\\lastnodetype
} now that
290 there are many more visible node types. TODO
: check the
291 eTeX manual for the expected return values.
294 int visible_last_node_type
(int n
)
297 if
(i
== whatsit_node
&& subtype(n) == local_par_node)
299 if
(i
== glyph_node
) {
301 return
7; /* old ligature value
*/
303 return
0; /* old character value
*/
305 if
(i
<= unset_node
) {
307 } else if
(i
<= delim_node
) {
308 return
15; /* so-called math nodes
*/
315 void lua_pdf_literal
(PDF pdf
, int i
)
317 const char
*s
= NULL;
319 lua_rawgeti
(Luas
, LUA_REGISTRYINDEX
, i
);
320 s
= lua_tolstring
(Luas
, -1, &l);
321 pdf_out_block
(pdf
, s
, l
);
322 pdf_out
(pdf
, 10); /* |pdf_print_nl|
*/
327 void copy_pdf_literal
(pointer r
, pointer p
)
329 pdf_literal_type
(r
) = pdf_literal_type
(p
);
330 pdf_literal_mode
(r
) = pdf_literal_mode
(p
);
331 if
(pdf_literal_type
(p
) == normal
) {
332 pdf_literal_data
(r
) = pdf_literal_data
(p
);
333 add_token_ref
(pdf_literal_data
(p
));
335 lua_rawgeti
(Luas
, LUA_REGISTRYINDEX
, pdf_literal_data
(p
));
336 pdf_literal_data
(r
) = luaL_ref
(Luas
, LUA_REGISTRYINDEX
);
341 void copy_late_lua
(pointer r
, pointer p
)
343 late_lua_type
(r
) = late_lua_type
(p
);
344 if
(late_lua_name
(p
) > 0)
345 add_token_ref
(late_lua_name
(p
));
346 if
(late_lua_type
(p
) == normal
) {
347 late_lua_data
(r
) = late_lua_data
(p
);
348 add_token_ref
(late_lua_data
(p
));
350 lua_rawgeti
(Luas
, LUA_REGISTRYINDEX
, late_lua_data
(p
));
351 late_lua_data
(r
) = luaL_ref
(Luas
, LUA_REGISTRYINDEX
);
356 void copy_user_lua
(pointer r
, pointer p
)
358 if
(user_node_value
(p
) != 0) {
359 lua_rawgeti
(Luas
, LUA_REGISTRYINDEX
, user_node_value
(p
));
360 user_node_value
(r
) = luaL_ref
(Luas
, LUA_REGISTRYINDEX
);
366 void free_pdf_literal
(pointer p
)
368 if
(pdf_literal_type
(p
) == normal
) {
369 delete_token_ref
(pdf_literal_data
(p
));
371 luaL_unref
(Luas
, LUA_REGISTRYINDEX
, pdf_literal_data
(p
));
375 void free_late_lua
(pointer p
)
377 if
(late_lua_name
(p
) > 0)
378 delete_token_ref
(late_lua_name
(p
));
379 if
(late_lua_type
(p
) == normal
) {
380 delete_token_ref
(late_lua_data
(p
));
382 luaL_unref
(Luas
, LUA_REGISTRYINDEX
, late_lua_data
(p
));
387 void free_user_lua
(pointer p
)
389 if
(user_node_value
(p
) != 0) {
390 luaL_unref
(Luas
, LUA_REGISTRYINDEX
, user_node_value
(p
));
396 void show_pdf_literal
(pointer p
)
398 tprint_esc
("pdfliteral");
399 switch
(pdf_literal_mode
(p
)) {
409 confusion
("literal2");
412 if
(pdf_literal_type
(p
) == normal
) {
413 print_mark
(pdf_literal_data
(p
));
415 lua_rawgeti
(Luas
, LUA_REGISTRYINDEX
, pdf_literal_data
(p
));
417 tprint(lua_tostring(Luas, -1));
423 void show_late_lua
(pointer p
)
425 tprint_esc
("latelua");
426 print_int
(late_lua_reg
(p
));
427 if
(late_lua_type
(p
) == normal
) {
428 print_mark
(late_lua_data
(p
));
430 lua_rawgeti
(Luas
, LUA_REGISTRYINDEX
, late_lua_data
(p
));
432 tprint(lua_tostring(Luas, -1));