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

--[[ldx--
<p>We save positional information in the main utility table. Not only
can we store much more information in <l n='lua'/> but it's also
more efficient.</p>
--ldx]]--

local texprint, concat, format = tex.print, table.concat, string.format

jobpositions           = jobpositions or { }
jobpositions.collected = jobpositions.collected or { }
jobpositions.tobesaved = jobpositions.tobesaved or { }

-- these are global since they are used often at the tex end

ptbs, pcol = jobpositions.tobesaved, jobpositions.collected -- global

local function initializer()
    ptbs, pcol = jobpositions.tobesaved, jobpositions.collected
end

job.register('jobpositions.collected', jobpositions.tobesaved, initializer)

function jobpositions.copy(target,source)
    jobpositions.collected[target] = jobpositions.collected[source] or ptbs[source]
end

function jobpositions.replace(name,...)
    jobpositions.collected[name] = {...}
end

function jobpositions.doifelse(name)
    commands.testcase(jobpositions.collected[name] or ptbs[name])
end

function jobpositions.MPp(id) local jpi = pcol[id] or ptbs[id] texprint((jpi and jpi[1]) or '0'  ) end
function jobpositions.MPx(id) local jpi = pcol[id] or ptbs[id] texprint((jpi and jpi[2]) or '0pt') end
function jobpositions.MPy(id) local jpi = pcol[id] or ptbs[id] texprint((jpi and jpi[3]) or '0pt') end
function jobpositions.MPw(id) local jpi = pcol[id] or ptbs[id] texprint((jpi and jpi[4]) or '0pt') end
function jobpositions.MPh(id) local jpi = pcol[id] or ptbs[id] texprint((jpi and jpi[5]) or '0pt') end
function jobpositions.MPd(id) local jpi = pcol[id] or ptbs[id] texprint((jpi and jpi[6]) or '0pt') end

-- the following are only for MP so there we can leave out the pt

function jobpositions.MPxy(id)
    local jpi = pcol[id] or ptbs[id]
    if jpi then
        texprint(format('(%s,%s)',jpi[2],jpi[3]))
    else
        texprint('(0,0)')
    end
end
function jobpositions.MPll(id)
    local jpi = pcol[id] or ptbs[id]
    if jpi then
        texprint(format('(%s,%s-%s)',jpi[2],jpi[3],jpi[6]))
    else
        texprint('(0,0)')
    end
end
function jobpositions.MPlr(id)
    local jpi = pcol[id] or ptbs[id]
    if jpi then
        texprint(format('(%s+%s,%s-%s)',jpi[2],jpi[4],jpi[3],jpi[6]))
    else
        texprint('(0,0)')
    end
end
function jobpositions.MPur(id)
    local jpi = pcol[id] or ptbs[id]
    if jpi then
        texprint(format('(%s+%s,%s+%s)',jpi[2],jpi[4],jpi[3],jpi[5]))
    else
        texprint('(0,0)')
    end
end
function jobpositions.MPul(id)
    local jpi = pcol[id] or ptbs[id]
    if jpi then
        texprint(format('(%s,%s+%s)',jpi[2],jpi[3],jpi[5]))
    else
        texprint('(0,0)')
    end
end
function jobpositions.MPpos(id)
    local jpi = pcol[id] or ptbs[id]
    if jpi then
        texprint(concat(jpi,',',1,6))
    else
        texprint('0,0,0,0,0,0')
    end
end
function jobpositions.MPplus(id,n,default)
    local jpi = pcol[id] or ptbs[id]
    texprint((jpi and jpi[6+n]) or default)
end
function jobpositions.MPrest(id,default)
    local jpi = pcol[id] or ptbs[id]
    texprint((jpi and jpi[7] and concat(jpi,",",7,#jpi)) or default)
end