summaryrefslogtreecommitdiff
path: root/tex/context/base/node-bck.lua
blob: e1e4d46525ea7c8fb726fdd8e7a47e0d16b1ec36 (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
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

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

local nodecodes      = nodes.nodecodes

local hlist_code     = nodecodes.hlist
local vlist_code     = nodecodes.vlist

local has_attribute  = node.has_attribute
local set_attribute  = node.set_attribute
local traverse       = node.traverse

local nodepool       = nodes.pool
local tasks          = nodes.tasks

local new_rule       = nodepool.rule
local new_glue       = nodepool.glue

local a_color        = attributes.private('color')
local a_transparency = attributes.private('transparency')
local a_colorspace   = attributes.private('colormodel')
local a_background   = attributes.private('background')

local function add_backgrounds(head) -- boxes, inline will be done too
    local id = head.id
    if id == vlist_code or id == hlist_code then
        local current = head.list
        while current do
            local id = current.id
            if id == hlist_code then -- and current.list
                local background = has_attribute(current,a_background)
                if background then
                    -- direct to hbox
                    -- colorspace is already set so we can omit that and stick to color
                    local mode = has_attribute(current,a_colorspace)
                    if mode then
                        local glue = new_glue(-current.width)
                        local rule = new_rule(current.width,current.height,current.depth)
                        local color = has_attribute(current,a_color)
                        local transparency = has_attribute(current,a_transparency)
                        set_attribute(rule,a_colorspace, mode)
                        if color then
                            set_attribute(rule,a_color, color)
                        end
                        if transparency then
                            set_attribute(rule,a_transparency,transparency)
                        end
                        rule.next = glue
                        glue.next = current.list
                        current.list = rule
                    end
                else
                    -- temporary hack for aligments
                    local list, background, found = current.list, nil, nil
                    for l in traverse(list) do
                        background = has_attribute(l,a_background)
                        if background then
                            found = l
                            break
                        end
                    end
                    if background then
                        local mode = has_attribute(found,a_colorspace)
                        if mode then
                            local glue = new_glue(-current.width)
                            local rule = new_rule(current.width,current.height,current.depth)
                            local color = has_attribute(found,a_color)
                            local transparency = has_attribute(found,a_transparency)
                            set_attribute(rule,a_colorspace, mode)
                            if color then
                                set_attribute(rule,a_color, color)
                            end
                            if transparency then
                                set_attribute(rule,a_transparency,transparency)
                            end
                            rule.next = glue
                            glue.next = list
                            current.list = rule
                        end
                    else
                        add_backgrounds(current)
                    end
                end
            elseif id == vlist_code then -- and current.list
                    -- direct to vbox
                local background = has_attribute(current,a_background)
                if background then
                    local mode = has_attribute(current,a_colorspace)
                    if mode then
                        local glue = new_glue(-current.height-current.depth)
                        local rule = new_rule(current.width,current.height,current.depth)
                        local color = has_attribute(current,a_color)
                        local transparency = has_attribute(current,a_transparency)
                        set_attribute(rule,a_colorspace, mode)
                        if color then
                            set_attribute(rule,a_color, color)
                        end
                        if transparency then
                            set_attribute(rule,a_transparency,transparency)
                        end
                        rule.next = glue
                        glue.next = current.list
                        current.list = rule
                    end
                end
                add_backgrounds(current)
            end
            current = current.next
        end
    end
    return head, true
end

nodes.handlers.backgrounds = add_backgrounds

tasks.appendaction("shipouts","normalizers","nodes.handlers.backgrounds")