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

local tonumber, next, setmetatable = tonumber, next, setmetatable
local concat, sort, remove, copy = table.concat, table.sort, table.remove, table.copy
local match, find = string.match, string.find
local lpegmatch, lpegpatterns = lpeg.match, lpeg.patterns
local setmetatableindex = table.setmetatableindex
local P, Cc = lpeg.P, lpeg.Cc

graphics                = graphics or { }
local synchronizers     = { }
graphics.synchronizers  = synchronizers

local p_number = lpegpatterns.cardinal/tonumber
local p_space  = lpegpatterns.whitespace^0
local p_tag    = P("syncpos:") * p_number * P(":") * p_number
local p_option = p_number * ((P(",") * p_space * P("reset") * Cc(true)) + Cc(false)) -- for now

local list     = { }
local kinds    = {
    above    = 1,
    continue = 2,
    nothing  = 3,
    normal   = 4,
    below    = 5,
}

local allentries = setmetatableindex(function(t,category)
    setmetatable(t,nil)
    for tag, pos in next, job.positions.collected do
        local c, n = lpegmatch(p_tag,tag)
        if c then
            local tc = t[c]
            if tc then
                tc[n] = pos
            else
                t[c] = { [n] = pos }
            end
        end
    end
    for k, list in next, t do
        sort(list,function(a,b)
            local ap = a.p
            local bp = b.p
            if ap == bp then
                return b.y < a.y
            else
                return ap < bp
            end
        end)
        list.start = 1
    end
    setmetatableindex(t,"table")
    return t[category]
end)

local lastdone = { }

function synchronizers.collect(category,realpage,region)
    local all = allentries[category]
    local m   = 0
    local n   = #all
    list = { }
    if region and region ~= "" then
        -- successive can be optimized when we sort by region
        local start = 1
        local done  = false
        local last, rtop, rbot
        for i=start,n do
            local pos = all[i]
            local p = pos.p
            local r = pos.r
            if r == region then
                if not done then
                    local region = job.positions.collected[r]
                    list.region  = region
                    list.page    = region
                    rtop = (region.y or 0) + (region.h or 0)
                    rbot = (region.y or 0) - (region.d or 0)
                    last = { kind = "nothing", top = rtop, bottom = 0, task = 0 }
                    m = m + 1 ; list[m] = last
                    done = true
                end
                local top = pos.y + pos.h
                last.bottom = top
                local task, reset = lpegmatch(p_option,pos.e)
                last = { kind = "normal", top = top, bottom = 0, task = task }
                m = m + 1 ; list[m] = last
            end
        end
        if done then
            last.bottom = rbot
        end
    else
        local start = all.start or 1
        local done  = false
        local last, rtop, rbot, ptop, pbot
        for i=start,n do
            local pos = all[i]
            local p = pos.p
            if p == realpage then
                if not done then
                    local region = job.positions.collected[pos.r]
                    local page   = job.positions.collected["page:"..realpage] or region
                    list.region  = region
                    list.page    = page
                    rtop = (region.y or 0) + (region.h or 0)
                    rbot = (region.y or 0) - (region.d or 0)
                    ptop = (page  .y or 0) + (page  .h or 0)
                    pbot = (page  .y or 0) - (page  .d or 0)
                    last = { kind = "above", top = ptop, bottom = rtop, task = 0 }
                    m = m + 1 ; list[m] = last
                    if i > 1 then
                        local task, reset = lpegmatch(p_option,all[i-1].e)
                        last = { kind = "continue", top = rtop, bottom = 0, task = task }
                        m = m + 1 ; list[m] = last
                    else
                        last = { kind = "nothing", top = rtop, bottom = 0, task = 0 }
                        m = m + 1 ; list[m] = last
                    end
                    done = true
                end
                local top = pos.y + pos.h
                last.bottom = top
                local task, reset = lpegmatch(p_option,pos.e)
                if reset then
                    local l = list[2]
                    l.kind = "nothing"
                    l.task = 0
                end
                last = { kind = "normal", top = top, bottom = 0, task = task }
                m = m + 1 ; list[m] = last
            elseif p > realpage then
                all.start = i -- tricky, only for page
                break
            end
        end
        if done then
            last.bottom = rbot
            last = { kind = "below", top = rbot, bottom = pbot, task = 0 }
            m = m + 1 ; list[m] = last
            lastdone[category] = {
                { kind = "above", top = ptop, bottom = rtop, task = 0 },
                { kind = "continue", top = rtop, bottom = rbot, task = list[#list-1].task }, -- lasttask
                { kind = "below", top = rbot, bottom = pbot, task = 0 },
                region = list.region,
                page   = list.page,
            }
        else
            local l = lastdone[category]
            if l then
                list = copy(l) -- inefficient, maybe metatable for region/page
                m    = 3
            end
        end
    end
    return m
end

function synchronizers.extend()
     local n = #list
     if n > 0 then
        for i=1,n do
            local l = list[i]
            local k = l.kind
            if k == "nothing" then
                local ll = list[i+1]
                if ll and ll.kind == "normal" then
                    ll.top = l.top
                    remove(list,i)
                    n = #list
                    break
                end
            end
        end
    end
    return n
end

function synchronizers.prune()
     local n = #list
     if n > 0 then
        if list[1].kind == "above" then
            remove(list,1)
        end
        if list[1].kind == "nothing" then
            remove(list,1)
        end
        if list[#list].kind == "below" then
            remove(list,#list)
        end
        n = #list
    end
    return n
end

function synchronizers.collapse()
    local n = #list
    if n > 0 then
        local m = 0
        local p = nil
        for i=1,n do
            local l = list[i]
            local t = l.task
            if p == t then
                list[m].bottom = l.bottom
            else
                m = m + 1
                list[m] = l
            end
            p = t
        end
        for i=n,m+1,-1 do
            list[i] = nil
        end
        n = m
    end
    return n
end

function synchronizers.getsize  ()  return #list               end
function synchronizers.gettop   (n) return list[n].top         end
function synchronizers.getbottom(n) return list[n].bottom      end
function synchronizers.getkind  (n) return kinds[list[n].kind] end
function synchronizers.gettask  (n) return list[n].task        end

function synchronizers.getx() return list.page.x or 0 end
function synchronizers.gety() return list.page.y or 0 end
function synchronizers.getw() return list.page.w or 0 end
function synchronizers.geth() return list.page.h or 0 end
function synchronizers.getd() return list.page.d or 0 end

-- function mp.xxOverlayRegion()
--     local r = tokens.getters.macro("m_overlay_region")
--     mp.quoted('"'.. r .. '"')
-- end