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 dnode = node.direct
44 local setfield = dnode.setfield
45 luatexja.setglue = node.direct.setglue or
46 function(g,w,st,sh,sto,sho)
47 setfield(g,'width', w or 0); setfield(g,'stretch',st or 0); setfield(g,'shrink', sh or 0)
48 setfield(g,'stretch_order',sto or 0)
49 setfield(g,'shrink_order', sho or 0)
51 for _,v in pairs({'char', 'depth', 'dir', 'font', 'height', 'kern', 'lang', 'next',
52 'nucleus', 'offsets', 'penalty', 'shift', 'sub', 'subtype', 'sup'}) do
53 if not dnode['set'..v] then
54 dnode['set'..v] = function(n, ...) return setfield(n, v, ...) end
57 local getfield = dnode.getfield
58 luatexja.getglue = node.direct.getglue or
60 return getfield(g,'width'), getfield(g,'stretch'), getfield(g,'shrink'),
61 getfield(g,'stretch_order'), getfield(g,'shrink_order')
63 for _,v in pairs({'box', 'components', 'char', 'depth', 'font', 'height', 'kern', 'lang',
64 'list', 'next', 'nucleus', 'offsets', 'penalty', 'prev', 'shift', 'sub',
65 'subtype', 'sup', 'whd', 'width', 'dir'}) do
66 if not dnode['get'..v] then
67 dnode['get'..v] = function(n, ...) return getfield(n, v, ...) end
74 local icflag_table = {}
75 luatexja.icflag_table = icflag_table
76 icflag_table.ITALIC = 1
77 icflag_table.PACKED = 2
78 icflag_table.KINSOKU = 3
79 icflag_table.FROM_JFM = 4
80 icflag_table.KANJI_SKIP = 68 -- = 4+64
81 icflag_table.KANJI_SKIP_JFM = 69
82 icflag_table.XKANJI_SKIP = 70
83 icflag_table.XKANJI_SKIP_JFM = 71
84 icflag_table.LINEEND = 72
85 icflag_table.PROCESSED = 73
86 icflag_table.IC_PROCESSED = 74
87 icflag_table.BOXBDD = 75
88 icflag_table.SPECIAL_JAGLUE = 76
89 -- 段落組版中のノードリストでは通常のノード (not whatsit) だが
90 -- 和文処理グルー挿入プロセスで長さが決定されるもの
91 icflag_table.PROCESSED_BEGIN_FLAG = 4096 -- sufficiently large power of 2
94 luatexja.stack_table_index = stack_ind
95 stack_ind.PRE = 0x200000 -- characterごと
96 stack_ind.POST = 0x400000 -- characterごと
97 stack_ind.KCAT = 0x600000 -- characterごと
98 stack_ind.XSP = 0x800000 -- characterごと
99 stack_ind.RIPRE = 0xA00000 -- characterごと,ruby pre
100 stack_ind.RIPOST = 0xC00000 -- characterごと,ruby post
101 stack_ind.JWP = 0 -- これだけ
102 stack_ind.KSK = 1 -- これだけ
103 stack_ind.XSK = 2 -- これだけ
104 stack_ind.MJT = 0x100 -- 0--255
105 stack_ind.MJS = 0x200 -- 0--255
106 stack_ind.MJSS = 0x300 -- 0--255
107 stack_ind.KSJ = 0x400 -- 0--9
110 luatexja.userid_table = uid_table
111 uid_table.IHB = luatexbase.newuserwhatsitid('inhibitglue', 'luatexja') -- \inhibitglue
112 uid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
113 uid_table.BPAR = luatexbase.newuserwhatsitid('begin_par', 'luatexja') -- 「段落始め」
114 uid_table.DIR = luatexbase.newuserwhatsitid('direction', 'luatexja') -- 組方向
115 uid_table.BOXB = luatexbase.newuserwhatsitid('box_boundary', 'luatexja') -- 「ボックス始め・終わり」
116 uid_table.JA_AL_BDD = luatexbase.newuserwhatsitid('ja_al_boundary', 'luatexja')
119 luatexja.dir_table = dir_table
120 dir_table.dir_dtou = 1
121 dir_table.dir_tate = 3
122 dir_table.dir_yoko = 4
123 dir_table.dir_math_mod = 8
124 dir_table.dir_node_auto = 128 -- 組方向を合わせるために自動で作られたもの
125 dir_table.dir_node_manual = 256 -- 寸法代入によって作られたもの
126 dir_table.dir_utod = dir_table.dir_tate + dir_table.dir_math_mod
127 -- 「縦数式ディレクション」 in pTeX
130 local load_module = luatexja.load_module
131 load_module 'base'; local ltjb = luatexja.base
132 if tex.outputmode==0 then
133 ltjb.package_error('luatexja',
134 'DVI output is not supported in LuaTeX-ja',
135 'Use lua*tex instead dvilua*tex.')
137 load_module 'rmlgbm'; local ltjr = luatexja.rmlgbm -- must be 1st
138 if luatexja_debug then load_module('debug') end
139 load_module 'lotf_aux'; local ltju = luatexja.lotf_aux
140 load_module 'charrange'; local ltjc = luatexja.charrange
141 load_module 'stack'; local ltjs = luatexja.stack
142 load_module 'direction'; local ltjd = luatexja.direction -- +1 hlist +1 attr_list
143 load_module 'lineskip'; local ltjl = luatexja.lineskip -- +1 hlist +1 attr_list
144 load_module 'jfont'; local ltjf = luatexja.jfont
145 load_module 'inputbuf'; local ltji = luatexja.inputbuf
146 load_module 'pretreat'; local ltjp = luatexja.pretreat
147 load_module 'setwidth'; local ltjw = luatexja.setwidth
148 load_module 'jfmglue'; local ltjj = luatexja.jfmglue -- +1 glue +1 gs +1 attr_list
149 load_module 'math'; local ltjm = luatexja.math
150 load_module 'base'; local ltjb = luatexja.base
152 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
153 local attr_jchar_code = luatexbase.attributes['ltj@charcode']
154 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
155 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
156 local attr_icflag = luatexbase.attributes['ltj@icflag']
157 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
158 local attr_dir = luatexbase.attributes['ltj@dir']
159 local cat_lp = luatexbase.catcodetables['latex-package']
161 -- Three aux. functions, borrowed from tex.web
163 local floor = math.floor
164 local function print_scaled(s)
165 local out, delta = '', 10
166 if s<0 then s, out = -s, out..'-' end
167 out=out..tostring(floor(s/65536)) .. '.'
170 if delta>65536 then s=s+32768-50000 end
171 out=out .. tostring(floor(s/65536))
172 s=10*(s%65536); delta=delta*10
176 luatexja.print_scaled = print_scaled
178 local function print_glue(d,order)
179 local out=print_scaled(d)
182 while order>1 do out=out..'l'; order=order-1 end
188 local function print_spec(p)
189 local out=print_scaled(p.width)..'pt'
191 out=out..' plus '..print_glue(p.stretch,p.stretch_order)
194 out=out..' minus '..print_glue(p.shrink,p.shrink_order)
200 ------------------------------------------------------------------------
201 -- CODE FOR GETTING/SETTING PARAMETERS
202 ------------------------------------------------------------------------
204 -- EXT: print parameters that don't need arguments
206 local tex_getattr = tex.getattribute
207 local function getattr(a, d)
208 local r = tex_getattr(a); d = d or 0
209 return (r==-0x7FFFFFFF) and d or r
211 luatexja.unary_pars = {
212 yalbaselineshift = function(t)
213 return print_scaled(getattr('ltj@yablshift'))..'pt'
215 yjabaselineshift = function(t)
216 return print_scaled(getattr('ltj@ykblshift'))..'pt'
218 talbaselineshift = function(t)
219 return print_scaled(getattr('ltj@tablshift'))..'pt'
221 tjabaselineshift = function(t)
222 return print_scaled(getattr('ltj@tkblshift'))..'pt'
224 kanjiskip = function(t)
225 return print_spec(ltjs.get_stack_skip(stack_ind.KSK, t))
227 xkanjiskip = function(t)
228 return print_spec(ltjs.get_stack_skip(stack_ind.XSK, t))
230 jcharwidowpenalty = function(t)
231 return ltjs.get_stack_table(stack_ind.JWP, 0, t)
233 autospacing = function(t)
234 return getattr('ltj@autospc', 1)
236 autoxspacing = function(t)
237 return getattr('ltj@autoxspc', 1)
239 differentjfm = function(t)
240 local f, r = luatexja.jfmglue.diffmet_rule, '???'
241 if f == math.max then r = 'large'
242 elseif f == math.min then r = 'small'
243 elseif f == math.two_average then r = 'average'
244 elseif f == math.two_paverage then r = 'paverage'
245 elseif f == math.two_pleft then r = 'pleft'
246 elseif f == math.two_pright then r = 'pright'
247 elseif f == math.two_add then r = 'both'
251 direction = function()
252 local v = ltjd.get_dir_count()
253 if math.abs(tex.nest[tex.nest.ptr].mode) == ltjs.mmode and v == dir_table.dir_tate then
254 v = dir_table.dir_utod
258 adjustdir = ltjd.get_adjust_dir_count,
261 local unary_pars = luatexja.unary_pars
262 local scan_arg = token.scan_argument
263 function luatexja.ext_get_parameter_unary()
265 if unary_pars[k] then
266 tex.write(tostring(unary_pars[k](tex.getcount('ltj@@stack'))))
268 ltjb.stop_time_measure('get_par')
273 -- EXT: print parameters that need arguments
275 luatexja.binary_pars = {
276 jacharrange = function(c, t)
277 if type(c)~='number' or c<-1 or c>31*ltjc.ATTR_RANGE then
278 -- 0, -1 はエラーにしない(隠し)
279 ltjb.package_error('luatexja',
280 'invalid character range number (' .. tostring(c) .. ')',
281 'A character range number should be in the range 1..'
282 .. 31*ltjc.ATTR_RANGE .. ",\n"..
283 'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
284 c=0 -- external range 217 == internal range 0
285 elseif c==31*ltjc.ATTR_RANGE then c=0
287 -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
288 if c<0 then return 1 else return (ltjc.get_range_setting(c)==0) and 0 or 1 end
290 prebreakpenalty = function(c, t)
291 return ltjs.get_stack_table(stack_ind.PRE + ltjb.in_unicode(c, true), 0, t)
293 postbreakpenalty = function(c, t)
294 return ltjs.get_stack_table(stack_ind.POST + ltjb.in_unicode(c, true), 0, t)
296 kcatcode = function(c, t)
297 return ltjs.get_stack_table(stack_ind.KCAT + ltjb.in_unicode(c, false), 0, t)
299 chartorange = function(c, t)
300 return ltjc.char_to_range(ltjb.in_unicode(c, false))
302 jaxspmode = function(c, t)
303 return ltjs.get_stack_table(stack_ind.XSP + ltjb.in_unicode(c, true), 3, t)
305 boxdir = function(c, t)
306 if type(c)~='number' or c<0 or c>65535 then
307 ltjb.package_error('luatexja',
308 'Bad register code (' .. tostring(c) .. ')',
309 'A register must be between 0 and 65535.\n'..
310 'I changed this one to zero.')
313 return ltjd.get_register_dir(c)
316 local binary_pars = luatexja.binary_pars
317 local scan_arg, scan_int = token.scan_argument, token.scan_int
318 binary_pars.alxspmode = binary_pars.jaxspmode
319 function luatexja.ext_get_parameter_binary(k, c)
320 if binary_pars[k] then
321 tex.write(tostring(binary_pars[k](c,tex.getcount('ltj@@stack'))))
323 ltjb.stop_time_measure('get_par')
327 -- EXT: print \global if necessary
328 function luatexja.ext_print_global()
329 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
335 local start_time_measure, stop_time_measure
336 = ltjb.start_time_measure, ltjb.stop_time_measure
337 local nullfunc = function (n) return n end
338 local to_node = node.direct.tonode
339 local to_direct = node.direct.todirect
340 local ensure_tex_attr = ltjb.ensure_tex_attr
341 local slide = node.slide
342 -- mode = true iff main_process is called from pre_linebreak_filter
343 local function main_process(head, mode, dir, gc)
344 ensure_tex_attr(attr_icflag, 0)
345 if gc == 'fin_row' then return head
347 start_time_measure('jfmglue')
349 local p = ltjj.main(to_direct(head),mode, dir)
350 stop_time_measure('jfmglue')
355 local function adjust_icflag(h)
356 -- kern from luaotfload will have icflag = 1
357 -- (same as italic correction)
358 ensure_tex_attr(attr_icflag, 1)
363 ltjb.add_to_callback(
364 'pre_linebreak_filter',
365 function (head,groupcode)
366 return main_process(head, true, tex.textdir, groupcode)
368 luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor')+1)
369 ltjb.add_to_callback(
371 function (head,groupcode,size,packtype, dir)
372 return main_process(head, false, dir, groupcode)
374 luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor')+1)
375 ltjb.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
376 ltjb.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
381 local id_glyph = node.id('glyph')
382 function luatexja.pltx_composite_last_node_char()
383 local n = tex.nest[tex.nest.ptr].tail
386 if n.id==id_glyph then
387 while n.componetns and n.subtype and n.subtype%4 >= 2 do
398 local cache_ver = 3 -- must be same as ltj-kinsoku.tex
399 local cache_outdate_fn = function (t) return t.version~=cache_ver end
400 local t = ltjs.charprop_stack_table
401 function luatexja.load_kinsoku()
402 for i,_ in pairs(t) do t[i]=nil end
403 local kinsoku = ltjb.load_cache('ltj-kinsoku_default',cache_outdate_fn)
404 if kinsoku and kinsoku[1] then
407 t[0] = {}; tex.print(cat_lp, '\\input ltj-kinsoku.tex\\relax')
409 luatexja.load_kinsoku=nil; ltjs.charprop_stack_table = nil
417 local node_type = node.type
418 local node_next = node.next
419 local has_attr = node.has_attribute
421 local id_penalty = node.id('penalty')
422 local id_glyph = node.id('glyph')
423 local id_glue = node.id('glue')
424 local id_kern = node.id('kern')
425 local id_hlist = node.id('hlist')
426 local id_vlist = node.id('vlist')
427 local id_rule = node.id('rule')
428 local id_math = node.id('math')
429 local id_whatsit = node.id('whatsit')
430 local sid_user = node.subtype('user_defined')
432 local prefix, inner_depth
433 local utfchar = utf.char
434 local function debug_show_node_X(p,print_fn, limit, inner_depth)
437 local pt, pic = node_type(p.id), (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
438 local base = prefix .. string.format('%X', pic) .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
439 if pt == 'glyph' then
441 .. (p.char<0xF0000 and utfchar(p.char) or '')
442 .. string.format(' (U+%X) ', p.char)
443 .. tostring(p.font) .. ' (' .. print_scaled(p.height) .. '+'
444 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
445 if p.xoffset~=0 or p.yoffset~=0 then
446 s = s .. ' off: (' .. print_scaled(p.xoffset)
447 .. ',' .. print_scaled(p.yoffset) .. ')'
450 elseif pt=='hlist' or pt=='vlist' or pt=='unset'or pt=='ins' then
452 s = base .. '(' .. print_scaled(p.height) .. '+'
453 .. print_scaled(p.depth) .. ')'
454 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
456 s = base .. '(' .. print_scaled(p.height) .. '+'
457 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
458 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
460 if (p.shift or 0)~=0 then
461 s = s .. ', shifted ' .. print_scaled(p.shift)
463 if p.glue_set and p.glue_sign ==2 or ( p.glue_sign==1 and p.glue_set>0) then
464 s = s .. ' glue set '
465 if p.glue_sign == 2 then s = s .. '-' end
466 s = s .. tostring(floor(p.glue_set*10000)/10000)
467 if p.glue_order == 0 then s = s .. 'pt'
470 for i = 2, p.glue_order do s = s .. 'l' end
473 if pic == icflag_table.PACKED then s = s .. ' (packed)' end
475 local bid = inner_depth
476 prefix, inner_depth = prefix.. '.', inner_depth + 1
477 if inner_depth < limit then
478 for q in node.traverse(p.head) do
479 debug_show_node_X(q, print_fn, limit, inner_depth)
483 elseif pt=='rule' then
484 s = base .. '(' .. print_scaled(p.height) .. '+'
485 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
486 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
488 elseif pt=='disc' then
490 local bid = inner_depth
491 if inner_depth < limit then
492 prefix, inner_depth = k.. 'p.', inner_depth + 1
493 for q in node.traverse(p.pre) do
494 debug_show_node_X(q, print_fn, limit, inner_depth)
497 for q in node.traverse(p.post) do
498 debug_show_node_X(q, print_fn, limit, inner_depth)
501 for q in node.traverse(p.replace) do
502 debug_show_node_X(q, print_fn, limit, inner_depth)
506 elseif pt == 'glue' then
507 s = base .. ' ' .. print_spec(p)
508 if pic>icflag_table.KINSOKU and pic<icflag_table.KANJI_SKIP then
509 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
510 elseif pic==icflag_table.KANJI_SKIP then
511 s = s .. ' (kanjiskip)'
512 elseif pic==icflag_table.KANJI_SKIP_JFM then
513 s = s .. ' (kanjiskip, JFM specified)'
514 elseif pic==icflag_table.XKANJI_SKIP then
515 s = s .. ' (xkanjiskip)'
516 elseif pic==icflag_table.XKANJI_SKIP_JFM then
517 s = s .. ' (xkanjiskip, JFM specified)'
520 elseif pt == 'kern' then
521 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
523 s = s .. ' (for accent)'
524 elseif pic==icflag_table.IC_PROCESSED then
525 s = s .. ' (italic correction)'
526 elseif pic==icflag_table.LINEEND then
527 s = s .. ' (end-of-line)'
528 elseif pic>icflag_table.KINSOKU
529 and pic<icflag_table.KANJI_SKIP then
530 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
533 elseif pt == 'penalty' then
534 s = base .. ' ' .. tostring(p.penalty)
535 if pic==icflag_table.KINSOKU then s = s .. ' (for kinsoku)' end
537 elseif pt == 'dir' then
538 print_fn(base .. ' ' .. tostring(p.dir) .. ' (level ' .. tostring(p.level) .. ')')
539 elseif pt == 'whatsit' then
541 if p.subtype==sid_user then
542 local t = tostring(p.user_id) .. ' (' ..
543 luatexbase.get_user_whatsit_name(p.user_id) .. ') '
544 if p.type ~= 110 then
545 s = s .. ' userid:' .. t .. p.value
548 s = s .. ' userid:' .. t .. '(node list)'
549 if p.user_id==uid_table.DIR then
550 s = s .. ' dir: ' .. tostring(node.has_attribute(p, attr_dir))
553 local bid = inner_depth
554 prefix, inner_depth = prefix.. '.', inner_depth + 1
555 if inner_depth < limit then
556 for q in node.traverse(p.value) do
557 debug_show_node_X(q, print_fn, limit, inner_depth)
560 prefix, inner_depth = k, bid
563 s = s .. (node.subtype(p.subtype) or '')
565 s = s .. ' stream=' .. p.stream
568 print_fn(s .. ' [' .. i .. '] = ' .. tostring(p.data[i] and p.date[i].csname))
570 elseif p.subtype==16 then
571 s = s .. ' mode=' .. p.mode .. ', literal="' .. p.data .. '"'
577 -------- math node --------
578 elseif pt=='noad' then
581 prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn, limit, inner_depth);
584 prefix = k .. '^'; debug_show_node_X(p.sup, print_fn, limit, inner_depth);
587 prefix = k .. '_'; debug_show_node_X(p.sub, print_fn, limit, inner_depth);
590 elseif pt=='math_char' then
591 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utfchar(p.char)
593 elseif pt=='sub_box' or pt=='sub_mlist' then
597 for q in node.traverse(p.head) do
598 debug_show_node_X(q, print_fn, limit, inner_depth)
601 elseif pt == 'attribute' then
602 s = base .. ' [' .. p.number .. '] = ' .. p.value
609 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
614 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth); head = node_next(head)
617 print_fn(prefix .. ' (null list)')
620 function luatexja.ext_show_node(head,depth,print_fn, lim)
624 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth)
626 print_fn(prefix .. ' (null list)')