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 setfield = node.direct.setfield
16 luatexja.setglue = node.direct.setglue or
17 function(g,w,st,sh,sto,sho)
18 setfield(g,'width', w or 0); setfield(g,'stretch',st or 0); setfield(g,'shrink', sh or 0)
19 setfield(g,'stretch_order',sto or 0)
20 setfield(g,'shrink_order', sho or 0)
22 local getfield = node.direct.getfield
23 luatexja.getglue = node.direct.getglue or
25 return getfield(g,'width'), getfield(g,'stretch'), getfield(g,'shrink'),
26 getfield(g,'stretch_order'), getfield(g,'shrink_order')
31 local icflag_table = {}
32 luatexja.icflag_table = icflag_table
33 icflag_table.ITALIC = 1
34 icflag_table.PACKED = 2
35 icflag_table.KINSOKU = 3
36 icflag_table.FROM_JFM = 4
37 icflag_table.KANJI_SKIP = 68 -- = 4+64
38 icflag_table.KANJI_SKIP_JFM = 69
39 icflag_table.XKANJI_SKIP = 70
40 icflag_table.XKANJI_SKIP_JFM = 71
41 icflag_table.LINEEND = 72
42 icflag_table.PROCESSED = 73
43 icflag_table.IC_PROCESSED = 74
44 icflag_table.BOXBDD = 75
45 icflag_table.SPECIAL_JAGLUE = 76
46 -- 段落組版中のノードリストでは通常のノード (not whatsit) だが
47 -- 和文処理グルー挿入プロセスで長さが決定されるもの
48 icflag_table.PROCESSED_BEGIN_FLAG = 4096 -- sufficiently large power of 2
51 luatexja.stack_table_index = stack_ind
52 stack_ind.PRE = 0x200000 -- characterごと
53 stack_ind.POST = 0x400000 -- characterごと
54 stack_ind.KCAT = 0x600000 -- characterごと
55 stack_ind.XSP = 0x800000 -- characterごと
56 stack_ind.RIPRE = 0xA00000 -- characterごと,ruby pre
57 stack_ind.RIPOST = 0xC00000 -- characterごと,ruby post
58 stack_ind.JWP = 0 -- これだけ
59 stack_ind.KSK = 1 -- これだけ
60 stack_ind.XSK = 2 -- これだけ
61 stack_ind.MJT = 0x100 -- 0--255
62 stack_ind.MJS = 0x200 -- 0--255
63 stack_ind.MJSS = 0x300 -- 0--255
64 stack_ind.KSJ = 0x400 -- 0--9
67 luatexja.userid_table = uid_table
68 uid_table.IHB = luatexbase.newuserwhatsitid('inhibitglue', 'luatexja') -- \inhibitglue
69 uid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
70 uid_table.BPAR = luatexbase.newuserwhatsitid('begin_par', 'luatexja') -- 「段落始め」
71 uid_table.DIR = luatexbase.newuserwhatsitid('direction', 'luatexja') -- 組方向
72 uid_table.BOXB = luatexbase.newuserwhatsitid('box_boundary', 'luatexja') -- 「ボックス始め・終わり」
73 uid_table.JA_AL_BDD = luatexbase.newuserwhatsitid('ja_al_boundary', 'luatexja')
76 luatexja.dir_table = dir_table
77 dir_table.dir_dtou = 1
78 dir_table.dir_tate = 3
79 dir_table.dir_yoko = 4
80 dir_table.dir_math_mod = 8
81 dir_table.dir_node_auto = 128 -- 組方向を合わせるために自動で作られたもの
82 dir_table.dir_node_manual = 256 -- 寸法代入によって作られたもの
83 dir_table.dir_utod = dir_table.dir_tate + dir_table.dir_math_mod
84 -- 「縦数式ディレクション」 in pTeX
87 local load_module = luatexja.load_module
88 load_module 'base'; local ltjb = luatexja.base
89 if tex.outputmode==0 then
90 ltjb.package_error('luatexja',
91 'DVI output is not supported in LuaTeX-ja',
92 'Use lua*tex instead dvilua*tex.')
94 load_module 'rmlgbm'; local ltjr = luatexja.rmlgbm -- must be 1st
95 if luatexja_debug then load_module('debug') end
96 load_module 'lotf_aux'; local ltju = luatexja.lotf_aux
97 load_module 'charrange'; local ltjc = luatexja.charrange
98 load_module 'stack'; local ltjs = luatexja.stack
99 load_module 'direction'; local ltjd = luatexja.direction -- +1 hlist +1 attr_list
100 load_module 'lineskip'; local ltjl = luatexja.lineskip -- +1 hlist +1 attr_list
101 load_module 'jfont'; local ltjf = luatexja.jfont
102 load_module 'inputbuf'; local ltji = luatexja.inputbuf
103 load_module 'pretreat'; local ltjp = luatexja.pretreat
104 load_module 'setwidth'; local ltjw = luatexja.setwidth
105 load_module 'jfmglue'; local ltjj = luatexja.jfmglue -- +1 glue +1 gs +1 attr_list
106 load_module 'math'; local ltjm = luatexja.math
107 load_module 'base'; local ltjb = luatexja.base
109 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
110 local attr_jchar_code = luatexbase.attributes['ltj@charcode']
111 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
112 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
113 local attr_icflag = luatexbase.attributes['ltj@icflag']
114 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
115 local attr_dir = luatexbase.attributes['ltj@dir']
116 local cat_lp = luatexbase.catcodetables['latex-package']
118 -- Three aux. functions, borrowed from tex.web
120 local floor = math.floor
121 local function print_scaled(s)
122 local out, delta = '', 10
123 if s<0 then s, out = -s, out..'-' end
124 out=out..tostring(floor(s/65536)) .. '.'
127 if delta>65536 then s=s+32768-50000 end
128 out=out .. tostring(floor(s/65536))
129 s=10*(s%65536); delta=delta*10
133 luatexja.print_scaled = print_scaled
135 local function print_glue(d,order)
136 local out=print_scaled(d)
139 while order>1 do out=out..'l'; order=order-1 end
145 local function print_spec(p)
146 local out=print_scaled(p.width)..'pt'
148 out=out..' plus '..print_glue(p.stretch,p.stretch_order)
151 out=out..' minus '..print_glue(p.shrink,p.shrink_order)
157 ------------------------------------------------------------------------
158 -- CODE FOR GETTING/SETTING PARAMETERS
159 ------------------------------------------------------------------------
161 -- EXT: print parameters that don't need arguments
163 local tex_getattr = tex.getattribute
164 local function getattr(a)
165 local r = tex_getattr(a)
166 return (r==-0x7FFFFFFF) and 0 or r
168 luatexja.unary_pars = {
169 yalbaselineshift = function(t)
170 return print_scaled(getattr('ltj@yablshift'))..'pt'
172 yjabaselineshift = function(t)
173 return print_scaled(getattr('ltj@ykblshift'))..'pt'
175 talbaselineshift = function(t)
176 return print_scaled(getattr('ltj@tablshift'))..'pt'
178 tjabaselineshift = function(t)
179 return print_scaled(getattr('ltj@tkblshift'))..'pt'
181 kanjiskip = function(t)
182 return print_spec(ltjs.get_stack_skip(stack_ind.KSK, t))
184 xkanjiskip = function(t)
185 return print_spec(ltjs.get_stack_skip(stack_ind.XSK, t))
187 jcharwidowpenalty = function(t)
188 return ltjs.get_stack_table(stack_ind.JWP, 0, t)
190 autospacing = function(t)
191 return getattr('ltj@autospc')
193 autoxspacing = function(t)
194 return getattr('ltj@autoxspc')
196 differentjfm = function(t)
197 local f, r = luatexja.jfmglue.diffmet_rule, '???'
198 if f == math.max then r = 'large'
199 elseif f == math.min then r = 'small'
200 elseif f == math.two_average then r = 'average'
201 elseif f == math.two_paverage then r = 'paverage'
202 elseif f == math.two_pleft then r = 'pleft'
203 elseif f == math.two_pright then r = 'pright'
204 elseif f == math.two_add then r = 'both'
208 direction = function()
209 local v = ltjd.get_dir_count()
210 if math.abs(tex.nest[tex.nest.ptr].mode) == ltjs.mmode and v == dir_table.dir_tate then
211 v = dir_table.dir_utod
215 adjustdir = ltjd.get_adjust_dir_count,
218 local unary_pars = luatexja.unary_pars
219 local scan_arg = token.scan_argument
220 function luatexja.ext_get_parameter_unary()
222 if unary_pars[k] then
223 tex.write(tostring(unary_pars[k](tex.getcount('ltj@@stack'))))
225 ltjb.stop_time_measure('get_par')
230 -- EXT: print parameters that need arguments
232 luatexja.binary_pars = {
233 jacharrange = function(c, t)
234 if type(c)~='number' or c<-1 or c>31*ltjc.ATTR_RANGE then
235 -- 0, -1 はエラーにしない(隠し)
236 ltjb.package_error('luatexja',
237 'invalid character range number (' .. tostring(c) .. ')',
238 'A character range number should be in the range 1..'
239 .. 31*ltjc.ATTR_RANGE .. ",\n"..
240 'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
241 c=0 -- external range 217 == internal range 0
242 elseif c==31*ltjc.ATTR_RANGE then c=0
244 -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
245 return (c<0) and 1 or ltjc.get_range_setting(c)
247 prebreakpenalty = function(c, t)
248 return ltjs.get_stack_table(stack_ind.PRE + ltjb.in_unicode(c, true), 0, t)
250 postbreakpenalty = function(c, t)
251 return ltjs.get_stack_table(stack_ind.POST + ltjb.in_unicode(c, true), 0, t)
253 kcatcode = function(c, t)
254 return ltjs.get_stack_table(stack_ind.KCAT + ltjb.in_unicode(c, false), 0, t)
256 chartorange = function(c, t)
257 return ltjc.char_to_range(ltjb.in_unicode(c, false))
259 jaxspmode = function(c, t)
260 return ltjs.get_stack_table(stack_ind.XSP + ltjb.in_unicode(c, true), 3, t)
262 boxdir = function(c, t)
263 if type(c)~='number' or c<0 or c>65535 then
264 ltjb.package_error('luatexja',
265 'Bad register code (' .. tostring(c) .. ')',
266 'A register must be between 0 and 65535.\n'..
267 'I changed this one to zero.')
270 return ltjd.get_register_dir(c)
273 local binary_pars = luatexja.binary_pars
274 local scan_arg, scan_int = token.scan_argument, token.scan_int
275 binary_pars.alxspmode = binary_pars.jaxspmode
276 function luatexja.ext_get_parameter_binary(k, c)
277 if binary_pars[k] then
278 tex.write(tostring(binary_pars[k](c,tex.getcount('ltj@@stack'))))
280 ltjb.stop_time_measure('get_par')
284 -- EXT: print \global if necessary
285 function luatexja.ext_print_global()
286 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
292 local start_time_measure, stop_time_measure
293 = ltjb.start_time_measure, ltjb.stop_time_measure
294 local nullfunc = function (n) return n end
295 local to_node = node.direct.tonode
296 local to_direct = node.direct.todirect
297 local ensure_tex_attr = ltjb.ensure_tex_attr
299 -- mode = true iff main_process is called from pre_linebreak_filter
300 local function main_process(head, mode, dir, gc)
301 ensure_tex_attr(attr_icflag, 0)
302 if gc == 'fin_row' then return head
304 start_time_measure('jfmglue')
305 local p = ltjj.main(to_direct(head),mode, dir)
306 stop_time_measure('jfmglue')
311 local function adjust_icflag(h)
312 -- kern from luaotfload will have icflag = 1
313 -- (same as italic correction)
314 ensure_tex_attr(attr_icflag, 1)
319 ltjb.add_to_callback(
320 'pre_linebreak_filter',
321 function (head,groupcode)
322 return main_process(head, true, tex.textdir, groupcode)
324 luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor')+1)
325 ltjb.add_to_callback(
327 function (head,groupcode,size,packtype, dir)
328 return main_process(head, false, dir, groupcode)
330 luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor')+1)
331 ltjb.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
332 ltjb.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
337 local id_glyph = node.id('glyph')
338 function luatexja.pltx_composite_last_node_char()
339 local n = tex.nest[tex.nest.ptr].tail
342 if n.id==id_glyph then
343 while n.componetns and n.subtype and n.subtype%4 >= 2 do
354 local cache_ver = 3 -- must be same as ltj-kinsoku.tex
355 local cache_outdate_fn = function (t) return t.version~=cache_ver end
356 local t = ltjs.charprop_stack_table
357 function luatexja.load_kinsoku()
358 for i,_ in pairs(t) do t[i]=nil end
359 local kinsoku = ltjb.load_cache('ltj-kinsoku_default',cache_outdate_fn)
360 if kinsoku and kinsoku[1] then
363 t[0] = {}; tex.print(cat_lp, '\\input ltj-kinsoku.tex\\relax')
365 luatexja.load_kinsoku=nil
373 local node_type = node.type
374 local node_next = node.next
375 local has_attr = node.has_attribute
377 local id_penalty = node.id('penalty')
378 local id_glyph = node.id('glyph')
379 local id_glue = node.id('glue')
380 local id_kern = node.id('kern')
381 local id_hlist = node.id('hlist')
382 local id_vlist = node.id('vlist')
383 local id_rule = node.id('rule')
384 local id_math = node.id('math')
385 local id_whatsit = node.id('whatsit')
386 local sid_user = node.subtype('user_defined')
388 local prefix, inner_depth
389 local utfchar = utf.char
390 local function debug_show_node_X(p,print_fn, limit, inner_depth)
393 local pt, pic = node_type(p.id), (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
394 local base = prefix .. string.format('%X', pic) .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
395 if pt == 'glyph' then
397 .. (p.char<0xF0000 and utfchar(p.char) or '')
398 .. string.format(' (U+%X) ', p.char)
399 .. tostring(p.font) .. ' (' .. print_scaled(p.height) .. '+'
400 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
401 if p.xoffset~=0 or p.yoffset~=0 then
402 s = s .. ' off: (' .. print_scaled(p.xoffset)
403 .. ',' .. print_scaled(p.yoffset) .. ')'
406 elseif pt=='hlist' or pt=='vlist' or pt=='unset'or pt=='ins' then
408 s = base .. '(' .. print_scaled(p.height) .. '+'
409 .. print_scaled(p.depth) .. ')'
410 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
412 s = base .. '(' .. print_scaled(p.height) .. '+'
413 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
414 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
416 if (p.shift or 0)~=0 then
417 s = s .. ', shifted ' .. print_scaled(p.shift)
419 if p.glue_set and p.glue_sign ==2 or ( p.glue_sign==1 and p.glue_set>0) then
420 s = s .. ' glue set '
421 if p.glue_sign == 2 then s = s .. '-' end
422 s = s .. tostring(floor(p.glue_set*10000)/10000)
423 if p.glue_order == 0 then s = s .. 'pt'
426 for i = 2, p.glue_order do s = s .. 'l' end
429 if pic == icflag_table.PACKED then s = s .. ' (packed)' end
431 local bid = inner_depth
432 prefix, inner_depth = prefix.. '.', inner_depth + 1
433 if inner_depth < limit then
434 for q in node.traverse(p.head) do
435 debug_show_node_X(q, print_fn, limit, inner_depth)
439 elseif pt=='rule' then
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 elseif pt=='disc' then
446 local bid = inner_depth
447 if inner_depth < limit then
448 prefix, inner_depth = k.. 'p.', inner_depth + 1
449 for q in node.traverse(p.pre) do
450 debug_show_node_X(q, print_fn, limit, inner_depth)
453 for q in node.traverse(p.post) do
454 debug_show_node_X(q, print_fn, limit, inner_depth)
457 for q in node.traverse(p.replace) do
458 debug_show_node_X(q, print_fn, limit, inner_depth)
462 elseif pt == 'glue' then
463 s = base .. ' ' .. print_spec(p)
464 if pic>icflag_table.KINSOKU and pic<icflag_table.KANJI_SKIP then
465 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
466 elseif pic==icflag_table.KANJI_SKIP then
467 s = s .. ' (kanjiskip)'
468 elseif pic==icflag_table.KANJI_SKIP_JFM then
469 s = s .. ' (kanjiskip, JFM specified)'
470 elseif pic==icflag_table.XKANJI_SKIP then
471 s = s .. ' (xkanjiskip)'
472 elseif pic==icflag_table.XKANJI_SKIP_JFM then
473 s = s .. ' (xkanjiskip, JFM specified)'
476 elseif pt == 'kern' then
477 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
479 s = s .. ' (for accent)'
480 elseif pic==icflag_table.IC_PROCESSED then
481 s = s .. ' (italic correction)'
482 elseif pic==icflag_table.LINEEND then
483 s = s .. ' (end-of-line)'
484 elseif pic>icflag_table.KINSOKU
485 and pic<icflag_table.KANJI_SKIP then
486 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
489 elseif pt == 'penalty' then
490 s = base .. ' ' .. tostring(p.penalty)
491 if pic==icflag_table.KINSOKU then s = s .. ' (for kinsoku)' end
493 elseif pt == 'dir' then
494 print_fn(base .. ' ' .. tostring(p.dir) .. ' (level ' .. tostring(p.level) .. ')')
495 elseif pt == 'whatsit' then
497 if p.subtype==sid_user then
498 local t = tostring(p.user_id) .. ' (' ..
499 luatexbase.get_user_whatsit_name(p.user_id) .. ') '
500 if p.type ~= 110 then
501 s = s .. ' userid:' .. t .. p.value
504 s = s .. ' userid:' .. t .. '(node list)'
505 if p.user_id==uid_table.DIR then
506 s = s .. ' dir: ' .. tostring(node.has_attribute(p, attr_dir))
509 local bid = inner_depth
510 prefix, inner_depth = prefix.. '.', inner_depth + 1
511 if inner_depth < limit then
512 for q in node.traverse(p.value) do
513 debug_show_node_X(q, print_fn, limit, inner_depth)
516 prefix, inner_depth = k, bid
519 s = s .. (node.subtype(p.subtype) or '')
521 s = s .. ' stream=' .. p.stream
524 print_fn(s .. ' [' .. i .. '] = ' .. tostring(p.data[i].csname))
526 elseif p.subtype==16 then
527 s = s .. ' mode=' .. p.mode .. ', literal="' .. p.data .. '"'
533 -------- math node --------
534 elseif pt=='noad' then
537 prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn, limit, inner_depth);
540 prefix = k .. '^'; debug_show_node_X(p.sup, print_fn, limit, inner_depth);
543 prefix = k .. '_'; debug_show_node_X(p.sub, print_fn, limit, inner_depth);
546 elseif pt=='math_char' then
547 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utfchar(p.char)
549 elseif pt=='sub_box' or pt=='sub_mlist' then
553 for q in node.traverse(p.head) do
554 debug_show_node_X(q, print_fn, limit, inner_depth)
562 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
567 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth); head = node_next(head)
570 print_fn(prefix .. ' (null list)')
573 function luatexja.ext_show_node(head,depth,print_fn, lim)
577 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth)
579 print_fn(prefix .. ' (null list)')