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

local context         = context

local trace           = false  trackers.register("backend", function(v) trace = v end)
local report          = logs.reporter("backend")

local scanners        = tokens.scanners
local scankeyword     = scanners.keyword
local scaninteger     = scanners.integer
local scanstring      = scanners.string
local scandimension   = scanners.dimension
local scanword        = scanners.word
local scanwhd         = scanners.whd

local implement       = interfaces.implement
local constants       = interfaces.constants
local variables       = interfaces.variables

local values          = tokens.values
----- dimension_value = values.dimension
local cardinal_value  = values.cardinal
----- direct_value    = values.direct

-- A box resource has an index. This happens to be an object number due to the pdf
-- backend but in fact it's an abstraction. This is why we have explicit fetchers.
-- The internal number (as in \Fm123) is yet another number.

-- this will change:
--
-- tex.*            : arglist, as in luatex
-- tex.boxresources : table

local codeinjections = backends.codeinjections
local nodeinjections = backends.nodeinjections

local savebox = function(...) return codeinjections.saveboxresource(...) end
local usebox  = function(...) return nodeinjections.useboxresource(...) end
local getbox  = function(...) return nodeinjections.getboxresourcebox(...) end
local getwhd  = function(...) return codeinjections.getboxresourcedimensions(...) end

tex.saveboxresource          = savebox
tex.useboxresource           = usebox
tex.getboxresourcebox        = getbox
tex.getboxresourcedimensions = getwhd

local boxresources = {
    save          = savebox,
    use           = usebox,
    getbox        = getbox,
    getdimensions = getwhd,
}

tex.boxresources = boxresources

local lastindex = 0

local function saveboxresource()
    local immediate  = true
    local kind       = scankeyword("type") and scaninteger() or 0
    local attributes = scankeyword("attr") and scanstring() or nil
    local resources  = scankeyword("resources") and scanstring() or nil
    local margin     = scankeyword("margin") and scandimension() or 0 -- register
    local boxnumber  = scaninteger()
    --
    lastindex = savebox(boxnumber,attributes,resources,immediate,kind,margin)
    if trace then
        report("\\saveboxresource: index %i",lastindex)
    end
end

local function lastsavedboxresourceindex()
    if trace then
        report("\\lastsaveboxresourceindex: index %i",lastindex)
    end
 -- context("%i",lastindex)
    return cardinal_value, lastindex or 0
end

local function useboxresource()
    local width, height, depth = scanwhd()
    local index = scaninteger()
    local node  = usebox(index,width,height,depth)
    if trace then
        report("\\useboxresource: index %i",index)
    end
    context(node)
end

implement {
    name    = "saveboxresource",
    actions = saveboxresource,
}
implement {
    name    = "lastsavedboxresourceindex",
    public  = true,
    usage   = "value",
    actions = lastsavedboxresourceindex,
}
implement {
    name    = "useboxresource",
    actions = useboxresource,
}

-- used in pack

do

    local nuts      = nodes.nuts
    local tonut     = nodes.tonut

    local setwhd    = nuts.setwhd
    local setlist   = nuts.setlist

    local new_hlist = nuts.pool.hlist

    function codeinjections.restoreboxresource(index)
        local hbox = new_hlist()
        local list, wd, ht, dp = usebox(index)
        setlist(hbox,tonut(list))
        setwhd(hbox,wd,ht,dp)
        return hbox -- so we return a nut !
    end

    function codeinjections.registerboxresource(n,offset,delay,objnum)
        local immediate = true
        if delay == true or delay == 1 then
            immediate = false
        end
        local r = savebox(n,nil,true,immediate,0,offset or 0,objnum)
        return r
    end

end

-- image resources

local imageresources = { }
local lastindex      = 0
local lastpages      = 1

local function saveimageresource()
    local width, height, depth = scanwhd()
    local page       = 1
    local immediate  = true
    local margin     = 0 -- or dimension
    local attributes = scankeyword("attr") and scanstring() or nil
    if scankeyword("named") then
        scanstring() -- ignored
    elseif scankeyword("page") then
        page = scaninteger()
    end
    local userpassword    = scankeyword("userpassword") and scanstring() or nil
    local ownerpassword   = scankeyword("ownerpassword") and scanstring() or nil
    local visiblefilename = scankeyword("visiblefilename") and scanstring() or nil
    local colorspace      = scankeyword("colorspace") and scaninteger() or nil
    local pagebox         = scanword() or nil
    local filename        = scanstring()
-- pcall
    context.getfiguredimensions( { filename }, {
        [constants.userpassword]  = userpassword,
        [constants.ownerpassword] = ownerpassword,
        [constants.page]          = page or 1,
        [constants.size]          = pagebox,
    })
    context.relax()
    lastindex = lastindex + 1
    lastpages = 1
    imageresources[lastindex] = {
        filename = filename,
        page     = page or 1,
        size     = pagebox,
        width    = width,
        height   = height,
        depth    = depth,
        attr     = attributes,
     -- margin   = margin,
     }
end

local function lastsavedimageresourceindex()
--     context("%i",lastindex or 0)
    return cardinal_value, lastindex or 0
end

local function lastsavedimageresourcepages()
--     context("%i",lastpages or 0) -- todo
    return cardinal_value, lastpages or 0
end

local function useimageresource()
    local width, height, depth = scanwhd()
    if scankeyword("keepopen") then
        -- ignored
    end
    local index = scaninteger()
    local l = imageresources[index]
    if l then
        if not (width or height or depth) then
            width  = l.width
            height = l.height
            depth  = l.depth
        end
        -- pcall / we could use a dedicated call instead:
        context.externalfigure( { l.filename }, {
            [constants.userpassword]  = l.userpassword,
            [constants.ownerpassword] = l.ownerpassword,
            [constants.width]         = width and (width .. "sp") or nil,
            [constants.height]        = height and (height .. "sp") or nil,
            [constants.page]          = l.page or 1,
            [constants.size]          = pagebox,
        })
        context.relax()
    else
        report("no valid image resource %a",index)
    end
end

implement {
    name    = "saveimageresource",
    actions = saveimageresource,
}

implement {
    name    = "lastsavedimageresourceindex",
    public  = true,
    usage   = "value",
    actions = lastsavedimageresourceindex,
}

implement {
    name    = "lastsavedimageresourcepages",
    public  = true,
    usage   = "value",
    actions = lastsavedimageresourcepages,
}

implement {
    name    = "useimageresource",
    actions = useimageresource,
}