3 tableunpack = table.unpack
5 ------------------------------------------------------------------------
7 -- ext_... : called from \directlua{}
8 -- int_... : called from other Lua codes, but not from \directlua{}
9 -- (other) : only called from this file
10 function luatexja.load_module(name)
11 require('ltj-' .. name.. '.lua')
13 function luatexja.load_lua(fn)
14 local found = kpse.find_file(fn, 'tex')
16 tex.error("LuaTeX-ja error: File `" .. fn .. "' not found")
18 texio.write_nl('(' .. found .. ')')
24 local setfield = node.direct.setfield
25 luatexja.setglue = node.direct.setglue or
26 function(g,w,st,sh,sto,sho)
27 setfield(g,'width', w or 0)
28 setfield(g,'stretch',st or 0)
29 setfield(g,'shrink', sh or 0)
30 setfield(g,'stretch_order',sto or 0)
31 setfield(g,'shrink_order', sho or 0)
36 local icflag_table = {}
37 luatexja.icflag_table = icflag_table
38 icflag_table.ITALIC = 1
39 icflag_table.PACKED = 2
40 icflag_table.KINSOKU = 3
41 icflag_table.FROM_JFM = 6
42 -- FROM_JFM: 4, 5, 6, 7, 8 →優先度高(伸びやすく,縮みやすい)
44 icflag_table.KANJI_SKIP = 9
45 icflag_table.KANJI_SKIP_JFM = 10
46 icflag_table.XKANJI_SKIP = 11
47 icflag_table.XKANJI_SKIP_JFM = 12
48 icflag_table.PROCESSED = 13
49 icflag_table.IC_PROCESSED = 14
50 icflag_table.BOXBDD = 15
51 icflag_table.PROCESSED_BEGIN_FLAG = 128
53 local stack_table_index = {}
54 luatexja.stack_table_index = stack_table_index
55 stack_table_index.PRE = 0x200000 -- characterごと
56 stack_table_index.POST = 0x400000 -- characterごと
57 stack_table_index.KCAT = 0x600000 -- characterごと
58 stack_table_index.XSP = 0x800000 -- characterごと
59 stack_table_index.RIPRE = 0xA00000 -- characterごと,ruby pre
60 stack_table_index.RIPOST = 0xC00000 -- characterごと,ruby post
61 stack_table_index.JWP = 0 -- これだけ
62 stack_table_index.KSK = 1 -- これだけ
63 stack_table_index.XSK = 2 -- これだけ
64 stack_table_index.MJT = 0x100 -- 0--255
65 stack_table_index.MJS = 0x200 -- 0--255
66 stack_table_index.MJSS = 0x300 -- 0--255
67 stack_table_index.KSJ = 0x400 -- 0--9
69 local userid_table = {}
70 luatexja.userid_table = userid_table
71 userid_table.IHB = luatexbase.newuserwhatsitid('inhibitglue', 'luatexja') -- \inhibitglue
72 userid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
73 userid_table.BPAR = luatexbase.newuserwhatsitid('begin_par', 'luatexja') -- 「段落始め」
74 userid_table.DIR = luatexbase.newuserwhatsitid('direction', 'luatexja') -- 組方向
77 luatexja.dir_table = dir_table
78 dir_table.dir_dtou = 1
79 dir_table.dir_tate = 3
80 dir_table.dir_yoko = 4
81 dir_table.dir_math_mod = 8
82 dir_table.dir_node_auto = 128 -- 組方向を合わせるために自動で作られたもの
83 dir_table.dir_node_manual = 256 -- 寸法代入によって作られたもの
84 dir_table.dir_utod = dir_table.dir_tate + dir_table.dir_math_mod
85 -- 「縦数式ディレクション」 in pTeX
88 local load_module = luatexja.load_module
89 load_module('base'); local ltjb = luatexja.base
90 load_module('rmlgbm'); local ltjr = luatexja.rmlgbm -- must be 1st
92 if luatexja_debug then load_module('debug') end
94 load_module('charrange'); local ltjc = luatexja.charrange
95 load_module('stack'); local ltjs = luatexja.stack
96 load_module('direction'); local ltjd = luatexja.direction -- +1 hlist +1 attr_list
97 load_module('jfont'); local ltjf = luatexja.jfont
98 load_module('inputbuf'); local ltji = luatexja.inputbuf
99 load_module('pretreat'); local ltjp = luatexja.pretreat
100 load_module('setwidth'); local ltjw = luatexja.setwidth
101 load_module('jfmglue'); local ltjj = luatexja.jfmglue -- +1 glue +1 gs +1 attr_list
102 load_module('math'); local ltjm = luatexja.math
103 load_module('tangle'); local ltjb = luatexja.base
106 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
107 local attr_jchar_code = luatexbase.attributes['ltj@charcode']
108 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
109 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
110 local attr_icflag = luatexbase.attributes['ltj@icflag']
111 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
112 local attr_dir = luatexbase.attributes['ltj@dir']
113 local cat_lp = luatexbase.catcodetables['latex-package']
115 -- Three aux. functions, bollowed from tex.web
118 local floor = math.floor
120 local function print_scaled(s)
126 out=out..tostring(floor(s/unity)) .. '.'
129 if delta>unity then s=s+32768-50000 end
130 out=out .. tostring(floor(s/unity))
136 luatexja.print_scaled = print_scaled
138 local function print_glue(d,order)
139 local out=print_scaled(d)
143 out=out..'l'; order=order-1
151 local function print_spec(p)
152 local out=print_scaled(p.width)..'pt'
154 out=out..' plus '..print_glue(p.stretch,p.stretch_order)
157 out=out..' minus '..print_glue(p.shrink,p.shrink_order)
163 ------------------------------------------------------------------------
164 -- CODE FOR GETTING/SETTING PARAMETERS
165 ------------------------------------------------------------------------
167 -- EXT: print parameters that don't need arguments
169 luatexja.unary_pars = {
170 yalbaselineshift = function(t)
171 return print_scaled(tex.getattribute('ltj@yablshift'))..'pt'
173 yjabaselineshift = function(t)
174 return print_scaled(tex.getattribute('ltj@ykblshift'))..'pt'
176 talbaselineshift = function(t)
177 return print_scaled(tex.getattribute('ltj@tablshift'))..'pt'
179 tjabaselineshift = function(t)
180 return print_scaled(tex.getattribute('ltj@tkblshift'))..'pt'
182 kanjiskip = function(t)
183 return print_spec(ltjs.get_stack_skip(stack_table_index.KSK, t))
185 xkanjiskip = function(t)
186 return print_spec(ltjs.get_stack_skip(stack_table_index.XSK, t))
188 jcharwidowpenalty = function(t)
189 return ltjs.get_stack_table(stack_table_index.JWP, 0, t)
191 autospacing = function(t)
192 return tex.getattribute('ltj@autospc')
194 autoxspacing = function(t)
195 return tex.getattribute('ltj@autoxspc')
197 differentjfm = function(t)
198 local f, r = luatexja.jfmglue.diffmet_rule, '???'
199 if f == math.max then r = 'large'
200 elseif f == math.min then r = 'small'
201 elseif f == math.two_average then r = 'average'
202 elseif f == math.two_paverage then r = 'paverage'
203 elseif f == math.two_pleft then r = 'pleft'
204 elseif f == math.two_pright then r = 'pright'
205 elseif f == math.two_add then r = 'both'
209 direction = function()
210 local v = ltjd.get_dir_count()
211 if math.abs(tex.nest[tex.nest.ptr].mode) == ltjs.mmode and v == dir_table.dir_tate then
212 v = dir_table.dir_utod
216 adjustdir = ltjd.get_adjust_dir_count,
219 local unary_pars = luatexja.unary_pars
220 function luatexja.ext_get_parameter_unary(k)
221 if unary_pars[k] then
222 tex.write(tostring(unary_pars[k](tex.getcount('ltj@@stack'))))
224 ltjb.stop_time_measure('get_par')
229 -- EXT: print parameters that need arguments
231 luatexja.binary_pars = {
232 jacharrange = function(c, t)
233 if type(c)~='number' or c<-1 or c>31*ltjc.ATTR_RANGE then
234 -- 0, -1 はエラーにしない(隠し)
235 ltjb.package_error('luatexja',
236 'invalid character range number (' .. tostring(c) .. ')',
237 'A character range number should be in the range 1..'
238 .. 31*ltjc.ATTR_RANGE .. ",\n"..
239 'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
240 c=0 -- external range 217 == internal range 0
241 elseif c==31*ltjc.ATTR_RANGE then c=0
243 -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
244 return (c<0) and 1 or ltjc.get_range_setting(c)
246 prebreakpenalty = function(c, t)
247 return ltjs.get_stack_table(stack_table_index.PRE
248 + ltjb.in_unicode(c, true), 0, t)
250 postbreakpenalty = function(c, t)
251 return ltjs.get_stack_table(stack_table_index.POST
252 + ltjb.in_unicode(c, true), 0, t)
254 kcatcode = function(c, t)
255 return ltjs.get_stack_table(stack_table_index.KCAT
256 + ltjb.in_unicode(c, false), 0, t)
258 chartorange = function(c, t)
259 return ltjc.char_to_range(ltjb.in_unicode(c, false))
261 jaxspmode = function(c, t)
262 return ltjs.get_stack_table(stack_table_index.XSP
263 + ltjb.in_unicode(c, true), 3, t)
265 boxdir = function(c, t)
266 if type(c)~='number' or c<0 or c>65535 then
267 ltjb.package_error('luatexja',
268 'Bad register code (' .. tostring(c) .. ')',
269 'A register must be between 0 and 65535.\n'..
270 'I changed this one to zero.')
273 return ltjd.get_register_dir(c)
276 local binary_pars = luatexja.binary_pars
278 binary_pars.alxspmode = binary_pars.jaxspmode
279 function luatexja.ext_get_parameter_binary(k,c)
280 if binary_pars[k] then
281 tex.write(tostring(binary_pars[k](c,tex.getcount('ltj@@stack'))))
283 ltjb.stop_time_measure('get_par')
287 -- EXT: print \global if necessary
288 function luatexja.ext_print_global()
289 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
295 local start_time_measure, stop_time_measure
296 = ltjb.start_time_measure, ltjb.stop_time_measure
297 local nullfunc = function (n) return n end
298 local to_node = node.direct.tonode
299 local to_direct = node.direct.todirect
300 local ensure_tex_attr = ltjb.ensure_tex_attr
302 -- mode = true iff main_process is called from pre_linebreak_filter
303 local function main_process(head, mode, dir, gc)
304 ensure_tex_attr(attr_icflag, 0)
305 if gc == 'fin_row' then return head
307 --luatexja.ext_show_node_list(head, 'T> ', print)
308 start_time_measure('jfmglue')
309 local p = ltjj.main(to_direct(head),mode, dir)
310 stop_time_measure('jfmglue')
315 local function adjust_icflag(h)
316 -- kern from luaotfload will have icflag = 1
317 -- (same as italic correction)
318 ensure_tex_attr(attr_icflag, 1)
323 ltjb.add_to_callback(
324 'pre_linebreak_filter',
325 function (head,groupcode)
326 return main_process(head, true, tex.textdir, groupcode)
328 luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor')+1)
329 ltjb.add_to_callback(
331 function (head,groupcode,size,packtype, dir)
332 return main_process(head, false, dir, groupcode)
334 luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor')+1)
335 ltjb.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
336 ltjb.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
341 function luatexja.ext_cleanup()
342 ltjf.cleanup_size_cache()
343 ltjd.remove_end_whatsit()
349 local id_glyph = node.id('glyph')
350 function luatexja.pltx_composite_last_node_char()
351 local n = tex.nest[tex.nest.ptr].tail
354 if n.id==id_glyph then
355 while n.componetns and n.subtype and n.subtype%4 >= 2 do
369 local node_type = node.type
370 local node_next = node.next
371 local has_attr = node.has_attribute
373 local id_penalty = node.id('penalty')
374 local id_glyph = node.id('glyph')
375 local id_glue = node.id('glue')
376 local id_kern = node.id('kern')
377 local id_hlist = node.id('hlist')
378 local id_vlist = node.id('vlist')
379 local id_rule = node.id('rule')
380 local id_math = node.id('math')
381 local id_whatsit = node.id('whatsit')
382 local sid_user = node.subtype('user_defined')
384 local function get_attr_icflag(p)
385 return (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
388 local prefix, inner_depth
390 local function debug_show_node_X(p,print_fn, limit)
393 local pt=node_type(p.id)
394 local base = prefix .. string.format('%X', get_attr_icflag(p))
395 .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
396 if pt == 'glyph' then
397 s = base .. ' ' .. utf.char(p.char) .. ' '
399 .. ' (' .. print_scaled(p.height) .. '+'
400 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
401 .. ' xoff: ' .. print_scaled(p.xoffset)
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
424 for i = 2, p.glue_order do s = s .. 'l' end
427 if get_attr_icflag(p) == icflag_table.PACKED then
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)
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 == 'glue' then
445 s = base .. ' ' .. print_spec(p)
446 if get_attr_icflag(p)>icflag_table.KINSOKU
447 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
448 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
449 elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP then
450 s = s .. ' (kanjiskip)'
451 elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP_JFM then
452 s = s .. ' (kanjiskip, JFM specified)'
453 elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP then
454 s = s .. ' (xkanjiskip)'
455 elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP_JFM then
456 s = s .. ' (xkanjiskip, JFM specified)'
459 elseif pt == 'kern' then
460 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
462 s = s .. ' (for accent)'
463 elseif get_attr_icflag(p)==icflag_table.IC_PROCESSED then
464 s = s .. ' (italic correction)'
465 -- elseif get_attr_icflag(p)==ITALIC then
466 -- s = s .. ' (italic correction)'
467 elseif get_attr_icflag(p)>icflag_table.KINSOKU
468 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
469 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
472 elseif pt == 'penalty' then
473 s = base .. ' ' .. tostring(p.penalty)
474 if get_attr_icflag(p)==icflag_table.KINSOKU then
475 s = s .. ' (for kinsoku)'
478 elseif pt == 'whatsit' then
480 if p.subtype==sid_user then
481 local t = tostring(p.user_id) .. ' (' ..
482 luatexbase.get_user_whatsit_name(p.user_id) .. ') '
483 if p.type ~= 110 then
484 s = s .. ' userid:' .. t .. p.value
487 s = s .. ' userid:' .. t .. '(node list)'
488 if p.user_id==userid_table.DIR then
489 s = s .. ' dir: ' .. tostring(node.has_attribute(p, attr_dir))
492 local bid = inner_depth
493 prefix, inner_depth =prefix.. '.', inner_depth + 1
494 if inner_depth < limit then
495 for q in node.traverse(p.value) do
496 debug_show_node_X(q, print_fn, limit)
499 prefix, inner_depth = k, bid
502 s = s .. (node.subtype(p.subtype) or '')
504 s = s .. ' stream=' .. p.stream
507 print_fn(s .. ' [' .. i .. '] = ' .. tostring(p.data[i].csname))
513 -------- math node --------
514 elseif pt=='noad' then
515 s = base ; print_fn(s)
517 prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn);
520 prefix = k .. '^'; debug_show_node_X(p.sup, print_fn);
523 prefix = k .. '_'; debug_show_node_X(p.sub, print_fn);
526 elseif pt=='math_char' then
527 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
529 elseif pt=='sub_box' or pt=='sub_mlist' then
533 for q in node.traverse(p.head) do
534 debug_show_node_X(q, print_fn)
542 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
547 debug_show_node_X(head, print_fn, lim or 1/0); head = node_next(head)
550 print_fn(prefix .. ' (null list)')
553 function luatexja.ext_show_node(head,depth,print_fn, lim)
557 debug_show_node_X(head, print_fn, lim or 1/0)
559 print_fn(prefix .. ' (null list)')