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
91 -- Three aux. functions, bollowed from tex.web
93 local function print_scaled(s)
99 out=out..tostring(floor(s/unity)) .. '.'
102 if delta>unity then s=s+32768-50000 end
103 out=out .. tostring(floor(s/unity))
110 local function print_glue(d,order)
111 local out=print_scaled(d)
115 out=out..'l'; order=order-1
123 local function print_spec(p)
124 local out=print_scaled(p.width)..'pt'
126 out=out..' plus '..print_glue(p.stretch,p.stretch_order)
129 out=out..' minus '..print_glue(p.shrink,p.shrink_order)
134 function math.two_add(a,b) return a+b end
135 function math.two_average(a,b) return (a+b)/2 end
137 ---- table: charprop_stack_table [stack_level].{pre|post|xsp}[chr_code]
139 ------------------------------------------------------------------------
140 -- CODE FOR GETTING/SETTING PARAMETERS
141 ------------------------------------------------------------------------
143 -- EXT: print parameters that don't need arguments
144 function luatexja.ext_get_parameter_unary(k)
145 if k == 'yalbaselineshift' then
146 tex.write(print_scaled(tex.getattribute('ltj@yablshift'))..'pt')
147 elseif k == 'yjabaselineshift' then
148 tex.write(print_scaled(tex.getattribute('ltj@ykblshift'))..'pt')
149 elseif k == 'kanjiskip' then
150 tex.write(print_spec(ltjs.get_skip_table('kanjiskip', tex.getcount('ltj@@stack'))))
151 elseif k == 'xkanjiskip' then
152 tex.write(print_spec(ltjs.get_skip_table('xkanjiskip', tex.getcount('ltj@@stack'))))
153 elseif k == 'jcharwidowpenalty' then
154 tex.write(ltjs.get_penalty_table('jwp', 0, 0, tex.getcount('ltj@@stack')))
155 elseif k == 'autospacing' then
156 tex.write(tex.getattribute('ltj@autospc'))
157 elseif k == 'autoxspacing' then
158 tex.write(tex.getattribute('ltj@autoxspc'))
159 elseif k == 'differentjfm' then
160 if luatexja.jfmglue.diffmet_rule == math.max then
162 elseif luatexja.jfmglue.diffmet_rule == math.min then
164 elseif luatexja.jfmglue.diffmet_rule == math.two_average then
166 elseif luatexja.jfmglue.diffmet_rule == math.two_add then
168 else -- This can't happen.
175 -- EXT: print parameters that need arguments
176 function luatexja.ext_get_parameter_binary(k,c)
177 if k == 'jacharrange' then
179 ltjb.package_error('luatexja',
180 'invalid character range number (' .. c .. ')',
181 'A character range number should be in the range 0..216,\n'..
182 'So I changed this one to zero.')
185 tex.write(ltjc.get_range_setting(c))
187 if c<0 or c>0x10FFFF then
188 ltjb.package_error('luatexja',
189 'bad character code (' .. c .. ')',
190 'A character number must be between -1 and 0x10ffff.\n'..
191 "(-1 is used for denoting `math boundary')\n"..
192 'So I changed this one to zero.')
195 if k == 'prebreakpenalty' then
196 tex.write(ltjs.get_penalty_table('pre', c, 0, tex.getcount('ltj@@stack')))
197 elseif k == 'postbreakpenalty' then
198 tex.write(ltjs.get_penalty_table('post', c, 0, tex.getcount('ltj@@stack')))
199 elseif k == 'kcatcode' then
200 tex.write(ltjs.get_penalty_table('kcat', c, 0, tex.getcount('ltj@@stack')))
201 elseif k == 'chartorange' then
202 tex.write(ltjc.char_to_range(c))
203 elseif k == 'jaxspmode' or k == 'alxspmode' then
204 tex.write(ltjs.get_penalty_table('xsp', c, 3, tex.getcount('ltj@@stack')))
209 -- EXT: print \global if necessary
210 function luatexja.ext_print_global()
211 if isglobal=='global' then tex.sprint(cat_lp, '\\global') end
215 -- mode = true iff main_process is called from pre_linebreak_filter
216 local function main_process(head, mode, dir)
218 p = ltjj.main(p,mode)
219 if p then p = ltjw.set_ja_width(p, dir) end
225 luatexbase.add_to_callback('pre_linebreak_filter',
226 function (head,groupcode)
227 return main_process(head, true, tex.textdir)
228 end,'ltj.pre_linebreak_filter',
229 luatexbase.priority_in_callback('pre_linebreak_filter',
230 'luaotfload.pre_linebreak_filter') + 1)
231 luatexbase.add_to_callback('hpack_filter',
232 function (head,groupcode,size,packtype, dir)
233 return main_process(head, false, dir)
234 end,'ltj.hpack_filter',
235 luatexbase.priority_in_callback('hpack_filter',
236 'luaotfload.hpack_filter') + 1)
241 local function debug_show_node_X(p,print_fn)
242 local k = debug_depth
244 local pt=node_type(p.id)
245 local base = debug_depth .. string.format('%X', has_attr(p,attr_icflag) or 0)
246 .. ' ' .. string.format('%X', has_attr(p, attr_uniqid) or 0)
247 .. ' ' .. node.type(p.id) .. ' ' .. tostring(p.subtype )
248 if pt == 'glyph' then
249 s = base .. ' ' .. utf.char(p.char) .. ' ' .. tostring(p.font)
250 .. ' (' .. print_scaled(p.height) .. '+'
251 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
253 elseif pt=='hlist' or pt=='vlist' then
254 s = base .. '(' .. print_scaled(p.height) .. '+'
255 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width) .. p.dir
257 s = s .. ', shifted ' .. print_scaled(p.shift)
259 if p.glue_sign >= 1 then
260 s = s .. ' glue set '
261 if p.glue_sign == 2 then s = s .. '-' end
262 s = s .. tostring(floor(p.glue_set*10000)/10000)
263 if p.glue_order == 0 then
267 for i = 2, p.glue_order do s = s .. 'l' end
270 if has_attr(p, attr_icflag, PACKED) then
275 debug_depth=debug_depth.. '.'
277 debug_show_node_X(q, print_fn); q = node_next(q)
280 elseif pt == 'glue' then
281 s = base .. ' ' .. print_spec(p.spec)
282 if has_attr(p, attr_icflag)==FROM_JFM then
283 s = s .. ' (from JFM)'
284 elseif has_attr(p, attr_icflag)==KANJI_SKIP then
285 s = s .. ' (kanjiskip)'
286 elseif has_attr(p, attr_icflag)==XKANJI_SKIP then
287 s = s .. ' (xkanjiskip)'
290 elseif pt == 'kern' then
291 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
293 s = s .. ' (for accent)'
294 elseif has_attr(p, attr_icflag)==IC_PROCESSED then
295 s = s .. ' (italic correction)'
296 -- elseif has_attr(p, attr_icflag)==ITALIC then
297 -- s = s .. ' (italic correction)'
298 elseif has_attr(p, attr_icflag)==FROM_JFM then
299 s = s .. ' (from JFM)'
300 elseif has_attr(p, attr_icflag)==LINE_END then
301 s = s .. " (from 'lineend' in JFM)"
304 elseif pt == 'penalty' then
305 s = base .. ' ' .. tostring(p.penalty)
306 if has_attr(p, attr_icflag)==KINSOKU then
307 s = s .. ' (for kinsoku)'
310 elseif pt == 'whatsit' then
311 s = base .. ' subtype: ' .. tostring(p.subtype)
312 if p.subtype==sid_user then
313 if p.type ~= 110 then
314 s = s .. ' user_id: ' .. p.user_id .. ' ' .. p.value
316 s = s .. ' user_id: ' .. p.user_id .. ' (node list)'
319 s = s .. node.subtype(p.subtype)
322 -------- math node --------
323 elseif pt=='noad' then
324 s = base ; print_fn(s)
326 debug_depth = k .. 'N'; debug_show_node_X(p.nucleus, print_fn);
329 debug_depth = k .. '^'; debug_show_node_X(p.sup, print_fn);
332 debug_depth = k .. '_'; debug_show_node_X(p.sub, print_fn);
335 elseif pt=='math_char' then
336 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
338 elseif pt=='sub_box' then
341 debug_depth = k .. '.'; debug_show_node_X(p.head, print_fn);
348 function luatexja.ext_show_node_list(head,depth,print_fn)
352 debug_show_node_X(head, print_fn); head = node_next(head)
355 print_fn(debug_depth .. ' (null list)')
358 function luatexja.ext_show_node(head,depth,print_fn)
361 debug_show_node_X(head, print_fn)
363 print_fn(debug_depth .. ' (null list)')