summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/strc-pag.lua
blob: 216b5f705ab37bf97abcb88897a68887d178e40a (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
if not modules then modules = { } end modules ['strc-pag'] = {
    version   = 1.001,
    comment   = "companion to strc-pag.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local allocate, mark = utilities.storage.allocate, utilities.storage.mark

local trace_pages         = false  trackers.register("structures.pages", function(v) trace_pages = v end)

local report_pages        = logs.reporter("structure","pages")

local structures          = structures

local helpers             = structures.helpers
local sections            = structures.sections
local pages               = structures.pages
local sets                = structures.sets
local counters            = structures.counters

local counterdata         = counters.data

local variables           = interfaces.variables
local context             = context
local commands            = commands
local implement           = interfaces.implement

local processors          = typesetters.processors
local applyprocessor      = processors.apply
local startapplyprocessor = processors.startapply
local stopapplyprocessor  = processors.stopapply

local texsetcount         = tex.setcount
local texgetcount         = tex.getcount

local ctx_convertnumber   = context.convertnumber

-- storage

local collected, tobesaved = allocate(), allocate()

pages.collected = collected
pages.tobesaved = tobesaved
pages.nofpages  = 0

-- utilitydata.structures.counters.collected.realpage[1]

local function initializer()
    collected = pages.collected
    tobesaved = pages.tobesaved
    -- tricky, with pageinjection we can have holes
 -- pages.nofpages = #collected
 -- pages.nofpages = table.count(collected) -- could be a helper
    local n = 0
    for k in next, collected do
        if k > n then
            n = k
        end
    end
    pages.nofpages = n
end

job.register('structures.pages.collected', tobesaved, initializer)

local specification = { } -- to be checked

function pages.save(prefixdata,numberdata,extradata)
    local realpage = texgetcount("realpageno")
    local userpage = texgetcount("userpageno")
    if realpage > 0 then
        if trace_pages then
            report_pages("saving page %s.%s",realpage,userpage)
        end
        local viewerprefix = extradata.viewerprefix
        local state = extradata.state
        local data = {
            number       = userpage,
            viewerprefix = viewerprefix ~= "" and viewerprefix or nil,
            state        = state ~= "" and state or nil, -- maybe let "start" be default
            block        = sections.currentblock(),
            prefixdata   = prefixdata and helpers.simplify(prefixdata),
            numberdata   = numberdata and helpers.simplify(numberdata),
        }
        tobesaved[realpage] = data
        if not collected[realpage] then
            collected[realpage] = data
        end
    elseif trace_pages then
        report_pages("not saving page %s.%s",realpage,userpage)
    end
end

-- We can set the pagenumber but as it only get incremented in the page
-- builder we have to make sure it starts at least at 1.

function counters.specials.userpage()
    local r = texgetcount("realpageno")
    if r > 0 then
        local t = tobesaved[r]
        if t then
            t.number = texgetcount("userpageno")
            if trace_pages then
                report_pages("forcing pagenumber of realpage %s to %s",r,t.number)
            end
            return
        end
    end
    local u = texgetcount("userpageno")
    if u == 0 then
        if trace_pages then
            report_pages("forcing pagenumber of realpage %s to %s (probably a bug)",r,1)
        end
        counters.setvalue("userpage",1)
        texsetcount("userpageno",1) -- not global ?
    end
end

-- local f_convert = string.formatters["\\convertnumber{%s}{%s}"]
--
-- local function convertnumber(str,n)
--     return f_convert(str or "numbers",n)
-- end

function pages.number(realdata,pagespec)
    local userpage      = realdata.number
    local block         = realdata.block or "" -- sections.currentblock()
    local numberspec    = realdata.numberdata
    local conversionset = (pagespec and pagespec.conversionset ~= "" and pagespec.conversionset) or (numberspec and numberspec.conversionset ~= "" and numberspec.conversionset) or ""
    local conversion    = (pagespec and pagespec.conversion    ~= "" and pagespec.conversion   ) or (numberspec and numberspec.conversion    ~= "" and numberspec.conversion   ) or ""
    local starter       = (pagespec and pagespec.starter       ~= "" and pagespec.starter      ) or (numberspec and numberspec.starter       ~= "" and numberspec.starter      ) or ""
    local stopper       = (pagespec and pagespec.stopper       ~= "" and pagespec.stopper      ) or (numberspec and numberspec.stopper       ~= "" and numberspec.stopper      ) or ""
    if starter ~= "" then
        applyprocessor(starter)
    end
    if conversion ~= "" then
        ctx_convertnumber(conversion,userpage)
    else
        if conversionset == "" then conversionset = "default" end
        local theconversion = sets.get("structure:conversions",block,conversionset,1,"numbers") -- to be checked: 1
        local data = startapplyprocessor(theconversion)
        ctx_convertnumber(data or "number",userpage)
        stopapplyprocessor()
    end
    if stopper ~= "" then
        applyprocessors(stopper)
    end
end

-- (pagespec.prefix == yes|unset) and (pages.prefix == yes) => prefix

function pages.analyze(entry,pagespecification)
    -- safeguard
    if not entry then
        return false, false, "no entry"
    end
    local references = entry.references
    if not references then
        return false, false, "no references"
    end
    local pagedata = references.pagedata -- sometimes resolved (external)
    if not pagedata then
        local realpage = references.realpage
        if realpage then
            pagedata = collected[realpage]
        else
            return false, false, "no realpage"
        end
    end
    if not pagedata then
        return false, false, "no pagedata"
    end
    local sectiondata = references.sectiondata -- sometimes resolved (external)
    if not sectiondata then
        local section = references.section
        if section then
            sectiondata = sections.collected[section]
        else
            return pagedata, false, "no section"
        end
    end
    if not sectiondata then
        return pagedata, false, "no sectiondata"
    end
    local no = variables.no
    -- local preferences
    if pagespecification and pagespecification.prefix == no then
        return pagedata, false, "current spec blocks prefix"
    end
    -- stored preferences
 -- if entry.prefix == no then
 --     return pagedata, false, "entry blocks prefix"
 -- end
    -- stored page state
    pagespecification = pagedata.prefixdata
    if pagespecification and pagespecification.prefix == no then
        return pagedata, false, "pagedata blocks prefix"
    end
    -- final verdict
    return pagedata, sectiondata, "okay"
end

function helpers.page(data,pagespec)
    if data then
        local pagedata = pages.analyze(data,pagespec)
        if pagedata then
            pages.number(pagedata,pagespec)
        end
    end
end

function helpers.prefixpage(data,prefixspec,pagespec)
    if data then
        local pagedata, prefixdata, e = pages.analyze(data,pagespec)
        if pagedata then
            if prefixdata then
                sections.typesetnumber(prefixdata,"prefix",prefixspec or false,prefixdata or false,pagedata.prefixdata or false)
            end
            pages.number(pagedata,pagespec)
        end
    end
end

function helpers.prefixlastpage(data,prefixspec,pagespec)
    if data then
        local r  = data.references
        local ls = r.section
        local lr = r.realpage
        r.section  = r.lastsection or r.section
        r.realpage = r.lastrealpage or r.realpage
        helpers.prefixpage(data,prefixspec,pagespec)
        r.section, r.realpage = ls, lr
    end
end

--

function helpers.analyze(entry,specification)
    -- safeguard
    if not entry then
        return false, false, "no entry"
    end
    local yes = variables.yes
    local no  = variables.no
    -- section data
    local references = entry.references
    if not references then
        return entry, false, "no references"
    end
    local section = references.section
    if not section then
        return entry, false, "no section"
    end
    local sectiondata = sections.collected[references.section]
    if not sectiondata then
        return entry, false, "no section data"
    end
    -- local preferences
    if specification and specification.prefix == no then
        return entry, false, "current spec blocks prefix"
    end
    -- stored preferences (not used)
    local prefixdata = entry.prefixdata
    if prefixdata and prefixdata.prefix == no then
        return entry, false, "entry blocks prefix"
    end
    -- final verdict
    return entry, sectiondata, "okay"
end

function helpers.prefix(data,prefixspec)
    if data then
        local _, prefixdata, status = helpers.analyze(data,prefixspec)
        if prefixdata then
            sections.typesetnumber(prefixdata,"prefix",prefixspec or false,data.prefixdata or false,prefixdata or false)
        end
    end
end

function helpers.pageofinternal(n,prefixspec,pagespec)
    local data = structures.references.internals[n]
    if not data then
        -- error
    elseif prefixspec then
        helpers.prefixpage(data,prefixspec,pagespec)
    else
        helpers.prefix(data,pagespec)
    end
end

function pages.is_odd(n)
    n = n or texgetcount("realpageno")
    if texgetcount("pagenoshift") % 2 == 0 then
        return n % 2 ~= 0
    else
        return n % 2 == 0
    end
end

function pages.on_right(n)
    local pagemode = texgetcount("pageduplexmode")
    if pagemode == 2 or pagemode == 1 then
        n = n or texgetcount("realpageno")
        if texgetcount("pagenoshift") % 2 == 0 then
            return n % 2 ~= 0
        else
            return n % 2 == 0
        end
    else
        return true
    end
end

function pages.in_body(n)
    return texgetcount("pagebodymode") > 0
end

function pages.fraction(n)
    local lastpage = texgetcount("lastpageno") -- can be cached
    return lastpage > 1 and (texgetcount("realpageno")-1)/(lastpage-1) or 1
end

-- move to strc-pag.lua

function counters.analyze(name,counterspecification)
    local cd = counterdata[name]
    -- safeguard
    if not cd then
        return false, false, "no counter data"
    end
    -- section data
    local sectiondata = sections.current()
    if not sectiondata then
        return cd, false, "not in section"
    end
    local references = sectiondata.references
    if not references then
        return cd, false, "no references"
    end
    local section = references.section
    if not section then
        return cd, false, "no section"
    end
    sectiondata = sections.collected[references.section]
    if not sectiondata then
        return cd, false, "no section data"
    end
    -- local preferences
    local no = variables.no
    if counterspecification and counterspecification.prefix == no then
        return cd, false, "current spec blocks prefix"
    end
    -- stored preferences (not used)
    if cd.prefix == no then
        return cd, false, "entry blocks prefix"
    end
    -- sectioning
    -- if sectiondata.prefix == no then
    --     return false, false, "sectiondata blocks prefix"
    -- end
    -- final verdict
    return cd, sectiondata, "okay"
end

function sections.prefixedconverted(name,prefixspec,numberspec)
    local cd, prefixdata, result = counters.analyze(name,prefixspec)
    if cd then
        if prefixdata then
            sections.typesetnumber(prefixdata,"prefix",prefixspec or false,cd or false)
        end
        counters.converted(name,numberspec)
    end
end

--

implement {
    name      = "savepagedata",
    actions   = pages.save,
    arguments = {
        {
            { "prefix" },
            { "separatorset" },
            { "conversionset" },
            { "conversion" },
            { "set" },
            { "segments" },
            { "connector" },
        },
        {
            { "conversionset" },
            { "conversion" },
            { "starter" },
            { "stopper" },
        },
        {
            { "viewerprefix" },
            { "state" },
        }
    }
}

implement { -- weird place
    name      = "prefixedconverted",
    actions   = sections.prefixedconverted,
    arguments = {
        "string",
        {
            { "prefix" },
            { "separatorset" },
            { "conversionset" },
            { "conversion" },
            { "starter" },
            { "stopper" },
            { "set" },
            { "segments" },
            { "connector" },
        },
        {
            { "order" },
            { "separatorset" },
            { "conversionset" },
            { "conversion" },
            { "starter" },
            { "stopper" },
            { "segments" },
            { "type" },
            { "criterium" },
        }
    }
}

interfaces.implement {
    name      = "pageofinternal",
    arguments = { "integer" },
    actions   = helpers.pageofinternal,
}