Created
February 2, 2014 12:02
-
-
Save phi-gamma/8767389 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local traverse_nodes = node.traverse | |
local iowrite = io.write | |
local tableconcat = table.concat | |
local utfchar = unicode.utf8.char | |
local stringformat = string.format | |
local stringrep = string.rep | |
local nodelength = node.length | |
local count_nodes = node.count | |
local nodecodes = table.mirrored (node.types()) | |
local glyph_t = nodecodes.glyph | |
local glue_t = nodecodes.glue | |
local hlist_t = nodecodes.hlist | |
local vlist_t = nodecodes.vlist | |
local cbk = function (hd, group, size, pack) | |
--[[-- | |
Demo function collecting text from glyph nodes in the list starting | |
with ``hd``, printing them to stdout. | |
--]]-- | |
local text = { | |
"\n[" | |
.. (group == "" and "par" or group) | |
--- the line below shows more info from the hpack_filter context | |
--.. (size and (":" .. tostring (size) .. ":" .. pack) or "") | |
.. "] " | |
} | |
for n in traverse_nodes (hd) do | |
local ntype = n.id | |
if ntype == glyph_t then | |
text [#text + 1] = utfchar (n.char) | |
elseif ntype == glue_t then | |
text [#text + 1] = " " | |
else -- print node type | |
text [#text + 1] = "<" .. nodecodes [ntype] .. ":" .. tostring (n.subtype) .. ">" | |
end | |
end | |
text [#text + 1] = "\n" | |
iowrite (tableconcat (text)) | |
return true | |
end | |
local rec_cbk rec_cbk = function (hd, _, depth, where) | |
--[[-- | |
Recursively walk a paragraph node list. | |
--]]-- | |
if not depth then | |
iowrite "\n" | |
depth = 1 | |
where = "par" | |
end | |
iowrite (stringrep (" ", depth) .. | |
stringformat ("[%s:%d] %d nodes, %d glyphs\n", | |
where, depth, | |
nodelength (hd), | |
count_nodes (glyph_t, hd))) | |
for n in traverse_nodes (hd) do | |
local ntype = n.id | |
if ntype == vlist_t then | |
rec_cbk (n.head, nil, depth + 1, "vbox") | |
elseif ntype == hlist_t then | |
rec_cbk (n.head, nil, depth + 1, "hbox") | |
--- else pass | |
end | |
end | |
return true | |
end | |
luatexbase.add_to_callback ("pre_linebreak_filter", cbk, "document.nodeprinter") | |
luatexbase.add_to_callback ("hpack_filter", cbk, "document.nodeprinter") | |
--luatexbase.add_to_callback ("pre_linebreak_filter", rec_cbk, "document.recursenodes") | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\documentclass {scrartcl} | |
\usepackage {luatexbase} | |
\RequireLuaModule {lualibs} | |
\directlua {dofile "\jobname.lua"} | |
\begin {document} | |
\hbox {Horizontal list only, without line break.}\par | |
\vbox {Vertical list, triggers the line break mechanism.}\par | |
\hbox {\hsize 1cm This text won’t fit but isn’t line broken either.}\par | |
This is some \hbox {ordinary} paragraph material. | |
\begin {figure} | |
\input ward | |
\caption {Some brief caption.} %% same as with the hbox above | |
\end {figure} | |
This is some \vbox {\hbox {significantly} | |
\hbox {less} | |
\hbox {ordinary}} paragraph material. | |
\end {document} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment