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(s//65536) .. '.'
170 if delta>65536 then s=s+32768-50000 end
171 out=out .. tostring(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 or p[1])..'pt'
190 if (p.stretch or p[2])~=0 then
191 out=out..' plus '..print_glue(p.stretch or p[2], p.stretch_order or p[4])
193 if (p.shrink or p[3])~=0 then
194 out=out..' minus '..print_glue(p.shrink or p[3], p.shrink_order or p[5])
200 ------------------------------------------------------------------------
201 -- CODE FOR GETTING/SETTING PARAMETERS
202 ------------------------------------------------------------------------
203 local getcount, texwrite = tex.getcount, tex.write
204 local cnt_stack = luatexbase.registernumber 'ltj@@stack'
206 -- EXT: print parameters that don't need arguments
208 local tex_getattr = tex.getattribute
209 local function getattr(a, d)
210 local r = tex_getattr(a); d = d or 0
211 return (r==-0x7FFFFFFF) and d or r
213 luatexja.unary_pars = {
214 yalbaselineshift = function(t)
215 return print_scaled(getattr('ltj@yablshift'))..'pt'
217 yjabaselineshift = function(t)
218 return print_scaled(getattr('ltj@ykblshift'))..'pt'
220 talbaselineshift = function(t)
221 return print_scaled(getattr('ltj@tablshift'))..'pt'
223 tjabaselineshift = function(t)
224 return print_scaled(getattr('ltj@tkblshift'))..'pt'
226 kanjiskip = function(t)
227 return print_spec(ltjs.get_stack_skip(stack_ind.KSK, t))
229 xkanjiskip = function(t)
230 return print_spec(ltjs.get_stack_skip(stack_ind.XSK, t))
232 jcharwidowpenalty = function(t)
233 return ltjs.get_stack_table(stack_ind.JWP, 0, t)
235 autospacing = function(t)
236 return getattr('ltj@autospc', 1)
238 autoxspacing = function(t)
239 return getattr('ltj@autoxspc', 1)
241 differentjfm = function(t)
242 local f, r = luatexja.jfmglue.diffmet_rule, '???'
243 if f == math.max then r = 'large'
244 elseif f == math.min then r = 'small'
245 elseif f == math.two_average then r = 'average'
246 elseif f == math.two_paverage then r = 'paverage'
247 elseif f == math.two_pleft then r = 'pleft'
248 elseif f == math.two_pright then r = 'pright'
249 elseif f == math.two_add then r = 'both'
253 direction = function()
254 local v = ltjd.get_dir_count()
255 if math.abs(tex.getnest().mode) == ltjs.mmode and v == dir_table.dir_tate then
256 v = dir_table.dir_utod
260 adjustdir = ltjd.get_adjust_dir_count,
263 local unary_pars = luatexja.unary_pars
264 local scan_arg = token.scan_argument
265 function luatexja.ext_get_parameter_unary()
267 if unary_pars[k] then
268 texwrite(tostring(unary_pars[k](getcount(cnt_stack))))
270 ltjb.stop_time_measure 'get_par'
275 -- EXT: print parameters that need arguments
277 luatexja.binary_pars = {
278 jacharrange = function(c, t)
279 if type(c)~='number' or c<-1 or c>31*ltjc.ATTR_RANGE then
280 -- 0, -1 はエラーにしない(隠し)
281 ltjb.package_error('luatexja',
282 'invalid character range number (' .. tostring(c) .. ')',
283 'A character range number should be in the range 1..'
284 .. 31*ltjc.ATTR_RANGE .. ",\n"..
285 'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
286 c=0 -- external range 217 == internal range 0
287 elseif c==31*ltjc.ATTR_RANGE then c=0
289 -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
290 if c<0 then return 1 else return (ltjc.get_range_setting(c)==0) and 0 or 1 end
292 prebreakpenalty = function(c, t)
293 return ltjs.get_stack_table(stack_ind.PRE + ltjb.in_unicode(c, true), 0, t)
295 postbreakpenalty = function(c, t)
296 return ltjs.get_stack_table(stack_ind.POST + ltjb.in_unicode(c, true), 0, t)
298 kcatcode = function(c, t)
299 return ltjs.get_stack_table(stack_ind.KCAT + ltjb.in_unicode(c, false), 0, t)
301 chartorange = function(c, t)
302 return ltjc.char_to_range(ltjb.in_unicode(c, false))
304 jaxspmode = function(c, t)
305 return ltjs.get_stack_table(stack_ind.XSP + ltjb.in_unicode(c, true), 3, t)
307 boxdir = function(c, t)
308 if type(c)~='number' or c<0 or c>65535 then
309 ltjb.package_error('luatexja',
310 'Bad register code (' .. tostring(c) .. ')',
311 'A register must be between 0 and 65535.\n'..
312 'I changed this one to zero.')
315 return ltjd.get_register_dir(c)
318 local binary_pars = luatexja.binary_pars
319 local scan_arg, scan_int = token.scan_argument, token.scan_int
320 binary_pars.alxspmode = binary_pars.jaxspmode
321 function luatexja.ext_get_parameter_binary(k, c)
322 if binary_pars[k] then
323 texwrite(tostring(binary_pars[k](c, getcount(cnt_stack))))
325 ltjb.stop_time_measure 'get_par'
329 -- EXT: print \global if necessary
330 function luatexja.ext_print_global()
331 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
337 local start_time_measure, stop_time_measure
338 = ltjb.start_time_measure, ltjb.stop_time_measure
339 local nullfunc = function (n) return n end
340 local to_node = node.direct.tonode
341 local to_direct = node.direct.todirect
342 local ensure_tex_attr = ltjb.ensure_tex_attr
343 local slide = node.slide
344 -- mode = true iff main_process is called from pre_linebreak_filter
345 local function main_process(head, mode, dir, gc)
346 ensure_tex_attr(attr_icflag, 0)
347 if gc == 'fin_row' then return head
349 start_time_measure 'jfmglue'
351 local p = ltjj.main(to_direct(head),mode, dir)
352 stop_time_measure 'jfmglue'
357 local function adjust_icflag(h)
358 -- kern from luaotfload will have icflag = 1
359 -- (same as italic correction)
360 ensure_tex_attr(attr_icflag, 1)
365 ltjb.add_to_callback(
366 'pre_linebreak_filter',
367 function (head,groupcode)
368 return main_process(head, true, tex.textdir, groupcode)
370 luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor')+1)
371 ltjb.add_to_callback(
373 function (head,groupcode,size,packtype, dir)
374 return main_process(head, false, dir, groupcode)
376 luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor')+1)
377 ltjb.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
378 ltjb.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
383 local getnest = tex.getnest
384 local id_glyph = node.id 'glyph'
385 function luatexja.pltx_composite_last_node_char()
389 if n.id==id_glyph then
390 while n.components and n.subtype and n.subtype%4 >= 2 do
401 local cache_ver = 4 -- must be same as ltj-kinsoku.tex
402 local cache_outdate_fn = function (t) return t.version~=cache_ver end
403 local t = ltjs.charprop_stack_table
404 function luatexja.load_kinsoku()
405 for i,_ in pairs(t) do t[i]=nil end
406 local kinsoku = ltjb.load_cache('ltj-kinsoku_default',cache_outdate_fn)
407 if kinsoku and kinsoku[1] then
410 t[0] = {}; tex.print(cat_lp, '\\input ltj-kinsoku.tex\\relax')
412 luatexja.load_kinsoku=nil
420 local node_type = node.type
421 local node_next = node.next
422 local get_attr = node.get_attribute
424 local id_penalty = node.id 'penalty'
425 local id_glyph = node.id 'glyph'
426 local id_glue = node.id 'glue'
427 local id_kern = node.id 'kern'
428 local id_hlist = node.id 'hlist'
429 local id_vlist = node.id 'vlist'
430 local id_rule = node.id 'rule'
431 local id_math = node.id 'math'
432 local id_whatsit = node.id 'whatsit'
433 local sid_user = node.subtype 'user_defined'
435 local prefix, inner_depth
436 local utfchar = utf.char
437 local function debug_show_node_X(p,print_fn, limit, inner_depth)
440 local pt, pic = node_type(p.id), (get_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
441 local base = prefix .. string.format('%X', pic) .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
442 if pt == 'glyph' then
444 .. (p.char<0xF0000 and utfchar(p.char) or '')
445 .. string.format(' (U+%X) ', p.char)
446 .. tostring(p.font) .. ' (' .. print_scaled(p.height) .. '+'
447 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
448 if p.xoffset~=0 or p.yoffset~=0 then
449 s = s .. ' off: (' .. print_scaled(p.xoffset)
450 .. ',' .. print_scaled(p.yoffset) .. ')'
453 elseif pt=='hlist' or pt=='vlist' or pt=='unset'or pt=='ins' then
455 s = base .. '(' .. print_scaled(p.height) .. '+'
456 .. print_scaled(p.depth) .. ')'
457 .. ', dir=' .. tostring(node.get_attribute(p, attr_dir))
459 s = base .. '(' .. print_scaled(p.height) .. '+'
460 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
461 .. ', dir=' .. tostring(node.get_attribute(p, attr_dir))
463 if (p.shift or 0)~=0 then
464 s = s .. ', shifted ' .. print_scaled(p.shift)
466 if p.glue_set and p.glue_sign ==2 or ( p.glue_sign==1 and p.glue_set>0) then
467 s = s .. ' glue set '
468 if p.glue_sign == 2 then s = s .. '-' end
469 s = s .. tostring(floor(p.glue_set*10000)/10000)
470 if p.glue_order == 0 then s = s .. 'pt'
473 for i = 2, p.glue_order do s = s .. 'l' end
476 if pic == icflag_table.PACKED then s = s .. ' (packed)' end
478 local bid = inner_depth
479 prefix, inner_depth = prefix.. '.', inner_depth + 1
480 if inner_depth < limit then
481 for q in node.traverse(p.head) do
482 debug_show_node_X(q, print_fn, limit, inner_depth)
486 elseif pt=='rule' then
487 s = base .. '(' .. print_scaled(p.height) .. '+'
488 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
489 .. ', dir=' .. tostring(node.get_attribute(p, attr_dir))
491 elseif pt=='disc' then
493 local bid = inner_depth
494 if inner_depth < limit then
495 prefix, inner_depth = k.. 'p.', inner_depth + 1
496 for q in node.traverse(p.pre) do
497 debug_show_node_X(q, print_fn, limit, inner_depth)
500 for q in node.traverse(p.post) do
501 debug_show_node_X(q, print_fn, limit, inner_depth)
504 for q in node.traverse(p.replace) do
505 debug_show_node_X(q, print_fn, limit, inner_depth)
509 elseif pt == 'glue' then
510 s = base .. ' ' .. print_spec(p)
511 if pic>icflag_table.KINSOKU and pic<icflag_table.KANJI_SKIP then
512 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
513 elseif pic==icflag_table.KANJI_SKIP then
514 s = s .. ' (kanjiskip)'
515 elseif pic==icflag_table.KANJI_SKIP_JFM then
516 s = s .. ' (kanjiskip, JFM specified)'
517 elseif pic==icflag_table.XKANJI_SKIP then
518 s = s .. ' (xkanjiskip)'
519 elseif pic==icflag_table.XKANJI_SKIP_JFM then
520 s = s .. ' (xkanjiskip, JFM specified)'
523 elseif pt == 'kern' then
524 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
526 s = s .. ' (for accent)'
527 elseif pic==icflag_table.IC_PROCESSED then
528 s = s .. ' (italic correction)'
529 elseif pic==icflag_table.LINEEND then
530 s = s .. ' (end-of-line)'
531 elseif pic>icflag_table.KINSOKU
532 and pic<icflag_table.KANJI_SKIP then
533 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
536 elseif pt == 'penalty' then
537 s = base .. ' ' .. tostring(p.penalty)
538 if pic==icflag_table.KINSOKU then s = s .. ' (for kinsoku)' end
540 elseif pt == 'dir' then
541 print_fn(base .. ' ' .. tostring(p.dir) .. ' (level ' .. tostring(p.level) .. ')')
542 elseif pt == 'whatsit' then
544 if p.subtype==sid_user then
545 local t = tostring(p.user_id) .. ' (' ..
546 luatexbase.get_user_whatsit_name(p.user_id) .. ') '
547 if p.type ~= 110 then
548 s = s .. ' userid:' .. t .. p.value
551 s = s .. ' userid:' .. t .. '(node list)'
552 if p.user_id==uid_table.DIR then
553 s = s .. ' dir: ' .. tostring(node.get_attribute(p, attr_dir))
556 local bid = inner_depth
557 prefix, inner_depth = prefix.. '.', inner_depth + 1
558 if inner_depth < limit then
559 for q in node.traverse(p.value) do
560 debug_show_node_X(q, print_fn, limit, inner_depth)
563 prefix, inner_depth = k, bid
566 s = s .. (node.subtype(p.subtype) or '')
568 s = s .. ' stream=' .. p.stream
571 print_fn(s .. ' [' .. i .. '] = ' .. tostring(p.data[i] and p.date[i].csname))
573 elseif p.subtype==16 then
574 s = s .. ' mode=' .. p.mode .. ', literal="' .. p.data .. '"'
580 -------- math node --------
581 elseif pt=='noad' then
584 prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn, limit, inner_depth);
587 prefix = k .. '^'; debug_show_node_X(p.sup, print_fn, limit, inner_depth);
590 prefix = k .. '_'; debug_show_node_X(p.sub, print_fn, limit, inner_depth);
593 elseif pt=='math_char' then
594 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utfchar(p.char)
596 elseif pt=='sub_box' or pt=='sub_mlist' then
600 for q in node.traverse(p.head) do
601 debug_show_node_X(q, print_fn, limit, inner_depth)
604 elseif pt == 'attribute' then
605 s = base .. ' [' .. p.number .. '] = ' .. p.value
612 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
617 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth); head = node_next(head)
620 print_fn(prefix .. ' (null list)')
623 function luatexja.ext_show_node(head,depth,print_fn, lim)
627 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth)
629 print_fn(prefix .. ' (null list)')