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

-- todo: adapt message

local tonumber, next = tonumber, next
local format, validstring = string.format, string.valid
local sortedkeys = table.sortedkeys

local context  = context
local commands = commands

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

local cache = { }

local function flush(page)
    local c = cache[page]
    if c then
        for i=1,#c do
         -- characters.showstring(c[i])
            context.viafile(c[i],format("page.%s",validstring(page,"nopage")))
        end
        cache[page] = nil
    end
end

local function setnextpage()
    local n = next(cache) and sortedkeys(cache)[1]
    if not n then
        n = 0           -- nothing in the cache
    elseif n == 0 then
        n = -1          -- generic buffer (0)
    elseif n > 0 then
                        -- upcoming page (realpageno)
    end
    texsetcount("global","c_page_postponed_blocks_next_page",n)
end

function commands.flushpostponedblocks(page)
    -- we need to flush previously pending pages as well and the zero
    -- slot is the generic one so that one is always flushed
    local t = sortedkeys(cache)
    local p = tonumber(page) or texgetcount("realpageno") or 0
    for i=1,#t do
        local ti = t[i]
        if ti <= p then
            flush(ti)
        else
            break
        end
    end
    setnextpage()
end

function commands.registerpostponedblock(page)
    if type(page) == "string" then
        if string.find(page,"^+") then
            page = texgetcount("realpageno") + (tonumber(page) or 1) -- future delta page
        else
            page = tonumber(page) or 0 -- preferred page or otherwise first possible occasion
        end
    end
    if not page then
        page = 0
    end
    local c = cache[page]
    if not c then
        c = { }
        cache[page] = c
    end
    c[#c+1] = buffers.raw("postponedblock")
    buffers.erase("postponedblock")
    if page == 0 then
        interfaces.showmessage("layouts",3,#c)
    else
        interfaces.showmessage("layouts",3,string.format("%s (realpage: %s)",#c,page))
    end
    setnextpage()
end