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)
336 function luatexja.ext_cleanup()
337 ltjf.cleanup_size_cache(); ltjd.remove_end_whatsit()
342 local id_glyph = node.id('glyph')
343 function luatexja.pltx_composite_last_node_char()
344 local n = tex.nest[tex.nest.ptr].tail
347 if n.id==id_glyph then
348 while n.componetns and n.subtype and n.subtype%4 >= 2 do
359 local cache_ver = 2 -- must be same as ltj-kinsoku.tex
360 local cache_outdate_fn = function (t) return t.version~=cache_ver end
361 local t = ltjs.charprop_stack_table
362 function luatexja.load_kinsoku()
363 for i,_ in pairs(t) do t[i]=nil end
364 local kinsoku = ltjb.load_cache('ltj-kinsoku_default',cache_outdate_fn)
365 if kinsoku and kinsoku[1] then
368 t[0] = {}; tex.print(cat_lp, '\\input ltj-kinsoku.tex\\relax')
370 luatexja.load_kinsoku=nil
378 local node_type = node.type
379 local node_next = node.next
380 local has_attr = node.has_attribute
382 local id_penalty = node.id('penalty')
383 local id_glyph = node.id('glyph')
384 local id_glue = node.id('glue')
385 local id_kern = node.id('kern')
386 local id_hlist = node.id('hlist')
387 local id_vlist = node.id('vlist')
388 local id_rule = node.id('rule')
389 local id_math = node.id('math')
390 local id_whatsit = node.id('whatsit')
391 local sid_user = node.subtype('user_defined')
393 local prefix, inner_depth
394 local utfchar = utf.char
395 local function debug_show_node_X(p,print_fn, limit, inner_depth)
398 local pt, pic = node_type(p.id), (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
399 local base = prefix .. string.format('%X', pic) .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
400 if pt == 'glyph' then
402 .. (p.char<0xF0000 and utfchar(p.char) or '')
403 .. string.format(' (U+%X) ', p.char)
404 .. tostring(p.font) .. ' (' .. print_scaled(p.height) .. '+'
405 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
406 if p.xoffset~=0 or p.yoffset~=0 then
407 s = s .. ' off: (' .. print_scaled(p.xoffset)
408 .. ',' .. print_scaled(p.yoffset) .. ')'
411 elseif pt=='hlist' or pt=='vlist' or pt=='unset'or pt=='ins' then
413 s = base .. '(' .. print_scaled(p.height) .. '+'
414 .. print_scaled(p.depth) .. ')'
415 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
417 s = base .. '(' .. print_scaled(p.height) .. '+'
418 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
419 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
421 if (p.shift or 0)~=0 then
422 s = s .. ', shifted ' .. print_scaled(p.shift)
424 if p.glue_set and p.glue_sign ==2 or ( p.glue_sign==1 and p.glue_set>0) then
425 s = s .. ' glue set '
426 if p.glue_sign == 2 then s = s .. '-' end
427 s = s .. tostring(floor(p.glue_set*10000)/10000)
428 if p.glue_order == 0 then s = s .. 'pt'
431 for i = 2, p.glue_order do s = s .. 'l' end
434 if pic == icflag_table.PACKED then s = s .. ' (packed)' end
436 local bid = inner_depth
437 prefix, inner_depth = prefix.. '.', inner_depth + 1
438 if inner_depth < limit then
439 for q in node.traverse(p.head) do
440 debug_show_node_X(q, print_fn, limit, inner_depth)
444 elseif pt=='rule' then
445 s = base .. '(' .. print_scaled(p.height) .. '+'
446 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
447 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
449 elseif pt=='disc' then
451 local bid = inner_depth
452 if inner_depth < limit then
453 prefix, inner_depth = k.. 'p.', inner_depth + 1
454 for q in node.traverse(p.pre) do
455 debug_show_node_X(q, print_fn, limit, inner_depth)
458 for q in node.traverse(p.post) do
459 debug_show_node_X(q, print_fn, limit, inner_depth)
462 for q in node.traverse(p.replace) do
463 debug_show_node_X(q, print_fn, limit, inner_depth)
467 elseif pt == 'glue' then
468 s = base .. ' ' .. print_spec(p)
469 if pic>icflag_table.KINSOKU and pic<icflag_table.KANJI_SKIP then
470 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
471 elseif pic==icflag_table.KANJI_SKIP then
472 s = s .. ' (kanjiskip)'
473 elseif pic==icflag_table.KANJI_SKIP_JFM then
474 s = s .. ' (kanjiskip, JFM specified)'
475 elseif pic==icflag_table.XKANJI_SKIP then
476 s = s .. ' (xkanjiskip)'
477 elseif pic==icflag_table.XKANJI_SKIP_JFM then
478 s = s .. ' (xkanjiskip, JFM specified)'
481 elseif pt == 'kern' then
482 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
484 s = s .. ' (for accent)'
485 elseif pic==icflag_table.IC_PROCESSED then
486 s = s .. ' (italic correction)'
487 elseif pic==icflag_table.LINEEND then
488 s = s .. ' (end-of-line)'
489 elseif pic>icflag_table.KINSOKU
490 and pic<icflag_table.KANJI_SKIP then
491 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
494 elseif pt == 'penalty' then
495 s = base .. ' ' .. tostring(p.penalty)
496 if pic==icflag_table.KINSOKU then s = s .. ' (for kinsoku)' end
498 elseif pt == 'dir' then
499 print_fn(base .. ' ' .. tostring(p.dir) .. ' (level ' .. tostring(p.level) .. ')')
500 elseif pt == 'whatsit' then
502 if p.subtype==sid_user then
503 local t = tostring(p.user_id) .. ' (' ..
504 luatexbase.get_user_whatsit_name(p.user_id) .. ') '
505 if p.type ~= 110 then
506 s = s .. ' userid:' .. t .. p.value
509 s = s .. ' userid:' .. t .. '(node list)'
510 if p.user_id==uid_table.DIR then
511 s = s .. ' dir: ' .. tostring(node.has_attribute(p, attr_dir))
514 local bid = inner_depth
515 prefix, inner_depth = prefix.. '.', inner_depth + 1
516 if inner_depth < limit then
517 for q in node.traverse(p.value) do
518 debug_show_node_X(q, print_fn, limit, inner_depth)
521 prefix, inner_depth = k, bid
524 s = s .. (node.subtype(p.subtype) or '')
526 s = s .. ' stream=' .. p.stream
529 print_fn(s .. ' [' .. i .. '] = ' .. tostring(p.data[i].csname))
531 elseif p.subtype==16 then
532 s = s .. ' mode=' .. p.mode .. ', literal="' .. p.data .. '"'
538 -------- math node --------
539 elseif pt=='noad' then
542 prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn, limit, inner_depth);
545 prefix = k .. '^'; debug_show_node_X(p.sup, print_fn, limit, inner_depth);
548 prefix = k .. '_'; debug_show_node_X(p.sub, print_fn, limit, inner_depth);
551 elseif pt=='math_char' then
552 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utfchar(p.char)
554 elseif pt=='sub_box' or pt=='sub_mlist' then
558 for q in node.traverse(p.head) do
559 debug_show_node_X(q, print_fn, limit, inner_depth)
567 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
572 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth); head = node_next(head)
575 print_fn(prefix .. ' (null list)')
578 function luatexja.ext_show_node(head,depth,print_fn, lim)
582 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth)
584 print_fn(prefix .. ' (null list)')