3 tableunpack = table.unpack
5 ------------------------------------------------------------------------
6 local function load_lua(fn)
7 local found = kpse.find_file(fn, 'tex')
9 tex.error("LuaTeX-ja error: File `" .. fn .. "' not found")
11 texio.write_nl('(' .. found .. ')')
15 luatexja.load_lua = load_lua
16 function luatexja.load_module(name)
17 require('ltj-' .. name.. '.lua')
21 local setfield = node.direct.setfield
22 luatexja.setglue = node.direct.setglue or
23 function(g,w,st,sh,sto,sho)
24 setfield(g,'width', w or 0)
25 setfield(g,'stretch',st or 0)
26 setfield(g,'shrink', sh or 0)
27 setfield(g,'stretch_order',sto or 0)
28 setfield(g,'shrink_order', sho or 0)
30 local getfield = node.direct.getfield
31 luatexja.getglue = node.direct.getglue or
33 return getfield(g,'width'),
34 getfield(g,'stretch'),
36 getfield(g,'stretch_order'),
37 getfield(g,'shrink_order')
42 local icflag_table = {}
43 luatexja.icflag_table = icflag_table
44 icflag_table.ITALIC = 1
45 icflag_table.PACKED = 2
46 icflag_table.KINSOKU = 3
47 icflag_table.FROM_JFM = 4
48 -- FROM_JFM: 4, 5, 6, 7, 8 →優先度高(伸びやすく,縮みやすい)
50 icflag_table.KANJI_SKIP = 68 -- = 4+64
51 icflag_table.KANJI_SKIP_JFM = 69
52 icflag_table.XKANJI_SKIP = 70
53 icflag_table.XKANJI_SKIP_JFM = 71
54 icflag_table.LINEEND = 72
55 icflag_table.PROCESSED = 73
56 icflag_table.IC_PROCESSED = 74
57 icflag_table.BOXBDD = 75
58 icflag_table.PROCESSED_BEGIN_FLAG = 4096 -- sufficiently large power of 2
60 local stack_table_index = {}
61 luatexja.stack_table_index = stack_table_index
62 stack_table_index.PRE = 0x200000 -- characterごと
63 stack_table_index.POST = 0x400000 -- characterごと
64 stack_table_index.KCAT = 0x600000 -- characterごと
65 stack_table_index.XSP = 0x800000 -- characterごと
66 stack_table_index.RIPRE = 0xA00000 -- characterごと,ruby pre
67 stack_table_index.RIPOST = 0xC00000 -- characterごと,ruby post
68 stack_table_index.JWP = 0 -- これだけ
69 stack_table_index.KSK = 1 -- これだけ
70 stack_table_index.XSK = 2 -- これだけ
71 stack_table_index.MJT = 0x100 -- 0--255
72 stack_table_index.MJS = 0x200 -- 0--255
73 stack_table_index.MJSS = 0x300 -- 0--255
74 stack_table_index.KSJ = 0x400 -- 0--9
76 local userid_table = {}
77 luatexja.userid_table = userid_table
78 userid_table.IHB = luatexbase.newuserwhatsitid('inhibitglue', 'luatexja') -- \inhibitglue
79 userid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
80 userid_table.BPAR = luatexbase.newuserwhatsitid('begin_par', 'luatexja') -- 「段落始め」
81 userid_table.DIR = luatexbase.newuserwhatsitid('direction', 'luatexja') -- 組方向
82 userid_table.BOXB = luatexbase.newuserwhatsitid('box_boundary', 'luatexja') -- 「ボックス始め・終わり」
85 luatexja.dir_table = dir_table
86 dir_table.dir_dtou = 1
87 dir_table.dir_tate = 3
88 dir_table.dir_yoko = 4
89 dir_table.dir_math_mod = 8
90 dir_table.dir_node_auto = 128 -- 組方向を合わせるために自動で作られたもの
91 dir_table.dir_node_manual = 256 -- 寸法代入によって作られたもの
92 dir_table.dir_utod = dir_table.dir_tate + dir_table.dir_math_mod
93 -- 「縦数式ディレクション」 in pTeX
96 local load_module = luatexja.load_module
97 load_module('base'); local ltjb = luatexja.base
98 load_module('rmlgbm'); local ltjr = luatexja.rmlgbm -- must be 1st
100 if luatexja_debug then load_module('debug') end
102 load_module('charrange'); local ltjc = luatexja.charrange
103 load_module('stack'); local ltjs = luatexja.stack
104 load_module('direction'); local ltjd = luatexja.direction -- +1 hlist +1 attr_list
105 load_module('lineskip'); local ltjl = luatexja.lineskip -- +1 hlist +1 attr_list
106 load_module('jfont'); local ltjf = luatexja.jfont
107 load_module('inputbuf'); local ltji = luatexja.inputbuf
108 load_module('pretreat'); local ltjp = luatexja.pretreat
109 load_module('setwidth'); local ltjw = luatexja.setwidth
110 load_module('jfmglue'); local ltjj = luatexja.jfmglue -- +1 glue +1 gs +1 attr_list
111 load_module('math'); local ltjm = luatexja.math
112 load_module('base'); local ltjb = luatexja.base
115 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
116 local attr_jchar_code = luatexbase.attributes['ltj@charcode']
117 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
118 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
119 local attr_icflag = luatexbase.attributes['ltj@icflag']
120 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
121 local attr_dir = luatexbase.attributes['ltj@dir']
122 local cat_lp = luatexbase.catcodetables['latex-package']
124 -- Three aux. functions, bollowed from tex.web
127 local floor = math.floor
129 local function print_scaled(s)
135 out=out..tostring(floor(s/unity)) .. '.'
138 if delta>unity then s=s+32768-50000 end
139 out=out .. tostring(floor(s/unity))
145 luatexja.print_scaled = print_scaled
147 local function print_glue(d,order)
148 local out=print_scaled(d)
152 out=out..'l'; order=order-1
160 local function print_spec(p)
161 local out=print_scaled(p.width)..'pt'
163 out=out..' plus '..print_glue(p.stretch,p.stretch_order)
166 out=out..' minus '..print_glue(p.shrink,p.shrink_order)
172 ------------------------------------------------------------------------
173 -- CODE FOR GETTING/SETTING PARAMETERS
174 ------------------------------------------------------------------------
176 -- EXT: print parameters that don't need arguments
178 local tex_getattr = tex.getattribute
179 local function getattr(a)
180 local r = tex.getattribute(a)
181 return (r==-0x7FFFFFFF) and 0 or r
183 luatexja.unary_pars = {
184 yalbaselineshift = function(t)
185 return print_scaled(getattr('ltj@yablshift'))..'pt'
187 yjabaselineshift = function(t)
188 return print_scaled(getattr('ltj@ykblshift'))..'pt'
190 talbaselineshift = function(t)
191 return print_scaled(getattr('ltj@tablshift'))..'pt'
193 tjabaselineshift = function(t)
194 return print_scaled(getattr('ltj@tkblshift'))..'pt'
196 kanjiskip = function(t)
197 return print_spec(ltjs.get_stack_skip(stack_table_index.KSK, t))
199 xkanjiskip = function(t)
200 return print_spec(ltjs.get_stack_skip(stack_table_index.XSK, t))
202 jcharwidowpenalty = function(t)
203 return ltjs.get_stack_table(stack_table_index.JWP, 0, t)
205 autospacing = function(t)
206 return getattr('ltj@autospc')
208 autoxspacing = function(t)
209 return getattr('ltj@autoxspc')
211 differentjfm = function(t)
212 local f, r = luatexja.jfmglue.diffmet_rule, '???'
213 if f == math.max then r = 'large'
214 elseif f == math.min then r = 'small'
215 elseif f == math.two_average then r = 'average'
216 elseif f == math.two_paverage then r = 'paverage'
217 elseif f == math.two_pleft then r = 'pleft'
218 elseif f == math.two_pright then r = 'pright'
219 elseif f == math.two_add then r = 'both'
223 direction = function()
224 local v = ltjd.get_dir_count()
225 if math.abs(tex.nest[tex.nest.ptr].mode) == ltjs.mmode and v == dir_table.dir_tate then
226 v = dir_table.dir_utod
230 adjustdir = ltjd.get_adjust_dir_count,
233 local unary_pars = luatexja.unary_pars
234 function luatexja.ext_get_parameter_unary(k)
235 if unary_pars[k] then
236 tex.write(tostring(unary_pars[k](tex.getcount('ltj@@stack'))))
238 ltjb.stop_time_measure('get_par')
243 -- EXT: print parameters that need arguments
245 luatexja.binary_pars = {
246 jacharrange = function(c, t)
247 if type(c)~='number' or c<-1 or c>31*ltjc.ATTR_RANGE then
248 -- 0, -1 はエラーにしない(隠し)
249 ltjb.package_error('luatexja',
250 'invalid character range number (' .. tostring(c) .. ')',
251 'A character range number should be in the range 1..'
252 .. 31*ltjc.ATTR_RANGE .. ",\n"..
253 'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
254 c=0 -- external range 217 == internal range 0
255 elseif c==31*ltjc.ATTR_RANGE then c=0
257 -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
258 return (c<0) and 1 or ltjc.get_range_setting(c)
260 prebreakpenalty = function(c, t)
261 return ltjs.get_stack_table(stack_table_index.PRE
262 + ltjb.in_unicode(c, true), 0, t)
264 postbreakpenalty = function(c, t)
265 return ltjs.get_stack_table(stack_table_index.POST
266 + ltjb.in_unicode(c, true), 0, t)
268 kcatcode = function(c, t)
269 return ltjs.get_stack_table(stack_table_index.KCAT
270 + ltjb.in_unicode(c, false), 0, t)
272 chartorange = function(c, t)
273 return ltjc.char_to_range(ltjb.in_unicode(c, false))
275 jaxspmode = function(c, t)
276 return ltjs.get_stack_table(stack_table_index.XSP
277 + ltjb.in_unicode(c, true), 3, t)
279 boxdir = function(c, t)
280 if type(c)~='number' or c<0 or c>65535 then
281 ltjb.package_error('luatexja',
282 'Bad register code (' .. tostring(c) .. ')',
283 'A register must be between 0 and 65535.\n'..
284 'I changed this one to zero.')
287 return ltjd.get_register_dir(c)
290 local binary_pars = luatexja.binary_pars
292 binary_pars.alxspmode = binary_pars.jaxspmode
293 function luatexja.ext_get_parameter_binary(k,c)
294 if binary_pars[k] then
295 tex.write(tostring(binary_pars[k](c,tex.getcount('ltj@@stack'))))
297 ltjb.stop_time_measure('get_par')
301 -- EXT: print \global if necessary
302 function luatexja.ext_print_global()
303 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
309 local start_time_measure, stop_time_measure
310 = ltjb.start_time_measure, ltjb.stop_time_measure
311 local nullfunc = function (n) return n end
312 local to_node = node.direct.tonode
313 local to_direct = node.direct.todirect
314 local ensure_tex_attr = ltjb.ensure_tex_attr
316 -- mode = true iff main_process is called from pre_linebreak_filter
317 local function main_process(head, mode, dir, gc)
318 ensure_tex_attr(attr_icflag, 0)
319 if gc == 'fin_row' then return head
321 --luatexja.ext_show_node_list(head, 'T> ', print)
322 start_time_measure('jfmglue')
323 local p = ltjj.main(to_direct(head),mode, dir)
324 stop_time_measure('jfmglue')
329 local function adjust_icflag(h)
330 -- kern from luaotfload will have icflag = 1
331 -- (same as italic correction)
332 ensure_tex_attr(attr_icflag, 1)
337 ltjb.add_to_callback(
338 'pre_linebreak_filter',
339 function (head,groupcode)
340 return main_process(head, true, tex.textdir, groupcode)
342 luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor')+1)
343 ltjb.add_to_callback(
345 function (head,groupcode,size,packtype, dir)
346 return main_process(head, false, dir, groupcode)
348 luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor')+1)
349 ltjb.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
350 ltjb.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
355 function luatexja.ext_cleanup()
356 ltjf.cleanup_size_cache()
357 ltjd.remove_end_whatsit()
363 local id_glyph = node.id('glyph')
364 function luatexja.pltx_composite_last_node_char()
365 local n = tex.nest[tex.nest.ptr].tail
368 if n.id==id_glyph then
369 while n.componetns and n.subtype and n.subtype%4 >= 2 do
383 local node_type = node.type
384 local node_next = node.next
385 local has_attr = node.has_attribute
387 local id_penalty = node.id('penalty')
388 local id_glyph = node.id('glyph')
389 local id_glue = node.id('glue')
390 local id_kern = node.id('kern')
391 local id_hlist = node.id('hlist')
392 local id_vlist = node.id('vlist')
393 local id_rule = node.id('rule')
394 local id_math = node.id('math')
395 local id_whatsit = node.id('whatsit')
396 local sid_user = node.subtype('user_defined')
398 local function get_attr_icflag(p)
399 return (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
402 local prefix, inner_depth
404 local function debug_show_node_X(p,print_fn, limit)
407 local pt=node_type(p.id)
408 local base = prefix .. string.format('%X', get_attr_icflag(p))
409 .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
410 if pt == 'glyph' then
412 (p.char>=0xF0000 and string.format('(U+%X)', p.char) or utf.char(p.char)) .. ' '
413 .. tostring(p.font) .. ' (' .. print_scaled(p.height) .. '+'
414 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
415 if p.xoffset~=0 or p.yoffset~=0 then
416 s = s .. ' off: (' .. print_scaled(p.xoffset)
417 .. ',' .. print_scaled(p.yoffset) .. ')'
420 elseif pt=='hlist' or pt=='vlist' or pt=='unset'or pt=='ins' then
422 s = base .. '(' .. print_scaled(p.height) .. '+'
423 .. print_scaled(p.depth) .. ')'
424 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
426 s = base .. '(' .. print_scaled(p.height) .. '+'
427 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
428 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
430 if (p.shift or 0)~=0 then
431 s = s .. ', shifted ' .. print_scaled(p.shift)
433 if p.glue_set and p.glue_sign ==2 or ( p.glue_sign==1 and p.glue_set>0) then
434 s = s .. ' glue set '
435 if p.glue_sign == 2 then s = s .. '-' end
436 s = s .. tostring(floor(p.glue_set*10000)/10000)
437 if p.glue_order == 0 then
441 for i = 2, p.glue_order do s = s .. 'l' end
444 if get_attr_icflag(p) == icflag_table.PACKED then
448 local bid = inner_depth
449 prefix, inner_depth = prefix.. '.', inner_depth + 1
450 if inner_depth < limit then
451 for q in node.traverse(p.head) do
452 debug_show_node_X(q, print_fn, limit)
456 elseif pt=='rule' then
457 s = base .. '(' .. print_scaled(p.height) .. '+'
458 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
459 .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
461 elseif pt == 'glue' then
462 s = base .. ' ' .. print_spec(p)
463 if get_attr_icflag(p)>icflag_table.KINSOKU
464 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
465 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
466 elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP then
467 s = s .. ' (kanjiskip)'
468 elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP_JFM then
469 s = s .. ' (kanjiskip, JFM specified)'
470 elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP then
471 s = s .. ' (xkanjiskip)'
472 elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP_JFM then
473 s = s .. ' (xkanjiskip, JFM specified)'
476 elseif pt == 'kern' then
477 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
479 s = s .. ' (for accent)'
480 elseif get_attr_icflag(p)==icflag_table.IC_PROCESSED then
481 s = s .. ' (italic correction)'
482 elseif get_attr_icflag(p)==icflag_table.LINEEND then
483 s = s .. ' (end-of-line)'
484 -- elseif get_attr_icflag(p)==ITALIC then
485 -- s = s .. ' (italic correction)'
486 elseif get_attr_icflag(p)>icflag_table.KINSOKU
487 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
488 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
491 elseif pt == 'penalty' then
492 s = base .. ' ' .. tostring(p.penalty)
493 if get_attr_icflag(p)==icflag_table.KINSOKU then
494 s = s .. ' (for kinsoku)'
497 elseif pt == 'whatsit' then
499 if p.subtype==sid_user then
500 local t = tostring(p.user_id) .. ' (' ..
501 luatexbase.get_user_whatsit_name(p.user_id) .. ') '
502 if p.type ~= 110 then
503 s = s .. ' userid:' .. t .. p.value
506 s = s .. ' userid:' .. t .. '(node list)'
507 if p.user_id==userid_table.DIR then
508 s = s .. ' dir: ' .. tostring(node.has_attribute(p, attr_dir))
511 local bid = inner_depth
512 prefix, inner_depth =prefix.. '.', inner_depth + 1
513 if inner_depth < limit then
514 for q in node.traverse(p.value) do
515 debug_show_node_X(q, print_fn, limit)
518 prefix, inner_depth = k, bid
521 s = s .. (node.subtype(p.subtype) or '')
523 s = s .. ' stream=' .. p.stream
526 print_fn(s .. ' [' .. i .. '] = ' .. tostring(p.data[i].csname))
532 -------- math node --------
533 elseif pt=='noad' then
534 s = base ; print_fn(s)
536 prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn, limit);
539 prefix = k .. '^'; debug_show_node_X(p.sup, print_fn, limit);
542 prefix = k .. '_'; debug_show_node_X(p.sub, print_fn, limit);
545 elseif pt=='math_char' then
546 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
548 elseif pt=='sub_box' or pt=='sub_mlist' then
552 for q in node.traverse(p.head) do
553 debug_show_node_X(q, print_fn)
561 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
566 debug_show_node_X(head, print_fn, lim or 1/0); head = node_next(head)
569 print_fn(prefix .. ' (null list)')
572 function luatexja.ext_show_node(head,depth,print_fn, lim)
576 debug_show_node_X(head, print_fn, lim or 1/0)
578 print_fn(prefix .. ' (null list)')