[luatex] reverse node lists

Paul Isambert zappathustra at free.fr
Tue Nov 1 10:27:44 CET 2011


Le 01/11/2011 10:03, Arno Trautmann a écrit :
>  Hi Patrick,
>
>  thanks for your answer!
>
>  Patrick Gundlach wrote:
> >> this is again a rather simple question and I am sorry for taking 
your time for this …
> >> In short, I want to reverse all glyph nodes in a line, and/or all 
lines in a paragraph. I tried something like the following (with the 
idea to store all lines in a list, then replace the lines with the list 
in reversed order):
> >>
> >> function reverse(head)
> >>
> >> newlines = {}
> >> i = 1
> >> for line in node.traverse_id(node.id"hhead",head) do
> >> newlines[i] = line
> >
> > a lua idiom is newlines[#newlines + 1] = ... - easer to read to the 
average Lua hackr.
>
>  Oh, of course … I fear I'll never get used to this Lua style …

Or table.insert(newlines, line).

> >> i = i+1
> >> end
> >>
> >> j = #newlines
> >>
> >> for line in node.traverse_id(node.id"hhead",head) do
> >> node.insert_before(head,line,newlines[j])
> >> j = j-1
> >> end
> >> return head
> >> end
> >
> > I have to admit that I don't understand exactly what is going on here.
>
>  Me neither ;)
>
> > I never use the node.insert_* functions as they are some black magic 
to me, I always change next/prev pointsers manually, so I have the 
feeling of what I have to do.
>
>  But shouldn't the node.insert_* just do the same?

They do, as far as I can tell.

Alternate solution which reverse all nodes in a list:

function invert_list (h)
   local l
   for n in node.traverse(h) do
     if l then
       l = node.insert_before(l, l, node.copy(n))
     else
       l = node.copy(n)
     end
   end
   node.flush_list(h)
   return l
end

Best,
Paul

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://tug.org/pipermail/luatex/attachments/20111101/5f4dd339/attachment.html>


More information about the luatex mailing list