summaryrefslogtreecommitdiff
path: root/tex/context/base/node-rul.lua
blob: 3f1d572b75d11aeeca819ad309a8dd8a9ed641a0 (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
if not modules then modules = { } end modules ['node-rul'] = {
    version   = 1.001,
    comment   = "companion to node-rul.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- todo: order and maybe other dimensions

local trace_ruled = false  trackers.register("nodes.ruled", function(v) trace_ruled = v end)

local floor = math.floor
local n_tostring, n_tosequence = nodes.ids_tostring, nodes.tosequence

local a_ruled        = attributes.private('ruled')
local a_color        = attributes.private('color')
local a_transparency = attributes.private('transparency')
local a_colorspace   = attributes.private('colormodel')

local glyph   = node.id("glyph")
local disc    = node.id("disc")
local glue    = node.id("glue")
local kern    = node.id("kern")
local hlist   = node.id("hlist")
local vlist   = node.id("vlist")
local rule    = node.id("rule")
local whatsit = node.id("whatsit")

local new_rule = nodes.rule
local new_kern = nodes.kern
local new_glue = nodes.glue

local insert_before, insert_after = node.insert_before, node.insert_after
local list_dimensions, has_attribute, set_attribute = node.dimensions, node.has_attribute, node.set_attribute
local dimenfactor = fonts.dimenfactor
local texwrite = tex.write

local fontdata  = fonts.ids
local variables = interfaces.variables

-- we can use this one elsewhere too
--
-- todo: functions: word, sentence
--
-- glyph rule unset whatsit glue margin_kern kern math disc

local checkdir = true

-- we assume {glyphruns} and no funny extra kerning, ok, maybe we need
-- a dummy character as start and end; anyway we only collect glyphs

local function process_words(attribute,data,flush,head,parent)
    local n = head
    if n then
        local f, l, a, d, i, level
        local continue, done = false, false
        while n do
            local id = n.id
            if id == glyph then
                local aa = has_attribute(n,attribute)
                if aa then
                    if aa == a then
                        if not f then -- ?
                            f = n
                        end
                        l = n
                    else
                        -- possible extensions: when in same class then keep spanning
                        if f then
                            head, done = flush(head,f,l,d,level,parent), true
                        end
                        f, l, a = n, n, aa
                        level, i = floor(a/1000), a%1000
                        d = data[i]
                        continue = d.continue == variables.yes
                    end
                else
                    if f then
                        head, done = flush(head,f,l,d,level,parent), true
                    end
                    f, l, a = nil, nil, nil
                end
            elseif f and id == disc then
                l = n
            elseif f and id == rule then
                l = n
            elseif f and id == kern and n.subtype == 0 then
                l = n
            elseif id == hlist or id == vlist then
                if f then
                    head, done = flush(head,f,l,d,level,parent), true
                    f, l, a = nil, nil, nil
                end
                local list = n.list
                if list then
                    n.list = process_words(attribute,data,flush,list,n)
                end
            elseif checkdir and id == whatsit and n.subtype == 7 then -- only changes in dir, we assume proper boundaries
                if f and a then
                    l = n
                end
            elseif f and not continue then
                head, done = flush(head,f,l,d,level,parent), true
                f, l, a = nil, nil, nil
            end
            n = n.next
        end
        if f then
            head, done = flush(head,f,l,d,level,parent), true
        end
        return head, true -- todo: done
    else
        return head, false
    end
end

nodes.process_words = process_words

--

nodes.rules      = nodes.rules      or { }
nodes.rules.data = nodes.rules.data or { }

storage.register("nodes/rules/data", nodes.rules.data, "nodes.rules.data")

local data = nodes.rules.data

function nodes.rules.define(settings)
    data[#data+1] = settings
    texwrite(#data)
end

local function flush_ruled(head,f,l,d,level,parent) -- not that fast but acceptable for this purpose
    local r, m
    local w = list_dimensions(parent.glue_set,parent.glue_sign,parent.glue_order,f,l.next)
    local method, offset, continue, dy, rulethickness, unit, order, max, ma, ca, ta =
        d.method, d.offset, d.continue, d.dy, d.rulethickness, d.unit, d.order, d.max, d.ma, d.ca, d.ta
    local e = dimenfactor(unit,fontdata[f.font])
    local colorspace   = (ma > 0 and ma) or has_attribute(f,a_colorspace) or 1
    local color        = (ca > 0 and ca) or has_attribute(f,a_color)
    local transparency = (ta > 0 and ta) or has_attribute(f,a_transparency)
    local foreground = order == variables.foreground
    rulethickness= rulethickness/2
    if level > max then
        level = max
    end
    if method == 0 then -- center
        offset = 2*offset
        m = (offset+(level-1)*dy+rulethickness)*e/2
    else
        m = 0
    end
    for i=1,level do
        local ht =  (offset+(i-1)*dy+rulethickness)*e - m
        local dp = -(offset+(i-1)*dy-rulethickness)*e + m
        local r = new_rule(w,ht,dp)
        if color then
            set_attribute(r,a_colorspace,colorspace)
            set_attribute(r,a_color,color)
        end
        if transparency then
            set_attribute(r,a_transparency,transparency)
        end
        local k = new_kern(-w)
        if foreground then
            insert_after(head,l,k)
            insert_after(head,k,r)
            l = r
        else
            head, _ = insert_before(head,f,r)
            insert_after(head,r,k)
        end
        if trace_ruled then
            logs.report("ruled", "level: %s, width: %s, nodes: %s, text: %s",level,w,n_tostring(f,l),n_tosequence(f,l,true))
        end
    end
    return head
end

local process = nodes.process_words

nodes.rules.process = function(head) return process(a_ruled,data,flush_ruled,head) end

function nodes.rules.enable()
    tasks.enableaction("shipouts","nodes.rules.process")
end

-- elsewhere:
--
-- tasks.appendaction ("shipouts", "normalizers", "nodes.rules.process")
-- tasks.disableaction("shipouts",                "nodes.rules.process") -- only kick in when used

local trace_shifted = false  trackers.register("nodes.shifted", function(v) trace_shifted = v end)

local a_shifted = attributes.private('shifted')

nodes.shifts      = nodes.shifts      or { }
nodes.shifts.data = nodes.shifts.data or { }

storage.register("nodes/shifts/data", nodes.shifts.data, "nodes.shifts.data")

local data = nodes.shifts.data

function nodes.shifts.define(settings)
    data[#data+1] = settings
    texwrite(#data)
end

local function flush_shifted(head,first,last,data,level,parent) -- not that fast but acceptable for this purpose
    local prev, next = first.prev, last.next
    first.prev, last.next = nil, nil
    local width, height, depth = list_dimensions(parent.glue_set,parent.glue_sign,parent.glue_order,first,next)
    local list = node.hpack(first,width,"exactly")
    if first == head then
        head = list
    end
    if prev then
        prev.next, list.prev = list, prev
    end
    if next then
        next.prev, list.next = list, next
    end
    local raise = data.dy * dimenfactor(data.unit,fontdata[first.font])
    list.shift, list.height, list.depth = raise, height, depth
    if trace_shifted then
        logs.report("shifted", "width: %s, nodes: %s, text: %s",width,n_tostring(first,last),n_tosequence(first,last,true))
    end
    return head
end

local process = nodes.process_words

nodes.shifts.process = function(head) return process(a_shifted,data,flush_shifted,head) end

function nodes.shifts.enable()
    tasks.enableaction("shipouts","nodes.shifts.process")
end