2 local floor = math.floor
6 ------------------------------------------------------------------------
8 -- ext_... : called from \directlua{}
9 -- int_... : called from other Lua codes, but not from \directlua{}
10 -- (other) : only called from this file
11 function luatexja.error(s,t)
12 tex.error('LuaTeX-ja error: ' .. s ,t)
14 function luatexja.load_module(name)
15 if not package.loaded['luatexja.' .. name] then
16 local fn = 'ltj-' .. name .. '.lua'
17 local found = kpse.find_file(fn, 'tex')
19 luatexja.error("File `" .. fn .. "' not found",
20 {'This file ' .. fn .. ' is required for LuaTeX-ja.', 'Please check your installation.'})
22 texio.write('(' .. found .. ')\n')
27 function luatexja.load_lua(fn)
28 local found = kpse.find_file(fn, 'tex')
30 error("File `" .. fn .. "' not found")
32 texio.write('(' .. found .. ')\n')
37 local load_module = luatexja.load_module
38 load_module('base'); local ltjb = luatexja.base
39 load_module('rmlgbm'); local ltjr = luatexja.rmlgbm -- must be 1st
40 load_module('charrange'); local ltjc = luatexja.charrange
41 load_module('jfont'); local ltjf = luatexja.jfont
42 load_module('inputbuf'); local ltji = luatexja.inputbuf
43 load_module('stack'); local ltjs = luatexja.stack
44 load_module('pretreat'); local ltjp = luatexja.pretreat
45 load_module('jfmglue'); local ltjj = luatexja.jfmglue
46 load_module('setwidth'); local ltjw = luatexja.setwidth
47 load_module('math'); local ltjm = luatexja.math
48 load_module('tangle'); local ltjb = luatexja.base
51 local node_type = node.type
52 local node_new = node.new
53 local node_prev = node.prev
54 local node_next = node.next
55 local has_attr = node.has_attribute
56 local node_insert_before = node.insert_before
57 local node_insert_after = node.insert_after
58 local node_hpack = node.hpack
60 local id_penalty = node.id('penalty')
61 local id_glyph = node.id('glyph')
62 local id_glue_spec = node.id('glue_spec')
63 local id_glue = node.id('glue')
64 local id_kern = node.id('kern')
65 local id_hlist = node.id('hlist')
66 local id_vlist = node.id('vlist')
67 local id_rule = node.id('rule')
68 local id_math = node.id('math')
69 local id_whatsit = node.id('whatsit')
70 local sid_user = node.subtype('user_defined')
72 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
73 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
74 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
75 local attr_icflag = luatexbase.attributes['ltj@icflag']
76 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
77 local cat_lp = luatexbase.catcodetables['latex-package']
87 local IC_PROCESSED = 9
89 local PROCESSED_BEGIN_FLAG = 16
92 -- Three aux. functions, bollowed from tex.web
94 local function print_scaled(s)
100 out=out..tostring(floor(s/unity)) .. '.'
103 if delta>unity then s=s+32768-50000 end
104 out=out .. tostring(floor(s/unity))
111 local function print_glue(d,order)
112 local out=print_scaled(d)
116 out=out..'l'; order=order-1
124 function print_spec(p)
125 local out=print_scaled(p.width)..'pt'
127 out=out..' plus '..print_glue(p.stretch,p.stretch_order)
130 out=out..' minus '..print_glue(p.shrink,p.shrink_order)
136 ---- table: charprop_stack_table [stack_level].{pre|post|xsp}[chr_code]
138 ------------------------------------------------------------------------
139 -- CODE FOR GETTING/SETTING PARAMETERS
140 ------------------------------------------------------------------------
142 -- EXT: print parameters that don't need arguments
143 function luatexja.ext_get_parameter_unary(k)
144 if k == 'yalbaselineshift' then
145 tex.write(print_scaled(tex.getattribute('ltj@yablshift'))..'pt')
146 elseif k == 'yjabaselineshift' then
147 tex.write(print_scaled(tex.getattribute('ltj@ykblshift'))..'pt')
148 elseif k == 'kanjiskip' then
149 tex.write(print_spec(ltjs.get_skip_table('kanjiskip', tex.getcount('ltj@@stack'))))
150 elseif k == 'xkanjiskip' then
151 tex.write(print_spec(ltjs.get_skip_table('xkanjiskip', tex.getcount('ltj@@stack'))))
152 elseif k == 'jcharwidowpenalty' then
153 tex.write(ltjs.get_penalty_table('jwp', 0, 0, tex.getcount('ltj@@stack')))
154 elseif k == 'autospacing' then
155 tex.write(tex.getattribute('ltj@autospc'))
156 elseif k == 'autoxspacing' then
157 tex.write(tex.getattribute('ltj@autoxspc'))
158 elseif k == 'differentjfm' then
159 if luatexja.jfmglue.diffmet_rule == math.max then
161 elseif luatexja.jfmglue.diffmet_rule == math.min then
163 elseif luatexja.jfmglue.diffmet_rule == math.two_average then
165 elseif luatexja.jfmglue.diffmet_rule == math.two_add then
167 else -- This can't happen.
174 -- EXT: print parameters that need arguments
175 function luatexja.ext_get_parameter_binary(k,c)
176 if type(c)~='number' then
177 ltjb.package_error('luatexja',
178 'invalid the second argument (' .. tostring(c) .. ')',
179 'I changed this one to zero.')
182 if k == 'jacharrange' then
184 ltjb.package_error('luatexja',
185 'invalid character range number (' .. c .. ')',
186 'A character range number should be in the range 0..216,\n'..
187 'So I changed this one to zero.')
190 tex.write(ltjc.get_range_setting(c))
192 if c<0 or c>0x10FFFF then
193 ltjb.package_error('luatexja',
194 'bad character code (' .. c .. ')',
195 'A character number must be between -1 and 0x10ffff.\n'..
196 "(-1 is used for denoting `math boundary')\n"..
197 'So I changed this one to zero.')
200 if k == 'prebreakpenalty' then
201 tex.write(ltjs.get_penalty_table('pre', c, 0, tex.getcount('ltj@@stack')))
202 elseif k == 'postbreakpenalty' then
203 tex.write(ltjs.get_penalty_table('post', c, 0, tex.getcount('ltj@@stack')))
204 elseif k == 'kcatcode' then
205 tex.write(ltjs.get_penalty_table('kcat', c, 0, tex.getcount('ltj@@stack')))
206 elseif k == 'chartorange' then
207 tex.write(ltjc.char_to_range(c))
208 elseif k == 'jaxspmode' or k == 'alxspmode' then
209 tex.write(ltjs.get_penalty_table('xsp', c, 3, tex.getcount('ltj@@stack')))
214 -- EXT: print \global if necessary
215 function luatexja.ext_print_global()
216 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
220 -- mode = true iff main_process is called from pre_linebreak_filter
221 local function main_process(head, mode, dir)
223 p = ltjj.main(p,mode)
224 if p then p = ltjw.set_ja_width(p, dir) end
230 luatexbase.add_to_callback('pre_linebreak_filter',
231 function (head,groupcode)
232 return main_process(head, true, tex.textdir)
233 end,'ltj.pre_linebreak_filter',
234 luatexbase.priority_in_callback('pre_linebreak_filter',
235 'luaotfload.pre_linebreak_filter') + 1)
236 luatexbase.add_to_callback('hpack_filter',
237 function (head,groupcode,size,packtype, dir)
238 return main_process(head, false, dir)
239 end,'ltj.hpack_filter',
240 luatexbase.priority_in_callback('hpack_filter',
241 'luaotfload.hpack_filter') + 1)
244 local function get_attr_icflag(p)
245 return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
250 local function debug_show_node_X(p,print_fn)
251 local k = debug_depth
253 local pt=node_type(p.id)
254 local base = debug_depth .. string.format('%X', get_attr_icflag(p))
255 .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
256 if pt == 'glyph' then
257 s = base .. ' ' .. utf.char(p.char) .. ' ' .. tostring(p.font)
258 .. ' (' .. print_scaled(p.height) .. '+'
259 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
261 elseif pt=='hlist' or pt=='vlist' then
262 s = base .. '(' .. print_scaled(p.height) .. '+'
263 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width) .. p.dir
265 s = s .. ', shifted ' .. print_scaled(p.shift)
267 if p.glue_sign >= 1 then
268 s = s .. ' glue set '
269 if p.glue_sign == 2 then s = s .. '-' end
270 s = s .. tostring(floor(p.glue_set*10000)/10000)
271 if p.glue_order == 0 then
275 for i = 2, p.glue_order do s = s .. 'l' end
278 if get_attr_icflag(p) == PACKED then
283 debug_depth=debug_depth.. '.'
285 debug_show_node_X(q, print_fn); q = node_next(q)
288 elseif pt == 'glue' then
289 s = base .. ' ' .. print_spec(p.spec)
290 if get_attr_icflag(p)==FROM_JFM then
291 s = s .. ' (from JFM)'
292 elseif get_attr_icflag(p)==KANJI_SKIP then
293 s = s .. ' (kanjiskip)'
294 elseif get_attr_icflag(p)==XKANJI_SKIP then
295 s = s .. ' (xkanjiskip)'
298 elseif pt == 'kern' then
299 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
301 s = s .. ' (for accent)'
302 elseif get_attr_icflag(p)==IC_PROCESSED then
303 s = s .. ' (italic correction)'
304 -- elseif get_attr_icflag(p)==ITALIC then
305 -- s = s .. ' (italic correction)'
306 elseif get_attr_icflag(p)==FROM_JFM then
307 s = s .. ' (from JFM)'
308 elseif get_attr_icflag(p)==LINE_END then
309 s = s .. " (from 'lineend' in JFM)"
312 elseif pt == 'penalty' then
313 s = base .. ' ' .. tostring(p.penalty)
314 if get_attr_icflag(p)==KINSOKU then
315 s = s .. ' (for kinsoku)'
318 elseif pt == 'whatsit' then
319 s = base .. ' subtype: ' .. tostring(p.subtype)
320 if p.subtype==sid_user then
321 if p.type ~= 110 then
322 s = s .. ' user_id: ' .. p.user_id .. ' ' .. p.value
325 s = s .. ' user_id: ' .. p.user_id .. ' (node list)'
328 debug_depth=debug_depth.. '.'
330 debug_show_node_X(q, print_fn); q = node_next(q)
335 s = s .. node.subtype(p.subtype); print_fn(s)
337 -------- math node --------
338 elseif pt=='noad' then
339 s = base ; print_fn(s)
341 debug_depth = k .. 'N'; debug_show_node_X(p.nucleus, print_fn);
344 debug_depth = k .. '^'; debug_show_node_X(p.sup, print_fn);
347 debug_depth = k .. '_'; debug_show_node_X(p.sub, print_fn);
350 elseif pt=='math_char' then
351 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
353 elseif pt=='sub_box' then
356 debug_depth = k .. '.'; debug_show_node_X(p.head, print_fn);
363 function luatexja.ext_show_node_list(head,depth,print_fn)
367 debug_show_node_X(head, print_fn); head = node_next(head)
370 print_fn(debug_depth .. ' (null list)')
373 function luatexja.ext_show_node(head,depth,print_fn)
376 debug_show_node_X(head, print_fn)
378 print_fn(debug_depth .. ' (null list)')