OSDN Git Service

use node.direct.{g/s}etdata
[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(s//65536) .. '.'
168    s=10*(s%65536)+5
169    repeat
170       if delta>65536 then s=s+32768-50000 end
171       out=out .. tostring(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 or p[1])..'pt'
190    if (p.stretch or p[2])~=0 then
191       out=out..' plus '..print_glue(p.stretch or p[2], p.stretch_order or p[4])
192    end
193    if (p.shrink or p[3])~=0 then
194       out=out..' minus '..print_glue(p.shrink or p[3], p.shrink_order or p[5])
195    end
196 return out
197 end
198
199
200 ------------------------------------------------------------------------
201 -- CODE FOR GETTING/SETTING PARAMETERS
202 ------------------------------------------------------------------------
203 local getcount, texwrite = tex.getcount, tex.write
204 local cnt_stack = luatexbase.registernumber 'ltj@@stack'
205
206 -- EXT: print parameters that don't need arguments
207 do
208    local tex_getattr = tex.getattribute
209    local function getattr(a, d)
210       local r = tex_getattr(a); d = d or 0
211       return (r==-0x7FFFFFFF) and d or r
212    end
213    luatexja.unary_pars = {
214       yalbaselineshift = function(t)
215          return print_scaled(getattr('ltj@yablshift'))..'pt'
216       end,
217       yjabaselineshift = function(t)
218          return print_scaled(getattr('ltj@ykblshift'))..'pt'
219       end,
220       talbaselineshift = function(t)
221          return print_scaled(getattr('ltj@tablshift'))..'pt'
222       end,
223       tjabaselineshift = function(t)
224          return print_scaled(getattr('ltj@tkblshift'))..'pt'
225       end,
226       kanjiskip = function(t)
227          return print_spec(ltjs.get_stack_skip(stack_ind.KSK, t))
228       end,
229       xkanjiskip = function(t)
230          return print_spec(ltjs.get_stack_skip(stack_ind.XSK, t))
231       end,
232       jcharwidowpenalty = function(t)
233          return ltjs.get_stack_table(stack_ind.JWP, 0, t)
234       end,
235       autospacing = function(t)
236          return getattr('ltj@autospc', 1)
237       end,
238       autoxspacing = function(t)
239          return getattr('ltj@autoxspc', 1)
240       end,
241       differentjfm = function(t)
242          local f, r = luatexja.jfmglue.diffmet_rule, '???'
243          if f == math.max then r = 'large'
244          elseif f == math.min then r = 'small'
245          elseif f == math.two_average then r = 'average'
246          elseif f == math.two_paverage then r = 'paverage'
247          elseif f == math.two_pleft then r = 'pleft'
248          elseif f == math.two_pright then r = 'pright'
249          elseif f == math.two_add then r = 'both'
250          end
251          return r
252       end,
253       direction = function()
254          local v = ltjd.get_dir_count()
255          if math.abs(tex.getnest().mode) == ltjs.mmode and v == dir_table.dir_tate then
256             v = dir_table.dir_utod
257          end
258          return v
259       end,
260       adjustdir = ltjd.get_adjust_dir_count,
261    }
262
263    local unary_pars = luatexja.unary_pars
264    local scan_arg = token.scan_argument
265    function luatexja.ext_get_parameter_unary()
266       local k= scan_arg()
267       if unary_pars[k] then
268          texwrite(tostring(unary_pars[k](getcount(cnt_stack))))
269       end
270       ltjb.stop_time_measure 'get_par'
271    end
272 end
273
274
275 -- EXT: print parameters that need arguments
276 do
277    luatexja.binary_pars = {
278       jacharrange = function(c, t)
279          if type(c)~='number' or c<-1 or c>31*ltjc.ATTR_RANGE then
280             -- 0, -1 はエラーにしない(隠し)
281             ltjb.package_error('luatexja',
282                                'invalid character range number (' .. tostring(c) .. ')',
283                                'A character range number should be in the range 1..'
284                                .. 31*ltjc.ATTR_RANGE .. ",\n"..
285                                'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
286             c=0 -- external range 217 == internal range 0
287          elseif c==31*ltjc.ATTR_RANGE then c=0
288          end
289          -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
290          if c<0 then return 1 else return (ltjc.get_range_setting(c)==0) and 0 or 1 end
291       end,
292       prebreakpenalty = function(c, t)
293          return ltjs.get_stack_table(stack_ind.PRE + ltjb.in_unicode(c, true), 0, t)
294       end,
295       postbreakpenalty = function(c, t)
296          return ltjs.get_stack_table(stack_ind.POST + ltjb.in_unicode(c, true), 0, t)
297       end,
298       kcatcode = function(c, t)
299          return ltjs.get_stack_table(stack_ind.KCAT + ltjb.in_unicode(c, false), 0, t)
300       end,
301       chartorange = function(c, t)
302          return ltjc.char_to_range(ltjb.in_unicode(c, false))
303       end,
304       jaxspmode = function(c, t)
305          return ltjs.get_stack_table(stack_ind.XSP + ltjb.in_unicode(c, true), 3, t)
306       end,
307       boxdir = function(c, t)
308          if type(c)~='number' or c<0 or c>65535 then
309             ltjb.package_error('luatexja',
310                                'Bad register code (' .. tostring(c) .. ')',
311                                'A register must be between 0 and 65535.\n'..
312                                'I changed this one to zero.')
313             c=0
314          end
315          return ltjd.get_register_dir(c)
316       end,
317    }
318    local binary_pars = luatexja.binary_pars
319    local scan_arg, scan_int = token.scan_argument, token.scan_int
320    binary_pars.alxspmode = binary_pars.jaxspmode
321    function luatexja.ext_get_parameter_binary(k, c)
322       if binary_pars[k] then
323          texwrite(tostring(binary_pars[k](c, getcount(cnt_stack))))
324       end
325       ltjb.stop_time_measure 'get_par'
326    end
327 end
328
329 -- EXT: print \global if necessary
330 function luatexja.ext_print_global()
331    if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
332 end
333
334
335 -- main process
336 do
337    local start_time_measure, stop_time_measure
338       = ltjb.start_time_measure, ltjb.stop_time_measure
339    local nullfunc = function (n) return n end
340    local to_node = node.direct.tonode
341    local to_direct = node.direct.todirect
342    local ensure_tex_attr = ltjb.ensure_tex_attr
343    local slide = node.slide
344    -- mode = true iff main_process is called from pre_linebreak_filter
345    local function main_process(head, mode, dir, gc)
346       ensure_tex_attr(attr_icflag, 0)
347       if gc == 'fin_row' then return head
348       else
349             start_time_measure 'jfmglue'
350             slide(head);
351             local p = ltjj.main(to_direct(head),mode, dir)
352             stop_time_measure 'jfmglue'
353             return to_node(p)
354       end
355    end
356
357    local function adjust_icflag(h)
358       -- kern from luaotfload will have icflag = 1
359       -- (same as italic correction)
360       ensure_tex_attr(attr_icflag, 1)
361       return h
362    end
363
364    -- callbacks
365    ltjb.add_to_callback(
366       'pre_linebreak_filter',
367       function (head,groupcode)
368          return main_process(head, true, tex.textdir, groupcode)
369       end,'ltj.main',
370       luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor')+1)
371    ltjb.add_to_callback(
372       'hpack_filter',
373       function (head,groupcode,size,packtype, dir)
374          return main_process(head, false, dir, groupcode)
375       end,'ltj.main',
376       luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor')+1)
377    ltjb.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
378    ltjb.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
379 end
380
381 -- lastnodechar
382 do
383    local getnest = tex.getnest
384    local id_glyph = node.id 'glyph'
385    function luatexja.pltx_composite_last_node_char()
386       local n = getnest()
387       local r = '-1'
388       if n then
389          if n.id==id_glyph then
390             while n.components and  n.subtype and n.subtype%4 >= 2 do
391                n = node.tail(n)
392             end
393             r = tostring(n.char)
394          end
395       end
396       tex.sprint(-2, r)
397    end
398 end
399
400 do
401     local cache_ver = 4 -- must be same as ltj-kinsoku.tex
402     local cache_outdate_fn = function (t) return t.version~=cache_ver end
403     local t = ltjs.charprop_stack_table
404     function luatexja.load_kinsoku()
405         for i,_ in pairs(t) do t[i]=nil end
406         local kinsoku = ltjb.load_cache('ltj-kinsoku_default',cache_outdate_fn)
407         if kinsoku and kinsoku[1] then
408             t[0] = kinsoku[1]
409         else
410             t[0] = {}; tex.print(cat_lp, '\\input ltj-kinsoku.tex\\relax')
411         end
412         luatexja.load_kinsoku=nil
413     end
414 end
415
416 -- debug
417
418 do
419
420 local node_type = node.type
421 local node_next = node.next
422 local get_attr = node.get_attribute
423
424 local id_penalty = node.id 'penalty'
425 local id_glyph = node.id 'glyph'
426 local id_glue = node.id 'glue'
427 local id_kern = node.id 'kern'
428 local id_hlist = node.id 'hlist'
429 local id_vlist = node.id 'vlist'
430 local id_rule = node.id 'rule'
431 local id_math = node.id 'math'
432 local id_whatsit = node.id 'whatsit'
433 local sid_user = node.subtype 'user_defined'
434
435 local prefix, inner_depth
436 local utfchar = utf.char
437 local function debug_show_node_X(p,print_fn, limit, inner_depth)
438    local k = prefix
439    local s
440    local pt, pic = node_type(p.id), (get_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
441    local base = prefix .. string.format('%X', pic) .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
442    if pt == 'glyph' then
443       s = base .. ' '
444           .. (p.char<0xF0000 and utfchar(p.char) or '')
445           .. string.format(' (U+%X) ', p.char)
446           .. tostring(p.font) .. ' (' .. print_scaled(p.height) .. '+'
447           .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
448       if p.xoffset~=0 or p.yoffset~=0 then
449          s = s .. ' off: (' .. print_scaled(p.xoffset)
450                .. ',' .. print_scaled(p.yoffset) .. ')'
451       end
452       print_fn(s)
453    elseif pt=='hlist' or pt=='vlist' or pt=='unset'or pt=='ins' then
454       if pt=='ins' then
455          s = base .. '(' .. print_scaled(p.height) .. '+'
456             .. print_scaled(p.depth) .. ')'
457             .. ', dir=' .. tostring(node.get_attribute(p, attr_dir))
458       else
459          s = base .. '(' .. print_scaled(p.height) .. '+'
460             .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
461             .. ', dir=' .. tostring(node.get_attribute(p, attr_dir))
462       end
463       if (p.shift or 0)~=0 then
464          s = s .. ', shifted ' .. print_scaled(p.shift)
465       end
466       if p.glue_set and p.glue_sign ==2 or ( p.glue_sign==1 and p.glue_set>0) then
467          s = s .. ' glue set '
468          if p.glue_sign == 2 then s = s .. '-' end
469          s = s .. tostring(floor(p.glue_set*10000)/10000)
470          if p.glue_order == 0 then s = s .. 'pt'
471          else
472             s = s .. 'fi'
473             for i = 2, p.glue_order do s = s .. 'l' end
474          end
475       end
476       if pic == icflag_table.PACKED then s = s .. ' (packed)' end
477       print_fn(s);
478       local bid = inner_depth
479       prefix, inner_depth = prefix.. '.', inner_depth + 1
480       if inner_depth < limit then
481          for q in node.traverse(p.head) do
482             debug_show_node_X(q, print_fn, limit, inner_depth)
483          end
484       end
485       prefix=k
486    elseif pt=='rule' then
487       s = base .. '(' .. print_scaled(p.height) .. '+'
488          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
489          .. ', dir=' .. tostring(node.get_attribute(p, attr_dir))
490       print_fn(s)
491    elseif pt=='disc' then
492       print_fn(s)
493       local bid = inner_depth
494       if inner_depth < limit then
495          prefix, inner_depth = k.. 'p.', inner_depth + 1
496          for q in node.traverse(p.pre) do
497             debug_show_node_X(q, print_fn, limit, inner_depth)
498          end
499          prefix = k.. 'P.'
500          for q in node.traverse(p.post) do
501             debug_show_node_X(q, print_fn, limit, inner_depth)
502          end
503          prefix = k.. 'R.'
504          for q in node.traverse(p.replace) do
505             debug_show_node_X(q, print_fn, limit, inner_depth)
506          end
507       end
508       prefix=k
509    elseif pt == 'glue' then
510       s = base .. ' ' ..  print_spec(p)
511       if pic>icflag_table.KINSOKU and pic<icflag_table.KANJI_SKIP then
512          s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
513       elseif pic==icflag_table.KANJI_SKIP then
514          s = s .. ' (kanjiskip)'
515       elseif pic==icflag_table.KANJI_SKIP_JFM then
516          s = s .. ' (kanjiskip, JFM specified)'
517       elseif pic==icflag_table.XKANJI_SKIP then
518          s = s .. ' (xkanjiskip)'
519       elseif pic==icflag_table.XKANJI_SKIP_JFM then
520          s = s .. ' (xkanjiskip, JFM specified)'
521       end
522       print_fn(s)
523    elseif pt == 'kern' then
524       s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
525       if p.subtype==2 then
526          s = s .. ' (for accent)'
527       elseif pic==icflag_table.IC_PROCESSED then
528          s = s .. ' (italic correction)'
529       elseif pic==icflag_table.LINEEND then
530          s = s .. ' (end-of-line)'
531       elseif pic>icflag_table.KINSOKU
532          and pic<icflag_table.KANJI_SKIP then
533          s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
534       end
535       print_fn(s)
536    elseif pt == 'penalty' then
537       s = base .. ' ' .. tostring(p.penalty)
538       if pic==icflag_table.KINSOKU then s = s .. ' (for kinsoku)' end
539       print_fn(s)
540    elseif pt == 'dir' then
541       print_fn(base .. ' ' .. tostring(p.dir) .. ' (level ' .. tostring(p.level) .. ')')
542    elseif pt == 'whatsit' then
543       s = base
544       if p.subtype==sid_user then
545          local t = tostring(p.user_id) .. ' (' ..
546             luatexbase.get_user_whatsit_name(p.user_id) .. ') '
547          if p.type ~= 110 then
548             s = s .. ' userid:' .. t .. p.value
549             print_fn(s)
550          else
551             s = s .. ' userid:' .. t .. '(node list)'
552             if p.user_id==uid_table.DIR then
553                s = s .. ' dir: ' .. tostring(node.get_attribute(p, attr_dir))
554             end
555             print_fn(s)
556             local bid = inner_depth
557             prefix, inner_depth = prefix.. '.', inner_depth + 1
558             if inner_depth < limit then
559                for q in node.traverse(p.value) do
560                   debug_show_node_X(q, print_fn, limit, inner_depth)
561                end
562             end
563             prefix, inner_depth = k, bid
564          end
565       else
566          s = s .. (node.subtype(p.subtype) or '')
567          if p.subtype==1 then
568             s = s .. ' stream=' .. p.stream
569             print_fn(s)
570             for i=1,#p.data do
571                print_fn(s .. '  [' .. i .. '] = ' .. tostring(p.data[i] and p.date[i].csname))
572             end
573          elseif p.subtype==16 then
574             s = s .. ' mode=' .. p.mode .. ', literal="' .. p.data .. '"'
575             print_fn(s)
576          else
577             print_fn(s)
578          end
579       end
580    -------- math node --------
581    elseif pt=='noad' then
582       print_fn(base)
583       if p.nucleus then
584          prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn, limit, inner_depth);
585       end
586       if p.sup then
587          prefix = k .. '^'; debug_show_node_X(p.sup, print_fn, limit, inner_depth);
588       end
589       if p.sub then
590          prefix = k .. '_'; debug_show_node_X(p.sub, print_fn, limit, inner_depth);
591       end
592       prefix = k;
593    elseif pt=='math_char' then
594       s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utfchar(p.char)
595       print_fn(s)
596    elseif pt=='sub_box' or pt=='sub_mlist' then
597       print_fn(base)
598       if p.head then
599          prefix = k .. '.';
600          for q in node.traverse(p.head) do
601             debug_show_node_X(q, print_fn, limit, inner_depth)
602          end
603       end
604    elseif pt == 'attribute' then
605       s = base .. ' [' .. p.number .. '] = ' .. p.value
606       print_fn(s)
607    else
608       print_fn(base)
609    end
610    p=node_next(p)
611 end
612 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
613    prefix = depth
614    inner_depth = 0
615    if head then
616       while head do
617          debug_show_node_X(head, print_fn, lim or 1/0, inner_depth); head = node_next(head)
618       end
619    else
620       print_fn(prefix .. ' (null list)')
621    end
622 end
623 function luatexja.ext_show_node(head,depth,print_fn, lim)
624    prefix = depth
625    inner_depth = 0
626    if head then
627       debug_show_node_X(head, print_fn, lim or 1/0, inner_depth)
628    else
629       print_fn(prefix .. ' (null list)')
630    end
631 end
632
633 end