2 ------------------------------------------------------------------------
3 local function load_lua(fn)
4 local found = kpse.find_file(fn, 'tex')
6 tex.error("LuaTeX-ja error: File `" .. fn .. "' not found")
8 texio.write_nl('(' .. found .. ')'); dofile(found)
11 luatexja.load_lua = load_lua
12 function luatexja.load_module(name) require('ltj-' .. name.. '.lua') end
15 local dnode = node.direct
16 local getfield, traverse = dnode.getfield, dnode.traverse
17 local node_new, set_attr, get_attr = dnode.new, dnode.set_attribute, dnode.get_attribute
18 local set_attrlist, get_attrlist = dnode.setattributelist, dnode.getattributelist
19 local unset_attr = dnode.unset_attribute
20 local attr_icflag = luatexbase.attributes['ltj@icflag']
21 local function node_inherit_attr(n, b, a)
23 local attrlist = get_attrlist(b or a)
24 local nic = get_attr(n, attr_icflag)
25 set_attrlist(n, attrlist); set_attr(n, attr_icflag, nic)
27 for na in traverse(attrlist) do
28 local id = getfield(na, 'number')
29 if id and id~=attr_icflag and getfield(na, 'value')~=get_attr(a, id) then
36 luatexja.node_inherit_attr = node_inherit_attr
37 luatexja.dnode_new = function (id, subtype, b, a)
38 local n = node_new(id, subtype); node_inherit_attr(n, b, a); return n
42 local setfield = node.direct.setfield
43 luatexja.setglue = node.direct.setglue or
44 function(g,w,st,sh,sto,sho)
45 setfield(g,'width', w or 0); setfield(g,'stretch',st or 0); setfield(g,'shrink', sh or 0)
46 setfield(g,'stretch_order',sto or 0)
47 setfield(g,'shrink_order', sho or 0)
49 local getfield = node.direct.getfield
50 luatexja.getglue = node.direct.getglue or
52 return getfield(g,'width'), getfield(g,'stretch'), getfield(g,'shrink'),
53 getfield(g,'stretch_order'), getfield(g,'shrink_order')
58 local icflag_table = {}
59 luatexja.icflag_table = icflag_table
60 icflag_table.ITALIC = 1
61 icflag_table.PACKED = 2
62 icflag_table.KINSOKU = 3
63 icflag_table.FROM_JFM = 4
64 icflag_table.KANJI_SKIP = 68 -- = 4+64
65 icflag_table.KANJI_SKIP_JFM = 69
66 icflag_table.XKANJI_SKIP = 70
67 icflag_table.XKANJI_SKIP_JFM = 71
68 icflag_table.LINEEND = 72
69 icflag_table.PROCESSED = 73
70 icflag_table.IC_PROCESSED = 74
71 icflag_table.BOXBDD = 75
72 icflag_table.SPECIAL_JAGLUE = 76
73 -- 段落組版中のノードリストでは通常のノード (not whatsit) だが
74 -- 和文処理グルー挿入プロセスで長さが決定されるもの
75 icflag_table.PROCESSED_BEGIN_FLAG = 4096 -- sufficiently large power of 2
78 luatexja.stack_table_index = stack_ind
79 stack_ind.PRE = 0x200000 -- characterごと
80 stack_ind.POST = 0x400000 -- characterごと
81 stack_ind.KCAT = 0x600000 -- characterごと
82 stack_ind.XSP = 0x800000 -- characterごと
83 stack_ind.RIPRE = 0xA00000 -- characterごと,ruby pre
84 stack_ind.RIPOST = 0xC00000 -- characterごと,ruby post
85 stack_ind.JWP = 0 -- これだけ
86 stack_ind.KSK = 1 -- これだけ
87 stack_ind.XSK = 2 -- これだけ
88 stack_ind.MJT = 0x100 -- 0--255
89 stack_ind.MJS = 0x200 -- 0--255
90 stack_ind.MJSS = 0x300 -- 0--255
91 stack_ind.KSJ = 0x400 -- 0--9
94 luatexja.userid_table = uid_table
95 uid_table.IHB = luatexbase.newuserwhatsitid('inhibitglue', 'luatexja') -- \inhibitglue
96 uid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
97 uid_table.BPAR = luatexbase.newuserwhatsitid('begin_par', 'luatexja') -- 「段落始め」
98 uid_table.DIR = luatexbase.newuserwhatsitid('direction', 'luatexja') -- 組方向
99 uid_table.BOXB = luatexbase.newuserwhatsitid('box_boundary', 'luatexja') -- 「ボックス始め・終わり」
100 uid_table.JA_AL_BDD = luatexbase.newuserwhatsitid('ja_al_boundary', 'luatexja')
103 luatexja.dir_table = dir_table
104 dir_table.dir_dtou = 1
105 dir_table.dir_tate = 3
106 dir_table.dir_yoko = 4
107 dir_table.dir_math_mod = 8
108 dir_table.dir_node_auto = 128 -- 組方向を合わせるために自動で作られたもの
109 dir_table.dir_node_manual = 256 -- 寸法代入によって作られたもの
110 dir_table.dir_utod = dir_table.dir_tate + dir_table.dir_math_mod
111 -- 「縦数式ディレクション」 in pTeX
114 local load_module = luatexja.load_module
115 load_module 'base'; local ltjb = luatexja.base
116 if tex.outputmode==0 then
117 ltjb.package_error('luatexja',
118 'DVI output is not supported in LuaTeX-ja',
119 'Use lua*tex instead dvilua*tex.')
121 load_module 'rmlgbm'; local ltjr = luatexja.rmlgbm -- must be 1st
122 if luatexja_debug then load_module('debug') end
123 load_module 'lotf_aux'; local ltju = luatexja.lotf_aux
124 load_module 'charrange'; local ltjc = luatexja.charrange
125 load_module 'stack'; local ltjs = luatexja.stack
126 load_module 'direction'; local ltjd = luatexja.direction -- +1 hlist +1 attr_list
127 load_module 'lineskip'; local ltjl = luatexja.lineskip -- +1 hlist +1 attr_list
128 load_module 'jfont'; local ltjf = luatexja.jfont
129 load_module 'inputbuf'; local ltji = luatexja.inputbuf
130 load_module 'pretreat'; local ltjp = luatexja.pretreat
131 load_module 'setwidth'; local ltjw = luatexja.setwidth
132 load_module 'jfmglue'; local ltjj = luatexja.jfmglue -- +1 glue +1 gs +1 attr_list
133 load_module 'math'; local ltjm = luatexja.math
134 load_module 'base'; local ltjb = luatexja.base
136 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
137 local attr_jchar_code = luatexbase.attributes['ltj@charcode']
138 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
139 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
140 local attr_icflag = luatexbase.attributes['ltj@icflag']
141 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
142 local attr_dir = luatexbase.attributes['ltj@dir']
143 local cat_lp = luatexbase.catcodetables['latex-package']
145 -- Three aux. functions, borrowed from tex.web
147 local floor = math.floor
148 local function print_scaled(s)
149 local out, delta = '', 10
150 if s<0 then s, out = -s, out..'-' end
151 out=out..tostring(floor(s/65536)) .. '.'
154 if delta>65536 then s=s+32768-50000 end
155 out=out .. tostring(floor(s/65536))
156 s=10*(s%65536); delta=delta*10
160 luatexja.print_scaled = print_scaled
162 local function print_glue(d,order)
163 local out=print_scaled(d)
166 while order>1 do out=out..'l'; order=order-1 end
172 local function print_spec(p)
173 local out=print_scaled(p.width)..'pt'
175 out=out..' plus '..print_glue(p.stretch,p.stretch_order)
178 out=out..' minus '..print_glue(p.shrink,p.shrink_order)
184 ------------------------------------------------------------------------
185 -- CODE FOR GETTING/SETTING PARAMETERS
186 ------------------------------------------------------------------------
188 -- EXT: print parameters that don't need arguments
190 local tex_getattr = tex.getattribute
191 local function getattr(a)
192 local r = tex_getattr(a)
193 return (r==-0x7FFFFFFF) and 0 or r
195 luatexja.unary_pars = {
196 yalbaselineshift = function(t)
197 return print_scaled(getattr('ltj@yablshift'))..'pt'
199 yjabaselineshift = function(t)
200 return print_scaled(getattr('ltj@ykblshift'))..'pt'
202 talbaselineshift = function(t)
203 return print_scaled(getattr('ltj@tablshift'))..'pt'
205 tjabaselineshift = function(t)
206 return print_scaled(getattr('ltj@tkblshift'))..'pt'
208 kanjiskip = function(t)
209 return print_spec(ltjs.get_stack_skip(stack_ind.KSK, t))
211 xkanjiskip = function(t)
212 return print_spec(ltjs.get_stack_skip(stack_ind.XSK, t))
214 jcharwidowpenalty = function(t)
215 return ltjs.get_stack_table(stack_ind.JWP, 0, t)
217 autospacing = function(t)
218 return getattr('ltj@autospc')
220 autoxspacing = function(t)
221 return getattr('ltj@autoxspc')
223 differentjfm = function(t)
224 local f, r = luatexja.jfmglue.diffmet_rule, '???'
225 if f == math.max then r = 'large'
226 elseif f == math.min then r = 'small'
227 elseif f == math.two_average then r = 'average'
228 elseif f == math.two_paverage then r = 'paverage'
229 elseif f == math.two_pleft then r = 'pleft'
230 elseif f == math.two_pright then r = 'pright'
231 elseif f == math.two_add then r = 'both'
235 direction = function()
236 local v = ltjd.get_dir_count()
237 if math.abs(tex.nest[tex.nest.ptr].mode) == ltjs.mmode and v == dir_table.dir_tate then
238 v = dir_table.dir_utod
242 adjustdir = ltjd.get_adjust_dir_count,
245 local unary_pars = luatexja.unary_pars
246 local scan_arg = token.scan_argument
247 function luatexja.ext_get_parameter_unary()
249 if unary_pars[k] then
250 tex.write(tostring(unary_pars[k](tex.getcount('ltj@@stack'))))
252 ltjb.stop_time_measure('get_par')
257 -- EXT: print parameters that need arguments
259 luatexja.binary_pars = {
260 jacharrange = function(c, t)
261 if type(c)~='number' or c<-1 or c>31*ltjc.ATTR_RANGE then
262 -- 0, -1 はエラーにしない(隠し)
263 ltjb.package_error('luatexja',
264 'invalid character range number (' .. tostring(c) .. ')',
265 'A character range number should be in the range 1..'
266 .. 31*ltjc.ATTR_RANGE .. ",\n"..
267 'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
268 c=0 -- external range 217 == internal range 0
269 elseif c==31*ltjc.ATTR_RANGE then c=0
271 -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
272 return (c<0) and 1 or ltjc.get_range_setting(c)
274 prebreakpenalty = function(c, t)
275 return ltjs.get_stack_table(stack_ind.PRE + ltjb.in_unicode(c, true), 0, t)
277 postbreakpenalty = function(c, t)
278 return ltjs.get_stack_table(stack_ind.POST + ltjb.in_unicode(c, true), 0, t)
280 kcatcode = function(c, t)
281 return ltjs.get_stack_table(stack_ind.KCAT + ltjb.in_unicode(c, false), 0, t)
283 chartorange = function(c, t)
284 return ltjc.char_to_range(ltjb.in_unicode(c, false))
286 jaxspmode = function(c, t)
287 return ltjs.get_stack_table(stack_ind.XSP + ltjb.in_unicode(c, true), 3, t)
289 boxdir = function(c, t)
290 if type(c)~='number' or c<0 or c>65535 then
291 ltjb.package_error('luatexja',
292 'Bad register code (' .. tostring(c) .. ')',
293 'A register must be between 0 and 65535.\n'..
294 'I changed this one to zero.')
297 return ltjd.get_register_dir(c)
300 local binary_pars = luatexja.binary_pars
301 local scan_arg, scan_int = token.scan_argument, token.scan_int
302 binary_pars.alxspmode = binary_pars.jaxspmode
303 function luatexja.ext_get_parameter_binary(k, c)
304 if binary_pars[k] then
305 tex.write(tostring(binary_pars[k](c,tex.getcount('ltj@@stack'))))
307 ltjb.stop_time_measure('get_par')
311 -- EXT: print \global if necessary
312 function luatexja.ext_print_global()
313 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
319 local start_time_measure, stop_time_measure
320 = ltjb.start_time_measure, ltjb.stop_time_measure
321 local nullfunc = function (n) return n end
322 local to_node = node.direct.tonode
323 local to_direct = node.direct.todirect
324 local ensure_tex_attr = ltjb.ensure_tex_attr
326 -- mode = true iff main_process is called from pre_linebreak_filter
327 local function main_process(head, mode, dir, gc)
328 ensure_tex_attr(attr_icflag, 0)
329 if gc == 'fin_row' then return head
331 start_time_measure('jfmglue')
332 local p = ltjj.main(to_direct(head),mode, dir)
333 stop_time_measure('jfmglue')
338 local function adjust_icflag(h)
339 -- kern from luaotfload will have icflag = 1
340 -- (same as italic correction)
341 ensure_tex_attr(attr_icflag, 1)
346 ltjb.add_to_callback(
347 'pre_linebreak_filter',
348 function (head,groupcode)
349 return main_process(head, true, tex.textdir, groupcode)
351 luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor')+1)
352 ltjb.add_to_callback(
354 function (head,groupcode,size,packtype, dir)
355 return main_process(head, false, dir, groupcode)
357 luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor')+1)
358 ltjb.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
359 ltjb.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
364 local id_glyph = node.id('glyph')
365 function luatexja.pltx_composite_last_node_char()
366 local n = tex.nest[tex.nest.ptr].tail
369 if n.id==id_glyph then
370 while n.componetns and n.subtype and n.subtype%4 >= 2 do
381 local cache_ver = 3 -- must be same as ltj-kinsoku.tex
382 local cache_outdate_fn = function (t) return t.version~=cache_ver end
383 local t = ltjs.charprop_stack_table
384 function luatexja.load_kinsoku()
385 for i,_ in pairs(t) do t[i]=nil end
386 local kinsoku = ltjb.load_cache('ltj-kinsoku_default',cache_outdate_fn)
387 if kinsoku and kinsoku[1] then
390 t[0] = {}; tex.print(cat_lp, '\\input ltj-kinsoku.tex\\relax')
392 luatexja.load_kinsoku=nil
400 local node_type = node.type
401 local node_next = node.next
402 local has_attr = node.has_attribute
404 local id_penalty = node.id('penalty')
405 local id_glyph = node.id('glyph')
406 local id_glue = node.id('glue')
407 local id_kern = node.id('kern')
408 local id_hlist = node.id('hlist')
409 local id_vlist = node.id('vlist')
410 local id_rule = node.id('rule')
411 local id_math = node.id('math')
412 local id_whatsit = node.id('whatsit')
413 local sid_user = node.subtype('user_defined')
415 local prefix, inner_depth
416 local utfchar = utf.char
417 local function debug_show_node_X(p,print_fn, limit, inner_depth)
420 local pt, pic = node_type(p.id), (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
421 local base = prefix .. string.format('%X', pic) .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
422 if pt == 'glyph' then
424 .. (p.char<0xF0000 and utfchar(p.char) or '')
425 .. string.format(' (U+%X) ', p.char)
426 .. tostring(p.font) .. ' (' .. print_scaled(p.height) .. '+'
427 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
428 if p.xoffset~=0 or p.yoffset~=0 then
429 s = s .. ' off: (' .. print_scaled(p.xoffset)
430 .. ',' .. print_scaled(p.yoffset) .. ')'
433 elseif pt=='hlist' or pt=='vlist' or pt=='unset'or pt=='ins' then
435 s = base .. '(' .. print_scaled(p.height) .. '+'
436 .. print_scaled(p.depth) .. ')'
437 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
439 s = base .. '(' .. print_scaled(p.height) .. '+'
440 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
441 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
443 if (p.shift or 0)~=0 then
444 s = s .. ', shifted ' .. print_scaled(p.shift)
446 if p.glue_set and p.glue_sign ==2 or ( p.glue_sign==1 and p.glue_set>0) then
447 s = s .. ' glue set '
448 if p.glue_sign == 2 then s = s .. '-' end
449 s = s .. tostring(floor(p.glue_set*10000)/10000)
450 if p.glue_order == 0 then s = s .. 'pt'
453 for i = 2, p.glue_order do s = s .. 'l' end
456 if pic == icflag_table.PACKED then s = s .. ' (packed)' end
458 local bid = inner_depth
459 prefix, inner_depth = prefix.. '.', inner_depth + 1
460 if inner_depth < limit then
461 for q in node.traverse(p.head) do
462 debug_show_node_X(q, print_fn, limit, inner_depth)
466 elseif pt=='rule' then
467 s = base .. '(' .. print_scaled(p.height) .. '+'
468 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
469 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
471 elseif pt=='disc' then
473 local bid = inner_depth
474 if inner_depth < limit then
475 prefix, inner_depth = k.. 'p.', inner_depth + 1
476 for q in node.traverse(p.pre) do
477 debug_show_node_X(q, print_fn, limit, inner_depth)
480 for q in node.traverse(p.post) do
481 debug_show_node_X(q, print_fn, limit, inner_depth)
484 for q in node.traverse(p.replace) do
485 debug_show_node_X(q, print_fn, limit, inner_depth)
489 elseif pt == 'glue' then
490 s = base .. ' ' .. print_spec(p)
491 if pic>icflag_table.KINSOKU and pic<icflag_table.KANJI_SKIP then
492 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
493 elseif pic==icflag_table.KANJI_SKIP then
494 s = s .. ' (kanjiskip)'
495 elseif pic==icflag_table.KANJI_SKIP_JFM then
496 s = s .. ' (kanjiskip, JFM specified)'
497 elseif pic==icflag_table.XKANJI_SKIP then
498 s = s .. ' (xkanjiskip)'
499 elseif pic==icflag_table.XKANJI_SKIP_JFM then
500 s = s .. ' (xkanjiskip, JFM specified)'
503 elseif pt == 'kern' then
504 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
506 s = s .. ' (for accent)'
507 elseif pic==icflag_table.IC_PROCESSED then
508 s = s .. ' (italic correction)'
509 elseif pic==icflag_table.LINEEND then
510 s = s .. ' (end-of-line)'
511 elseif pic>icflag_table.KINSOKU
512 and pic<icflag_table.KANJI_SKIP then
513 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
516 elseif pt == 'penalty' then
517 s = base .. ' ' .. tostring(p.penalty)
518 if pic==icflag_table.KINSOKU then s = s .. ' (for kinsoku)' end
520 elseif pt == 'dir' then
521 print_fn(base .. ' ' .. tostring(p.dir) .. ' (level ' .. tostring(p.level) .. ')')
522 elseif pt == 'whatsit' then
524 if p.subtype==sid_user then
525 local t = tostring(p.user_id) .. ' (' ..
526 luatexbase.get_user_whatsit_name(p.user_id) .. ') '
527 if p.type ~= 110 then
528 s = s .. ' userid:' .. t .. p.value
531 s = s .. ' userid:' .. t .. '(node list)'
532 if p.user_id==uid_table.DIR then
533 s = s .. ' dir: ' .. tostring(node.has_attribute(p, attr_dir))
536 local bid = inner_depth
537 prefix, inner_depth = prefix.. '.', inner_depth + 1
538 if inner_depth < limit then
539 for q in node.traverse(p.value) do
540 debug_show_node_X(q, print_fn, limit, inner_depth)
543 prefix, inner_depth = k, bid
546 s = s .. (node.subtype(p.subtype) or '')
548 s = s .. ' stream=' .. p.stream
551 print_fn(s .. ' [' .. i .. '] = ' .. tostring(p.data[i] and p.date[i].csname))
553 elseif p.subtype==16 then
554 s = s .. ' mode=' .. p.mode .. ', literal="' .. p.data .. '"'
560 -------- math node --------
561 elseif pt=='noad' then
564 prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn, limit, inner_depth);
567 prefix = k .. '^'; debug_show_node_X(p.sup, print_fn, limit, inner_depth);
570 prefix = k .. '_'; debug_show_node_X(p.sub, print_fn, limit, inner_depth);
573 elseif pt=='math_char' then
574 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utfchar(p.char)
576 elseif pt=='sub_box' or pt=='sub_mlist' then
580 for q in node.traverse(p.head) do
581 debug_show_node_X(q, print_fn, limit, inner_depth)
589 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
594 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth); head = node_next(head)
597 print_fn(prefix .. ' (null list)')
600 function luatexja.ext_show_node(head,depth,print_fn, lim)
604 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth)
606 print_fn(prefix .. ' (null list)')