OSDN Git Service

support for not setting \ltj@{y,t}{a,k}blshift (unset -> 0pt)
[luatex-ja/luatexja.git] / src / luatexja.lua
1 require 'lualibs'
2 ------------------------------------------------------------------------
3 local function load_lua(fn)
4    local found = kpse.find_file(fn, 'tex')
5    if not found then
6       tex.error("LuaTeX-ja error: File `" .. fn .. "' not found")
7    else
8       texio.write_nl('(' .. found .. ')'); dofile(found)
9    end
10 end
11 luatexja.load_lua = load_lua
12 function luatexja.load_module(name) require('ltj-' .. name.. '.lua') end
13
14 do
15     local dnode = node.direct
16     local getfield, traverse = dnode.getfield, dnode.traverse
17     local node_new, set_attr, get_attr = dnode.new, dnode.set_attribute, dnode.get_attribute
18     local set_attrlist, get_attrlist = dnode.setattributelist, dnode.getattributelist
19     local unset_attr = dnode.unset_attribute
20     local attr_icflag = luatexbase.attributes['ltj@icflag']
21     local function node_inherit_attr(n, b, a)
22         if b or a then
23             local attrlist = get_attrlist(b or a)
24             local nic = get_attr(n, attr_icflag)
25             set_attrlist(n, attrlist); set_attr(n, attr_icflag, nic)
26             if b and a then
27                 for na in traverse(attrlist) do
28                     local id = getfield(na, 'number')
29                     if id and id~=attr_icflag and getfield(na, 'value')~=get_attr(a, id) then
30                         unset_attr(n, id)
31                     end
32                 end
33             end
34         end
35         return n
36     end
37     luatexja.node_inherit_attr = node_inherit_attr
38     luatexja.dnode_new = function (id, subtype, b, a)
39         return node_inherit_attr(node_new(id, subtype), b, a)
40     end
41 end
42 do
43    local dnode = node.direct
44    local setfield = dnode.setfield
45    luatexja.setglue = node.direct.setglue or
46    function(g,w,st,sh,sto,sho)
47       setfield(g,'width', w or 0); setfield(g,'stretch',st or 0); setfield(g,'shrink', sh or 0)
48       setfield(g,'stretch_order',sto or 0)
49       setfield(g,'shrink_order', sho or 0)
50    end
51    for _,v in pairs({'char', 'depth', 'dir', 'font', 'height', 'kern', 'lang', 'next', 
52                       'nucleus', 'offsets', 'penalty', 'shift', 'sub', 'subtype', 'sup'}) do
53          if not dnode['set'..v] then 
54              dnode['set'..v] = function(n, ...) return setfield(n, v, ...) end
55          end
56    end
57    local getfield = dnode.getfield
58    luatexja.getglue = node.direct.getglue or
59    function(g)
60       return getfield(g,'width'), getfield(g,'stretch'), getfield(g,'shrink'),
61              getfield(g,'stretch_order'), getfield(g,'shrink_order')
62    end
63    for _,v in pairs({'box', 'components', 'char', 'depth', 'font', 'height', 'kern', 'lang', 
64                      'list', 'next', 'nucleus', 'offsets', 'penalty', 'prev', 'shift', 'sub',
65                      'subtype', 'sup', 'whd', 'width', 'dir'}) do
66          if not dnode['get'..v] then 
67              dnode['get'..v] = function(n, ...) return getfield(n, v, ...) end
68          end
69     end
70 end
71
72
73 --- 以下は全ファイルで共有される定数
74 local icflag_table = {}
75 luatexja.icflag_table = icflag_table
76 icflag_table.ITALIC          = 1
77 icflag_table.PACKED          = 2
78 icflag_table.KINSOKU         = 3
79 icflag_table.FROM_JFM        = 4
80 icflag_table.KANJI_SKIP      = 68 -- = 4+64
81 icflag_table.KANJI_SKIP_JFM  = 69
82 icflag_table.XKANJI_SKIP     = 70
83 icflag_table.XKANJI_SKIP_JFM = 71
84 icflag_table.LINEEND         = 72
85 icflag_table.PROCESSED       = 73
86 icflag_table.IC_PROCESSED    = 74
87 icflag_table.BOXBDD          = 75
88 icflag_table.SPECIAL_JAGLUE  = 76
89 -- 段落組版中のノードリストでは通常のノード (not whatsit) だが
90 -- 和文処理グルー挿入プロセスで長さが決定されるもの
91 icflag_table.PROCESSED_BEGIN_FLAG = 4096 -- sufficiently large power of 2
92
93 local stack_ind = {}
94 luatexja.stack_table_index = stack_ind
95 stack_ind.PRE  = 0x200000 -- characterごと
96 stack_ind.POST = 0x400000 -- characterごと
97 stack_ind.KCAT = 0x600000 -- characterごと
98 stack_ind.XSP  = 0x800000 -- characterごと
99 stack_ind.RIPRE  = 0xA00000 -- characterごと,ruby pre
100 stack_ind.RIPOST = 0xC00000 -- characterごと,ruby post
101 stack_ind.JWP  = 0 -- これだけ
102 stack_ind.KSK  = 1 -- これだけ
103 stack_ind.XSK  = 2 -- これだけ
104 stack_ind.MJT  = 0x100 -- 0--255
105 stack_ind.MJS  = 0x200 -- 0--255
106 stack_ind.MJSS = 0x300 -- 0--255
107 stack_ind.KSJ  = 0x400 -- 0--9
108
109 local uid_table = {}
110 luatexja.userid_table = uid_table
111 uid_table.IHB  = luatexbase.newuserwhatsitid('inhibitglue',  'luatexja') -- \inhibitglue
112 uid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
113 uid_table.BPAR = luatexbase.newuserwhatsitid('begin_par',    'luatexja') -- 「段落始め」
114 uid_table.DIR  = luatexbase.newuserwhatsitid('direction',    'luatexja') -- 組方向
115 uid_table.BOXB = luatexbase.newuserwhatsitid('box_boundary', 'luatexja') -- 「ボックス始め・終わり」
116 uid_table.JA_AL_BDD = luatexbase.newuserwhatsitid('ja_al_boundary', 'luatexja')
117
118 local dir_table = {}
119 luatexja.dir_table = dir_table
120 dir_table.dir_dtou = 1
121 dir_table.dir_tate = 3
122 dir_table.dir_yoko = 4
123 dir_table.dir_math_mod    = 8
124 dir_table.dir_node_auto   = 128 -- 組方向を合わせるために自動で作られたもの
125 dir_table.dir_node_manual = 256 -- 寸法代入によって作られたもの
126 dir_table.dir_utod = dir_table.dir_tate + dir_table.dir_math_mod
127   -- 「縦数式ディレクション」 in pTeX
128 --- 定義終わり
129
130 local load_module = luatexja.load_module
131 load_module 'base';      local ltjb = luatexja.base
132 if tex.outputmode==0 then
133     ltjb.package_error('luatexja',
134       'DVI output is not supported in LuaTeX-ja',
135       'Use lua*tex instead dvilua*tex.')
136 end
137 load_module 'rmlgbm';    local ltjr = luatexja.rmlgbm -- must be 1st
138 if luatexja_debug then load_module('debug') end
139 load_module 'lotf_aux';  local ltju = luatexja.lotf_aux
140 load_module 'charrange'; local ltjc = luatexja.charrange
141 load_module 'stack';     local ltjs = luatexja.stack
142 load_module 'direction'; local ltjd = luatexja.direction -- +1 hlist +1 attr_list
143 load_module 'lineskip';  local ltjl = luatexja.lineskip -- +1 hlist +1 attr_list
144 load_module 'jfont';     local ltjf = luatexja.jfont
145 load_module 'inputbuf';  local ltji = luatexja.inputbuf
146 load_module 'pretreat';  local ltjp = luatexja.pretreat
147 load_module 'setwidth';  local ltjw = luatexja.setwidth
148 load_module 'jfmglue';   local ltjj = luatexja.jfmglue -- +1 glue +1 gs +1 attr_list
149 load_module 'math';      local ltjm = luatexja.math
150 load_module 'base';      local ltjb = luatexja.base
151
152 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
153 local attr_jchar_code = luatexbase.attributes['ltj@charcode']
154 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
155 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
156 local attr_icflag = luatexbase.attributes['ltj@icflag']
157 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
158 local attr_dir = luatexbase.attributes['ltj@dir']
159 local cat_lp = luatexbase.catcodetables['latex-package']
160
161 -- Three aux. functions, borrowed from tex.web
162
163 local floor = math.floor
164 local function print_scaled(s)
165    local out, delta = '', 10
166    if s<0 then s, out = -s, out..'-' end
167    out=out..tostring(floor(s/65536)) .. '.'
168    s=10*(s%65536)+5
169    repeat
170       if delta>65536 then s=s+32768-50000 end
171       out=out .. tostring(floor(s/65536))
172       s=10*(s%65536); delta=delta*10
173    until s<=delta
174    return out
175 end
176 luatexja.print_scaled = print_scaled
177
178 local function print_glue(d,order)
179    local out=print_scaled(d)
180    if order>0 then
181       out=out..'fi'
182       while order>1 do out=out..'l'; order=order-1 end
183    else out=out..'pt'
184    end
185    return out
186 end
187
188 local function print_spec(p)
189    local out=print_scaled(p.width)..'pt'
190    if p.stretch~=0 then
191       out=out..' plus '..print_glue(p.stretch,p.stretch_order)
192    end
193    if p.shrink~=0 then
194       out=out..' minus '..print_glue(p.shrink,p.shrink_order)
195    end
196 return out
197 end
198
199
200 ------------------------------------------------------------------------
201 -- CODE FOR GETTING/SETTING PARAMETERS
202 ------------------------------------------------------------------------
203
204 -- EXT: print parameters that don't need arguments
205 do
206    local tex_getattr = tex.getattribute
207    local function getattr(a, d)
208       local r = tex_getattr(a); d = d or 0
209       return (r==-0x7FFFFFFF) and d or r
210    end 
211    luatexja.unary_pars = {
212       yalbaselineshift = function(t)
213          return print_scaled(getattr('ltj@yablshift'))..'pt'
214       end,
215       yjabaselineshift = function(t)
216          return print_scaled(getattr('ltj@ykblshift'))..'pt'
217       end,
218       talbaselineshift = function(t)
219          return print_scaled(getattr('ltj@tablshift'))..'pt'
220       end,
221       tjabaselineshift = function(t)
222          return print_scaled(getattr('ltj@tkblshift'))..'pt'
223       end,
224       kanjiskip = function(t)
225          return print_spec(ltjs.get_stack_skip(stack_ind.KSK, t))
226       end,
227       xkanjiskip = function(t)
228          return print_spec(ltjs.get_stack_skip(stack_ind.XSK, t))
229       end,
230       jcharwidowpenalty = function(t)
231          return ltjs.get_stack_table(stack_ind.JWP, 0, t)
232       end,
233       autospacing = function(t)
234          return getattr('ltj@autospc', 1)
235       end,
236       autoxspacing = function(t)
237          return getattr('ltj@autoxspc', 1)
238       end,
239       differentjfm = function(t)
240          local f, r = luatexja.jfmglue.diffmet_rule, '???'
241          if f == math.max then r = 'large'
242          elseif f == math.min then r = 'small'
243          elseif f == math.two_average then r = 'average'
244          elseif f == math.two_paverage then r = 'paverage'
245          elseif f == math.two_pleft then r = 'pleft'
246          elseif f == math.two_pright then r = 'pright'
247          elseif f == math.two_add then r = 'both'
248          end
249          return r
250       end,
251       direction = function()
252          local v = ltjd.get_dir_count()
253          if math.abs(tex.nest[tex.nest.ptr].mode) == ltjs.mmode and v == dir_table.dir_tate then
254             v = dir_table.dir_utod
255          end
256          return v
257       end,
258       adjustdir = ltjd.get_adjust_dir_count,
259    }
260
261    local unary_pars = luatexja.unary_pars
262    local scan_arg = token.scan_argument
263    function luatexja.ext_get_parameter_unary()
264       local k= scan_arg()
265       if unary_pars[k] then
266          tex.write(tostring(unary_pars[k](tex.getcount('ltj@@stack'))))
267       end
268       ltjb.stop_time_measure('get_par')
269    end
270 end
271
272
273 -- EXT: print parameters that need arguments
274 do
275    luatexja.binary_pars = {
276       jacharrange = function(c, t)
277          if type(c)~='number' or c<-1 or c>31*ltjc.ATTR_RANGE then
278             -- 0, -1 はエラーにしない(隠し)
279             ltjb.package_error('luatexja',
280                                'invalid character range number (' .. tostring(c) .. ')',
281                                'A character range number should be in the range 1..'
282                                .. 31*ltjc.ATTR_RANGE .. ",\n"..
283                                'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
284             c=0 -- external range 217 == internal range 0
285          elseif c==31*ltjc.ATTR_RANGE then c=0
286          end
287          -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
288          if c<0 then return 1 else return (ltjc.get_range_setting(c)==0) and 0 or 1 end
289       end,
290       prebreakpenalty = function(c, t)
291          return ltjs.get_stack_table(stack_ind.PRE + ltjb.in_unicode(c, true), 0, t)
292       end,
293       postbreakpenalty = function(c, t)
294          return ltjs.get_stack_table(stack_ind.POST + ltjb.in_unicode(c, true), 0, t)
295       end,
296       kcatcode = function(c, t)
297          return ltjs.get_stack_table(stack_ind.KCAT + ltjb.in_unicode(c, false), 0, t)
298       end,
299       chartorange = function(c, t)
300          return ltjc.char_to_range(ltjb.in_unicode(c, false))
301       end,
302       jaxspmode = function(c, t)
303          return ltjs.get_stack_table(stack_ind.XSP + ltjb.in_unicode(c, true), 3, t)
304       end,
305       boxdir = function(c, t)
306          if type(c)~='number' or c<0 or c>65535 then
307             ltjb.package_error('luatexja',
308                                'Bad register code (' .. tostring(c) .. ')',
309                                'A register must be between 0 and 65535.\n'..
310                                'I changed this one to zero.')
311             c=0
312          end
313          return ltjd.get_register_dir(c)
314       end,
315    }
316    local binary_pars = luatexja.binary_pars
317    local scan_arg, scan_int = token.scan_argument, token.scan_int
318    binary_pars.alxspmode = binary_pars.jaxspmode
319    function luatexja.ext_get_parameter_binary(k, c)
320       if binary_pars[k] then
321          tex.write(tostring(binary_pars[k](c,tex.getcount('ltj@@stack'))))
322       end
323       ltjb.stop_time_measure('get_par')
324    end
325 end
326
327 -- EXT: print \global if necessary
328 function luatexja.ext_print_global()
329    if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
330 end
331
332
333 -- main process
334 do
335    local start_time_measure, stop_time_measure
336       = ltjb.start_time_measure, ltjb.stop_time_measure
337    local nullfunc = function (n) return n end
338    local to_node = node.direct.tonode
339    local to_direct = node.direct.todirect
340    local ensure_tex_attr = ltjb.ensure_tex_attr
341    local slide = node.slide
342    -- mode = true iff main_process is called from pre_linebreak_filter
343    local function main_process(head, mode, dir, gc)
344       ensure_tex_attr(attr_icflag, 0)
345       if gc == 'fin_row' then return head
346       else
347             start_time_measure('jfmglue')
348             slide(head);
349             local p = ltjj.main(to_direct(head),mode, dir)
350             stop_time_measure('jfmglue')
351             return to_node(p)
352       end
353    end
354
355    local function adjust_icflag(h)
356       -- kern from luaotfload will have icflag = 1
357       -- (same as italic correction)
358       ensure_tex_attr(attr_icflag, 1)
359       return h
360    end
361
362    -- callbacks
363    ltjb.add_to_callback(
364       'pre_linebreak_filter',
365       function (head,groupcode)
366          return main_process(head, true, tex.textdir, groupcode)
367       end,'ltj.main',
368       luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor')+1)
369    ltjb.add_to_callback(
370       'hpack_filter',
371       function (head,groupcode,size,packtype, dir)
372          return main_process(head, false, dir, groupcode)
373       end,'ltj.main',
374       luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor')+1)
375    ltjb.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
376    ltjb.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
377 end
378
379 -- lastnodechar
380 do
381    local id_glyph = node.id('glyph')
382    function luatexja.pltx_composite_last_node_char()
383       local n = tex.nest[tex.nest.ptr].tail
384       local r = '-1'
385       if n then
386          if n.id==id_glyph then
387             while n.componetns and  n.subtype and n.subtype%4 >= 2 do
388                n = node.tail(n)
389             end
390             r = tostring(n.char)
391          end
392       end
393       tex.sprint(-2, r)
394    end
395 end
396
397 do
398     local cache_ver = 3 -- must be same as ltj-kinsoku.tex
399     local cache_outdate_fn = function (t) return t.version~=cache_ver end
400     local t = ltjs.charprop_stack_table
401     function luatexja.load_kinsoku()
402         for i,_ in pairs(t) do t[i]=nil end
403         local kinsoku = ltjb.load_cache('ltj-kinsoku_default',cache_outdate_fn)
404         if kinsoku and kinsoku[1] then
405             t[0] = kinsoku[1]
406         else
407             t[0] = {}; tex.print(cat_lp, '\\input ltj-kinsoku.tex\\relax')
408         end
409         luatexja.load_kinsoku=nil; ltjs.charprop_stack_table = nil
410     end
411 end
412
413 -- debug
414
415 do
416
417 local node_type = node.type
418 local node_next = node.next
419 local has_attr = node.has_attribute
420
421 local id_penalty = node.id('penalty')
422 local id_glyph = node.id('glyph')
423 local id_glue = node.id('glue')
424 local id_kern = node.id('kern')
425 local id_hlist = node.id('hlist')
426 local id_vlist = node.id('vlist')
427 local id_rule = node.id('rule')
428 local id_math = node.id('math')
429 local id_whatsit = node.id('whatsit')
430 local sid_user = node.subtype('user_defined')
431
432 local prefix, inner_depth
433 local utfchar = utf.char
434 local function debug_show_node_X(p,print_fn, limit, inner_depth)
435    local k = prefix
436    local s
437    local pt, pic = node_type(p.id), (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
438    local base = prefix .. string.format('%X', pic) .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
439    if pt == 'glyph' then
440       s = base .. ' ' 
441           .. (p.char<0xF0000 and utfchar(p.char) or '') 
442           .. string.format(' (U+%X) ', p.char)
443           .. tostring(p.font) .. ' (' .. print_scaled(p.height) .. '+'
444           .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
445       if p.xoffset~=0 or p.yoffset~=0 then
446          s = s .. ' off: (' .. print_scaled(p.xoffset)
447                .. ',' .. print_scaled(p.yoffset) .. ')'
448       end
449       print_fn(s)
450    elseif pt=='hlist' or pt=='vlist' or pt=='unset'or pt=='ins' then
451       if pt=='ins' then
452          s = base .. '(' .. print_scaled(p.height) .. '+'
453             .. print_scaled(p.depth) .. ')'
454             .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
455       else
456          s = base .. '(' .. print_scaled(p.height) .. '+'
457             .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
458             .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
459       end
460       if (p.shift or 0)~=0 then
461          s = s .. ', shifted ' .. print_scaled(p.shift)
462       end
463       if p.glue_set and p.glue_sign ==2 or ( p.glue_sign==1 and p.glue_set>0) then
464          s = s .. ' glue set '
465          if p.glue_sign == 2 then s = s .. '-' end
466          s = s .. tostring(floor(p.glue_set*10000)/10000)
467          if p.glue_order == 0 then s = s .. 'pt'
468          else
469             s = s .. 'fi'
470             for i = 2, p.glue_order do s = s .. 'l' end
471          end
472       end
473       if pic == icflag_table.PACKED then s = s .. ' (packed)' end
474       print_fn(s);
475       local bid = inner_depth
476       prefix, inner_depth = prefix.. '.', inner_depth + 1
477       if inner_depth < limit then
478          for q in node.traverse(p.head) do
479             debug_show_node_X(q, print_fn, limit, inner_depth)
480          end
481       end
482       prefix=k
483    elseif pt=='rule' then
484       s = base .. '(' .. print_scaled(p.height) .. '+'
485          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
486          .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
487       print_fn(s)
488    elseif pt=='disc' then
489       print_fn(s)
490       local bid = inner_depth
491       if inner_depth < limit then
492          prefix, inner_depth = k.. 'p.', inner_depth + 1
493          for q in node.traverse(p.pre) do
494             debug_show_node_X(q, print_fn, limit, inner_depth)
495          end
496          prefix = k.. 'P.'
497          for q in node.traverse(p.post) do
498             debug_show_node_X(q, print_fn, limit, inner_depth)
499          end
500          prefix = k.. 'R.'
501          for q in node.traverse(p.replace) do
502             debug_show_node_X(q, print_fn, limit, inner_depth)
503          end
504       end
505       prefix=k
506    elseif pt == 'glue' then
507       s = base .. ' ' ..  print_spec(p)
508       if pic>icflag_table.KINSOKU and pic<icflag_table.KANJI_SKIP then
509          s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
510       elseif pic==icflag_table.KANJI_SKIP then
511          s = s .. ' (kanjiskip)'
512       elseif pic==icflag_table.KANJI_SKIP_JFM then
513          s = s .. ' (kanjiskip, JFM specified)'
514       elseif pic==icflag_table.XKANJI_SKIP then
515          s = s .. ' (xkanjiskip)'
516       elseif pic==icflag_table.XKANJI_SKIP_JFM then
517          s = s .. ' (xkanjiskip, JFM specified)'
518       end
519       print_fn(s)
520    elseif pt == 'kern' then
521       s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
522       if p.subtype==2 then
523          s = s .. ' (for accent)'
524       elseif pic==icflag_table.IC_PROCESSED then
525          s = s .. ' (italic correction)'
526       elseif pic==icflag_table.LINEEND then
527          s = s .. ' (end-of-line)'
528       elseif pic>icflag_table.KINSOKU
529          and pic<icflag_table.KANJI_SKIP then
530          s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
531       end
532       print_fn(s)
533    elseif pt == 'penalty' then
534       s = base .. ' ' .. tostring(p.penalty)
535       if pic==icflag_table.KINSOKU then s = s .. ' (for kinsoku)' end
536       print_fn(s)
537    elseif pt == 'dir' then
538       print_fn(base .. ' ' .. tostring(p.dir) .. ' (level ' .. tostring(p.level) .. ')')
539    elseif pt == 'whatsit' then
540       s = base
541       if p.subtype==sid_user then
542          local t = tostring(p.user_id) .. ' (' ..
543             luatexbase.get_user_whatsit_name(p.user_id) .. ') '
544          if p.type ~= 110 then
545             s = s .. ' userid:' .. t .. p.value
546             print_fn(s)
547          else
548             s = s .. ' userid:' .. t .. '(node list)'
549             if p.user_id==uid_table.DIR then
550                s = s .. ' dir: ' .. tostring(node.has_attribute(p, attr_dir))
551             end
552             print_fn(s)
553             local bid = inner_depth
554             prefix, inner_depth = prefix.. '.', inner_depth + 1
555             if inner_depth < limit then
556                for q in node.traverse(p.value) do
557                   debug_show_node_X(q, print_fn, limit, inner_depth)
558                end
559             end
560             prefix, inner_depth = k, bid
561          end
562       else
563          s = s .. (node.subtype(p.subtype) or '')
564          if p.subtype==1 then
565             s = s .. ' stream=' .. p.stream
566             print_fn(s)
567             for i=1,#p.data do
568                print_fn(s .. '  [' .. i .. '] = ' .. tostring(p.data[i] and p.date[i].csname))
569             end
570          elseif p.subtype==16 then
571             s = s .. ' mode=' .. p.mode .. ', literal="' .. p.data .. '"'
572             print_fn(s)
573          else
574             print_fn(s)
575          end
576       end
577    -------- math node --------
578    elseif pt=='noad' then
579       print_fn(base)
580       if p.nucleus then
581          prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn, limit, inner_depth);
582       end
583       if p.sup then
584          prefix = k .. '^'; debug_show_node_X(p.sup, print_fn, limit, inner_depth);
585       end
586       if p.sub then
587          prefix = k .. '_'; debug_show_node_X(p.sub, print_fn, limit, inner_depth);
588       end
589       prefix = k;
590    elseif pt=='math_char' then
591       s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utfchar(p.char)
592       print_fn(s)
593    elseif pt=='sub_box' or pt=='sub_mlist' then
594       print_fn(base)
595       if p.head then
596          prefix = k .. '.';
597          for q in node.traverse(p.head) do
598             debug_show_node_X(q, print_fn, limit, inner_depth)
599          end
600       end
601    elseif pt == 'attribute' then
602       s = base .. ' [' .. p.number .. '] = ' .. p.value
603       print_fn(s)
604    else
605       print_fn(base)
606    end
607    p=node_next(p)
608 end
609 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
610    prefix = depth
611    inner_depth = 0
612    if head then
613       while head do
614          debug_show_node_X(head, print_fn, lim or 1/0, inner_depth); head = node_next(head)
615       end
616    else
617       print_fn(prefix .. ' (null list)')
618    end
619 end
620 function luatexja.ext_show_node(head,depth,print_fn, lim)
621    prefix = depth
622    inner_depth = 0
623    if head then
624       debug_show_node_X(head, print_fn, lim or 1/0, inner_depth)
625    else
626       print_fn(prefix .. ' (null list)')
627    end
628 end
629
630 end