Traversing TeX nodes

From LuaTeXWiki
Revision as of 22:04, 7 December 2010 by Patrick (talk | contribs) (Copied from the old bluwiki.com luatex wiki (is also licensed under the terms of the GNU FDL))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Traversing TeX nodes

Put this code at the beginning of any TeX file:

\directlua{
 local glyph = node.id('glyph')
 local hlist = node.id('hlist')
 local vlist = node.id('vlist')

 function gothru(h,prof)
   for t in node.traverse(h) do
     texio.write_nl(string.rep("...",prof) .. 'NODE type=' ..  node.type(t.id) .. ' subtype=' .. t.subtype )
     if t.id == hlist or t.id == vlist then
       texio.write(' w=' .. t.width .. ' h=' .. t.height .. ' d=' .. t.depth .. ' s=' .. t.shift )
	   gothru(t.list,prof+1)
     end
     if t.id == glyph then
       texio.write(' font=' .. t.font .. ' char=' .. t.char .. ' width=' ..  font.fonts[t.font].characters[t.char]['width'])
     end
   end
 end

 callback.register('pre_linebreak_filter',
   function(h)
      gothru(h,0) 
      return true 
   end
   )
}

It will display a list of all nodes on the terminal and in the log file. For example, for the document

\font\x=omlgc
\x bl? bla bla bla

which contains a Greek letter (in UTF-8), you will get:

This is luaTeX, Version 3.141592-snapshot-2007062922 (Web2C 7.5.6)
(
NODE type=whatsit subtype=6
NODE type=hlist subtype=0
NODE type=glyph subtype=0 font=51 char=98
NODE type=glyph subtype=0 font=51 char=108
NODE type=glyph subtype=0 font=51 char=941
NODE type=glue subtype=0
NODE type=glyph subtype=0 font=51 char=98
NODE type=glyph subtype=0 font=51 char=108
NODE type=glyph subtype=0 font=51 char=97
NODE type=glue subtype=0
NODE type=glyph subtype=0 font=51 char=98
NODE type=glyph subtype=0 font=51 char=108
NODE type=glyph subtype=0 font=51 char=97
NODE type=glue subtype=0
NODE type=glyph subtype=0 font=51 char=98
NODE type=glyph subtype=0 font=51 char=108
NODE type=glyph subtype=0 font=51 char=97
NODE type=glue subtype=0
Underfull \hbox (badness 10000) in paragraph at lines 14--15

[1] )

Be aware of the fact that the nodes will be created only if the glyph requested indeed exists in the font.

Yannis.Haralambous 02:52, 4 July 2007 (EDT)