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 icflag_table = {}
25 luatexja.icflag_table = icflag_table
26 icflag_table.ITALIC = 1
27 icflag_table.PACKED = 2
28 icflag_table.KINSOKU = 3
29 icflag_table.FROM_JFM = 6
30 -- FROM_JFM: 4, 5, 6, 7, 8 →優先度高(伸びやすく,縮みやすい)
32 icflag_table.KANJI_SKIP = 9
33 icflag_table.KANJI_SKIP_JFM = 10
34 icflag_table.XKANJI_SKIP = 11
35 icflag_table.XKANJI_SKIP_JFM = 12
36 icflag_table.PROCESSED = 13
37 icflag_table.IC_PROCESSED = 14
38 icflag_table.BOXBDD = 15
39 icflag_table.PROCESSED_BEGIN_FLAG = 128
41 local stack_table_index = {}
42 luatexja.stack_table_index = stack_table_index
43 stack_table_index.PRE = 0x200000 -- characterごと
44 stack_table_index.POST = 0x400000 -- characterごと
45 stack_table_index.KCAT = 0x600000 -- characterごと
46 stack_table_index.XSP = 0x800000 -- characterごと
47 stack_table_index.RIPRE = 0xA00000 -- characterごと,ruby pre
48 stack_table_index.RIPOST = 0xC00000 -- characterごと,ruby post
49 stack_table_index.JWP = 0 -- これだけ
50 stack_table_index.KSK = 1 -- これだけ
51 stack_table_index.XSK = 2 -- これだけ
52 stack_table_index.MJT = 0x100 -- 0--255
53 stack_table_index.MJS = 0x200 -- 0--255
54 stack_table_index.MJSS = 0x300 -- 0--255
55 stack_table_index.KSJ = 0x400 -- 0--9
57 local userid_table = {}
58 luatexja.userid_table = userid_table
59 userid_table.IHB = luatexbase.newuserwhatsitid('inhibitglue', 'luatexja') -- \inhibitglue
60 userid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
61 userid_table.BPAR = luatexbase.newuserwhatsitid('begin_par', 'luatexja') -- 「段落始め」
62 userid_table.DIR = luatexbase.newuserwhatsitid('direction', 'luatexja') -- 組方向
65 luatexja.dir_table = dir_table
66 dir_table.dir_dtou = 1
67 dir_table.dir_tate = 3
68 dir_table.dir_yoko = 4
69 dir_table.dir_math_mod = 8
70 dir_table.dir_node_auto = 128 -- 組方向を合わせるために自動で作られたもの
71 dir_table.dir_node_manual = 256 -- 寸法代入によって作られたもの
72 dir_table.dir_utod = dir_table.dir_tate + dir_table.dir_math_mod
73 -- 「縦数式ディレクション」 in pTeX
76 local load_module = luatexja.load_module
77 load_module('base'); local ltjb = luatexja.base
78 load_module('rmlgbm'); local ltjr = luatexja.rmlgbm -- must be 1st
80 if luatexja_debug then load_module('debug') end
82 load_module('charrange'); local ltjc = luatexja.charrange
83 load_module('stack'); local ltjs = luatexja.stack
84 load_module('direction'); local ltjd = luatexja.direction -- +1 hlist +1 attr_list
85 load_module('jfont'); local ltjf = luatexja.jfont
86 load_module('inputbuf'); local ltji = luatexja.inputbuf
87 load_module('pretreat'); local ltjp = luatexja.pretreat
88 load_module('setwidth'); local ltjw = luatexja.setwidth
89 load_module('jfmglue'); local ltjj = luatexja.jfmglue -- +1 glue +1 gs +1 attr_list
90 load_module('math'); local ltjm = luatexja.math
91 load_module('tangle'); local ltjb = luatexja.base
94 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
95 local attr_jchar_code = luatexbase.attributes['ltj@charcode']
96 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
97 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
98 local attr_icflag = luatexbase.attributes['ltj@icflag']
99 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
100 local attr_dir = luatexbase.attributes['ltj@dir']
101 local cat_lp = luatexbase.catcodetables['latex-package']
103 -- Three aux. functions, bollowed from tex.web
106 local floor = math.floor
108 local function print_scaled(s)
114 out=out..tostring(floor(s/unity)) .. '.'
117 if delta>unity then s=s+32768-50000 end
118 out=out .. tostring(floor(s/unity))
124 luatexja.print_scaled = print_scaled
126 local function print_glue(d,order)
127 local out=print_scaled(d)
131 out=out..'l'; order=order-1
139 local function print_spec(p)
140 local out=print_scaled(p.width)..'pt'
142 out=out..' plus '..print_glue(p.stretch,p.stretch_order)
145 out=out..' minus '..print_glue(p.shrink,p.shrink_order)
151 ------------------------------------------------------------------------
152 -- CODE FOR GETTING/SETTING PARAMETERS
153 ------------------------------------------------------------------------
155 -- EXT: print parameters that don't need arguments
157 luatexja.unary_pars = {
158 yalbaselineshift = function(t)
159 return print_scaled(tex.getattribute('ltj@yablshift'))..'pt'
161 yjabaselineshift = function(t)
162 return print_scaled(tex.getattribute('ltj@ykblshift'))..'pt'
164 talbaselineshift = function(t)
165 return print_scaled(tex.getattribute('ltj@tablshift'))..'pt'
167 tjabaselineshift = function(t)
168 return print_scaled(tex.getattribute('ltj@tkblshift'))..'pt'
170 kanjiskip = function(t)
171 return print_spec(ltjs.get_stack_skip(stack_table_index.KSK, t))
173 xkanjiskip = function(t)
174 return print_spec(ltjs.get_stack_skip(stack_table_index.XSK, t))
176 jcharwidowpenalty = function(t)
177 return ltjs.get_stack_table(stack_table_index.JWP, 0, t)
179 autospacing = function(t)
180 return tex.getattribute('ltj@autospc')
182 autoxspacing = function(t)
183 return tex.getattribute('ltj@autoxspc')
185 differentjfm = function(t)
186 local f, r = luatexja.jfmglue.diffmet_rule, '???'
187 if f == math.max then r = 'large'
188 elseif f == math.min then r = 'small'
189 elseif f == math.two_average then r = 'average'
190 elseif f == math.two_paverage then r = 'paverage'
191 elseif f == math.two_pleft then r = 'pleft'
192 elseif f == math.two_pright then r = 'pright'
193 elseif f == math.two_add then r = 'both'
197 direction = function()
198 local v = ltjd.get_dir_count()
199 if math.abs(tex.nest[tex.nest.ptr].mode) == ltjs.mmode and v == dir_table.dir_tate then
200 v = dir_table.dir_utod
204 adjustdir = ltjd.get_adjust_dir_count,
207 local unary_pars = luatexja.unary_pars
208 function luatexja.ext_get_parameter_unary(k)
209 if unary_pars[k] then
210 tex.write(tostring(unary_pars[k](tex.getcount('ltj@@stack'))))
212 ltjb.stop_time_measure('get_par')
217 -- EXT: print parameters that need arguments
219 luatexja.binary_pars = {
220 jacharrange = function(c, t)
221 if type(c)~='number' or c<0 or c>31*ltjc.ATTR_RANGE then
223 ltjb.package_error('luatexja',
224 'invalid character range number (' .. tostring(c) .. ')',
225 'A character range number should be in the range 1..'
226 .. 31*ltjc.ATTR_RANGE .. ",\n"..
227 'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
228 c=0 -- external range 217 == internal range 0
229 elseif c==31*ltjc.ATTR_RANGE then c=0
231 -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
232 return (c<0) and 1 or ltjc.get_range_setting(c)
234 prebreakpenalty = function(c, t)
235 return ltjs.get_stack_table(stack_table_index.PRE
236 + ltjb.in_unicode(c, true), 0, t)
238 postbreakpenalty = function(c, t)
239 return ltjs.get_stack_table(stack_table_index.POST
240 + ltjb.in_unicode(c, true), 0, t)
242 kcatcode = function(c, t)
243 return ltjs.get_stack_table(stack_table_index.KCAT
244 + ltjb.in_unicode(c, false), 0, t)
246 chartorange = function(c, t)
247 return ltjc.char_to_range(ltjb.in_unicode(c, false))
249 jaxspmode = function(c, t)
250 return ltjs.get_stack_table(stack_table_index.XSP
251 + ltjb.in_unicode(c, true), 3, t)
253 boxdir = function(c, t)
254 if type(c)~='number' or c<0 or c>65535 then
255 ltjb.package_error('luatexja',
256 'Bad register code (' .. tostring(c) .. ')',
257 'A register must be between 0 and 65535.\n'..
258 'I changed this one to zero.')
261 return ltjd.get_register_dir(c)
264 local binary_pars = luatexja.binary_pars
266 binary_pars.alxspmode = binary_pars.jaxspmode
267 function luatexja.ext_get_parameter_binary(k,c)
268 if binary_pars[k] then
269 tex.write(tostring(binary_pars[k](c,tex.getcount('ltj@@stack'))))
271 ltjb.stop_time_measure('get_par')
275 -- EXT: print \global if necessary
276 function luatexja.ext_print_global()
277 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
283 local start_time_measure, stop_time_measure
284 = ltjb.start_time_measure, ltjb.stop_time_measure
285 local nullfunc = function (n) return n end
286 local to_node = node.direct.tonode
287 local to_direct = node.direct.todirect
288 local ensure_tex_attr = ltjb.ensure_tex_attr
290 -- mode = true iff main_process is called from pre_linebreak_filter
291 local function main_process(head, mode, dir, gc)
292 ensure_tex_attr(attr_icflag, 0)
293 if gc == 'fin_row' then return head
295 --luatexja.ext_show_node_list(head, 'T> ', print)
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)
329 function luatexja.ext_cleanup()
330 ltjf.cleanup_size_cache()
331 ltjd.remove_end_whatsit()
339 local node_type = node.type
340 local node_next = node.next
341 local has_attr = node.has_attribute
343 local id_penalty = node.id('penalty')
344 local id_glyph = node.id('glyph')
345 local id_glue = node.id('glue')
346 local id_kern = node.id('kern')
347 local id_hlist = node.id('hlist')
348 local id_vlist = node.id('vlist')
349 local id_rule = node.id('rule')
350 local id_math = node.id('math')
351 local id_whatsit = node.id('whatsit')
352 local sid_user = node.subtype('user_defined')
354 local function get_attr_icflag(p)
355 return (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
358 local prefix, inner_depth
360 local function debug_show_node_X(p,print_fn, limit)
363 local pt=node_type(p.id)
364 local base = prefix .. string.format('%X', get_attr_icflag(p))
365 .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
366 if pt == 'glyph' then
367 s = base .. ' ' .. utf.char(p.char) .. ' '
369 .. ' (' .. print_scaled(p.height) .. '+'
370 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
372 elseif pt=='hlist' or pt=='vlist' or pt=='unset'or pt=='ins' then
374 s = base .. '(' .. print_scaled(p.height) .. '+'
375 .. print_scaled(p.depth) .. ')'
376 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
378 s = base .. '(' .. print_scaled(p.height) .. '+'
379 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
380 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
382 if (p.shift or 0)~=0 then
383 s = s .. ', shifted ' .. print_scaled(p.shift)
385 if p.glue_set and p.glue_sign ==2 or ( p.glue_sign==1 and p.glue_set>0) then
386 s = s .. ' glue set '
387 if p.glue_sign == 2 then s = s .. '-' end
388 s = s .. tostring(floor(p.glue_set*10000)/10000)
389 if p.glue_order == 0 then
393 for i = 2, p.glue_order do s = s .. 'l' end
396 if get_attr_icflag(p) == icflag_table.PACKED then
400 local bid = inner_depth
401 prefix, inner_depth = prefix.. '.', inner_depth + 1
402 if inner_depth < limit then
403 for q in node.traverse(p.head) do
404 debug_show_node_X(q, print_fn, limit)
408 elseif pt=='rule' then
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 elseif pt == 'glue' then
414 s = base .. ' ' .. print_spec(p)
415 if get_attr_icflag(p)>icflag_table.KINSOKU
416 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
417 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
418 elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP then
419 s = s .. ' (kanjiskip)'
420 elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP_JFM then
421 s = s .. ' (kanjiskip, JFM specified)'
422 elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP then
423 s = s .. ' (xkanjiskip)'
424 elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP_JFM then
425 s = s .. ' (xkanjiskip, JFM specified)'
428 elseif pt == 'kern' then
429 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
431 s = s .. ' (for accent)'
432 elseif get_attr_icflag(p)==icflag_table.IC_PROCESSED then
433 s = s .. ' (italic correction)'
434 -- elseif get_attr_icflag(p)==ITALIC then
435 -- s = s .. ' (italic correction)'
436 elseif get_attr_icflag(p)>icflag_table.KINSOKU
437 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
438 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
441 elseif pt == 'penalty' then
442 s = base .. ' ' .. tostring(p.penalty)
443 if get_attr_icflag(p)==icflag_table.KINSOKU then
444 s = s .. ' (for kinsoku)'
447 elseif pt == 'whatsit' then
449 if p.subtype==sid_user then
450 local t = tostring(p.user_id) .. ' (' ..
451 luatexbase.get_user_whatsit_name(p.user_id) .. ') '
452 if p.type ~= 110 then
453 s = s .. ' userid:' .. t .. p.value
456 s = s .. ' userid:' .. t .. '(node list)'
457 if p.user_id==userid_table.DIR then
458 s = s .. ' dir: ' .. tostring(node.has_attribute(p, attr_dir))
461 local bid = inner_depth
462 prefix, inner_depth =prefix.. '.', inner_depth + 1
463 if inner_depth < limit then
464 for q in node.traverse(p.value) do
465 debug_show_node_X(q, print_fn, limit)
468 prefix, inner_depth = k, bid
471 s = s .. (node.subtype(p.subtype) or '')
473 s = s .. ' stream=' .. p.stream
476 print_fn(s .. ' [' .. i .. '] = ' .. tostring(p.data[i].csname))
482 -------- math node --------
483 elseif pt=='noad' then
484 s = base ; print_fn(s)
486 prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn);
489 prefix = k .. '^'; debug_show_node_X(p.sup, print_fn);
492 prefix = k .. '_'; debug_show_node_X(p.sub, print_fn);
495 elseif pt=='math_char' then
496 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
498 elseif pt=='sub_box' or pt=='sub_mlist' then
502 for q in node.traverse(p.head) do
503 debug_show_node_X(q, print_fn)
511 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
516 debug_show_node_X(head, print_fn, lim or 1/0); head = node_next(head)
519 print_fn(prefix .. ' (null list)')
522 function luatexja.ext_show_node(head,depth,print_fn, lim)
526 debug_show_node_X(head, print_fn, lim or 1/0)
528 print_fn(prefix .. ' (null list)')