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
37 luatexja.node_inherit_attr = node_inherit_attr
38 luatexja.dnode_new = function (id, subtype, b, a)
39 return node_inherit_attr(node_new(id, subtype), b, a)
43 local setfield = node.direct.setfield
44 luatexja.setglue = node.direct.setglue or
45 function(g,w,st,sh,sto,sho)
46 setfield(g,'width', w or 0); setfield(g,'stretch',st or 0); setfield(g,'shrink', sh or 0)
47 setfield(g,'stretch_order',sto or 0)
48 setfield(g,'shrink_order', sho or 0)
50 local getfield = node.direct.getfield
51 luatexja.getglue = node.direct.getglue or
53 return getfield(g,'width'), getfield(g,'stretch'), getfield(g,'shrink'),
54 getfield(g,'stretch_order'), getfield(g,'shrink_order')
59 local icflag_table = {}
60 luatexja.icflag_table = icflag_table
61 icflag_table.ITALIC = 1
62 icflag_table.PACKED = 2
63 icflag_table.KINSOKU = 3
64 icflag_table.FROM_JFM = 4
65 icflag_table.KANJI_SKIP = 68 -- = 4+64
66 icflag_table.KANJI_SKIP_JFM = 69
67 icflag_table.XKANJI_SKIP = 70
68 icflag_table.XKANJI_SKIP_JFM = 71
69 icflag_table.LINEEND = 72
70 icflag_table.PROCESSED = 73
71 icflag_table.IC_PROCESSED = 74
72 icflag_table.BOXBDD = 75
73 icflag_table.SPECIAL_JAGLUE = 76
74 -- 段落組版中のノードリストでは通常のノード (not whatsit) だが
75 -- 和文処理グルー挿入プロセスで長さが決定されるもの
76 icflag_table.PROCESSED_BEGIN_FLAG = 4096 -- sufficiently large power of 2
79 luatexja.stack_table_index = stack_ind
80 stack_ind.PRE = 0x200000 -- characterごと
81 stack_ind.POST = 0x400000 -- characterごと
82 stack_ind.KCAT = 0x600000 -- characterごと
83 stack_ind.XSP = 0x800000 -- characterごと
84 stack_ind.RIPRE = 0xA00000 -- characterごと,ruby pre
85 stack_ind.RIPOST = 0xC00000 -- characterごと,ruby post
86 stack_ind.JWP = 0 -- これだけ
87 stack_ind.KSK = 1 -- これだけ
88 stack_ind.XSK = 2 -- これだけ
89 stack_ind.MJT = 0x100 -- 0--255
90 stack_ind.MJS = 0x200 -- 0--255
91 stack_ind.MJSS = 0x300 -- 0--255
92 stack_ind.KSJ = 0x400 -- 0--9
95 luatexja.userid_table = uid_table
96 uid_table.IHB = luatexbase.newuserwhatsitid('inhibitglue', 'luatexja') -- \inhibitglue
97 uid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
98 uid_table.BPAR = luatexbase.newuserwhatsitid('begin_par', 'luatexja') -- 「段落始め」
99 uid_table.DIR = luatexbase.newuserwhatsitid('direction', 'luatexja') -- 組方向
100 uid_table.BOXB = luatexbase.newuserwhatsitid('box_boundary', 'luatexja') -- 「ボックス始め・終わり」
101 uid_table.JA_AL_BDD = luatexbase.newuserwhatsitid('ja_al_boundary', 'luatexja')
104 luatexja.dir_table = dir_table
105 dir_table.dir_dtou = 1
106 dir_table.dir_tate = 3
107 dir_table.dir_yoko = 4
108 dir_table.dir_math_mod = 8
109 dir_table.dir_node_auto = 128 -- 組方向を合わせるために自動で作られたもの
110 dir_table.dir_node_manual = 256 -- 寸法代入によって作られたもの
111 dir_table.dir_utod = dir_table.dir_tate + dir_table.dir_math_mod
112 -- 「縦数式ディレクション」 in pTeX
115 local load_module = luatexja.load_module
116 load_module 'base'; local ltjb = luatexja.base
117 if tex.outputmode==0 then
118 ltjb.package_error('luatexja',
119 'DVI output is not supported in LuaTeX-ja',
120 'Use lua*tex instead dvilua*tex.')
122 load_module 'rmlgbm'; local ltjr = luatexja.rmlgbm -- must be 1st
123 if luatexja_debug then load_module('debug') end
124 load_module 'lotf_aux'; local ltju = luatexja.lotf_aux
125 load_module 'charrange'; local ltjc = luatexja.charrange
126 load_module 'stack'; local ltjs = luatexja.stack
127 load_module 'direction'; local ltjd = luatexja.direction -- +1 hlist +1 attr_list
128 load_module 'lineskip'; local ltjl = luatexja.lineskip -- +1 hlist +1 attr_list
129 load_module 'jfont'; local ltjf = luatexja.jfont
130 load_module 'inputbuf'; local ltji = luatexja.inputbuf
131 load_module 'pretreat'; local ltjp = luatexja.pretreat
132 load_module 'setwidth'; local ltjw = luatexja.setwidth
133 load_module 'jfmglue'; local ltjj = luatexja.jfmglue -- +1 glue +1 gs +1 attr_list
134 load_module 'math'; local ltjm = luatexja.math
135 load_module 'base'; local ltjb = luatexja.base
137 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
138 local attr_jchar_code = luatexbase.attributes['ltj@charcode']
139 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
140 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
141 local attr_icflag = luatexbase.attributes['ltj@icflag']
142 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
143 local attr_dir = luatexbase.attributes['ltj@dir']
144 local cat_lp = luatexbase.catcodetables['latex-package']
146 -- Three aux. functions, borrowed from tex.web
148 local floor = math.floor
149 local function print_scaled(s)
150 local out, delta = '', 10
151 if s<0 then s, out = -s, out..'-' end
152 out=out..tostring(floor(s/65536)) .. '.'
155 if delta>65536 then s=s+32768-50000 end
156 out=out .. tostring(floor(s/65536))
157 s=10*(s%65536); delta=delta*10
161 luatexja.print_scaled = print_scaled
163 local function print_glue(d,order)
164 local out=print_scaled(d)
167 while order>1 do out=out..'l'; order=order-1 end
173 local function print_spec(p)
174 local out=print_scaled(p.width)..'pt'
176 out=out..' plus '..print_glue(p.stretch,p.stretch_order)
179 out=out..' minus '..print_glue(p.shrink,p.shrink_order)
185 ------------------------------------------------------------------------
186 -- CODE FOR GETTING/SETTING PARAMETERS
187 ------------------------------------------------------------------------
189 -- EXT: print parameters that don't need arguments
191 local tex_getattr = tex.getattribute
192 local function getattr(a)
193 local r = tex_getattr(a)
194 return (r==-0x7FFFFFFF) and 0 or r
196 luatexja.unary_pars = {
197 yalbaselineshift = function(t)
198 return print_scaled(getattr('ltj@yablshift'))..'pt'
200 yjabaselineshift = function(t)
201 return print_scaled(getattr('ltj@ykblshift'))..'pt'
203 talbaselineshift = function(t)
204 return print_scaled(getattr('ltj@tablshift'))..'pt'
206 tjabaselineshift = function(t)
207 return print_scaled(getattr('ltj@tkblshift'))..'pt'
209 kanjiskip = function(t)
210 return print_spec(ltjs.get_stack_skip(stack_ind.KSK, t))
212 xkanjiskip = function(t)
213 return print_spec(ltjs.get_stack_skip(stack_ind.XSK, t))
215 jcharwidowpenalty = function(t)
216 return ltjs.get_stack_table(stack_ind.JWP, 0, t)
218 autospacing = function(t)
219 return getattr('ltj@autospc')
221 autoxspacing = function(t)
222 return getattr('ltj@autoxspc')
224 differentjfm = function(t)
225 local f, r = luatexja.jfmglue.diffmet_rule, '???'
226 if f == math.max then r = 'large'
227 elseif f == math.min then r = 'small'
228 elseif f == math.two_average then r = 'average'
229 elseif f == math.two_paverage then r = 'paverage'
230 elseif f == math.two_pleft then r = 'pleft'
231 elseif f == math.two_pright then r = 'pright'
232 elseif f == math.two_add then r = 'both'
236 direction = function()
237 local v = ltjd.get_dir_count()
238 if math.abs(tex.nest[tex.nest.ptr].mode) == ltjs.mmode and v == dir_table.dir_tate then
239 v = dir_table.dir_utod
243 adjustdir = ltjd.get_adjust_dir_count,
246 local unary_pars = luatexja.unary_pars
247 local scan_arg = token.scan_argument
248 function luatexja.ext_get_parameter_unary()
250 if unary_pars[k] then
251 tex.write(tostring(unary_pars[k](tex.getcount('ltj@@stack'))))
253 ltjb.stop_time_measure('get_par')
258 -- EXT: print parameters that need arguments
260 luatexja.binary_pars = {
261 jacharrange = function(c, t)
262 if type(c)~='number' or c<-1 or c>31*ltjc.ATTR_RANGE then
263 -- 0, -1 はエラーにしない(隠し)
264 ltjb.package_error('luatexja',
265 'invalid character range number (' .. tostring(c) .. ')',
266 'A character range number should be in the range 1..'
267 .. 31*ltjc.ATTR_RANGE .. ",\n"..
268 'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
269 c=0 -- external range 217 == internal range 0
270 elseif c==31*ltjc.ATTR_RANGE then c=0
272 -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
273 return (c<0) and 1 or ltjc.get_range_setting(c)
275 prebreakpenalty = function(c, t)
276 return ltjs.get_stack_table(stack_ind.PRE + ltjb.in_unicode(c, true), 0, t)
278 postbreakpenalty = function(c, t)
279 return ltjs.get_stack_table(stack_ind.POST + ltjb.in_unicode(c, true), 0, t)
281 kcatcode = function(c, t)
282 return ltjs.get_stack_table(stack_ind.KCAT + ltjb.in_unicode(c, false), 0, t)
284 chartorange = function(c, t)
285 return ltjc.char_to_range(ltjb.in_unicode(c, false))
287 jaxspmode = function(c, t)
288 return ltjs.get_stack_table(stack_ind.XSP + ltjb.in_unicode(c, true), 3, t)
290 boxdir = function(c, t)
291 if type(c)~='number' or c<0 or c>65535 then
292 ltjb.package_error('luatexja',
293 'Bad register code (' .. tostring(c) .. ')',
294 'A register must be between 0 and 65535.\n'..
295 'I changed this one to zero.')
298 return ltjd.get_register_dir(c)
301 local binary_pars = luatexja.binary_pars
302 local scan_arg, scan_int = token.scan_argument, token.scan_int
303 binary_pars.alxspmode = binary_pars.jaxspmode
304 function luatexja.ext_get_parameter_binary(k, c)
305 if binary_pars[k] then
306 tex.write(tostring(binary_pars[k](c,tex.getcount('ltj@@stack'))))
308 ltjb.stop_time_measure('get_par')
312 -- EXT: print \global if necessary
313 function luatexja.ext_print_global()
314 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
320 local start_time_measure, stop_time_measure
321 = ltjb.start_time_measure, ltjb.stop_time_measure
322 local nullfunc = function (n) return n end
323 local to_node = node.direct.tonode
324 local to_direct = node.direct.todirect
325 local ensure_tex_attr = ltjb.ensure_tex_attr
327 -- mode = true iff main_process is called from pre_linebreak_filter
328 local function main_process(head, mode, dir, gc)
329 ensure_tex_attr(attr_icflag, 0)
330 if gc == 'fin_row' then return head
332 start_time_measure('jfmglue')
333 local p = ltjj.main(to_direct(head),mode, dir)
334 stop_time_measure('jfmglue')
339 local function adjust_icflag(h)
340 -- kern from luaotfload will have icflag = 1
341 -- (same as italic correction)
342 ensure_tex_attr(attr_icflag, 1)
347 ltjb.add_to_callback(
348 'pre_linebreak_filter',
349 function (head,groupcode)
350 return main_process(head, true, tex.textdir, groupcode)
352 luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor')+1)
353 ltjb.add_to_callback(
355 function (head,groupcode,size,packtype, dir)
356 return main_process(head, false, dir, groupcode)
358 luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor')+1)
359 ltjb.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
360 ltjb.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
365 local id_glyph = node.id('glyph')
366 function luatexja.pltx_composite_last_node_char()
367 local n = tex.nest[tex.nest.ptr].tail
370 if n.id==id_glyph then
371 while n.componetns and n.subtype and n.subtype%4 >= 2 do
382 local cache_ver = 3 -- must be same as ltj-kinsoku.tex
383 local cache_outdate_fn = function (t) return t.version~=cache_ver end
384 local t = ltjs.charprop_stack_table
385 function luatexja.load_kinsoku()
386 for i,_ in pairs(t) do t[i]=nil end
387 local kinsoku = ltjb.load_cache('ltj-kinsoku_default',cache_outdate_fn)
388 if kinsoku and kinsoku[1] then
391 t[0] = {}; tex.print(cat_lp, '\\input ltj-kinsoku.tex\\relax')
393 luatexja.load_kinsoku=nil
401 local node_type = node.type
402 local node_next = node.next
403 local has_attr = node.has_attribute
405 local id_penalty = node.id('penalty')
406 local id_glyph = node.id('glyph')
407 local id_glue = node.id('glue')
408 local id_kern = node.id('kern')
409 local id_hlist = node.id('hlist')
410 local id_vlist = node.id('vlist')
411 local id_rule = node.id('rule')
412 local id_math = node.id('math')
413 local id_whatsit = node.id('whatsit')
414 local sid_user = node.subtype('user_defined')
416 local prefix, inner_depth
417 local utfchar = utf.char
418 local function debug_show_node_X(p,print_fn, limit, inner_depth)
421 local pt, pic = node_type(p.id), (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
422 local base = prefix .. string.format('%X', pic) .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
423 if pt == 'glyph' then
425 .. (p.char<0xF0000 and utfchar(p.char) or '')
426 .. string.format(' (U+%X) ', p.char)
427 .. tostring(p.font) .. ' (' .. print_scaled(p.height) .. '+'
428 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
429 if p.xoffset~=0 or p.yoffset~=0 then
430 s = s .. ' off: (' .. print_scaled(p.xoffset)
431 .. ',' .. print_scaled(p.yoffset) .. ')'
434 elseif pt=='hlist' or pt=='vlist' or pt=='unset'or pt=='ins' then
436 s = base .. '(' .. print_scaled(p.height) .. '+'
437 .. print_scaled(p.depth) .. ')'
438 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
440 s = base .. '(' .. print_scaled(p.height) .. '+'
441 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
442 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
444 if (p.shift or 0)~=0 then
445 s = s .. ', shifted ' .. print_scaled(p.shift)
447 if p.glue_set and p.glue_sign ==2 or ( p.glue_sign==1 and p.glue_set>0) then
448 s = s .. ' glue set '
449 if p.glue_sign == 2 then s = s .. '-' end
450 s = s .. tostring(floor(p.glue_set*10000)/10000)
451 if p.glue_order == 0 then s = s .. 'pt'
454 for i = 2, p.glue_order do s = s .. 'l' end
457 if pic == icflag_table.PACKED then s = s .. ' (packed)' end
459 local bid = inner_depth
460 prefix, inner_depth = prefix.. '.', inner_depth + 1
461 if inner_depth < limit then
462 for q in node.traverse(p.head) do
463 debug_show_node_X(q, print_fn, limit, inner_depth)
467 elseif pt=='rule' then
468 s = base .. '(' .. print_scaled(p.height) .. '+'
469 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
470 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
472 elseif pt=='disc' then
474 local bid = inner_depth
475 if inner_depth < limit then
476 prefix, inner_depth = k.. 'p.', inner_depth + 1
477 for q in node.traverse(p.pre) do
478 debug_show_node_X(q, print_fn, limit, inner_depth)
481 for q in node.traverse(p.post) do
482 debug_show_node_X(q, print_fn, limit, inner_depth)
485 for q in node.traverse(p.replace) do
486 debug_show_node_X(q, print_fn, limit, inner_depth)
490 elseif pt == 'glue' then
491 s = base .. ' ' .. print_spec(p)
492 if pic>icflag_table.KINSOKU and pic<icflag_table.KANJI_SKIP then
493 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
494 elseif pic==icflag_table.KANJI_SKIP then
495 s = s .. ' (kanjiskip)'
496 elseif pic==icflag_table.KANJI_SKIP_JFM then
497 s = s .. ' (kanjiskip, JFM specified)'
498 elseif pic==icflag_table.XKANJI_SKIP then
499 s = s .. ' (xkanjiskip)'
500 elseif pic==icflag_table.XKANJI_SKIP_JFM then
501 s = s .. ' (xkanjiskip, JFM specified)'
504 elseif pt == 'kern' then
505 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
507 s = s .. ' (for accent)'
508 elseif pic==icflag_table.IC_PROCESSED then
509 s = s .. ' (italic correction)'
510 elseif pic==icflag_table.LINEEND then
511 s = s .. ' (end-of-line)'
512 elseif pic>icflag_table.KINSOKU
513 and pic<icflag_table.KANJI_SKIP then
514 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
517 elseif pt == 'penalty' then
518 s = base .. ' ' .. tostring(p.penalty)
519 if pic==icflag_table.KINSOKU then s = s .. ' (for kinsoku)' end
521 elseif pt == 'dir' then
522 print_fn(base .. ' ' .. tostring(p.dir) .. ' (level ' .. tostring(p.level) .. ')')
523 elseif pt == 'whatsit' then
525 if p.subtype==sid_user then
526 local t = tostring(p.user_id) .. ' (' ..
527 luatexbase.get_user_whatsit_name(p.user_id) .. ') '
528 if p.type ~= 110 then
529 s = s .. ' userid:' .. t .. p.value
532 s = s .. ' userid:' .. t .. '(node list)'
533 if p.user_id==uid_table.DIR then
534 s = s .. ' dir: ' .. tostring(node.has_attribute(p, attr_dir))
537 local bid = inner_depth
538 prefix, inner_depth = prefix.. '.', inner_depth + 1
539 if inner_depth < limit then
540 for q in node.traverse(p.value) do
541 debug_show_node_X(q, print_fn, limit, inner_depth)
544 prefix, inner_depth = k, bid
547 s = s .. (node.subtype(p.subtype) or '')
549 s = s .. ' stream=' .. p.stream
552 print_fn(s .. ' [' .. i .. '] = ' .. tostring(p.data[i] and p.date[i].csname))
554 elseif p.subtype==16 then
555 s = s .. ' mode=' .. p.mode .. ', literal="' .. p.data .. '"'
561 -------- math node --------
562 elseif pt=='noad' then
565 prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn, limit, inner_depth);
568 prefix = k .. '^'; debug_show_node_X(p.sup, print_fn, limit, inner_depth);
571 prefix = k .. '_'; debug_show_node_X(p.sub, print_fn, limit, inner_depth);
574 elseif pt=='math_char' then
575 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utfchar(p.char)
577 elseif pt=='sub_box' or pt=='sub_mlist' then
581 for q in node.traverse(p.head) do
582 debug_show_node_X(q, print_fn, limit, inner_depth)
590 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
595 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth); head = node_next(head)
598 print_fn(prefix .. ' (null list)')
601 function luatexja.ext_show_node(head,depth,print_fn, lim)
605 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth)
607 print_fn(prefix .. ' (null list)')