2 ------------------------------------------------------------------------
5 function luatexja.input_path_clear() for i in ipairs(ipath) do ipath[i]=nil end end
6 function luatexja.input_path_add(s) ipath[#ipath+1]=s end
7 function luatexja.load_lua(fn)
8 local found = kpse.find_file(fn, 'tex')
10 for _,v in ipairs(ipath) do
11 found = kpse.find_file(v .. fn, 'tex'); if found then break end
15 tex.error("LuaTeX-ja error: File `" .. fn .. "' not found")
17 texio.write_nl('(' .. found .. ')'); dofile(found)
21 function luatexja.load_module(name) require('ltj-' .. name.. '.lua') end
24 local dnode = node.direct
25 local getfield, traverse = dnode.getfield, dnode.traverse
26 local node_new, set_attr, get_attr = dnode.new, dnode.set_attribute, dnode.get_attribute
27 local set_attrlist, get_attrlist = dnode.setattributelist, dnode.getattributelist
28 local unset_attr = dnode.unset_attribute
29 local attr_icflag = luatexbase.attributes['ltj@icflag']
30 local function node_inherit_attr(n, b, a)
32 local attrlist = get_attrlist(b or a)
33 local nic = get_attr(n, attr_icflag)
34 set_attrlist(n, attrlist); set_attr(n, attr_icflag, nic)
36 for na in traverse(attrlist) do
37 local id = getfield(na, 'number')
38 if id and id~=attr_icflag and getfield(na, 'value')~=get_attr(a, id) then
46 luatexja.node_inherit_attr = node_inherit_attr
47 luatexja.dnode_new = function (id, subtype, b, a)
48 return node_inherit_attr(node_new(id, subtype), b, a)
53 local icflag_table = {}
54 luatexja.icflag_table = icflag_table
55 icflag_table.ITALIC = 1
56 icflag_table.PACKED = 2
57 icflag_table.KINSOKU = 3
58 icflag_table.FROM_JFM = 4
59 icflag_table.KANJI_SKIP = 68 -- = 4+64
60 icflag_table.KANJI_SKIP_JFM = 69
61 icflag_table.XKANJI_SKIP = 70
62 icflag_table.XKANJI_SKIP_JFM = 71
63 icflag_table.LINEEND = 72
64 icflag_table.PROCESSED = 73
65 icflag_table.IC_PROCESSED = 74
66 icflag_table.BOXBDD = 75
67 icflag_table.SPECIAL_JAGLUE = 76
68 -- 段落組版中のノードリストでは通常のノード (not whatsit) だが
69 -- 和文処理グルー挿入プロセスで長さが決定されるもの
70 icflag_table.PROCESSED_BEGIN_FLAG = 4096 -- sufficiently large power of 2
73 luatexja.stack_table_index = stack_ind
74 stack_ind.PRE = 0x200000 -- characterごと
75 stack_ind.POST = 0x400000 -- characterごと
76 stack_ind.KCAT = 0x600000 -- characterごと
77 stack_ind.XSP = 0x800000 -- characterごと
78 stack_ind.RIPRE = 0xA00000 -- characterごと,ruby pre
79 stack_ind.RIPOST = 0xC00000 -- characterごと,ruby post
80 stack_ind.JWP = 0 -- これだけ
81 stack_ind.KSK = 1 -- これだけ
82 stack_ind.XSK = 2 -- これだけ
83 stack_ind.MJT = 0x100 -- 0--255
84 stack_ind.MJS = 0x200 -- 0--255
85 stack_ind.MJSS = 0x300 -- 0--255
86 stack_ind.KSJ = 0x400 -- 0--9
89 luatexja.userid_table = uid_table
90 uid_table.IHB = luatexbase.newuserwhatsitid('inhibitglue', 'luatexja') -- \inhibitglue
91 uid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
92 uid_table.BPAR = luatexbase.newuserwhatsitid('begin_par', 'luatexja') -- 「段落始め」
93 uid_table.DIR = luatexbase.newuserwhatsitid('direction', 'luatexja') -- 組方向
94 uid_table.BOXB = luatexbase.newuserwhatsitid('box_boundary', 'luatexja') -- 「ボックス始め・終わり」
95 uid_table.JA_AL_BDD = luatexbase.newuserwhatsitid('ja_al_boundary', 'luatexja')
98 luatexja.dir_table = dir_table
99 dir_table.dir_dtou = 1
100 dir_table.dir_tate = 3
101 dir_table.dir_yoko = 4
102 dir_table.dir_math_mod = 8
103 dir_table.dir_node_auto = 128 -- 組方向を合わせるために自動で作られたもの
104 dir_table.dir_node_manual = 256 -- 寸法代入によって作られたもの
105 dir_table.dir_utod = dir_table.dir_tate + dir_table.dir_math_mod
106 -- 「縦数式ディレクション」 in pTeX
109 local load_module = luatexja.load_module
110 load_module 'base'; local ltjb = luatexja.base
111 if tex.outputmode==0 then
112 ltjb.package_error('luatexja',
113 'DVI output is not supported in LuaTeX-ja',
114 'Use lua*tex instead dvilua*tex.')
116 load_module 'rmlgbm'; local ltjr = luatexja.rmlgbm -- must be 1st
117 if luatexja_debug then load_module 'debug' end
118 load_module 'lotf_aux'; local ltju = luatexja.lotf_aux
119 load_module 'charrange'; local ltjc = luatexja.charrange
120 load_module 'stack'; local ltjs = luatexja.stack
121 load_module 'direction'; local ltjd = luatexja.direction -- +1 hlist +1 attr_list
122 load_module 'lineskip'; local ltjl = luatexja.lineskip -- +1 hlist +1 attr_list
123 load_module 'jfont'; local ltjf = luatexja.jfont
124 load_module 'inputbuf'; local ltji = luatexja.inputbuf
125 load_module 'pretreat'; local ltjp = luatexja.pretreat
126 load_module 'setwidth'; local ltjw = luatexja.setwidth
127 load_module 'jfmglue'; local ltjj = luatexja.jfmglue -- +1 glue +1 gs +1 attr_list
128 load_module 'math'; local ltjm = luatexja.math
129 load_module 'base'; local ltjb = luatexja.base
131 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
132 local attr_jchar_code = luatexbase.attributes['ltj@charcode']
133 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
134 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
135 local attr_icflag = luatexbase.attributes['ltj@icflag']
136 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
137 local attr_dir = luatexbase.attributes['ltj@dir']
138 local cat_lp = luatexbase.catcodetables['latex-package']
140 -- Three aux. functions, borrowed from tex.web
142 local floor = math.floor
143 local function print_scaled(s)
144 local out, delta = '', 10
145 if s<0 then s, out = -s, out..'-' end
146 out=out..tostring(floor(s/65536)) .. '.'
149 if delta>65536 then s=s+32768-50000 end
150 out=out .. tostring(floor(s/65536))
151 s=10*(s%65536); delta=delta*10
155 luatexja.print_scaled = print_scaled
157 local function print_glue(d,order)
158 local out=print_scaled(d)
161 while order>1 do out=out..'l'; order=order-1 end
167 local function print_spec(p)
168 local out=print_scaled(p.width or p[1])..'pt'
169 if (p.stretch or p[2])~=0 then
170 out=out..' plus '..print_glue(p.stretch or p[2], p.stretch_order or p[4])
172 if (p.shrink or p[3])~=0 then
173 out=out..' minus '..print_glue(p.shrink or p[3], p.shrink_order or p[5])
179 ------------------------------------------------------------------------
180 -- CODE FOR GETTING/SETTING PARAMETERS
181 ------------------------------------------------------------------------
182 local getcount, texwrite = tex.getcount, tex.write
183 local cnt_stack = luatexbase.registernumber 'ltj@@stack'
185 -- EXT: print parameters that don't need arguments
187 local tex_getattr, getnest = tex.getattribute, tex.getnest
188 local function getattr(a, d)
189 local r = tex_getattr(a); d = d or 0
190 return (r==-0x7FFFFFFF) and d or r
192 luatexja.unary_pars = {
193 yalbaselineshift = function(t)
194 return print_scaled(getattr('ltj@yablshift'))..'pt'
196 yjabaselineshift = function(t)
197 return print_scaled(getattr('ltj@ykblshift'))..'pt'
199 talbaselineshift = function(t)
200 return print_scaled(getattr('ltj@tablshift'))..'pt'
202 tjabaselineshift = function(t)
203 return print_scaled(getattr('ltj@tkblshift'))..'pt'
205 kanjiskip = function(t)
206 return print_spec(ltjs.get_stack_skip(stack_ind.KSK, t))
208 xkanjiskip = function(t)
209 return print_spec(ltjs.get_stack_skip(stack_ind.XSK, t))
211 jcharwidowpenalty = function(t)
212 return ltjs.get_stack_table(stack_ind.JWP, 0, t)
214 autospacing = function(t)
215 return getattr('ltj@autospc', 1)
217 autoxspacing = function(t)
218 return getattr('ltj@autoxspc', 1)
220 differentjfm = function(t)
221 local f, r = luatexja.jfmglue.diffmet_rule, '???'
222 if f == math.max then r = 'large'
223 elseif f == math.min then r = 'small'
224 elseif f == math.two_average then r = 'average'
225 elseif f == math.two_paverage then r = 'paverage'
226 elseif f == math.two_pleft then r = 'pleft'
227 elseif f == math.two_pright then r = 'pright'
228 elseif f == math.two_add then r = 'both'
232 direction = function()
233 local v = ltjd.get_dir_count()
234 if math.abs(getnest().mode) == ltjs.mmode and v == dir_table.dir_tate then
235 v = dir_table.dir_utod
239 adjustdir = ltjd.get_adjust_dir_count,
242 local unary_pars = luatexja.unary_pars
243 local scan_arg = token.scan_argument
244 function luatexja.ext_get_parameter_unary()
246 if unary_pars[k] then
247 texwrite(tostring(unary_pars[k](getcount(cnt_stack))))
249 ltjb.stop_time_measure 'get_par'
254 -- EXT: print parameters that need arguments
256 luatexja.binary_pars = {
257 jacharrange = function(c, t)
258 if type(c)~='number' or c<-1 or c>31*ltjc.ATTR_RANGE then
259 -- 0, -1 はエラーにしない(隠し)
260 ltjb.package_error('luatexja',
261 'invalid character range number (' .. tostring(c) .. ')',
262 'A character range number should be in the range 1..'
263 .. 31*ltjc.ATTR_RANGE .. ",\n"..
264 'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
265 c=0 -- external range 217 == internal range 0
266 elseif c==31*ltjc.ATTR_RANGE then c=0
268 -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
269 if c<0 then return 1 else return (ltjc.get_range_setting(c)==0) and 0 or 1 end
271 prebreakpenalty = function(c, t)
272 return ltjs.get_stack_table(stack_ind.PRE + ltjb.in_unicode(c, true), 0, t)
274 postbreakpenalty = function(c, t)
275 return ltjs.get_stack_table(stack_ind.POST + ltjb.in_unicode(c, true), 0, t)
277 kcatcode = function(c, t)
278 return ltjs.get_stack_table(stack_ind.KCAT + ltjb.in_unicode(c, false), 0, t)
280 chartorange = function(c, t)
281 return ltjc.char_to_range(ltjb.in_unicode(c, false))
283 jaxspmode = function(c, t)
284 return ltjs.get_stack_table(stack_ind.XSP + ltjb.in_unicode(c, true), 3, t)
286 boxdir = function(c, t)
287 if type(c)~='number' or c<0 or c>65535 then
288 ltjb.package_error('luatexja',
289 'Bad register code (' .. tostring(c) .. ')',
290 'A register must be between 0 and 65535.\n'..
291 'I changed this one to zero.')
294 return ltjd.get_register_dir(c)
297 local binary_pars = luatexja.binary_pars
298 local scan_arg, scan_int = token.scan_argument, token.scan_int
299 binary_pars.alxspmode = binary_pars.jaxspmode
300 function luatexja.ext_get_parameter_binary(k, c)
301 if binary_pars[k] then
302 texwrite(tostring(binary_pars[k](c, getcount(cnt_stack))))
304 ltjb.stop_time_measure 'get_par'
308 -- EXT: print \global if necessary
309 function luatexja.ext_print_global()
310 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
316 local start_time_measure, stop_time_measure
317 = ltjb.start_time_measure, ltjb.stop_time_measure
318 local nullfunc = function (n) return n end
319 local to_node = node.direct.tonode
320 local to_direct = node.direct.todirect
321 local ensure_tex_attr = ltjb.ensure_tex_attr
322 local slide = node.slide
323 -- mode = true iff main_process is called from pre_linebreak_filter
324 local function main_process(head, mode, dir, gc)
325 ensure_tex_attr(attr_icflag, 0)
326 if gc == 'fin_row' then return head
328 start_time_measure 'jfmglue'
330 local p = ltjj.main(to_direct(head),mode, dir)
331 stop_time_measure 'jfmglue'
336 local function adjust_icflag(h)
337 -- kern from luaotfload will have icflag = 1
338 -- (same as italic correction)
339 ensure_tex_attr(attr_icflag, 1)
344 ltjb.add_to_callback(
345 'pre_linebreak_filter',
346 function (head,groupcode)
347 return main_process(head, true, tex.textdir, groupcode)
349 luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor')+1)
350 ltjb.add_to_callback(
352 function (head,groupcode,size,packtype, dir)
353 return main_process(head, false, dir, groupcode)
355 luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor')+1)
356 ltjb.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
357 ltjb.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
362 local get_attr, traverse_glyph = node.get_attribute, node.traverse_glyph
363 local getnest = tex.getnest
364 local id_hlist = node.id 'hlist'
365 local id_glyph = node.id 'glyph'
366 local PACKED, PROCESSED_BEGIN_FLAG = icflag_table.PACKED, icflag_table.PROCESSED_BEGIN_FLAG
367 function luatexja.pltx_composite_last_node_char()
368 local n = getnest().tail
372 and (get_attr(n, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG == PACKED then
373 for i in traverse_glyph(n.head) do n = i; break end
375 if n.id==id_glyph then
376 while n.components and n.subtype and n.subtype%4 >= 2 do
387 local cache_ver = 4 -- must be same as ltj-kinsoku.tex
388 local cache_outdate_fn = function (t) return t.version~=cache_ver end
389 local t = ltjs.charprop_stack_table
390 function luatexja.load_kinsoku()
391 for i,_ in pairs(t) do t[i]=nil end
392 local kinsoku = ltjb.load_cache('ltj-kinsoku_default',cache_outdate_fn)
393 if kinsoku and kinsoku[1] then
396 t[0] = {}; tex.print(cat_lp, '\\input ltj-kinsoku.tex\\relax')
398 luatexja.load_kinsoku=nil
406 local node_type = node.type
407 local node_next = node.next
408 local get_attr = node.get_attribute
410 local id_penalty = node.id 'penalty'
411 local id_glyph = node.id 'glyph'
412 local id_glue = node.id 'glue'
413 local id_kern = node.id 'kern'
414 local id_hlist = node.id 'hlist'
415 local id_vlist = node.id 'vlist'
416 local id_rule = node.id 'rule'
417 local id_math = node.id 'math'
418 local id_whatsit = node.id 'whatsit'
419 local sid_user = node.subtype 'user_defined'
421 local prefix, inner_depth
422 local utfchar = utf.char
423 local function debug_show_node_X(p,print_fn, limit, inner_depth)
426 local pt, pic = node_type(p.id), (get_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
427 local base = prefix .. '[' .. string.format('%7d', node.direct.todirect(p)) .. '] ' ..
428 string.format('%X', pic) .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
429 if pt == 'glyph' then
431 .. (p.char<0xF0000 and utfchar(p.char) or '')
432 .. string.format(' (U+%X) ', p.char)
433 .. tostring(p.font) .. ' (' .. print_scaled(p.height) .. '+'
434 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
435 if p.xoffset~=0 or p.yoffset~=0 then
436 s = s .. ' off: (' .. print_scaled(p.xoffset)
437 .. ',' .. print_scaled(p.yoffset) .. ')'
440 elseif pt=='hlist' or pt=='vlist' or pt=='unset'or pt=='ins' then
442 s = base .. '(' .. print_scaled(p.height) .. '+'
443 .. print_scaled(p.depth) .. ')'
444 .. ', dir=' .. tostring(node.get_attribute(p, attr_dir))
446 s = base .. '(' .. print_scaled(p.height) .. '+'
447 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
448 .. ', dir=' .. tostring(node.get_attribute(p, attr_dir))
450 if (p.shift or 0)~=0 then
451 s = s .. ', shifted ' .. print_scaled(p.shift)
453 if p.glue_set and p.glue_sign ==2 or ( p.glue_sign==1 and p.glue_set>0) then
454 s = s .. ' glue set '
455 if p.glue_sign == 2 then s = s .. '-' end
456 s = s .. tostring(floor(p.glue_set*10000)/10000)
457 if p.glue_order == 0 then s = s .. 'pt'
460 for i = 2, p.glue_order do s = s .. 'l' end
463 if pic == icflag_table.PACKED then s = s .. ' (packed)' end
465 local bid = inner_depth
466 prefix, inner_depth = prefix.. '.', inner_depth + 1
467 if inner_depth < limit then
468 for q in node.traverse(p.head) do
469 debug_show_node_X(q, print_fn, limit, inner_depth)
473 elseif pt=='rule' then
474 s = base .. '(' .. print_scaled(p.height) .. '+'
475 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
476 .. ', dir=' .. tostring(node.get_attribute(p, attr_dir))
478 elseif pt=='disc' then
480 local bid = inner_depth
481 if inner_depth < limit then
482 prefix, inner_depth = k.. 'p.', inner_depth + 1
483 for q in node.traverse(p.pre) do
484 debug_show_node_X(q, print_fn, limit, inner_depth)
487 for q in node.traverse(p.post) do
488 debug_show_node_X(q, print_fn, limit, inner_depth)
491 for q in node.traverse(p.replace) do
492 debug_show_node_X(q, print_fn, limit, inner_depth)
496 elseif pt == 'glue' then
497 s = base .. ' ' .. print_spec(p)
498 if pic>icflag_table.KINSOKU and pic<icflag_table.KANJI_SKIP then
499 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
500 elseif pic==icflag_table.KANJI_SKIP then
501 s = s .. ' (kanjiskip)'
502 elseif pic==icflag_table.KANJI_SKIP_JFM then
503 s = s .. ' (kanjiskip, JFM specified)'
504 elseif pic==icflag_table.XKANJI_SKIP then
505 s = s .. ' (xkanjiskip)'
506 elseif pic==icflag_table.XKANJI_SKIP_JFM then
507 s = s .. ' (xkanjiskip, JFM specified)'
510 elseif pt == 'kern' then
511 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
513 s = s .. ' (for accent)'
514 elseif pic==icflag_table.IC_PROCESSED then
515 s = s .. ' (italic correction)'
516 elseif pic==icflag_table.LINEEND then
517 s = s .. ' (end-of-line)'
518 elseif pic>icflag_table.KINSOKU
519 and pic<icflag_table.KANJI_SKIP then
520 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
523 elseif pt == 'penalty' then
524 s = base .. ' ' .. tostring(p.penalty)
525 if pic==icflag_table.KINSOKU then s = s .. ' (for kinsoku)' end
527 elseif pt == 'dir' then
528 print_fn(base .. ' ' .. tostring(p.dir) .. ' (level ' .. tostring(p.level) .. ')')
529 elseif pt == 'whatsit' then
531 if p.subtype==sid_user then
532 local t = tostring(p.user_id) .. ' (' ..
533 luatexbase.get_user_whatsit_name(p.user_id) .. ') '
534 if p.type ~= 110 then
535 s = s .. ' userid:' .. t .. tostring(p.value)
538 s = s .. ' userid:' .. t .. '(node list)'
539 if p.user_id==uid_table.DIR then
540 s = s .. ' dir: ' .. tostring(node.get_attribute(p, attr_dir))
543 local bid = inner_depth
544 prefix, inner_depth = prefix.. '.', inner_depth + 1
545 if inner_depth < limit then
546 for q in node.traverse(p.value) do
547 debug_show_node_X(q, print_fn, limit, inner_depth)
550 prefix, inner_depth = k, bid
553 s = s .. (node.subtype(p.subtype) or '')
555 s = s .. ' stream=' .. p.stream
558 print_fn(s .. ' [' .. i .. '] = ' .. tostring(p.data[i] and p.date[i].csname))
560 elseif p.subtype==16 then
561 s = s .. ' mode=' .. p.mode .. ', literal="' .. p.data .. '"'
567 -------- math node --------
568 elseif pt=='noad' then
571 prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn, limit, inner_depth);
574 prefix = k .. '^'; debug_show_node_X(p.sup, print_fn, limit, inner_depth);
577 prefix = k .. '_'; debug_show_node_X(p.sub, print_fn, limit, inner_depth);
580 elseif pt=='math_char' then
581 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utfchar(p.char)
583 elseif pt=='sub_box' or pt=='sub_mlist' then
587 for q in node.traverse(p.head) do
588 debug_show_node_X(q, print_fn, limit, inner_depth)
591 elseif pt == 'attribute' then
592 s = base .. ' [' .. p.number .. '] = ' .. p.value
599 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
604 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth); head = node_next(head)
607 print_fn(prefix .. ' (null list)')
610 function luatexja.ext_show_node(head,depth,print_fn, lim)
614 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth)
616 print_fn(prefix .. ' (null list)')