summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/node-shp.lua
blob: 6ebfd767fb4bd15ac33df3a04c400e4a120c2f23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
if not modules then modules = { } end modules ['node-shp'] = {
    version   = 1.001,
    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 nodes, node = nodes, node

local next, type = next, type
local format = string.format
local concat, sortedpairs = table.concat, table.sortedpairs
local setmetatableindex = table.setmetatableindex

local nodecodes      = nodes.nodecodes
local whatsitcodes   = nodes.whatsitcodes
local tasks          = nodes.tasks
local handlers       = nodes.handlers

local hlist_code     = nodecodes.hlist
local vlist_code     = nodecodes.vlist
local disc_code      = nodecodes.disc
local mark_code      = nodecodes.mark
local kern_code      = nodecodes.kern
local glue_code      = nodecodes.glue
local whatsit_code   = nodecodes.whatsit

local texgetbox      = tex.getbox

local free_node      = node.free
local remove_node    = node.remove
local traverse_nodes = node.traverse

local removables     = {
    [whatsitcodes.open]       = true,
    [whatsitcodes.close]      = true,
    [whatsitcodes.write]      = true,
    [whatsitcodes.pdfdest]    = true,
    [whatsitcodes.pdfsavepos] = true,
    [whatsitcodes.latelua]    = true,
}

local function cleanup_redundant(head)
    local start = head
    while start do
        local id = start.id
        if id == disc_code then
            head, start = remove_node(head,start,true)
     -- elseif id == glue_code then
     --     if start.writable then
     --         start = start.next
     --     elseif some_complex_check_on_glue_spec then
     --         head, start = remove_node(head,start,true)
     --     else
     --         start = start.next
     --     end
        elseif id == kern_code then
            if start.kern == 0 then
                head, start = remove_node(head,start,true)
            else
                start = start.next
            end
        elseif id == mark_code then
            head, start = remove_node(head,start,true)
        elseif id == hlist_code or id == vlist_code then
            local sl = start.list
            if sl then
                start.list = cleanup_redundant(sl)
                start = start.next
            else
                head, start = remove_node(head,start,true)
            end
        else
            start = start.next
        end
    end
    return head
end

local function cleanup_flushed(head) -- rough
    local start = head
    while start do
        local id = start.id
        if id == whatsit_code and removables[start.subtype] then
            head, start = remove_node(head,start,true)
        elseif id == hlist_code or id == vlist_code then
            local sl = start.list
            if sl then
                start.list = cleanup_flushed(sl)
                start = start.next
            else
                head, start = remove_node(head,start,true)
            end
        else
            start = start.next
        end
    end
    return head
end

function handlers.cleanuppage(head)
    -- about 10% of the nodes make no sense for the backend
    return cleanup_redundant(head), true
end

function handlers.cleanupbox(head)
    return cleanup_flushed(head), true
end

directives.register("backend.cleanup", function()
    tasks.enableaction("shipouts","nodes.handlers.cleanuppage")
end)

local actions = tasks.actions("shipouts")  -- no extra arguments

function handlers.finalize(head) -- problem, attr loaded before node, todo ...
    return actions(head)
end

function commands.cleanupbox(n)
    cleanup_flushed(texgetbox(n))
end

-- handlers.finalize = actions

-- interface

function commands.finalizebox(n)
    actions(texgetbox(n))
end

-- 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 in traverse_nodes(head) do
        local id = n.id
        local dn = data[nodecodes[n.id]]
        dn[subcategory] = dn[subcategory] + 1
        if id == hlist_code or id == vlist_code then
            count(n.list,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)