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

--[[ldx--
<p>We save object references in the main utility table. jobobjects are
reusable components.</p>
--ldx]]--

local context         = context
local codeinjections  = backends.codeinjections

local ctx_doifelse    = commands.doifelse

local nuts            = nodes.nuts

local setlink         = nuts.setlink
local getlist         = nuts.getlist
local setbox          = nuts.setbox

local new_latelua     = nuts.pool.latelua

local settexdimen     = tokens.setters.dimen
local settexcount     = tokens.setters.count

local gettexbox       = tokens.getters.box
local gettexdimen     = tokens.getters.dimen
local gettexcount     = tokens.getters.count

local implement       = interfaces.implement
local setmacro        = interfaces.setmacro

local allocate        = utilities.storage.allocate

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

local jobobjects      = {
    collected = collected,
    tobesaved = tobesaved,
}

job.objects           = jobobjects

local function initializer()
    collected = jobobjects.collected
    tobesaved = jobobjects.tobesaved
end

job.register('job.objects.collected', tobesaved, initializer, nil)

local function saveobject(tag,number,page)
    local t = { number, page }
    tobesaved[tag], collected[tag] = t, t
end

local function setobject(tag,number,page)
    collected[tag] = { number, page }
end

local function getobject(tag)
    return collected[tag] or tobesaved[tag]
end

local function getobjectnumber(tag,default)
    local o = collected[tag] or tobesaved[tag]
    return o and o[1] or default
end

local function getobjectpage(tag,default)
    local o = collected[tag] or tobesaved[tag]
    return o and o[2] or default
end

jobobjects.save   = saveobject
jobobjects.set    = setobject
jobobjects.get    = getobject
jobobjects.number = getobjectnumber
jobobjects.page   = getobjectpage

-- implement {
--     name      = "saveobject",
--     actions   = saveobject
-- }
--
-- implement {
--     name      = "setobject",
--     actions   = setobject,
--     arguments = { "string", "integer", "integer" }
-- }
--
-- implement {
--     name      = "objectnumber",
--     actions   = { getobjectnumber, context },
--     arguments = { "string", "string" },
-- }
--
-- implement {
--     name      = "objectpage",
--     actions   = { getobjectpage, context },
--     arguments = { "string", "string" },
-- }
--
-- implement {
--     name      = "doifelseobjectreferencefound",
--     actions   = { getobject, commands.doifelse },
--     arguments = "string"
-- }

-- if false then
--     -- we can flush the inline ref ourselves now if we want
--     local flush = new_latelua("pdf.flushxform("..index..")")
--     flush.next = list
--     next.prev = flush
-- end

local data = table.setmetatableindex("table")

objects = {
    data = data,
    n    = 0,
}

function objects.register(ns,id,b,referenced)
    objects.n = objects.n + 1
    nodes.handlers.finalize(gettexbox(b))
    data[ns][id] = {
        codeinjections.registerboxresource(b), -- a box number
        gettexdimen("objectoff"),
        referenced
    }
end

function objects.restore(ns,id)
    local d = data[ns][id]
    if d then
        local index  = d[1]
        local offset = d[2]
        local status = d[3]
        local hbox   = codeinjections.restoreboxresource(index) -- a nut !
        if status then
            local list = getlist(hbox)
            local page = new_latelua(function()
                saveobject(ns .. "::" .. id,index,gettexcount("realpageno"))
            end)
            setlink(list,page)
        end
        setbox("objectbox",hbox)
        settexdimen("objectoff",offset)
    else
        setbox("objectbox",nil)
        settexdimen("objectoff",0)
    end
end

function objects.dimensions(index)
    local d = data[ns][id]
    if d then
        return codeinjections.boxresourcedimensions(d[1])
    else
        return 0, 0, 0
    end
end

function objects.reference(ns,id)
    local d = data[ns][id]
    if d then
        return d[1]
    else
        return getobjectnumber(ns .."::" .. id,0)
    end
end

function objects.page(ns,id)
    return getobjectpage(ns .."::" .. id,gettexcount("realpageno"))
end

function objects.found(ns,id)
    return data[ns][id]
end

implement {
    name      = "registerreferencedobject",
    arguments = { "string", "string", "integer", true },
    actions   = objects.register,
}

implement {
    name      = "registerobject",
    arguments = { "string", "string", "integer" },
    actions   = objects.register,
}

implement {
    name      = "restoreobject",
    arguments = { "string", "string" },
    actions   = objects.restore,
}

implement {
    name      = "doifelseobject",
    arguments = { "string", "string" },
    actions   = function(ns,id)
        ctx_doifelse(data[ns][id])
     -- ctx_doifelse(objects.reference(ns,id))
    end,
}

implement {
    name      = "doifelseobjectreference",
    arguments = { "string", "string" },
    actions   = function(ns,id)
     -- ctx_doifelse(data[ns][id])
        ctx_doifelse(objects.reference(ns,id))
    end,
}

implement {
    name      = "getobjectreference",
    arguments = { "string", "string", "csname" },
    actions   = function(ns,id,target)
        setmacro(target,objects.reference(ns,id),"global")
    end
}

implement {
    name      = "getobjectreferencepage",
    arguments = { "string", "string", "csname" },
    actions   = function(ns,id,target)
        setmacro(target,objects.page(ns,id),"global")
    end
}

implement {
    name      = "getobjectdimensions",
    arguments = { "string", "string" },
    actions   = function(ns,id)
        local o = data[ns][id]
        local w, h, d = 0, 0, 0
        if d then
            w, h, d = codeinjections.boxresourcedimensions(o[1])
        end
        settexdimen("objectwd",w or 0)
        settexdimen("objectht",h or 0)
        settexdimen("objectdp",d or 0)
        settexdimen("objectoff",o[2])
    end
}

-- for the moment here:

implement {
    name      = "registerbackendsymbol",
    arguments = { "string", "integer" },
    actions   = function(...)
        codeinjections.registersymbol(...)
    end
}