summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/node-bck.lua
blob: 995620710c71cbca6e3a0d097f4846977d4dce21 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
if not modules then modules = { } end modules ['node-bck'] = {
    version   = 1.001,
    comment   = "companion to node-bck.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- beware, this one takes quite some runtime, so we need a status flag
-- maybe some page related state

-- todo: done (or just get rid of done altogether) ... saves no purpose
-- any longer

local attributes, nodes, node = attributes, nodes, node

local enableaction      = nodes.tasks.enableaction

local nodecodes         = nodes.nodecodes
local listcodes         = nodes.listcodes

local hlist_code        = nodecodes.hlist
local vlist_code        = nodecodes.vlist
local alignment_code    = listcodes.alignment
local cell_code         = listcodes.cell

local nuts              = nodes.nuts
local nodepool          = nuts.pool

local tonode            = nuts.tonode
local tonut             = nuts.tonut

local getnext           = nuts.getnext
local getprev           = nuts.getprev
local getid             = nuts.getid
local getlist           = nuts.getlist
local getattr           = nuts.getattr
local getsubtype        = nuts.getsubtype
local getwhd            = nuts.getwhd
local getwidth          = nuts.getwidth
local getprop           = nuts.getprop

local setattr           = nuts.setattr
local setlink           = nuts.setlink
local setlist           = nuts.setlist
local setattributelist  = nuts.setattributelist
local setprop           = nuts.setprop

local takebox           = nuts.takebox
local findtail          = nuts.tail

local traverse          = nuts.traverse
local traverse_id       = nuts.traverse_id

local flush_node_list   = nuts.flush_list

local new_rule          = nodepool.rule
local new_kern          = nodepool.kern

local privateattributes = attributes.private
local unsetvalue        = attributes.unsetvalue

local linefillers       = nodes.linefillers

local a_color           = privateattributes("color")
local a_transparency    = privateattributes("transparency")
local a_colormodel      = privateattributes("colormodel")
local a_background      = privateattributes("background")
local a_alignbackground = privateattributes("alignbackground")
local a_linefiller      = privateattributes("linefiller")
local a_ruled           = privateattributes("ruled")

local trace_alignment   = false
local report_alignment  = logs.reported("backgrounds","alignment")

trackers.register("backgrounds.alignments",function(v) trace_alignment = v end)

-- We can't use listbuilders with where=alignment because at that stage we have
-- unset boxes. Also, post_linebreak is unsuitable for nested processing as we
-- get the same stuff many times (wrapped again and again).
--
-- After many experiments with different callbacks the shipout is still the best
-- place but then we need to store some settings longer or save them with the node.
-- For color only we can get away with it with an extra attribute flagging a row
-- but for more complex stuff we can better do as we do here now.

local function colored_a(current,list,template,id)
    local width, height, depth = getwhd(current)
    local total = height + depth
    if width > 0 and total > 0 then
        local rule = nil
        --
        local a = getattr(template,a_linefiller)
        if a then
            local d = linefillers.data[a%1000]
            if d then
                rule = linefillers.filler(template,d,width,height,depth)
            end
        end
        --
        if not rule then
            rule = new_rule(width,height,depth)
        end
        local back = new_kern(-((id == vlist_code and total) or width))
        setattributelist(rule,template)
        return setlink(rule,back,list)
    end
end

local function colored_b(current,list,template,id,indent)
    local width, height, depth = getwhd(current)
    local total = height + depth
    if width > 0 and total > 0 then
        local fore = (indent ~= 0) and new_kern(indent)
        local rule = nil
        --
        local a = getattr(template,a_linefiller)
        if a then
            local d = linefillers.data[a%1000]
            if d then
                rule = linefillers.filler(template,d,width-indent,height,depth)
            end
        end
        --
        if not rule then
            rule = new_rule(width-indent,height,depth)
            setattributelist(rule,template)
        end
        local back = new_kern(-((id == vlist_code and total) or width))
        return setlink(fore,rule,back,list)
    end
end

local templates  = { }
local currentrow = 0
local enabled    = false
local alignments = false

local function add_alignbackgrounds(head,list)
    for current in traverse_id(hlist_code,list) do
        if getsubtype(current) == cell_code then
            local list = getlist(current)
            if list then
                for template in traverse_id(hlist_code,list) do
                    local background = getattr(template,a_alignbackground)
                    if background then
                        local list = colored_a(current,list,template)
                        if list then
                            setlist(current,list)
                        end
                        setattr(template,a_alignbackground,unsetvalue) -- or property
                    end
                    break
                end
            end
        end
    end
    local template = getprop(head,"alignmentchecked")
    if template then
        list = colored_b(head,list,template[1],hlist_code,template[2])
        flush_node_list(template)
        templates[currentrow] = false
        return list
    end
end

local function add_backgrounds(head,id,list)
    if list then
        for current, id in traverse(list) do
            if id == hlist_code or id == vlist_code then
                local list = getlist(current)
                if list then
                    if alignments and getsubtype(current) == alignment_code then
                        local l = add_alignbackgrounds(current,list)
                        if l then
                            list = l
                            setlist(current,list)
                        end
                    end
                    local l = add_backgrounds(current,id,list)
                    if l then
                        list = l
                        setlist(current,l)
                    end
                end
            end
        end
    end
    if id == hlist_code or id == vlist_code then
        local background = getattr(head,a_background)
        if background then
            list = colored_a(head,list,head,id)
            -- not needed
            setattr(head,a_background,unsetvalue) -- or property
            return list
        end
    end
end

function nodes.handlers.backgrounds(head)
    local h = tonut(head)
    add_backgrounds(h,getid(h),getlist(h))
    return head, true
end

function nodes.handlers.backgroundspage(head,where)
    if head and where == "alignment" then
        local head = tonut(head)
        for n in traverse_id(hlist_code,head) do
            local p = getprop(n,"alignmentchecked")
            if not p and getsubtype(n) == alignment_code then
                currentrow = currentrow + 1
                local template = templates[currentrow]
                if trace_alignment then
                    report_alignment("%03i %s %s",currentrow,"page",template and "+" or "-")
                end
                setprop(n,"alignmentchecked",template)
            end
        end
    end
    return head, true
end

function nodes.handlers.backgroundsvbox(head,where)
    if head and where == "vbox" then
        local head = tonut(head)
        for n in traverse_id(hlist_code,getlist(head)) do
            local p = getprop(n,"alignmentchecked")
            if not p and getsubtype(n) == alignment_code then
                currentrow = currentrow + 1
                local template = templates[currentrow]
                if trace_alignment then
                    report_alignment("%03i %s %s",currentrow,"vbox",template and "+" or "-")
                end
                setprop(n,"alignmentchecked",template)
            end
        end
    end
    return head, true
end

-- interfaces.implement {
--     name      = "enablebackgroundboxes",
--     onlyonce  = true,
--     actions   = enableaction,
--     arguments = { "'shipouts'", "'nodes.handlers.backgrounds'" }
-- }
--
-- doing it in the shipout works as well but this is nicer

local function enable(alignmentstoo)
    if not enabled then
        enabled = true
        enableaction("shipouts","nodes.handlers.backgrounds")
    end
    if not alignments and alignmentstoo then
        alignments = true
        enableaction("vboxbuilders","nodes.handlers.backgroundsvbox")
        enableaction("mvlbuilders", "nodes.handlers.backgroundspage")
    end
end

interfaces.implement {
    name      = "enablebackgroundboxes",
    onlyonce  = true,
    actions   = enable,
}

interfaces.implement {
    name      = "enablebackgroundalign",
    onlyonce  = true,
    actions   = function()
        enable(true)
    end,
}

interfaces.implement {
    name      = "setbackgroundrowdata",
    arguments = { "integer", "integer", "dimension" },
    actions   = function(row,box,indent)
        row = row -1 -- better here than in tex
        if box == 0 then
            templates[row] = false
        else
            templates[row] = { takebox(box), indent }
        end
    end,
}

interfaces.implement {
    name      = "resetbackgroundrowdata",
    actions   = function()
        currentrow = 0
    end,
}