summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/anch-snc.lua
blob: 8006f3bd674a317b6d058266cc766d58344ad0f8 (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
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"
}


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

-- use factors as in mlib-int.lua

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 = lpeg.match

local setmetatableindex = table.setmetatableindex

local factor            = number.dimenfactors.bp
local mpprint           = mp.print
local mpnumeric         = mp.numeric
local mppoints          = mp.points
local texgetdimen       = tex.getdimen

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

local list     = { }
local colors   = setmetatableindex("table")

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 mp.sync_collect(category,realpage,useregion)
    local all = allentries[category]
    local m   = 0
    local n   = #all
    list = { }
    if useregion 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 == useregion 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, mayb emetatable for region/page
                m    = 3
            end
        end
    end
    mpnumeric(m)
end

function mp.sync_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
    mpnumeric(n)
end

function mp.sync_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
    mpnumeric(n)
end

function mp.sync_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
    mpnumeric(n)
end

function mp.sync_set_color(category,n,v)
    colors[category][n] = v
end

function mp.sync_get_color(category,n)
    mpprint(colors[category][n])
end

-- function mp.sync_get_size  ()  mpnumeric(#list) end
-- function mp.sync_get_top   (n) mppoints (list[n].top) end
-- function mp.sync_get_bottom(n) mppoints (list[n].bottom) end
-- function mp.sync_get_kind  (n) mpnumeric(kinds[list[n].kind]) end
-- function mp.sync_get_task  (n) mpnumeric(list[n].task) end

-- function mp.sync_get_x() mppoints(list.page.x or 0) end
-- function mp.sync_get_y() mppoints(list.page.y or 0) end
-- function mp.sync_get_w() mppoints(list.page.w or 0) end
-- function mp.sync_get_h() mppoints(list.page.h or 0) end
-- function mp.sync_get_d() mppoints(list.page.d or 0) end

function mp.sync_get_size  ()  mpnumeric(#list) end
function mp.sync_get_top   (n) mpnumeric(list[n].top * factor) end
function mp.sync_get_bottom(n) mpnumeric(list[n].bottom * factor) end
function mp.sync_get_kind  (n) mpnumeric(kinds[list[n].kind]) end
function mp.sync_get_task  (n) mpnumeric(list[n].task) end

function mp.sync_get_x() mpnumeric((list.page.x or 0)*factor) end
function mp.sync_get_y() mpnumeric((list.page.y or 0)*factor) end
function mp.sync_get_w() mpnumeric((list.page.w or 0)*factor) end
function mp.sync_get_h() mpnumeric((list.page.h or 0)*factor) end
function mp.sync_get_d() mpnumeric((list.page.d or 0)*factor) end