3 tableunpack = table.unpack
5 ------------------------------------------------------------------------
7 -- ext_... : called from \directlua{}
8 -- int_... : called from other Lua codes, but not from \directlua{}
9 -- (other) : only called from this file
10 function luatexja.load_module(name)
11 require('ltj-' .. name.. '.lua')
13 function luatexja.load_lua(fn)
14 local found = kpse.find_file(fn, 'tex')
16 tex.error("LuaTeX-ja error: File `" .. fn .. "' not found")
18 texio.write_nl('(' .. found .. ')')
23 local setfield = node.direct.setfield
24 luatexja.setglue = node.direct.setglue or
25 function(g,w,st,sh,sto,sho)
26 setfield(g,'width', w or 0)
27 setfield(g,'stretch',st or 0)
28 setfield(g,'shrink', sh or 0)
29 setfield(g,'stretch_order',sto or 0)
30 setfield(g,'shrink_order', sho or 0)
33 -- check token library
35 luatexja.token = newtoken
37 local luatex_version = status.list().luatex_version
38 if luatex_version >80 then
39 luatexja.token = token
44 local icflag_table = {}
45 luatexja.icflag_table = icflag_table
46 icflag_table.ITALIC = 1
47 icflag_table.PACKED = 2
48 icflag_table.KINSOKU = 3
49 icflag_table.FROM_JFM = 6
50 -- FROM_JFM: 4, 5, 6, 7, 8 →優先度高(伸びやすく,縮みやすい)
52 icflag_table.KANJI_SKIP = 9
53 icflag_table.KANJI_SKIP_JFM = 10
54 icflag_table.XKANJI_SKIP = 11
55 icflag_table.XKANJI_SKIP_JFM = 12
56 icflag_table.PROCESSED = 13
57 icflag_table.IC_PROCESSED = 14
58 icflag_table.BOXBDD = 15
59 icflag_table.PROCESSED_BEGIN_FLAG = 128
61 local stack_table_index = {}
62 luatexja.stack_table_index = stack_table_index
63 stack_table_index.PRE = 0x200000 -- characterごと
64 stack_table_index.POST = 0x400000 -- characterごと
65 stack_table_index.KCAT = 0x600000 -- characterごと
66 stack_table_index.XSP = 0x800000 -- characterごと
67 stack_table_index.RIPRE = 0xA00000 -- characterごと,ruby pre
68 stack_table_index.RIPOST = 0xC00000 -- characterごと,ruby post
69 stack_table_index.JWP = 0 -- これだけ
70 stack_table_index.KSK = 1 -- これだけ
71 stack_table_index.XSK = 2 -- これだけ
72 stack_table_index.MJT = 0x100 -- 0--255
73 stack_table_index.MJS = 0x200 -- 0--255
74 stack_table_index.MJSS = 0x300 -- 0--255
75 stack_table_index.KSJ = 0x400 -- 0--9
77 local userid_table = {}
78 luatexja.userid_table = userid_table
79 userid_table.IHB = luatexbase.newuserwhatsitid('inhibitglue', 'luatexja') -- \inhibitglue
80 userid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
81 userid_table.BPAR = luatexbase.newuserwhatsitid('begin_par', 'luatexja') -- 「段落始め」
82 userid_table.DIR = luatexbase.newuserwhatsitid('direction', 'luatexja') -- 組方向
85 luatexja.dir_table = dir_table
86 dir_table.dir_dtou = 1
87 dir_table.dir_tate = 3
88 dir_table.dir_yoko = 4
89 dir_table.dir_math_mod = 8
90 dir_table.dir_node_auto = 128 -- 組方向を合わせるために自動で作られたもの
91 dir_table.dir_node_manual = 256 -- 寸法代入によって作られたもの
92 dir_table.dir_utod = dir_table.dir_tate + dir_table.dir_math_mod
93 -- 「縦数式ディレクション」 in pTeX
96 local load_module = luatexja.load_module
97 load_module('base'); local ltjb = luatexja.base
98 load_module('rmlgbm'); local ltjr = luatexja.rmlgbm -- must be 1st
100 if luatexja_debug then load_module('debug') end
102 load_module('charrange'); local ltjc = luatexja.charrange
103 load_module('stack'); local ltjs = luatexja.stack
104 load_module('direction'); local ltjd = luatexja.direction -- +1 hlist +1 attr_list
105 load_module('jfont'); local ltjf = luatexja.jfont
106 load_module('inputbuf'); local ltji = luatexja.inputbuf
107 load_module('pretreat'); local ltjp = luatexja.pretreat
108 load_module('setwidth'); local ltjw = luatexja.setwidth
109 load_module('jfmglue'); local ltjj = luatexja.jfmglue -- +1 glue +1 gs +1 attr_list
110 load_module('math'); local ltjm = luatexja.math
111 load_module('tangle'); local ltjb = luatexja.base
114 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
115 local attr_jchar_code = luatexbase.attributes['ltj@charcode']
116 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
117 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
118 local attr_icflag = luatexbase.attributes['ltj@icflag']
119 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
120 local attr_dir = luatexbase.attributes['ltj@dir']
121 local cat_lp = luatexbase.catcodetables['latex-package']
123 -- Three aux. functions, bollowed from tex.web
126 local floor = math.floor
128 local function print_scaled(s)
134 out=out..tostring(floor(s/unity)) .. '.'
137 if delta>unity then s=s+32768-50000 end
138 out=out .. tostring(floor(s/unity))
144 luatexja.print_scaled = print_scaled
146 local function print_glue(d,order)
147 local out=print_scaled(d)
151 out=out..'l'; order=order-1
159 local function print_spec(p)
160 local out=print_scaled(p.width)..'pt'
162 out=out..' plus '..print_glue(p.stretch,p.stretch_order)
165 out=out..' minus '..print_glue(p.shrink,p.shrink_order)
171 ------------------------------------------------------------------------
172 -- CODE FOR GETTING/SETTING PARAMETERS
173 ------------------------------------------------------------------------
175 -- EXT: print parameters that don't need arguments
177 luatexja.unary_pars = {
178 yalbaselineshift = function(t)
179 return print_scaled(tex.getattribute('ltj@yablshift'))..'pt'
181 yjabaselineshift = function(t)
182 return print_scaled(tex.getattribute('ltj@ykblshift'))..'pt'
184 talbaselineshift = function(t)
185 return print_scaled(tex.getattribute('ltj@tablshift'))..'pt'
187 tjabaselineshift = function(t)
188 return print_scaled(tex.getattribute('ltj@tkblshift'))..'pt'
190 kanjiskip = function(t)
191 return print_spec(ltjs.get_stack_skip(stack_table_index.KSK, t))
193 xkanjiskip = function(t)
194 return print_spec(ltjs.get_stack_skip(stack_table_index.XSK, t))
196 jcharwidowpenalty = function(t)
197 return ltjs.get_stack_table(stack_table_index.JWP, 0, t)
199 autospacing = function(t)
200 return tex.getattribute('ltj@autospc')
202 autoxspacing = function(t)
203 return tex.getattribute('ltj@autoxspc')
205 differentjfm = function(t)
206 local f, r = luatexja.jfmglue.diffmet_rule, '???'
207 if f == math.max then r = 'large'
208 elseif f == math.min then r = 'small'
209 elseif f == math.two_average then r = 'average'
210 elseif f == math.two_paverage then r = 'paverage'
211 elseif f == math.two_pleft then r = 'pleft'
212 elseif f == math.two_pright then r = 'pright'
213 elseif f == math.two_add then r = 'both'
217 direction = function()
218 local v = ltjd.get_dir_count()
219 if math.abs(tex.nest[tex.nest.ptr].mode) == ltjs.mmode and v == dir_table.dir_tate then
220 v = dir_table.dir_utod
224 adjustdir = ltjd.get_adjust_dir_count,
227 local unary_pars = luatexja.unary_pars
228 function luatexja.ext_get_parameter_unary(k)
229 if unary_pars[k] then
230 tex.write(tostring(unary_pars[k](tex.getcount('ltj@@stack'))))
232 ltjb.stop_time_measure('get_par')
237 -- EXT: print parameters that need arguments
239 luatexja.binary_pars = {
240 jacharrange = function(c, t)
241 if type(c)~='number' or c<0 or c>31*ltjc.ATTR_RANGE then
243 ltjb.package_error('luatexja',
244 'invalid character range number (' .. tostring(c) .. ')',
245 'A character range number should be in the range 1..'
246 .. 31*ltjc.ATTR_RANGE .. ",\n"..
247 'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
248 c=0 -- external range 217 == internal range 0
249 elseif c==31*ltjc.ATTR_RANGE then c=0
251 -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
252 return (c<0) and 1 or ltjc.get_range_setting(c)
254 prebreakpenalty = function(c, t)
255 return ltjs.get_stack_table(stack_table_index.PRE
256 + ltjb.in_unicode(c, true), 0, t)
258 postbreakpenalty = function(c, t)
259 return ltjs.get_stack_table(stack_table_index.POST
260 + ltjb.in_unicode(c, true), 0, t)
262 kcatcode = function(c, t)
263 return ltjs.get_stack_table(stack_table_index.KCAT
264 + ltjb.in_unicode(c, false), 0, t)
266 chartorange = function(c, t)
267 return ltjc.char_to_range(ltjb.in_unicode(c, false))
269 jaxspmode = function(c, t)
270 return ltjs.get_stack_table(stack_table_index.XSP
271 + ltjb.in_unicode(c, true), 3, t)
273 boxdir = function(c, t)
274 if type(c)~='number' or c<0 or c>65535 then
275 ltjb.package_error('luatexja',
276 'Bad register code (' .. tostring(c) .. ')',
277 'A register must be between 0 and 65535.\n'..
278 'I changed this one to zero.')
281 return ltjd.get_register_dir(c)
284 local binary_pars = luatexja.binary_pars
286 binary_pars.alxspmode = binary_pars.jaxspmode
287 function luatexja.ext_get_parameter_binary(k,c)
288 if binary_pars[k] then
289 tex.write(tostring(binary_pars[k](c,tex.getcount('ltj@@stack'))))
291 ltjb.stop_time_measure('get_par')
295 -- EXT: print \global if necessary
296 function luatexja.ext_print_global()
297 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
303 local start_time_measure, stop_time_measure
304 = ltjb.start_time_measure, ltjb.stop_time_measure
305 local nullfunc = function (n) return n end
306 local to_node = node.direct.tonode
307 local to_direct = node.direct.todirect
308 local ensure_tex_attr = ltjb.ensure_tex_attr
310 -- mode = true iff main_process is called from pre_linebreak_filter
311 local function main_process(head, mode, dir, gc)
312 ensure_tex_attr(attr_icflag, 0)
313 if gc == 'fin_row' then return head
315 --luatexja.ext_show_node_list(head, 'T> ', print)
316 start_time_measure('jfmglue')
317 local p = ltjj.main(to_direct(head),mode, dir)
318 stop_time_measure('jfmglue')
323 local function adjust_icflag(h)
324 -- kern from luaotfload will have icflag = 1
325 -- (same as italic correction)
326 ensure_tex_attr(attr_icflag, 1)
331 ltjb.add_to_callback(
332 'pre_linebreak_filter',
333 function (head,groupcode)
334 return main_process(head, true, tex.textdir, groupcode)
336 luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor')+1)
337 ltjb.add_to_callback(
339 function (head,groupcode,size,packtype, dir)
340 return main_process(head, false, dir, groupcode)
342 luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor')+1)
343 ltjb.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
344 ltjb.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
349 function luatexja.ext_cleanup()
350 ltjf.cleanup_size_cache()
351 ltjd.remove_end_whatsit()
359 local node_type = node.type
360 local node_next = node.next
361 local has_attr = node.has_attribute
363 local id_penalty = node.id('penalty')
364 local id_glyph = node.id('glyph')
365 local id_glue = node.id('glue')
366 local id_kern = node.id('kern')
367 local id_hlist = node.id('hlist')
368 local id_vlist = node.id('vlist')
369 local id_rule = node.id('rule')
370 local id_math = node.id('math')
371 local id_whatsit = node.id('whatsit')
372 local sid_user = node.subtype('user_defined')
374 local function get_attr_icflag(p)
375 return (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
378 local prefix, inner_depth
380 local function debug_show_node_X(p,print_fn, limit)
383 local pt=node_type(p.id)
384 local base = prefix .. string.format('%X', get_attr_icflag(p))
385 .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
386 if pt == 'glyph' then
387 s = base .. ' ' .. utf.char(p.char) .. ' '
389 .. ' (' .. print_scaled(p.height) .. '+'
390 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
392 elseif pt=='hlist' or pt=='vlist' or pt=='unset'or pt=='ins' then
394 s = base .. '(' .. print_scaled(p.height) .. '+'
395 .. print_scaled(p.depth) .. ')'
396 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
398 s = base .. '(' .. print_scaled(p.height) .. '+'
399 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
400 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
402 if (p.shift or 0)~=0 then
403 s = s .. ', shifted ' .. print_scaled(p.shift)
405 if p.glue_set and p.glue_sign ==2 or ( p.glue_sign==1 and p.glue_set>0) then
406 s = s .. ' glue set '
407 if p.glue_sign == 2 then s = s .. '-' end
408 s = s .. tostring(floor(p.glue_set*10000)/10000)
409 if p.glue_order == 0 then
413 for i = 2, p.glue_order do s = s .. 'l' end
416 if get_attr_icflag(p) == icflag_table.PACKED then
420 local bid = inner_depth
421 prefix, inner_depth = prefix.. '.', inner_depth + 1
422 if inner_depth < limit then
423 for q in node.traverse(p.head) do
424 debug_show_node_X(q, print_fn, limit)
428 elseif pt=='rule' then
429 s = base .. '(' .. print_scaled(p.height) .. '+'
430 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
431 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
433 elseif pt == 'glue' then
434 s = base .. ' ' .. print_spec(p)
435 if get_attr_icflag(p)>icflag_table.KINSOKU
436 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
437 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
438 elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP then
439 s = s .. ' (kanjiskip)'
440 elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP_JFM then
441 s = s .. ' (kanjiskip, JFM specified)'
442 elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP then
443 s = s .. ' (xkanjiskip)'
444 elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP_JFM then
445 s = s .. ' (xkanjiskip, JFM specified)'
448 elseif pt == 'kern' then
449 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
451 s = s .. ' (for accent)'
452 elseif get_attr_icflag(p)==icflag_table.IC_PROCESSED then
453 s = s .. ' (italic correction)'
454 -- elseif get_attr_icflag(p)==ITALIC then
455 -- s = s .. ' (italic correction)'
456 elseif get_attr_icflag(p)>icflag_table.KINSOKU
457 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
458 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
461 elseif pt == 'penalty' then
462 s = base .. ' ' .. tostring(p.penalty)
463 if get_attr_icflag(p)==icflag_table.KINSOKU then
464 s = s .. ' (for kinsoku)'
467 elseif pt == 'whatsit' then
469 if p.subtype==sid_user then
470 local t = tostring(p.user_id) .. ' (' ..
471 luatexbase.get_user_whatsit_name(p.user_id) .. ') '
472 if p.type ~= 110 then
473 s = s .. ' userid:' .. t .. p.value
476 s = s .. ' userid:' .. t .. '(node list)'
477 if p.user_id==userid_table.DIR then
478 s = s .. ' dir: ' .. tostring(node.has_attribute(p, attr_dir))
481 local bid = inner_depth
482 prefix, inner_depth =prefix.. '.', inner_depth + 1
483 if inner_depth < limit then
484 for q in node.traverse(p.value) do
485 debug_show_node_X(q, print_fn, limit)
488 prefix, inner_depth = k, bid
491 s = s .. (node.subtype(p.subtype) or '')
493 s = s .. ' stream=' .. p.stream
496 print_fn(s .. ' [' .. i .. '] = ' .. tostring(p.data[i].csname))
502 -------- math node --------
503 elseif pt=='noad' then
504 s = base ; print_fn(s)
506 prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn);
509 prefix = k .. '^'; debug_show_node_X(p.sup, print_fn);
512 prefix = k .. '_'; debug_show_node_X(p.sub, print_fn);
515 elseif pt=='math_char' then
516 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
518 elseif pt=='sub_box' or pt=='sub_mlist' then
522 for q in node.traverse(p.head) do
523 debug_show_node_X(q, print_fn)
531 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
536 debug_show_node_X(head, print_fn, lim or 1/0); head = node_next(head)
539 print_fn(prefix .. ' (null list)')
542 function luatexja.ext_show_node(head,depth,print_fn, lim)
546 debug_show_node_X(head, print_fn, lim or 1/0)
548 print_fn(prefix .. ' (null list)')