summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/node-bck.lua
blob: 054135669e1a0014874afa87bec6d12526e37d26 (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
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 setattr           = nuts.setattr
local setlink           = nuts.setlink
local setlist           = nuts.setlist
local setattributelist  = nuts.setattributelist

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 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")

-- actually we can be more clever now: we can store cells and row data
-- and apply it

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 function add_backgrounds(head)
    for current, id in traverse(head) do
        if id == hlist_code or id == vlist_code then
            local list = getlist(current)
            if list then
                local head = add_backgrounds(list)
                if head then
                    setlist(current,head)
                    list = head
                end
            end
            local background = getattr(current,a_background)
            if background then
                local list = colored_a(current,list,current,id)
                if list then
                    setlist(current,list)
                end
            end
        end
    end
    return head, true
end

-- We use a fake hlist with proper attributes.

local templates  = { }
local currentrow = 0

local function add_alignbackgrounds(head)
    for current in traverse_id(hlist_code,head) do -- what is valign?
        if getsubtype(current) == alignment_code then
            local list = getlist(current)
            if list then
                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
                                end
                                break
                            end
                        end
                    end
                end
            end
            currentrow = currentrow + 1
            local template = templates[currentrow]
            if template then
                local list = colored_b(current,list,template[1],hlist_code,template[2])
                if list then
                    setlist(current,list)
                end
                flush_node_list(template)
                templates[currentrow] = false
            end
        end
    end
    return head, true
end

function nodes.handlers.backgrounds(head,where)
    local head, done = add_backgrounds(tonut(head))
    return tonode(head), done
end

function nodes.handlers.alignbackgrounds(head,where)
    if where == "alignment" and head then
        local head, done = add_alignbackgrounds(tonut(head))
        return tonode(head), done
    else
        return head, false
    end
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

interfaces.implement {
    name      = "enablebackgroundboxes",
    onlyonce  = true,
    actions   = function()
        enableaction("mvlbuilders", "nodes.handlers.backgrounds")
        enableaction("vboxbuilders","nodes.handlers.backgrounds")
    end,
}

interfaces.implement {
    name      = "enablebackgroundalign",
    onlyonce  = true,
    actions   = function()
        enableaction("mvlbuilders", "nodes.handlers.alignbackgrounds")
        enableaction("vboxbuilders","nodes.handlers.alignbackgrounds")
    end,
}

interfaces.implement {
    name      = "setbackgroundrowdata",
    arguments = { "integer", "integer", "dimension" },
    actions   = function(row,box,indent)
        templates[row] = { takebox(box), indent }
    end,
}

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