[texhax] visualize font expansion

Paul Isambert zappathustra at free.fr
Wed Jun 22 11:15:09 CEST 2011

Le 22/06/2011 10:21, Arno Trautmann a écrit :
> Paul Isambert wrote:
>>    function (head)
>>      local f = font.getfont(font.current()).characters
>>      for l in node.traverse_id(0, head) do
>>        local g = l.head
>>        while not(g.id == 37) do
>>          g = g.next
>>        end
>>        texio.write_nl("Expansion: " .. g.width / f[g.char].width)
>>      end
>>      return head
>>    end
> I did:
>    for line in node.traverse_id(HLIST,head) do
>      g = line.head
>      while not(g.next == nil) do
>        g = g.next
>        if g.id == 37 then
>          act_width = g.width
>          normal_width =
> font.getfont(font.current()).characters[g.char].width
>          exp_factor = act_width / normal_width
>        end
>      end
>    end
> So, as far as I can see, the difference is that I load the font at each
> comparison which seems to slow down things very much, especially for
> some 30-line-paragraph. With your code, I don't see any speed loss, too.

Oh, no, what makes your code so slow is that you inspect all glyphs in a 
line. So obviously that takes some time. And actually it takes time to 
print the values to the terminal, not to process the code; if I call 
LuaTeX with your code without the command line, then it isn't slow.

I suppose your inspecting every glyph is unintentional: your "while not 
(g.next == nil)" loop is true as long as your're not on the last node of 
the line. I suppose you meant to retrieve only the first glyph of a 
line, then you should have done:

local g = line.head
while not (g.id == 37) then
   g = g.next
... then retrieve the width...

Note that this code will crash in the unlikely case where there is no 
glyph in a line. I let you fix it.


More information about the texhax mailing list