4 ------------------------------------------------------------------------
6 -- ext_... : called from \directlua{}
7 -- int_... : called from other Lua codes, but not from \directlua{}
8 -- (other) : only called from this file
9 function luatexja.load_module(name)
10 require('ltj-' .. name.. '.lua')
12 function luatexja.load_lua(fn)
13 local found = kpse.find_file(fn, 'tex')
15 tex.error("LuaTeX-ja error: File `" .. fn .. "' not found")
17 texio.write_nl('(' .. found .. ')')
23 local icflag_table = {}
24 luatexja.icflag_table = icflag_table
25 icflag_table.ITALIC = 1
26 icflag_table.PACKED = 2
27 icflag_table.KINSOKU = 3
28 icflag_table.FROM_JFM = 6
29 -- FROM_JFM: 4, 5, 6, 7, 8 →優先度高(伸びやすく,縮みやすい)
31 icflag_table.KANJI_SKIP = 9
32 icflag_table.KANJI_SKIP_JFM = 10
33 icflag_table.XKANJI_SKIP = 11
34 icflag_table.XKANJI_SKIP_JFM = 12
35 icflag_table.PROCESSED = 13
36 icflag_table.IC_PROCESSED = 14
37 icflag_table.BOXBDD = 15
38 icflag_table.PROCESSED_BEGIN_FLAG = 128
40 local stack_table_index = {}
41 luatexja.stack_table_index = stack_table_index
42 stack_table_index.PRE = 0x200000 -- characterごと
43 stack_table_index.POST = 0x400000 -- characterごと
44 stack_table_index.KCAT = 0x600000 -- characterごと
45 stack_table_index.XSP = 0x800000 -- characterごと
46 stack_table_index.RIPRE = 0xA00000 -- characterごと,ruby pre
47 stack_table_index.RIPOST = 0xC00000 -- characterごと,ruby post
48 stack_table_index.JWP = 0 -- これだけ
49 stack_table_index.KSK = 1 -- これだけ
50 stack_table_index.XSK = 2 -- これだけ
51 stack_table_index.MJT = 0x100 -- 0--255
52 stack_table_index.MJS = 0x200 -- 0--255
53 stack_table_index.MJSS = 0x300 -- 0--255
54 stack_table_index.KSJ = 0x400 -- 0--9
56 local userid_table = {}
57 luatexja.userid_table = userid_table
58 userid_table.IHB = luatexbase.newuserwhatsitid('inhibitglue', 'luatexja') -- \inhibitglue
59 userid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
60 userid_table.BPAR = luatexbase.newuserwhatsitid('begin_par', 'luatexja') -- 「段落始め」
61 userid_table.DIR = luatexbase.newuserwhatsitid('direction', 'luatexja') -- 組方向
64 luatexja.dir_table = dir_table
65 dir_table.dir_dtou = 1
66 dir_table.dir_tate = 3
67 dir_table.dir_yoko = 4
68 dir_table.dir_math_mod = 8 -- 組方向を合わせるために自動で作られたもの
69 dir_table.dir_node_auto = 128 -- 組方向を合わせるために自動で作られたもの
70 dir_table.dir_node_manual = 256 -- 寸法代入によって作られたもの
71 dir_table.dir_utod = dir_table.dir_tate + dir_table.dir_math_mod
72 -- 「縦数式ディレクション」 in pTeX
75 ------------------------------------------------------------------------
77 ------------------------------------------------------------------------
79 local node_remove, node_next, node_prev = node.remove, node.next, node.prev
80 function luatexja.node_remove (head, current)
82 local q, r = node_next(current), node_prev(current)
83 if q then q.prev = r end
84 if r and node_next(r)==current then
89 return node_remove(head, current)
92 local Dnode = node.direct or node
94 local Dnode_remove, setfield = Dnode.remove, Dnode.setfield
95 local Dnode_next, Dnode_prev = Dnode.getnext, Dnode.getprev
96 function luatexja.Dnode_remove (head, current)
98 local q, r = Dnode_next(current), Dnode_prev(current)
99 if q then setfield(q, 'prev', r) end
100 if r and Dnode_next(r) == current then
101 setfield(r, 'next', q)
105 return Dnode_remove(head, current)
109 luatexja.Dnode_remove = luatexja.node_remove
115 local load_module = luatexja.load_module
116 load_module('base'); local ltjb = luatexja.base
117 load_module('rmlgbm'); local ltjr = luatexja.rmlgbm -- must be 1st
119 if luatexja_debug then load_module('debug') end
121 load_module('charrange'); local ltjc = luatexja.charrange
122 load_module('stack'); local ltjs = luatexja.stack
123 load_module('direction'); local ltjd = luatexja.direction
124 load_module('jfont'); local ltjf = luatexja.jfont
125 load_module('inputbuf'); local ltji = luatexja.inputbuf
126 load_module('pretreat'); local ltjp = luatexja.pretreat
127 load_module('jfmglue'); local ltjj = luatexja.jfmglue
128 load_module('setwidth'); local ltjw = luatexja.setwidth
129 load_module('math'); local ltjm = luatexja.math
130 load_module('tangle'); local ltjb = luatexja.base
132 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
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, bollowed from tex.web
143 local floor = math.floor
145 local function print_scaled(s)
151 out=out..tostring(floor(s/unity)) .. '.'
154 if delta>unity then s=s+32768-50000 end
155 out=out .. tostring(floor(s/unity))
162 local function print_glue(d,order)
163 local out=print_scaled(d)
167 out=out..'l'; order=order-1
175 local function print_spec(p)
176 local out=print_scaled(p.width)..'pt'
178 out=out..' plus '..print_glue(p.stretch,p.stretch_order)
181 out=out..' minus '..print_glue(p.shrink,p.shrink_order)
187 ------------------------------------------------------------------------
188 -- CODE FOR GETTING/SETTING PARAMETERS
189 ------------------------------------------------------------------------
191 -- EXT: print parameters that don't need arguments
193 luatexja.unary_pars = {
194 yalbaselineshift = function(t)
195 return print_scaled(tex.getattribute('ltj@yablshift'))..'pt'
197 yjabaselineshift = function(t)
198 return print_scaled(tex.getattribute('ltj@ykblshift'))..'pt'
200 talbaselineshift = function(t)
201 return print_scaled(tex.getattribute('ltj@tablshift'))..'pt'
203 tjabaselineshift = function(t)
204 return print_scaled(tex.getattribute('ltj@tkblshift'))..'pt'
206 kanjiskip = function(t)
207 return print_spec(ltjs.get_stack_skip(stack_table_index.KSK, t))
209 xkanjiskip = function(t)
210 return print_spec(ltjs.get_stack_skip(stack_table_index.XSK, t))
212 jcharwidowpenalty = function(t)
213 return ltjs.get_stack_table(stack_table_index.JWP, 0, t)
215 autospacing = function(t)
216 return tex.getattribute('ltj@autospc')
218 autoxspacing = function(t)
219 return tex.getattribute('ltj@autoxspc')
221 differentjfm = function(t)
222 local f, r = luatexja.jfmglue.diffmet_rule, '???'
223 if f == math.max then r = 'large'
224 elseif f == math.min then r = 'small'
225 elseif f == math.two_average then r = 'average'
226 elseif f == math.two_paverage then r = 'paverage'
227 elseif f == math.two_pleft then r = 'pleft'
228 elseif f == math.two_pright then r = 'pright'
229 elseif f == math.two_add then r = 'both'
233 direction = function()
234 local v = ltjd.get_dir_count()
235 if math.abs(tex.nest[tex.nest.ptr].mode) == ltjs.mmode and v == dir_table.dir_tate then
236 v = dir_table.dir_utod
240 adjustdir = ltjd.get_adjust_dir_count,
243 local unary_pars = luatexja.unary_pars
244 function luatexja.ext_get_parameter_unary(k)
245 if unary_pars[k] then
246 tex.write(tostring(unary_pars[k](tex.getcount('ltj@@stack'))))
252 -- EXT: print parameters that need arguments
254 luatexja.binary_pars = {
255 jacharrange = function(c, t)
256 if type(c)~='number' or c<0 or c>31*ltjc.ATTR_RANGE then
258 ltjb.package_error('luatexja',
259 'invalid character range number (' .. tostring(c) .. ')',
260 'A character range number should be in the range 1..'
261 .. 31*ltjc.ATTR_RANGE .. ",\n"..
262 'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
263 c=0 -- external range 217 == internal range 0
264 elseif c==31*ltjc.ATTR_RANGE then c=0
266 -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
267 return (c<0) and 1 or ltjc.get_range_setting(c)
269 prebreakpenalty = function(c, t)
270 return ltjs.get_stack_table(stack_table_index.PRE
271 + ltjb.in_unicode(c, true), 0, t)
273 postbreakpenalty = function(c, t)
274 return ltjs.get_stack_table(stack_table_index.POST
275 + ltjb.in_unicode(c, true), 0, t)
277 kcatcode = function(c, t)
278 return ltjs.get_stack_table(stack_table_index.KCAT
279 + ltjb.in_unicode(c, false), 0, t)
281 chartorange = function(c, t)
282 return ltjc.char_to_range(ltjb.in_unicode(c, false))
284 jaxspmode = function(c, t)
285 return ltjs.get_stack_table(stack_table_index.XSP
286 + ltjb.in_unicode(c, true), 3, t)
288 boxdir = function(c, t)
289 if type(c)~='number' or c<0 or c>65535 then
290 ltjb.package_error('luatexja',
291 'Bad register code (' .. tostring(c) .. ')',
292 'A register must be between 0 and 65535.\n'..
293 'I changed this one to zero.')
296 return ltjd.get_register_dir(c)
299 local binary_pars = luatexja.binary_pars
301 binary_pars.alxspmode = binary_pars.jaxspmode
302 function luatexja.ext_get_parameter_binary(k,c)
303 if binary_pars[k] then
304 tex.write(tostring(binary_pars[k](c,tex.getcount('ltj@@stack'))))
309 -- EXT: print \global if necessary
310 function luatexja.ext_print_global()
311 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
317 local start_time_measure, stop_time_measure
318 = ltjb.start_time_measure, ltjb.stop_time_measure
319 local Dnode = node.direct or node
320 local nullfunc = function (n) return n end
321 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
322 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
323 local tex_set_attr = tex.setattribute
324 -- mode = true iff main_process is called from pre_linebreak_filter
325 local function main_process(head, mode, dir, gc)
326 tex_set_attr('global', attr_icflag, 0)
327 if gc == 'fin_row' then return head
329 local p = to_direct(head)
330 start_time_measure('jfmglue')
331 p = ltjj.main(p,mode)
332 stop_time_measure('jfmglue'); start_time_measure('setwidth')
333 if p then p = ltjw.set_ja_width(p, dir) end
334 stop_time_measure('setwidth')
339 local function adjust_icflag(h)
340 -- kern from luaotfload will have icflag = 1
341 -- (same as italic correction)
342 tex_set_attr('global', attr_icflag, 1)
347 luatexbase.add_to_callback(
348 'pre_linebreak_filter',
349 function (head,groupcode)
350 return main_process(head, true, tex.textdir, groupcode)
351 end,'ltj.pre_linebreak_filter',
352 luatexbase.priority_in_callback('pre_linebreak_filter',
353 'luaotfload.node_processor') + 1)
354 luatexbase.add_to_callback(
356 function (head,groupcode,size,packtype, dir)
357 return main_process(head, false, dir, groupcode)
358 end,'ltj.hpack_filter',
359 luatexbase.priority_in_callback('hpack_filter',
360 'luaotfload.node_processor') + 1)
361 luatexbase.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
362 luatexbase.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
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_spec = node.id('glue_spec')
377 local id_glue = node.id('glue')
378 local id_kern = node.id('kern')
379 local id_hlist = node.id('hlist')
380 local id_vlist = node.id('vlist')
381 local id_rule = node.id('rule')
382 local id_math = node.id('math')
383 local id_whatsit = node.id('whatsit')
384 local sid_user = node.subtype('user_defined')
386 local function get_attr_icflag(p)
387 return (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
392 local function debug_show_node_X(p,print_fn)
393 local k = debug_depth
395 local pt=node_type(p.id)
396 local base = debug_depth .. string.format('%X', get_attr_icflag(p))
397 .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
398 if pt == 'glyph' then
399 s = base .. ' ' .. utf.char(p.char) .. ' '
401 .. ' (' .. print_scaled(p.height) .. '+'
402 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
404 elseif pt=='hlist' or pt=='vlist' or pt=='unset' then
405 s = base .. '(' .. print_scaled(p.height) .. '+'
406 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
407 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
408 if (p.shift or 0)~=0 then
409 s = s .. ', shifted ' .. print_scaled(p.shift)
411 if p.glue_sign >= 1 then
412 s = s .. ' glue set '
413 if p.glue_sign == 2 then s = s .. '-' end
414 s = s .. tostring(floor(p.glue_set*10000)/10000)
415 if p.glue_order == 0 then
419 for i = 2, p.glue_order do s = s .. 'l' end
422 if get_attr_icflag(p) == icflag_table.PACKED then
425 print_fn(s); debug_depth=debug_depth.. '.'
426 for q in node.traverse(p.head) do
427 debug_show_node_X(q, print_fn)
430 elseif pt == 'glue' then
431 s = base .. ' ' .. print_spec(p.spec)
432 if get_attr_icflag(p)>icflag_table.KINSOKU
433 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
434 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
435 elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP then
436 s = s .. ' (kanjiskip)'
437 elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP_JFM then
438 s = s .. ' (kanjiskip, JFM specified)'
439 elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP then
440 s = s .. ' (xkanjiskip)'
441 elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP_JFM then
442 s = s .. ' (xkanjiskip, JFM specified)'
445 elseif pt == 'kern' then
446 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
448 s = s .. ' (for accent)'
449 elseif get_attr_icflag(p)==icflag_table.IC_PROCESSED then
450 s = s .. ' (italic correction)'
451 -- elseif get_attr_icflag(p)==ITALIC then
452 -- s = s .. ' (italic correction)'
453 elseif get_attr_icflag(p)>icflag_table.KINSOKU
454 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
455 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
458 elseif pt == 'penalty' then
459 s = base .. ' ' .. tostring(p.penalty)
460 if get_attr_icflag(p)==icflag_table.KINSOKU then
461 s = s .. ' (for kinsoku)'
464 elseif pt == 'whatsit' then
466 if p.subtype==sid_user then
467 local t = tostring(p.user_id) .. ' (' ..
468 luatexbase.get_user_whatsit_name(p.user_id) .. ') '
469 if p.type ~= 110 then
470 s = s .. ' userid:' .. t .. p.value
473 s = s .. ' userid:' .. t .. '(node list)'
474 if p.user_id==userid_table.DIR then
475 s = s .. ' dir: ' .. tostring(node.has_attribute(p, attr_dir))
478 debug_depth=debug_depth.. '.'
479 for q in node.traverse(p.value) do
480 debug_show_node_X(q, print_fn)
485 s = s .. node.subtype(p.subtype)
487 s = s .. ' stream=' .. p.stream
490 print_fn(base .. ' [' .. i .. '] = ' .. tostring(p.data[i]))
496 -------- math node --------
497 elseif pt=='noad' then
498 s = base ; print_fn(s)
500 debug_depth = k .. 'N'; debug_show_node_X(p.nucleus, print_fn);
503 debug_depth = k .. '^'; debug_show_node_X(p.sup, print_fn);
506 debug_depth = k .. '_'; debug_show_node_X(p.sub, print_fn);
509 elseif pt=='math_char' then
510 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
512 elseif pt=='sub_box' or pt=='sub_mlist' then
515 debug_depth = k .. '.';
516 for q in node.traverse(p.head) do
517 debug_show_node_X(q, print_fn)
525 function luatexja.ext_show_node_list(head,depth,print_fn)
529 debug_show_node_X(head, print_fn); head = node_next(head)
532 print_fn(debug_depth .. ' (null list)')
535 function luatexja.ext_show_node(head,depth,print_fn)
538 debug_show_node_X(head, print_fn)
540 print_fn(debug_depth .. ' (null list)')