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 local load_module = luatexja.load_module
76 load_module('base'); local ltjb = luatexja.base
77 load_module('rmlgbm'); local ltjr = luatexja.rmlgbm -- must be 1st
79 if luatexja_debug then load_module('debug') end
81 load_module('charrange'); local ltjc = luatexja.charrange
82 load_module('stack'); local ltjs = luatexja.stack
83 load_module('direction'); local ltjd = luatexja.direction -- +1 hlist +1 attr_list
84 load_module('jfont'); local ltjf = luatexja.jfont
85 load_module('inputbuf'); local ltji = luatexja.inputbuf
86 load_module('pretreat'); local ltjp = luatexja.pretreat
87 load_module('setwidth'); local ltjw = luatexja.setwidth
88 load_module('jfmglue'); local ltjj = luatexja.jfmglue -- +1 glue +1 gs +1 attr_list
89 load_module('math'); local ltjm = luatexja.math
90 load_module('tangle'); local ltjb = luatexja.base
93 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
94 local attr_jchar_code = luatexbase.attributes['ltj@charcode']
95 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
96 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
97 local attr_icflag = luatexbase.attributes['ltj@icflag']
98 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
99 local attr_dir = luatexbase.attributes['ltj@dir']
100 local cat_lp = luatexbase.catcodetables['latex-package']
102 -- Three aux. functions, bollowed from tex.web
105 local floor = math.floor
107 local function print_scaled(s)
113 out=out..tostring(floor(s/unity)) .. '.'
116 if delta>unity then s=s+32768-50000 end
117 out=out .. tostring(floor(s/unity))
124 local function print_glue(d,order)
125 local out=print_scaled(d)
129 out=out..'l'; order=order-1
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 luatexja.unary_pars = {
156 yalbaselineshift = function(t)
157 return print_scaled(tex.getattribute('ltj@yablshift'))..'pt'
159 yjabaselineshift = function(t)
160 return print_scaled(tex.getattribute('ltj@ykblshift'))..'pt'
162 talbaselineshift = function(t)
163 return print_scaled(tex.getattribute('ltj@tablshift'))..'pt'
165 tjabaselineshift = function(t)
166 return print_scaled(tex.getattribute('ltj@tkblshift'))..'pt'
168 kanjiskip = function(t)
169 return print_spec(ltjs.get_stack_skip(stack_table_index.KSK, t))
171 xkanjiskip = function(t)
172 return print_spec(ltjs.get_stack_skip(stack_table_index.XSK, t))
174 jcharwidowpenalty = function(t)
175 return ltjs.get_stack_table(stack_table_index.JWP, 0, t)
177 autospacing = function(t)
178 return tex.getattribute('ltj@autospc')
180 autoxspacing = function(t)
181 return tex.getattribute('ltj@autoxspc')
183 differentjfm = function(t)
184 local f, r = luatexja.jfmglue.diffmet_rule, '???'
185 if f == math.max then r = 'large'
186 elseif f == math.min then r = 'small'
187 elseif f == math.two_average then r = 'average'
188 elseif f == math.two_paverage then r = 'paverage'
189 elseif f == math.two_pleft then r = 'pleft'
190 elseif f == math.two_pright then r = 'pright'
191 elseif f == math.two_add then r = 'both'
195 direction = function()
196 local v = ltjd.get_dir_count()
197 if math.abs(tex.nest[tex.nest.ptr].mode) == ltjs.mmode and v == dir_table.dir_tate then
198 v = dir_table.dir_utod
202 adjustdir = ltjd.get_adjust_dir_count,
205 local unary_pars = luatexja.unary_pars
206 function luatexja.ext_get_parameter_unary(k)
207 if unary_pars[k] then
208 tex.write(tostring(unary_pars[k](tex.getcount('ltj@@stack'))))
210 ltjb.stop_time_measure('get_par')
215 -- EXT: print parameters that need arguments
217 luatexja.binary_pars = {
218 jacharrange = function(c, t)
219 if type(c)~='number' or c<0 or c>31*ltjc.ATTR_RANGE then
221 ltjb.package_error('luatexja',
222 'invalid character range number (' .. tostring(c) .. ')',
223 'A character range number should be in the range 1..'
224 .. 31*ltjc.ATTR_RANGE .. ",\n"..
225 'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
226 c=0 -- external range 217 == internal range 0
227 elseif c==31*ltjc.ATTR_RANGE then c=0
229 -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
230 return (c<0) and 1 or ltjc.get_range_setting(c)
232 prebreakpenalty = function(c, t)
233 return ltjs.get_stack_table(stack_table_index.PRE
234 + ltjb.in_unicode(c, true), 0, t)
236 postbreakpenalty = function(c, t)
237 return ltjs.get_stack_table(stack_table_index.POST
238 + ltjb.in_unicode(c, true), 0, t)
240 kcatcode = function(c, t)
241 return ltjs.get_stack_table(stack_table_index.KCAT
242 + ltjb.in_unicode(c, false), 0, t)
244 chartorange = function(c, t)
245 return ltjc.char_to_range(ltjb.in_unicode(c, false))
247 jaxspmode = function(c, t)
248 return ltjs.get_stack_table(stack_table_index.XSP
249 + ltjb.in_unicode(c, true), 3, t)
251 boxdir = function(c, t)
252 if type(c)~='number' or c<0 or c>65535 then
253 ltjb.package_error('luatexja',
254 'Bad register code (' .. tostring(c) .. ')',
255 'A register must be between 0 and 65535.\n'..
256 'I changed this one to zero.')
259 return ltjd.get_register_dir(c)
262 local binary_pars = luatexja.binary_pars
264 binary_pars.alxspmode = binary_pars.jaxspmode
265 function luatexja.ext_get_parameter_binary(k,c)
266 if binary_pars[k] then
267 tex.write(tostring(binary_pars[k](c,tex.getcount('ltj@@stack'))))
269 ltjb.stop_time_measure('get_par')
273 -- EXT: print \global if necessary
274 function luatexja.ext_print_global()
275 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
281 local start_time_measure, stop_time_measure
282 = ltjb.start_time_measure, ltjb.stop_time_measure
283 local Dnode = node.direct or node
284 local nullfunc = function (n) return n end
285 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
286 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
287 local tex_set_attr = tex.setattribute
288 local tex_get_attr, node_copy = tex.getattribute, Dnode.copy
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 --luatexja.ext_show_node_list(head, 'T> ', print)
297 start_time_measure('jfmglue')
298 local p = ltjj.main(to_direct(head),mode, dir)
299 stop_time_measure('jfmglue')
304 local function adjust_icflag(h)
305 -- kern from luaotfload will have icflag = 1
306 -- (same as italic correction)
307 ensure_tex_attr(attr_icflag, 1)
312 luatexbase.add_to_callback(
313 'pre_linebreak_filter',
314 function (head,groupcode)
315 return main_process(head, true, tex.textdir, groupcode)
316 end,'ltj.pre_linebreak_filter',
317 luatexbase.priority_in_callback('pre_linebreak_filter',
318 'luaotfload.node_processor') + 1)
319 luatexbase.add_to_callback(
321 function (head,groupcode,size,packtype, dir)
322 return main_process(head, false, dir, groupcode)
323 end,'ltj.hpack_filter',
324 luatexbase.priority_in_callback('hpack_filter',
325 'luaotfload.node_processor') + 1)
326 luatexbase.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
327 luatexbase.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
335 local node_type = node.type
336 local node_next = node.next
337 local has_attr = node.has_attribute
339 local id_penalty = node.id('penalty')
340 local id_glyph = node.id('glyph')
341 local id_glue_spec = node.id('glue_spec')
342 local id_glue = node.id('glue')
343 local id_kern = node.id('kern')
344 local id_hlist = node.id('hlist')
345 local id_vlist = node.id('vlist')
346 local id_rule = node.id('rule')
347 local id_math = node.id('math')
348 local id_whatsit = node.id('whatsit')
349 local sid_user = node.subtype('user_defined')
351 local function get_attr_icflag(p)
352 return (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
355 local prefix, inner_depth
357 local function debug_show_node_X(p,print_fn, limit)
360 local pt=node_type(p.id)
361 local base = prefix .. string.format('%X', get_attr_icflag(p))
362 .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
363 if pt == 'glyph' then
364 s = base .. ' ' .. utf.char(p.char) .. ' '
366 .. ' (' .. print_scaled(p.height) .. '+'
367 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
369 elseif pt=='hlist' or pt=='vlist' or pt=='unset'or pt=='ins' then
371 s = base .. '(' .. print_scaled(p.height) .. '+'
372 .. print_scaled(p.depth) .. ')'
373 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
375 s = base .. '(' .. print_scaled(p.height) .. '+'
376 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
377 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
379 if (p.shift or 0)~=0 then
380 s = s .. ', shifted ' .. print_scaled(p.shift)
382 if p.glue_set and p.glue_sign ==2 or ( p.glue_sign==1 and p.glue_set>0) then
383 s = s .. ' glue set '
384 if p.glue_sign == 2 then s = s .. '-' end
385 s = s .. tostring(floor(p.glue_set*10000)/10000)
386 if p.glue_order == 0 then
390 for i = 2, p.glue_order do s = s .. 'l' end
393 if get_attr_icflag(p) == icflag_table.PACKED then
397 local bid = inner_depth
398 prefix, inner_depth = prefix.. '.', inner_depth + 1
399 if inner_depth < limit then
400 for q in node.traverse(p.head) do
401 debug_show_node_X(q, print_fn, limit)
405 elseif pt=='rule' then
406 s = base .. '(' .. print_scaled(p.height) .. '+'
407 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
408 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
410 elseif pt == 'glue' then
411 s = base .. ' ' .. print_spec(p.spec)
412 if get_attr_icflag(p)>icflag_table.KINSOKU
413 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
414 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
415 elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP then
416 s = s .. ' (kanjiskip)'
417 elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP_JFM then
418 s = s .. ' (kanjiskip, JFM specified)'
419 elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP then
420 s = s .. ' (xkanjiskip)'
421 elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP_JFM then
422 s = s .. ' (xkanjiskip, JFM specified)'
425 elseif pt == 'kern' then
426 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
428 s = s .. ' (for accent)'
429 elseif get_attr_icflag(p)==icflag_table.IC_PROCESSED then
430 s = s .. ' (italic correction)'
431 -- elseif get_attr_icflag(p)==ITALIC then
432 -- s = s .. ' (italic correction)'
433 elseif get_attr_icflag(p)>icflag_table.KINSOKU
434 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
435 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
438 elseif pt == 'penalty' then
439 s = base .. ' ' .. tostring(p.penalty)
440 if get_attr_icflag(p)==icflag_table.KINSOKU then
441 s = s .. ' (for kinsoku)'
444 elseif pt == 'whatsit' then
446 if p.subtype==sid_user then
447 local t = tostring(p.user_id) .. ' (' ..
448 luatexbase.get_user_whatsit_name(p.user_id) .. ') '
449 if p.type ~= 110 then
450 s = s .. ' userid:' .. t .. p.value
453 s = s .. ' userid:' .. t .. '(node list)'
454 if p.user_id==userid_table.DIR then
455 s = s .. ' dir: ' .. tostring(node.has_attribute(p, attr_dir))
458 local bid = inner_depth
459 prefix, inner_depth =prefix.. '.', inner_depth + 1
460 if inner_depth < limit then
461 for q in node.traverse(p.value) do
462 debug_show_node_X(q, print_fn, limit)
465 prefix, inner_depth = k, bid
468 s = s .. node.subtype(p.subtype)
470 s = s .. ' stream=' .. p.stream
473 print_fn(s .. ' [' .. i .. '] = ' .. tostring(token.command_name(p.data[i])))
479 -------- math node --------
480 elseif pt=='noad' then
481 s = base ; print_fn(s)
483 prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn);
486 prefix = k .. '^'; debug_show_node_X(p.sup, print_fn);
489 prefix = k .. '_'; debug_show_node_X(p.sub, print_fn);
492 elseif pt=='math_char' then
493 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
495 elseif pt=='sub_box' or pt=='sub_mlist' then
499 for q in node.traverse(p.head) do
500 debug_show_node_X(q, print_fn)
508 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
513 debug_show_node_X(head, print_fn, lim or 1/0); head = node_next(head)
516 print_fn(prefix .. ' (null list)')
519 function luatexja.ext_show_node(head,depth,print_fn, lim)
523 debug_show_node_X(head, print_fn, lim or 1/0)
525 print_fn(prefix .. ' (null list)')