summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkxl/node-shp.lmt
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/context/base/mkxl/node-shp.lmt')
-rw-r--r--Master/texmf-dist/tex/context/base/mkxl/node-shp.lmt115
1 files changed, 115 insertions, 0 deletions
diff --git a/Master/texmf-dist/tex/context/base/mkxl/node-shp.lmt b/Master/texmf-dist/tex/context/base/mkxl/node-shp.lmt
new file mode 100644
index 00000000000..64508ecc746
--- /dev/null
+++ b/Master/texmf-dist/tex/context/base/mkxl/node-shp.lmt
@@ -0,0 +1,115 @@
+if not modules then modules = { } end modules ['node-shp'] = {
+ version = 1.001,
+ optimize = true,
+ comment = "companion to node-ini.mkiv",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "see context related readme files"
+}
+
+local next, type = next, type
+local format = string.format
+local setmetatableindex = table.setmetatableindex
+
+local nodes = nodes
+local tasks = nodes.tasks
+local handlers = nodes.handlers
+
+local nodecodes = nodes.nodecodes
+local hlist_code = nodecodes.hlist
+local vlist_code = nodecodes.vlist
+
+local nuts = nodes.nuts
+local tonut = nuts.tonut
+
+local nextnode = nuts.traversers.node
+local getlist = nuts.getlist
+local getbox = nuts.getbox
+
+local implement = interfaces.implement
+
+local actions = tasks.actions("shipouts")
+
+function handlers.finalizebox(box)
+ actions(getbox(box)) -- nut
+end
+
+handlers.cleanuppage = nuts.flatten_discretionaries
+
+-- interface
+
+implement {
+ name = "finalizebox",
+ arguments = "integer",
+ actions = handlers.finalizebox,
+}
+
+-- just in case we want to optimize lookups:
+
+local frequencies = { }
+
+nodes.tracers.frequencies = frequencies
+
+local data = { }
+local done = false
+
+setmetatableindex(data,function(t,k)
+ local v = { }
+ setmetatableindex(v,function(t,k)
+ local v = { }
+ t[k] = v
+ setmetatableindex(v,function(t,k)
+ t[k] = 0
+ return 0
+ end)
+ return v
+ end)
+ t[k] = v
+ return v
+end)
+
+local function count(head,data,subcategory)
+ -- no components, pre, post, replace .. can maybe an option .. but
+ -- we use this for optimization so it makes sense to look the the
+ -- main node only
+ for n, id in nextnode, tonut(head) do
+ local dn = data[nodecodes[id]] -- we could use id and then later convert to nodecodes
+ dn[subcategory] = dn[subcategory] + 1
+ if id == hlist_code or id == vlist_code then
+ count(getlist(n),data,subcategory)
+ end
+ end
+end
+
+local function register(category,subcategory)
+ return function(head)
+ done = true
+ count(head,data[category],subcategory)
+ return head, false
+ end
+end
+
+frequencies.register = register
+frequencies.filename = nil
+
+trackers.register("nodes.frequencies",function(v)
+ if type(v) == "string" then
+ frequencies.filename = v
+ end
+ handlers.frequencies_shipouts_before = register("shipouts", "begin")
+ handlers.frequencies_shipouts_after = register("shipouts", "end")
+ handlers.frequencies_processors_before = register("processors", "begin")
+ handlers.frequencies_processors_after = register("processors", "end")
+ tasks.prependaction("shipouts", "before", "nodes.handlers.frequencies_shipouts_before")
+ tasks.appendaction ("shipouts", "after", "nodes.handlers.frequencies_shipouts_after")
+ tasks.prependaction("processors", "before", "nodes.handlers.frequencies_processors_before")
+ tasks.appendaction ("processors", "after", "nodes.handlers.frequencies_processors_after")
+end)
+
+statistics.register("node frequencies", function()
+ if done then
+ local filename = frequencies.filename or (tex.jobname .. "-frequencies.lua")
+ io.savedata(filename,table.serialize(data,true))
+ return format("saved in %q",filename)
+ end
+end)