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
*/
27 #include
"lua/luatex-api.h"
30 static const char
*group_code_names
[] = {
56 const char
*pack_type_name
[] = { "exactly", "additional" };
61 lua_node_filter_s
(int filterid
, int extrainfo
)
64 int callback_id
= callback_defined
(filterid
);
65 int s_top
= lua_gettop
(L
);
66 if
(callback_id
<= 0) {
70 if
(!get_callback
(L
, callback_id
)) {
74 lua_push_string_by_index
(L
,extrainfo
); /* arg
1 */
75 if
(lua_pcall
(L
, 1, 0, 0) != 0) {
76 fprintf
(stdout
, "error: %s\n", lua_tostring
(L
, -1));
88 lua_node_filter
(int filterid
, int extrainfo
, halfword head_node
, halfword
* tail_node
)
93 int s_top
= lua_gettop
(L
);
94 int callback_id
= callback_defined
(filterid
);
95 if
(head_node
== null || vlink
(head_node
) == null || callback_id
<= 0) {
99 if
(!get_callback
(L
, callback_id
)) {
100 lua_settop
(L
, s_top
);
103 alink
(vlink
(head_node
)) = null
; /* hh-ls
*/
104 nodelist_to_lua
(L
, vlink
(head_node
)); /* arg
1 */
105 lua_push_group_code
(L
,extrainfo
); /* arg
2 */
106 if
(lua_pcall
(L
, 2, 1, 0) != 0) { /* no arg
, 1 result
*/
107 fprintf
(stdout
, "error: %s\n", lua_tostring
(L
, -1));
108 lua_settop
(L
, s_top
);
112 if
(lua_isboolean
(L
, -1)) {
113 if
(lua_toboolean
(L
, -1) != 1) {
114 flush_node_list
(vlink
(head_node
));
115 vlink
(head_node
) = null
;
118 a
= nodelist_from_lua
(L
);
119 try_couple_nodes
(head_node
,a
);
121 lua_pop
(L
, 2); /* result and callback container table
*/
123 fix_node_list
(head_node
);
124 ret
= vlink
(head_node
);
126 while
(vlink
(ret
) != null
)
130 *tail_node
= head_node
;
132 lua_settop
(L
, s_top
);
138 lua_linebreak_callback
(int is_broken
, halfword head_node
, halfword
* new_head
)
141 register halfword
*p
;
142 int ret
= 0; /* failure
*/
144 int s_top
= lua_gettop
(L
);
145 int callback_id
= callback_defined
(linebreak_filter_callback
);
146 if
(head_node
== null || vlink
(head_node
) == null || callback_id
<= 0) {
147 lua_settop
(L
, s_top
);
150 if
(!get_callback
(L
, callback_id
)) {
151 lua_settop
(L
, s_top
);
154 alink
(vlink
(head_node
)) = null
; /* hh-ls
*/
155 nodelist_to_lua
(L
, vlink
(head_node
)); /* arg
1 */
156 lua_pushboolean
(L
, is_broken
); /* arg
2 */
157 if
(lua_pcall
(L
, 2, 1, 0) != 0) { /* no arg
, 1 result
*/
158 fprintf
(stdout
, "error: %s\n", lua_tostring
(L
, -1));
159 lua_settop
(L
, s_top
);
164 p
= lua_touserdata
(L
, -1);
166 a
= nodelist_from_lua
(L
);
167 try_couple_nodes
(*new_head
,a
);
170 lua_settop
(L
, s_top
);
178 lua_hpack_filter
(halfword head_node
, scaled size
, int pack_type
, int extrainfo
,
183 int s_top
= lua_gettop
(L
);
184 int callback_id
= callback_defined
(hpack_filter_callback
);
185 if
(head_node
== null || callback_id
<= 0) {
186 lua_settop
(L
, s_top
);
189 if
(!get_callback
(L
, callback_id
)) {
190 lua_settop
(L
, s_top
);
193 alink
(head_node
) = null
; /* hh-ls
*/
194 nodelist_to_lua
(L
, head_node
);
195 lua_push_group_code
(L
,extrainfo
);
196 lua_pushnumber
(L
, size
);
197 lua_push_pack_type
(L
,pack_type
);
198 if
(pack_direction
>= 0)
199 lua_push_dir_par
(L
, pack_direction
);
202 if
(lua_pcall
(L
, 5, 1, 0) != 0) { /* no arg
, 1 result
*/
203 fprintf
(stdout
, "error: %s\n", lua_tostring
(L
, -1));
204 lua_settop
(L
, s_top
);
209 if
(lua_isboolean
(L
, -1)) {
210 if
(lua_toboolean
(L
, -1) != 1) {
211 flush_node_list
(head_node
);
215 ret
= nodelist_from_lua
(L
);
217 lua_settop
(L
, s_top
);
219 lua_gc
(L
,LUA_GCSTEP
, LUA_GC_STEP_SIZE
);
228 lua_vpack_filter
(halfword head_node
, scaled size
, int pack_type
, scaled maxd
,
229 int extrainfo
, int pack_direction
)
234 int s_top
= lua_gettop
(L
);
235 if
(head_node
== null
) {
236 lua_settop
(L
, s_top
);
239 if
(extrainfo
== 8) { /* output
*/
240 callback_id
= callback_defined
(pre_output_filter_callback
);
242 callback_id
= callback_defined
(vpack_filter_callback
);
244 if
(callback_id
<= 0) {
245 lua_settop
(L
, s_top
);
248 if
(!get_callback
(L
, callback_id
)) {
249 lua_settop
(L
, s_top
);
252 alink
(head_node
) = null
; /* hh-ls
*/
253 nodelist_to_lua
(L
, head_node
);
254 lua_push_group_code
(L
,extrainfo
);
255 lua_pushnumber
(L
, size
);
256 lua_push_pack_type
(L
,pack_type
);
257 lua_pushnumber
(L
, maxd
);
258 if
(pack_direction
>= 0)
259 lua_push_dir_par
(L
, pack_direction
);
262 if
(lua_pcall
(L
, 6, 1, 0) != 0) { /* no arg
, 1 result
*/
263 fprintf
(stdout
, "error: %s\n", lua_tostring
(L
, -1));
264 lua_settop
(L
, s_top
);
269 if
(lua_isboolean
(L
, -1)) {
270 if
(lua_toboolean
(L
, -1) != 1) {
271 flush_node_list
(head_node
);
275 ret
= nodelist_from_lua
(L
);
277 lua_settop
(L
, s_top
);
279 lua_gc
(L
,LUA_GCSTEP
, LUA_GC_STEP_SIZE
);
287 @ This is a quick hack to fix etex's \.
{\\lastnodetype
} now that
288 there are many more visible node types. TODO
: check the
289 eTeX manual for the expected return values.
292 int visible_last_node_type
(int n
)
295 if
(i
== whatsit_node
&& subtype(n) == local_par_node)
297 if
(i
== glyph_node
) {
299 return
7; /* old ligature value
*/
301 return
0; /* old character value
*/
303 if
(i
<= unset_node
) {
305 } else if
(i
<= delim_node
) {
306 return
15; /* so-called math nodes
*/
313 void lua_pdf_literal
(PDF pdf
, int i
)
315 const char
*s
= NULL;
317 lua_rawgeti
(Luas
, LUA_REGISTRYINDEX
, i
);
318 s
= lua_tolstring
(Luas
, -1, &l);
319 pdf_out_block
(pdf
, s
, l
);
320 pdf_out
(pdf
, 10); /* |pdf_print_nl|
*/
325 void copy_pdf_literal
(pointer r
, pointer p
)
327 pdf_literal_type
(r
) = pdf_literal_type
(p
);
328 pdf_literal_mode
(r
) = pdf_literal_mode
(p
);
329 if
(pdf_literal_type
(p
) == normal
) {
330 pdf_literal_data
(r
) = pdf_literal_data
(p
);
331 add_token_ref
(pdf_literal_data
(p
));
333 lua_rawgeti
(Luas
, LUA_REGISTRYINDEX
, pdf_literal_data
(p
));
334 pdf_literal_data
(r
) = luaL_ref
(Luas
, LUA_REGISTRYINDEX
);
339 void copy_late_lua
(pointer r
, pointer p
)
341 late_lua_type
(r
) = late_lua_type
(p
);
342 if
(late_lua_name
(p
) > 0)
343 add_token_ref
(late_lua_name
(p
));
344 if
(late_lua_type
(p
) == normal
) {
345 late_lua_data
(r
) = late_lua_data
(p
);
346 add_token_ref
(late_lua_data
(p
));
348 lua_rawgeti
(Luas
, LUA_REGISTRYINDEX
, late_lua_data
(p
));
349 late_lua_data
(r
) = luaL_ref
(Luas
, LUA_REGISTRYINDEX
);
354 void copy_user_lua
(pointer r
, pointer p
)
356 if
(user_node_value
(p
) != 0) {
357 lua_rawgeti
(Luas
, LUA_REGISTRYINDEX
, user_node_value
(p
));
358 user_node_value
(r
) = luaL_ref
(Luas
, LUA_REGISTRYINDEX
);
364 void free_pdf_literal
(pointer p
)
366 if
(pdf_literal_type
(p
) == normal
) {
367 delete_token_ref
(pdf_literal_data
(p
));
369 luaL_unref
(Luas
, LUA_REGISTRYINDEX
, pdf_literal_data
(p
));
373 void free_late_lua
(pointer p
)
375 if
(late_lua_name
(p
) > 0)
376 delete_token_ref
(late_lua_name
(p
));
377 if
(late_lua_type
(p
) == normal
) {
378 delete_token_ref
(late_lua_data
(p
));
380 luaL_unref
(Luas
, LUA_REGISTRYINDEX
, late_lua_data
(p
));
385 void free_user_lua
(pointer p
)
387 if
(user_node_value
(p
) != 0) {
388 luaL_unref
(Luas
, LUA_REGISTRYINDEX
, user_node_value
(p
));
394 void show_pdf_literal
(pointer p
)
396 tprint_esc
("pdfliteral");
397 switch
(pdf_literal_mode
(p
)) {
407 confusion
("literal2");
410 if
(pdf_literal_type
(p
) == normal
) {
411 print_mark
(pdf_literal_data
(p
));
413 lua_rawgeti
(Luas
, LUA_REGISTRYINDEX
, pdf_literal_data
(p
));
415 tprint(lua_tostring(Luas, -1));
422 void show_late_lua
(pointer p
)
424 tprint_esc
("latelua");
425 print_int
(late_lua_reg
(p
));
426 if
(late_lua_type
(p
) == normal
) {
427 print_mark
(late_lua_data
(p
));
429 tprint
(" <function ");
430 print_int
(late_lua_data
(p
));