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)
44 local icflag_table = {}
45 luatexja.icflag_table = icflag_table
46 icflag_table.ITALIC = 1
47 icflag_table.PACKED = 2
48 icflag_table.KINSOKU = 3
49 icflag_table.FROM_JFM = 4
50 icflag_table.KANJI_SKIP = 68 -- = 4+64
51 icflag_table.KANJI_SKIP_JFM = 69
52 icflag_table.XKANJI_SKIP = 70
53 icflag_table.XKANJI_SKIP_JFM = 71
54 icflag_table.LINEEND = 72
55 icflag_table.PROCESSED = 73
56 icflag_table.IC_PROCESSED = 74
57 icflag_table.BOXBDD = 75
58 icflag_table.SPECIAL_JAGLUE = 76
59 -- 段落組版中のノードリストでは通常のノード (not whatsit) だが
60 -- 和文処理グルー挿入プロセスで長さが決定されるもの
61 icflag_table.PROCESSED_BEGIN_FLAG = 4096 -- sufficiently large power of 2
64 luatexja.stack_table_index = stack_ind
65 stack_ind.PRE = 0x200000 -- characterごと
66 stack_ind.POST = 0x400000 -- characterごと
67 stack_ind.KCAT = 0x600000 -- characterごと
68 stack_ind.XSP = 0x800000 -- characterごと
69 stack_ind.RIPRE = 0xA00000 -- characterごと,ruby pre
70 stack_ind.RIPOST = 0xC00000 -- characterごと,ruby post
71 stack_ind.JWP = 0 -- これだけ
72 stack_ind.KSK = 1 -- これだけ
73 stack_ind.XSK = 2 -- これだけ
74 stack_ind.MJT = 0x100 -- 0--255
75 stack_ind.MJS = 0x200 -- 0--255
76 stack_ind.MJSS = 0x300 -- 0--255
77 stack_ind.KSJ = 0x400 -- 0--9
80 luatexja.userid_table = uid_table
81 uid_table.IHB = luatexbase.newuserwhatsitid('inhibitglue', 'luatexja') -- \inhibitglue
82 uid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
83 uid_table.BPAR = luatexbase.newuserwhatsitid('begin_par', 'luatexja') -- 「段落始め」
84 uid_table.DIR = luatexbase.newuserwhatsitid('direction', 'luatexja') -- 組方向
85 uid_table.BOXB = luatexbase.newuserwhatsitid('box_boundary', 'luatexja') -- 「ボックス始め・終わり」
86 uid_table.JA_AL_BDD = luatexbase.newuserwhatsitid('ja_al_boundary', 'luatexja')
89 luatexja.dir_table = dir_table
90 dir_table.dir_dtou = 1
91 dir_table.dir_tate = 3
92 dir_table.dir_yoko = 4
93 dir_table.dir_math_mod = 8
94 dir_table.dir_node_auto = 128 -- 組方向を合わせるために自動で作られたもの
95 dir_table.dir_node_manual = 256 -- 寸法代入によって作られたもの
96 dir_table.dir_utod = dir_table.dir_tate + dir_table.dir_math_mod
97 -- 「縦数式ディレクション」 in pTeX
100 local load_module = luatexja.load_module
101 load_module 'base'; local ltjb = luatexja.base
102 if tex.outputmode==0 then
103 ltjb.package_error('luatexja',
104 'DVI output is not supported in LuaTeX-ja',
105 'Use lua*tex instead dvilua*tex.')
107 load_module 'rmlgbm'; local ltjr = luatexja.rmlgbm -- must be 1st
108 if luatexja_debug then load_module 'debug' end
109 load_module 'lotf_aux'; local ltju = luatexja.lotf_aux
110 load_module 'charrange'; local ltjc = luatexja.charrange
111 load_module 'stack'; local ltjs = luatexja.stack
112 load_module 'direction'; local ltjd = luatexja.direction -- +1 hlist +1 attr_list
113 load_module 'lineskip'; local ltjl = luatexja.lineskip -- +1 hlist +1 attr_list
114 load_module 'jfont'; local ltjf = luatexja.jfont
115 load_module 'inputbuf'; local ltji = luatexja.inputbuf
116 load_module 'pretreat'; local ltjp = luatexja.pretreat
117 load_module 'setwidth'; local ltjw = luatexja.setwidth
118 load_module 'jfmglue'; local ltjj = luatexja.jfmglue -- +1 glue +1 gs +1 attr_list
119 load_module 'math'; local ltjm = luatexja.math
120 load_module 'base'; local ltjb = luatexja.base
122 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
123 local attr_jchar_code = luatexbase.attributes['ltj@charcode']
124 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
125 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
126 local attr_icflag = luatexbase.attributes['ltj@icflag']
127 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
128 local attr_dir = luatexbase.attributes['ltj@dir']
129 local cat_lp = luatexbase.catcodetables['latex-package']
131 -- Three aux. functions, borrowed from tex.web
133 local floor = math.floor
134 local function print_scaled(s)
135 local out, delta = '', 10
136 if s<0 then s, out = -s, out..'-' end
137 out=out..tostring(floor(s/65536)) .. '.'
140 if delta>65536 then s=s+32768-50000 end
141 out=out .. tostring(floor(s/65536))
142 s=10*(s%65536); delta=delta*10
146 luatexja.print_scaled = print_scaled
148 local function print_glue(d,order)
149 local out=print_scaled(d)
152 while order>1 do out=out..'l'; order=order-1 end
158 local function print_spec(p)
159 local out=print_scaled(p.width or p[1])..'pt'
160 if (p.stretch or p[2])~=0 then
161 out=out..' plus '..print_glue(p.stretch or p[2], p.stretch_order or p[4])
163 if (p.shrink or p[3])~=0 then
164 out=out..' minus '..print_glue(p.shrink or p[3], p.shrink_order or p[5])
170 ------------------------------------------------------------------------
171 -- CODE FOR GETTING/SETTING PARAMETERS
172 ------------------------------------------------------------------------
173 local getcount, texwrite = tex.getcount, tex.write
174 local cnt_stack = luatexbase.registernumber 'ltj@@stack'
176 -- EXT: print parameters that don't need arguments
178 local tex_getattr, getnest = tex.getattribute, tex.getnest
179 local function getattr(a, d)
180 local r = tex_getattr(a); d = d or 0
181 return (r==-0x7FFFFFFF) and d or r
183 luatexja.unary_pars = {
184 yalbaselineshift = function(t)
185 return print_scaled(getattr('ltj@yablshift'))..'pt'
187 yjabaselineshift = function(t)
188 return print_scaled(getattr('ltj@ykblshift'))..'pt'
190 talbaselineshift = function(t)
191 return print_scaled(getattr('ltj@tablshift'))..'pt'
193 tjabaselineshift = function(t)
194 return print_scaled(getattr('ltj@tkblshift'))..'pt'
196 kanjiskip = function(t)
197 return print_spec(ltjs.get_stack_skip(stack_ind.KSK, t))
199 xkanjiskip = function(t)
200 return print_spec(ltjs.get_stack_skip(stack_ind.XSK, t))
202 jcharwidowpenalty = function(t)
203 return ltjs.get_stack_table(stack_ind.JWP, 0, t)
205 autospacing = function(t)
206 return getattr('ltj@autospc', 1)
208 autoxspacing = function(t)
209 return getattr('ltj@autoxspc', 1)
211 differentjfm = function(t)
212 local f, r = luatexja.jfmglue.diffmet_rule, '???'
213 if f == math.max then r = 'large'
214 elseif f == math.min then r = 'small'
215 elseif f == math.two_average then r = 'average'
216 elseif f == math.two_paverage then r = 'paverage'
217 elseif f == math.two_pleft then r = 'pleft'
218 elseif f == math.two_pright then r = 'pright'
219 elseif f == math.two_add then r = 'both'
223 direction = function()
224 local v = ltjd.get_dir_count()
225 if math.abs(getnest().mode) == ltjs.mmode and v == dir_table.dir_tate then
226 v = dir_table.dir_utod
230 adjustdir = ltjd.get_adjust_dir_count,
233 local unary_pars = luatexja.unary_pars
234 local scan_arg = token.scan_argument
235 function luatexja.ext_get_parameter_unary()
237 if unary_pars[k] then
238 texwrite(tostring(unary_pars[k](getcount(cnt_stack))))
240 ltjb.stop_time_measure 'get_par'
245 -- EXT: print parameters that need arguments
247 luatexja.binary_pars = {
248 jacharrange = function(c, t)
249 if type(c)~='number' or c<-1 or c>31*ltjc.ATTR_RANGE then
250 -- 0, -1 はエラーにしない(隠し)
251 ltjb.package_error('luatexja',
252 'invalid character range number (' .. tostring(c) .. ')',
253 'A character range number should be in the range 1..'
254 .. 31*ltjc.ATTR_RANGE .. ",\n"..
255 'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
256 c=0 -- external range 217 == internal range 0
257 elseif c==31*ltjc.ATTR_RANGE then c=0
259 -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
260 if c<0 then return 1 else return (ltjc.get_range_setting(c)==0) and 0 or 1 end
262 prebreakpenalty = function(c, t)
263 return ltjs.get_stack_table(stack_ind.PRE + ltjb.in_unicode(c, true), 0, t)
265 postbreakpenalty = function(c, t)
266 return ltjs.get_stack_table(stack_ind.POST + ltjb.in_unicode(c, true), 0, t)
268 kcatcode = function(c, t)
269 return ltjs.get_stack_table(stack_ind.KCAT + ltjb.in_unicode(c, false), 0, t)
271 chartorange = function(c, t)
272 return ltjc.char_to_range(ltjb.in_unicode(c, false))
274 jaxspmode = function(c, t)
275 return ltjs.get_stack_table(stack_ind.XSP + ltjb.in_unicode(c, true), 3, t)
277 boxdir = function(c, t)
278 if type(c)~='number' or c<0 or c>65535 then
279 ltjb.package_error('luatexja',
280 'Bad register code (' .. tostring(c) .. ')',
281 'A register must be between 0 and 65535.\n'..
282 'I changed this one to zero.')
285 return ltjd.get_register_dir(c)
288 local binary_pars = luatexja.binary_pars
289 local scan_arg, scan_int = token.scan_argument, token.scan_int
290 binary_pars.alxspmode = binary_pars.jaxspmode
291 function luatexja.ext_get_parameter_binary(k, c)
292 if binary_pars[k] then
293 texwrite(tostring(binary_pars[k](c, getcount(cnt_stack))))
295 ltjb.stop_time_measure 'get_par'
299 -- EXT: print \global if necessary
300 function luatexja.ext_print_global()
301 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
307 local start_time_measure, stop_time_measure
308 = ltjb.start_time_measure, ltjb.stop_time_measure
309 local nullfunc = function (n) return n end
310 local to_node = node.direct.tonode
311 local to_direct = node.direct.todirect
312 local ensure_tex_attr = ltjb.ensure_tex_attr
313 local slide = node.slide
314 -- mode = true iff main_process is called from pre_linebreak_filter
315 local function main_process(head, mode, dir, gc)
316 ensure_tex_attr(attr_icflag, 0)
317 if gc == 'fin_row' then return head
319 start_time_measure 'jfmglue'
321 local p = ltjj.main(to_direct(head),mode, dir)
322 stop_time_measure 'jfmglue'
327 local function adjust_icflag(h)
328 -- kern from luaotfload will have icflag = 1
329 -- (same as italic correction)
330 ensure_tex_attr(attr_icflag, 1)
335 ltjb.add_to_callback(
336 'pre_linebreak_filter',
337 function (head,groupcode)
338 return main_process(head, true, tex.textdir, groupcode)
340 luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor')+1)
341 ltjb.add_to_callback(
343 function (head,groupcode,size,packtype, dir)
344 return main_process(head, false, dir, groupcode)
346 luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor')+1)
347 ltjb.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
348 ltjb.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
353 local getnest = tex.getnest
354 local id_glyph = node.id 'glyph'
355 function luatexja.pltx_composite_last_node_char()
356 local n = getnest().tail
359 if n.id==id_glyph then
360 while n.components and n.subtype and n.subtype%4 >= 2 do
371 local cache_ver = 4 -- must be same as ltj-kinsoku.tex
372 local cache_outdate_fn = function (t) return t.version~=cache_ver end
373 local t = ltjs.charprop_stack_table
374 function luatexja.load_kinsoku()
375 for i,_ in pairs(t) do t[i]=nil end
376 local kinsoku = ltjb.load_cache('ltj-kinsoku_default',cache_outdate_fn)
377 if kinsoku and kinsoku[1] then
380 t[0] = {}; tex.print(cat_lp, '\\input ltj-kinsoku.tex\\relax')
382 luatexja.load_kinsoku=nil
390 local node_type = node.type
391 local node_next = node.next
392 local get_attr = node.get_attribute
394 local id_penalty = node.id 'penalty'
395 local id_glyph = node.id 'glyph'
396 local id_glue = node.id 'glue'
397 local id_kern = node.id 'kern'
398 local id_hlist = node.id 'hlist'
399 local id_vlist = node.id 'vlist'
400 local id_rule = node.id 'rule'
401 local id_math = node.id 'math'
402 local id_whatsit = node.id 'whatsit'
403 local sid_user = node.subtype 'user_defined'
405 local prefix, inner_depth
406 local utfchar = utf.char
407 local function debug_show_node_X(p,print_fn, limit, inner_depth)
410 local pt, pic = node_type(p.id), (get_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
411 local base = prefix .. string.format('%X', pic) .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
412 if pt == 'glyph' then
414 .. (p.char<0xF0000 and utfchar(p.char) or '')
415 .. string.format(' (U+%X) ', p.char)
416 .. tostring(p.font) .. ' (' .. print_scaled(p.height) .. '+'
417 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
418 if p.xoffset~=0 or p.yoffset~=0 then
419 s = s .. ' off: (' .. print_scaled(p.xoffset)
420 .. ',' .. print_scaled(p.yoffset) .. ')'
423 elseif pt=='hlist' or pt=='vlist' or pt=='unset'or pt=='ins' then
425 s = base .. '(' .. print_scaled(p.height) .. '+'
426 .. print_scaled(p.depth) .. ')'
427 .. ', dir=' .. tostring(node.get_attribute(p, attr_dir))
429 s = base .. '(' .. print_scaled(p.height) .. '+'
430 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
431 .. ', dir=' .. tostring(node.get_attribute(p, attr_dir))
433 if (p.shift or 0)~=0 then
434 s = s .. ', shifted ' .. print_scaled(p.shift)
436 if p.glue_set and p.glue_sign ==2 or ( p.glue_sign==1 and p.glue_set>0) then
437 s = s .. ' glue set '
438 if p.glue_sign == 2 then s = s .. '-' end
439 s = s .. tostring(floor(p.glue_set*10000)/10000)
440 if p.glue_order == 0 then s = s .. 'pt'
443 for i = 2, p.glue_order do s = s .. 'l' end
446 if pic == icflag_table.PACKED then s = s .. ' (packed)' end
448 local bid = inner_depth
449 prefix, inner_depth = prefix.. '.', inner_depth + 1
450 if inner_depth < limit then
451 for q in node.traverse(p.head) do
452 debug_show_node_X(q, print_fn, limit, inner_depth)
456 elseif pt=='rule' then
457 s = base .. '(' .. print_scaled(p.height) .. '+'
458 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
459 .. ', dir=' .. tostring(node.get_attribute(p, attr_dir))
461 elseif pt=='disc' then
463 local bid = inner_depth
464 if inner_depth < limit then
465 prefix, inner_depth = k.. 'p.', inner_depth + 1
466 for q in node.traverse(p.pre) do
467 debug_show_node_X(q, print_fn, limit, inner_depth)
470 for q in node.traverse(p.post) do
471 debug_show_node_X(q, print_fn, limit, inner_depth)
474 for q in node.traverse(p.replace) do
475 debug_show_node_X(q, print_fn, limit, inner_depth)
479 elseif pt == 'glue' then
480 s = base .. ' ' .. print_spec(p)
481 if pic>icflag_table.KINSOKU and pic<icflag_table.KANJI_SKIP then
482 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
483 elseif pic==icflag_table.KANJI_SKIP then
484 s = s .. ' (kanjiskip)'
485 elseif pic==icflag_table.KANJI_SKIP_JFM then
486 s = s .. ' (kanjiskip, JFM specified)'
487 elseif pic==icflag_table.XKANJI_SKIP then
488 s = s .. ' (xkanjiskip)'
489 elseif pic==icflag_table.XKANJI_SKIP_JFM then
490 s = s .. ' (xkanjiskip, JFM specified)'
493 elseif pt == 'kern' then
494 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
496 s = s .. ' (for accent)'
497 elseif pic==icflag_table.IC_PROCESSED then
498 s = s .. ' (italic correction)'
499 elseif pic==icflag_table.LINEEND then
500 s = s .. ' (end-of-line)'
501 elseif pic>icflag_table.KINSOKU
502 and pic<icflag_table.KANJI_SKIP then
503 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
506 elseif pt == 'penalty' then
507 s = base .. ' ' .. tostring(p.penalty)
508 if pic==icflag_table.KINSOKU then s = s .. ' (for kinsoku)' end
510 elseif pt == 'dir' then
511 print_fn(base .. ' ' .. tostring(p.dir) .. ' (level ' .. tostring(p.level) .. ')')
512 elseif pt == 'whatsit' then
514 if p.subtype==sid_user then
515 local t = tostring(p.user_id) .. ' (' ..
516 luatexbase.get_user_whatsit_name(p.user_id) .. ') '
517 if p.type ~= 110 then
518 s = s .. ' userid:' .. t .. p.value
521 s = s .. ' userid:' .. t .. '(node list)'
522 if p.user_id==uid_table.DIR then
523 s = s .. ' dir: ' .. tostring(node.get_attribute(p, attr_dir))
526 local bid = inner_depth
527 prefix, inner_depth = prefix.. '.', inner_depth + 1
528 if inner_depth < limit then
529 for q in node.traverse(p.value) do
530 debug_show_node_X(q, print_fn, limit, inner_depth)
533 prefix, inner_depth = k, bid
536 s = s .. (node.subtype(p.subtype) or '')
538 s = s .. ' stream=' .. p.stream
541 print_fn(s .. ' [' .. i .. '] = ' .. tostring(p.data[i] and p.date[i].csname))
543 elseif p.subtype==16 then
544 s = s .. ' mode=' .. p.mode .. ', literal="' .. p.data .. '"'
550 -------- math node --------
551 elseif pt=='noad' then
554 prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn, limit, inner_depth);
557 prefix = k .. '^'; debug_show_node_X(p.sup, print_fn, limit, inner_depth);
560 prefix = k .. '_'; debug_show_node_X(p.sub, print_fn, limit, inner_depth);
563 elseif pt=='math_char' then
564 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utfchar(p.char)
566 elseif pt=='sub_box' or pt=='sub_mlist' then
570 for q in node.traverse(p.head) do
571 debug_show_node_X(q, print_fn, limit, inner_depth)
574 elseif pt == 'attribute' then
575 s = base .. ' [' .. p.number .. '] = ' .. p.value
582 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
587 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth); head = node_next(head)
590 print_fn(prefix .. ' (null list)')
593 function luatexja.ext_show_node(head,depth,print_fn, lim)
597 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth)
599 print_fn(prefix .. ' (null list)')