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.PROCESSED_BEGIN_FLAG = 4096 -- sufficiently large power of 2
48 luatexja.stack_table_index = stack_ind
49 stack_ind.PRE = 0x200000 -- characterごと
50 stack_ind.POST = 0x400000 -- characterごと
51 stack_ind.KCAT = 0x600000 -- characterごと
52 stack_ind.XSP = 0x800000 -- characterごと
53 stack_ind.RIPRE = 0xA00000 -- characterごと,ruby pre
54 stack_ind.RIPOST = 0xC00000 -- characterごと,ruby post
55 stack_ind.JWP = 0 -- これだけ
56 stack_ind.KSK = 1 -- これだけ
57 stack_ind.XSK = 2 -- これだけ
58 stack_ind.MJT = 0x100 -- 0--255
59 stack_ind.MJS = 0x200 -- 0--255
60 stack_ind.MJSS = 0x300 -- 0--255
61 stack_ind.KSJ = 0x400 -- 0--9
64 luatexja.userid_table = uid_table
65 uid_table.IHB = luatexbase.newuserwhatsitid('inhibitglue', 'luatexja') -- \inhibitglue
66 uid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
67 uid_table.BPAR = luatexbase.newuserwhatsitid('begin_par', 'luatexja') -- 「段落始め」
68 uid_table.DIR = luatexbase.newuserwhatsitid('direction', 'luatexja') -- 組方向
69 uid_table.BOXB = luatexbase.newuserwhatsitid('box_boundary', 'luatexja') -- 「ボックス始め・終わり」
70 uid_table.JA_AL_BDD = luatexbase.newuserwhatsitid('ja_al_boundary', 'luatexja')
73 luatexja.dir_table = dir_table
74 dir_table.dir_dtou = 1
75 dir_table.dir_tate = 3
76 dir_table.dir_yoko = 4
77 dir_table.dir_math_mod = 8
78 dir_table.dir_node_auto = 128 -- 組方向を合わせるために自動で作られたもの
79 dir_table.dir_node_manual = 256 -- 寸法代入によって作られたもの
80 dir_table.dir_utod = dir_table.dir_tate + dir_table.dir_math_mod
81 -- 「縦数式ディレクション」 in pTeX
84 local load_module = luatexja.load_module
85 load_module 'base'; local ltjb = luatexja.base
86 load_module 'rmlgbm'; local ltjr = luatexja.rmlgbm -- must be 1st
87 if luatexja_debug then load_module('debug') end
88 load_module 'lotf_aux'; local ltju = luatexja.lotf_aux
89 load_module 'charrange'; local ltjc = luatexja.charrange
90 load_module 'stack'; local ltjs = luatexja.stack
91 load_module 'direction'; local ltjd = luatexja.direction -- +1 hlist +1 attr_list
92 load_module 'lineskip'; local ltjl = luatexja.lineskip -- +1 hlist +1 attr_list
93 load_module 'jfont'; local ltjf = luatexja.jfont
94 load_module 'inputbuf'; local ltji = luatexja.inputbuf
95 load_module 'pretreat'; local ltjp = luatexja.pretreat
96 load_module 'setwidth'; local ltjw = luatexja.setwidth
97 load_module 'jfmglue'; local ltjj = luatexja.jfmglue -- +1 glue +1 gs +1 attr_list
98 load_module 'math'; local ltjm = luatexja.math
99 load_module 'base'; local ltjb = luatexja.base
101 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
102 local attr_jchar_code = luatexbase.attributes['ltj@charcode']
103 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
104 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
105 local attr_icflag = luatexbase.attributes['ltj@icflag']
106 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
107 local attr_dir = luatexbase.attributes['ltj@dir']
108 local cat_lp = luatexbase.catcodetables['latex-package']
110 -- Three aux. functions, borrowed from tex.web
112 local floor = math.floor
113 local function print_scaled(s)
114 local out, delta = '', 10
115 if s<0 then s, out = -s, out..'-' end
116 out=out..tostring(floor(s/65536)) .. '.'
119 if delta>65536 then s=s+32768-50000 end
120 out=out .. tostring(floor(s/65536))
121 s=10*(s%65536); delta=delta*10
125 luatexja.print_scaled = print_scaled
127 local function print_glue(d,order)
128 local out=print_scaled(d)
131 while order>1 do out=out..'l'; order=order-1 end
137 local function print_spec(p)
138 local out=print_scaled(p.width)..'pt'
140 out=out..' plus '..print_glue(p.stretch,p.stretch_order)
143 out=out..' minus '..print_glue(p.shrink,p.shrink_order)
149 ------------------------------------------------------------------------
150 -- CODE FOR GETTING/SETTING PARAMETERS
151 ------------------------------------------------------------------------
153 -- EXT: print parameters that don't need arguments
155 local tex_getattr = tex.getattribute
156 local function getattr(a)
157 local r = tex_getattr(a)
158 return (r==-0x7FFFFFFF) and 0 or r
160 luatexja.unary_pars = {
161 yalbaselineshift = function(t)
162 return print_scaled(getattr('ltj@yablshift'))..'pt'
164 yjabaselineshift = function(t)
165 return print_scaled(getattr('ltj@ykblshift'))..'pt'
167 talbaselineshift = function(t)
168 return print_scaled(getattr('ltj@tablshift'))..'pt'
170 tjabaselineshift = function(t)
171 return print_scaled(getattr('ltj@tkblshift'))..'pt'
173 kanjiskip = function(t)
174 return print_spec(ltjs.get_stack_skip(stack_ind.KSK, t))
176 xkanjiskip = function(t)
177 return print_spec(ltjs.get_stack_skip(stack_ind.XSK, t))
179 jcharwidowpenalty = function(t)
180 return ltjs.get_stack_table(stack_ind.JWP, 0, t)
182 autospacing = function(t)
183 return getattr('ltj@autospc')
185 autoxspacing = function(t)
186 return getattr('ltj@autoxspc')
188 differentjfm = function(t)
189 local f, r = luatexja.jfmglue.diffmet_rule, '???'
190 if f == math.max then r = 'large'
191 elseif f == math.min then r = 'small'
192 elseif f == math.two_average then r = 'average'
193 elseif f == math.two_paverage then r = 'paverage'
194 elseif f == math.two_pleft then r = 'pleft'
195 elseif f == math.two_pright then r = 'pright'
196 elseif f == math.two_add then r = 'both'
200 direction = function()
201 local v = ltjd.get_dir_count()
202 if math.abs(tex.nest[tex.nest.ptr].mode) == ltjs.mmode and v == dir_table.dir_tate then
203 v = dir_table.dir_utod
207 adjustdir = ltjd.get_adjust_dir_count,
210 local unary_pars = luatexja.unary_pars
211 local scan_arg = token.scan_argument
212 function luatexja.ext_get_parameter_unary()
214 if unary_pars[k] then
215 tex.write(tostring(unary_pars[k](tex.getcount('ltj@@stack'))))
217 ltjb.stop_time_measure('get_par')
222 -- EXT: print parameters that need arguments
224 luatexja.binary_pars = {
225 jacharrange = function(c, t)
226 if type(c)~='number' or c<-1 or c>31*ltjc.ATTR_RANGE then
227 -- 0, -1 はエラーにしない(隠し)
228 ltjb.package_error('luatexja',
229 'invalid character range number (' .. tostring(c) .. ')',
230 'A character range number should be in the range 1..'
231 .. 31*ltjc.ATTR_RANGE .. ",\n"..
232 'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
233 c=0 -- external range 217 == internal range 0
234 elseif c==31*ltjc.ATTR_RANGE then c=0
236 -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
237 return (c<0) and 1 or ltjc.get_range_setting(c)
239 prebreakpenalty = function(c, t)
240 return ltjs.get_stack_table(stack_ind.PRE + ltjb.in_unicode(c, true), 0, t)
242 postbreakpenalty = function(c, t)
243 return ltjs.get_stack_table(stack_ind.POST + ltjb.in_unicode(c, true), 0, t)
245 kcatcode = function(c, t)
246 return ltjs.get_stack_table(stack_ind.KCAT + ltjb.in_unicode(c, false), 0, t)
248 chartorange = function(c, t)
249 return ltjc.char_to_range(ltjb.in_unicode(c, false))
251 jaxspmode = function(c, t)
252 return ltjs.get_stack_table(stack_ind.XSP + ltjb.in_unicode(c, true), 3, t)
254 boxdir = function(c, t)
255 if type(c)~='number' or c<0 or c>65535 then
256 ltjb.package_error('luatexja',
257 'Bad register code (' .. tostring(c) .. ')',
258 'A register must be between 0 and 65535.\n'..
259 'I changed this one to zero.')
262 return ltjd.get_register_dir(c)
265 local binary_pars = luatexja.binary_pars
266 local scan_arg, scan_int = token.scan_argument, token.scan_int
267 binary_pars.alxspmode = binary_pars.jaxspmode
268 function luatexja.ext_get_parameter_binary(k, c)
269 if binary_pars[k] then
270 tex.write(tostring(binary_pars[k](c,tex.getcount('ltj@@stack'))))
272 ltjb.stop_time_measure('get_par')
276 -- EXT: print \global if necessary
277 function luatexja.ext_print_global()
278 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
284 local start_time_measure, stop_time_measure
285 = ltjb.start_time_measure, ltjb.stop_time_measure
286 local nullfunc = function (n) return n end
287 local to_node = node.direct.tonode
288 local to_direct = node.direct.todirect
289 local ensure_tex_attr = ltjb.ensure_tex_attr
291 -- mode = true iff main_process is called from pre_linebreak_filter
292 local function main_process(head, mode, dir, gc)
293 ensure_tex_attr(attr_icflag, 0)
294 if gc == 'fin_row' then return head
296 start_time_measure('jfmglue')
297 local p = ltjj.main(to_direct(head),mode, dir)
298 stop_time_measure('jfmglue')
303 local function adjust_icflag(h)
304 -- kern from luaotfload will have icflag = 1
305 -- (same as italic correction)
306 ensure_tex_attr(attr_icflag, 1)
311 ltjb.add_to_callback(
312 'pre_linebreak_filter',
313 function (head,groupcode)
314 return main_process(head, true, tex.textdir, groupcode)
316 luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor')+1)
317 ltjb.add_to_callback(
319 function (head,groupcode,size,packtype, dir)
320 return main_process(head, false, dir, groupcode)
322 luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor')+1)
323 ltjb.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
324 ltjb.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
328 function luatexja.ext_cleanup()
329 ltjf.cleanup_size_cache(); ltjd.remove_end_whatsit()
334 local id_glyph = node.id('glyph')
335 function luatexja.pltx_composite_last_node_char()
336 local n = tex.nest[tex.nest.ptr].tail
339 if n.id==id_glyph then
340 while n.componetns and n.subtype and n.subtype%4 >= 2 do
351 local cache_ver = 1 -- must be same as ltj-kinsoku.tex
352 local cache_outdate_fn = function (t) return t.version~=cache_ver end
353 local t = luatexja.stack.charprop_stack_table
354 function luatexja.load_kinsoku()
355 for i,_ in pairs(t) do t[i]=nil; end
356 tex.setcount('ltj@@stack',0)
357 local kinsoku = ltjb.load_cache('ltj-kinsoku',cache_outdate_fn)
358 t[0] = kinsoku and kinsoku[1] or {}
360 tex.print(cat_lp, '\\input ltj-kinsoku.tex\\relax')
362 luatexja.load_kinsoku=nil
370 local node_type = node.type
371 local node_next = node.next
372 local has_attr = node.has_attribute
374 local id_penalty = node.id('penalty')
375 local id_glyph = node.id('glyph')
376 local id_glue = node.id('glue')
377 local id_kern = node.id('kern')
378 local id_hlist = node.id('hlist')
379 local id_vlist = node.id('vlist')
380 local id_rule = node.id('rule')
381 local id_math = node.id('math')
382 local id_whatsit = node.id('whatsit')
383 local sid_user = node.subtype('user_defined')
385 local prefix, inner_depth
386 local utfchar = utf.char
387 local function debug_show_node_X(p,print_fn, limit, inner_depth)
390 local pt, pic = node_type(p.id), (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
391 local base = prefix .. string.format('%X', pic) .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
392 if pt == 'glyph' then
394 .. (p.char<0xF0000 and utfchar(p.char) or '')
395 .. string.format(' (U+%X) ', p.char)
396 .. tostring(p.font) .. ' (' .. print_scaled(p.height) .. '+'
397 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
398 if p.xoffset~=0 or p.yoffset~=0 then
399 s = s .. ' off: (' .. print_scaled(p.xoffset)
400 .. ',' .. print_scaled(p.yoffset) .. ')'
403 elseif pt=='hlist' or pt=='vlist' or pt=='unset'or pt=='ins' then
405 s = base .. '(' .. print_scaled(p.height) .. '+'
406 .. print_scaled(p.depth) .. ')'
407 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
409 s = base .. '(' .. print_scaled(p.height) .. '+'
410 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
411 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
413 if (p.shift or 0)~=0 then
414 s = s .. ', shifted ' .. print_scaled(p.shift)
416 if p.glue_set and p.glue_sign ==2 or ( p.glue_sign==1 and p.glue_set>0) then
417 s = s .. ' glue set '
418 if p.glue_sign == 2 then s = s .. '-' end
419 s = s .. tostring(floor(p.glue_set*10000)/10000)
420 if p.glue_order == 0 then s = s .. 'pt'
423 for i = 2, p.glue_order do s = s .. 'l' end
426 if pic == icflag_table.PACKED then s = s .. ' (packed)' end
428 local bid = inner_depth
429 prefix, inner_depth = prefix.. '.', inner_depth + 1
430 if inner_depth < limit then
431 for q in node.traverse(p.head) do
432 debug_show_node_X(q, print_fn, limit, inner_depth)
436 elseif pt=='rule' then
437 s = base .. '(' .. print_scaled(p.height) .. '+'
438 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
439 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
441 elseif pt=='disc' then
443 local bid = inner_depth
444 if inner_depth < limit then
445 prefix, inner_depth = k.. 'p.', inner_depth + 1
446 for q in node.traverse(p.pre) do
447 debug_show_node_X(q, print_fn, limit, inner_depth)
450 for q in node.traverse(p.post) do
451 debug_show_node_X(q, print_fn, limit, inner_depth)
454 for q in node.traverse(p.replace) do
455 debug_show_node_X(q, print_fn, limit, inner_depth)
459 elseif pt == 'glue' then
460 s = base .. ' ' .. print_spec(p)
461 if pic>icflag_table.KINSOKU and pic<icflag_table.KANJI_SKIP then
462 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
463 elseif pic==icflag_table.KANJI_SKIP then
464 s = s .. ' (kanjiskip)'
465 elseif pic==icflag_table.KANJI_SKIP_JFM then
466 s = s .. ' (kanjiskip, JFM specified)'
467 elseif pic==icflag_table.XKANJI_SKIP then
468 s = s .. ' (xkanjiskip)'
469 elseif pic==icflag_table.XKANJI_SKIP_JFM then
470 s = s .. ' (xkanjiskip, JFM specified)'
473 elseif pt == 'kern' then
474 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
476 s = s .. ' (for accent)'
477 elseif pic==icflag_table.IC_PROCESSED then
478 s = s .. ' (italic correction)'
479 elseif pic==icflag_table.LINEEND then
480 s = s .. ' (end-of-line)'
481 elseif pic>icflag_table.KINSOKU
482 and pic<icflag_table.KANJI_SKIP then
483 s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
486 elseif pt == 'penalty' then
487 s = base .. ' ' .. tostring(p.penalty)
488 if pic==icflag_table.KINSOKU then s = s .. ' (for kinsoku)' end
490 elseif pt == 'dir' then
491 print_fn(base .. ' ' .. tostring(p.dir) .. ' (level ' .. tostring(p.level) .. ')')
492 elseif pt == 'whatsit' then
494 if p.subtype==sid_user then
495 local t = tostring(p.user_id) .. ' (' ..
496 luatexbase.get_user_whatsit_name(p.user_id) .. ') '
497 if p.type ~= 110 then
498 s = s .. ' userid:' .. t .. p.value
501 s = s .. ' userid:' .. t .. '(node list)'
502 if p.user_id==uid_table.DIR then
503 s = s .. ' dir: ' .. tostring(node.has_attribute(p, attr_dir))
506 local bid = inner_depth
507 prefix, inner_depth = prefix.. '.', inner_depth + 1
508 if inner_depth < limit then
509 for q in node.traverse(p.value) do
510 debug_show_node_X(q, print_fn, limit, inner_depth)
513 prefix, inner_depth = k, bid
516 s = s .. (node.subtype(p.subtype) or '')
518 s = s .. ' stream=' .. p.stream
521 print_fn(s .. ' [' .. i .. '] = ' .. tostring(p.data[i].csname))
523 elseif p.subtype==16 then
524 s = s .. ' mode=' .. p.mode .. ', literal="' .. p.data .. '"'
530 -------- math node --------
531 elseif pt=='noad' then
534 prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn, limit, inner_depth);
537 prefix = k .. '^'; debug_show_node_X(p.sup, print_fn, limit, inner_depth);
540 prefix = k .. '_'; debug_show_node_X(p.sub, print_fn, limit, inner_depth);
543 elseif pt=='math_char' then
544 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utfchar(p.char)
546 elseif pt=='sub_box' or pt=='sub_mlist' then
550 for q in node.traverse(p.head) do
551 debug_show_node_X(q, print_fn, limit, inner_depth)
559 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
564 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth); head = node_next(head)
567 print_fn(prefix .. ' (null list)')
570 function luatexja.ext_show_node(head,depth,print_fn, lim)
574 debug_show_node_X(head, print_fn, lim or 1/0, inner_depth)
576 print_fn(prefix .. ' (null list)')