summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/node-pro.lua
blob: 3251b0133388091524c5258f36dd048eccc8ad8f (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
if not modules then modules = { } end modules ['node-pro'] = {
    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 trace_callbacks  = false  trackers  .register("nodes.callbacks",        function(v) trace_callbacks  = v end)
local force_processors = false  directives.register("nodes.processors.force", function(v) force_processors = v end)

local report_nodes = logs.reporter("nodes","processors")

local nodes        = nodes
local tasks        = nodes.tasks
local nuts         = nodes.nuts

nodes.processors   = nodes.processors or { }
local processors   = nodes.processors

-- vbox: grouptype: vbox vtop output split_off split_keep  | box_type: exactly|aditional
-- hbox: grouptype: hbox adjusted_hbox(=hbox_in_vmode)     | box_type: exactly|aditional

local actions = tasks.actions("processors")

do

    local tonut   = nuts.tonut
    local isglyph = nuts.isglyph
    local getnext = nuts.getnext

    local utfchar = utf.char
    local concat  = table.concat

    local n = 0

    local function reconstruct(head) -- we probably have a better one
        local t, n, h = { }, 0, tonut(head)
        while h do
            n = n + 1
            local char, id = isglyph(h)
            if char then -- todo: disc etc
                t[n] = utfchar(char)
            else
                t[n] = "[]"
            end
            h = getnext(h)
        end
        return concat(t)
    end

    function processors.tracer(what,state,head,groupcode,before,after,show)
        if not groupcode then
            groupcode = "unknown"
        elseif groupcode == "" then
            groupcode = "mvl"
        end
        n = n + 1
        if show then
            report_nodes("%s: location %a, state %a, group %a, # before %a, # after %s, stream: %s",what,n,state,groupcode,before,after,reconstruct(head))
        else
            report_nodes("%s: location %a, state %a, group %a, # before %a, # after %s",what,n,state,groupcode,before,after)
        end
    end

end

local tracer = processors.tracer

processors.enabled = true -- this will become a proper state (like trackers)

do

    local has_glyph = nodes.has_glyph

    function processors.pre_linebreak_filter(head,groupcode) -- ,size,packtype,direction
        local found = force_processors or has_glyph(head)
        if found then
            if trace_callbacks then
                local before = nodes.count(head,true)
                local head, done = actions(head,groupcode) -- ,size,packtype,direction
                local after = nodes.count(head,true)
                if done then
                    tracer("pre_linebreak","changed",head,groupcode,before,after,true)
                else
                    tracer("pre_linebreak","unchanged",head,groupcode,before,after,true)
                end
                return done and head or true
            else
                local head, done = actions(head,groupcode) -- ,size,packtype,direction
                return done and head or true
            end
        elseif trace_callbacks then
            local n = nodes.count(head,false)
            tracer("pre_linebreak","no chars",head,groupcode,n,n)
        end
        return true
    end

    local function hpack_filter(head,groupcode,size,packtype,direction,attributes)
        local found = force_processors or has_glyph(head)
        if found then
            if trace_callbacks then
                local before = nodes.count(head,true)
                local head, done = actions(head,groupcode,size,packtype,direction,attributes)
                local after = nodes.count(head,true)
                if done then
                    tracer("hpack","changed",head,groupcode,before,after,true)
                else
                    tracer("hpack","unchanged",head,groupcode,before,after,true)
                end
                return done and head or true
            else
                local head, done = actions(head,groupcode,size,packtype,direction,attributes)
                return done and head or true
            end
        elseif trace_callbacks then
            local n = nodes.count(head,false)
            tracer("hpack","no chars",head,groupcode,n,n)
        end
        return true
    end

    processors.hpack_filter = hpack_filter

    do

        local setfield = nodes.setfield
        local hpack    = nodes.hpack

        function nodes.fullhpack(head,...)
            local ok = hpack_filter(head)
            if not done or done == true then
                ok = head
            end
            local hp, b = hpack(ok,...)
            setfield(hp,"prev",nil)
            setfield(hp,"next",nil)
            return hp, b
        end

    end

    do

        local setboth = nuts.setboth
        local hpack   = nuts.hpack

        function nuts.fullhpack(head,...)
            local ok = hpack_filter(tonode(head))
            if not done or done == true then
                ok = head
            else
                ok = tonut(ok)
            end
            local hp, b = hpack(...)
            setboth(hp)
            return hp, b
        end

    end

    callbacks.register('pre_linebreak_filter', processors.pre_linebreak_filter, "all kind of horizontal manipulations (before par break)")
    callbacks.register('hpack_filter'        , processors.hpack_filter,         "all kind of horizontal manipulations (before hbox creation)")

end

do

    local actions = tasks.actions("finalizers") -- head, where

    -- beware, these are packaged boxes so no first_glyph test
    -- maybe some day a hash with valid groupcodes
    --
    -- beware, much can pass twice, for instance vadjust passes two times
    --
    -- something weird here .. group mvl when making a vbox

    function processors.post_linebreak_filter(head,groupcode)
        if trace_callbacks then
            local before = nodes.count(head,true)
            local head, done = actions(head,groupcode)
            local after = nodes.count(head,true)
            if done then
                tracer("post_linebreak","changed",head,groupcode,before,after,true)
            else
                tracer("post_linebreak","unchanged",head,groupcode,before,after,true)
            end
            return done and head or true
        else
            local head, done = actions(head,groupcode)
            return done and head or true
        end
    end

    callbacks.register('post_linebreak_filter', processors.post_linebreak_filter,"all kind of horizontal manipulations (after par break)")

end

do

    local texnest    = tex.nest

    local getlist    = nodes.getlist
    local setlist    = nodes.setlist
    local getsubtype = nodes.getsubtype

    local line_code  = nodes.listcodes.line

    local actions    = tasks.actions("contributers")

    function processors.contribute_filter(groupcode)
        if groupcode == "box" then -- "pre_box"
            local whatever = texnest[texnest.ptr]
            if whatever then
                local line = whatever.tail
                if line and getsubtype(line) == line_code then
                    local head = getlist(line)
                    if head then
                        local okay, done = actions(head,groupcode,line)
                        if okay and okay ~= head then
                            setlist(line,okay)
                        end
                    end
                end
            end
        end
    end

    callbacks.register('contribute_filter', processors.contribute_filter,"things done with lines")

end

statistics.register("h-node processing time", function()
    return statistics.elapsedseconds(nodes,"including kernel") -- hm, ok here?
end)