Difference between revisions of "Traversing TeX nodes"

From LuaTeXWiki
(Traversing TeX nodes)
m (Reverted edits by 193.212.103.173 (talk) to last revision by Patrick)
Line 1: Line 1:
D6ZnzD <a href="http://lippdztrobdy.com/">lippdztrobdy</a>, [url=http://lpynykdfzzuy.com/]lpynykdfzzuy[/url], [link=http://chynmakugbbj.com/]chynmakugbbj[/link], http://lhapzbrslbcu.com/
+
== Traversing TeX nodes ==
 +
 
 +
Put this code at the beginning of any TeX file:
 +
 
 +
<pre>
 +
\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
 +
  )
 +
}
 +
</pre>
 +
 
 +
It will display a list of all nodes on the terminal and in the log file. For example, for the document
 +
 
 +
<pre>
 +
\font\x=omlgc
 +
\x bl? bla bla bla
 +
</pre>
 +
 
 +
which contains a Greek letter (in UTF-8), you will get:
 +
 
 +
<pre>
 +
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] )
 +
</pre>
 +
 
 +
Be aware of the fact that the nodes will be created only if the glyph requested indeed exists in the font.
 +
 
 +
[[User:Yannis.Haralambous|Yannis.Haralambous]] 02:52, 4 July 2007 (EDT)

Revision as of 10:12, 28 December 2010

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)