summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/node-pro.lmt
blob: 9dcb2ffa8891b673e120e362c4e3c3bca0c3e116 (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
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
local tonut        = nodes.tonut

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 n = 0

    local function reconstruct(head)
        return nodes.listtoutf(head,"",false,nil,true)
    end

    function processors.tracer(what,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, group %a, # before %a, # after %s, stream: %s",what,n,groupcode,before,after,reconstruct(head))
        else
            report_nodes("%s: location %a, group %a, # before %a, # after %s",what,n,groupcode,before,after)
        end
    end

end

-- do
--
--     local count_nodes = nodes.countall
--     local texget      = tex.get
--     local tracer      = processors.tracer
--
--     local function pre_linebreak_filter(head,groupcode)
--         if trace_callbacks then
--             local before = count_nodes(head,true)
--             head = actions(head,groupcode)
--             local after = count_nodes(head,true)
--             tracer("pre_linebreak",head,groupcode,before,after,true)
--         else
--             head = actions(head,groupcode)
--         end
--         return head
--     end
--
--     local function hpack_filter(head,groupcode,size,packtype,direction,attributes)
--         if not direction then
--             direction = texget("textdir")
--         end
--         --
--         if trace_callbacks then
--             local before = count_nodes(head,true)
--             head = actions(head,groupcode,size,packtype,direction,attributes)
--             local after = count_nodes(head,true)
--             tracer("hpack",head,groupcode,before,after,true)
--         else
--             head = actions(head,groupcode,size,packtype,direction,attributes)
--         end
--         return head
--     end
--
--     processors.pre_linebreak_filter = pre_linebreak_filter
--     processors.hpack_filter         = hpack_filter
--
--     do
--
--         local hpack = nodes.hpack
--
--         function nodes.fullhpack(head,...)
--             return hpack((hpack_filter(head)),...)
--         end
--
--     end
--
--     do
--
--         local hpack = nuts.hpack
--
--         function nuts.fullhpack(head,...)
--             return hpack(tonut(hpack_filter(tonode(head))),...)
--         end
--
--     end
--
--     callbacks.register('pre_linebreak_filter', pre_linebreak_filter, "horizontal manipulations (before par break)")
--     callbacks.register('hpack_filter'        , hpack_filter,         "horizontal manipulations (before hbox creation)")
--
-- end

do

    local count_nodes = nodes.countall
    local texget      = tex.get
    local tracer      = processors.tracer
    local hbox_code   = tex.groupcodes.hbox

    local function glyph_run(head,groupcode,direction)
        if not groupcode then
            groupcode = hbox_code
        end
        if trace_callbacks then
            local before = count_nodes(head,true)
            head = actions(head,groupcode,direction)
            local after = count_nodes(head,true)
            tracer("glyph_run",head,groupcode,before,after,true)
        else
            head = actions(head,groupcode,direction)
        end
        return head
    end

    processors.glyph_run = glyph_run

    do
        local hpack = nodes.hpack
        function nodes.fullhpack(head,...)
            return hpack((glyph_run(head)),...)
        end
    end

    do
        local hpack = nuts.hpack
        function nuts.fullhpack(head,...)
            return hpack(tonut(glyph_run(tonode(head))),...)
        end
    end

    callbacks.register("glyph_run", glyph_run, "glyph processing")

end

do
    -- Beware, these are packaged boxes so no firstglyph test needed. Maybe some day I'll add a hash
    -- with valid groupcodes. Watch out, much can pass twice, for instance vadjust passes two times,

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

    local tracer      = processors.tracer

    local function post_linebreak_filter(head,groupcode)
        if trace_callbacks then
            local before = count_nodes(head,true)
            head = actions(head,groupcode)
            local after = count_nodes(head,true)
            tracer("post_linebreak",head,groupcode,before,after,true)
        else
            head = actions(head,groupcode)
        end
        return head
    end

    processors.post_linebreak_filter = post_linebreak_filter

    callbacks.register("post_linebreak_filter", post_linebreak_filter,"horizontal manipulations (after par break)")

end

do

    ----- texnest       = tex.nest
    local getnest       = tex.getnest

    local getlist       = nuts.getlist
    local setlist       = nuts.setlist
    local getsubtype    = nuts.getsubtype

    local linelist_code = nodes.listcodes.line

    local actions       = tasks.actions("contributers")

    function processors.contribute_filter(groupcode)
        if groupcode == "box" then -- "pre_box"
            local whatever = getnest()
            if whatever then
                local line = whatever.tail
                if line then
                    line = tonut(line)
                    if getsubtype(line) == linelist_code then
                        local head = getlist(line)
                        if head then
                            local result = actions(head,groupcode,line)
                            if result and result ~= head then
                                setlist(line,result)
                            end
                        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)