back-res.lua /size: 6676 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['back-res'] = {
2    version   = 1.001,
3    comment   = "companion to back-ini.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local context       = context
10
11local trace         = false  trackers.register("backend", function(v) trace = v end)
12local report        = logs.reporter("backend")
13
14local scanners      = tokens.scanners
15local scankeyword   = scanners.keyword
16local scaninteger   = scanners.integer
17local scanstring    = scanners.string
18local scandimension = scanners.dimension
19local scanword      = scanners.word
20local scanwhd       = scanners.whd
21
22local implement     = interfaces.implement
23local constants     = interfaces.constants
24local variables     = interfaces.variables
25
26-- A box resource has an index. This happens to be an object number due to the pdf
27-- backend but in fact it's an abstraction. This is why we have explicit fetchers.
28-- The internal number (as in \Fm123) is yet another number.
29
30local tex_saveboxresource          = tex.saveboxresource
31local tex_useboxresource           = tex.useboxresource
32local tex_getboxresourcebox        = tex.getboxresourcebox
33local tex_getboxresourcedimensions = tex.getboxresourcedimensions
34
35updaters.register("backend.update",function()
36    tex_saveboxresource          = tex.saveboxresource
37    tex_useboxresource           = tex.useboxresource
38    tex_getboxresourcebox        = tex.getboxresourcebox
39    tex_getboxresourcedimensions = tex.getboxresourcedimensions
40end)
41
42local savebox = function(...) return tex_saveboxresource(...) end
43local usebox  = function(...) return tex_useboxresource(...) end
44local getbox  = function(...) return tex_getboxresourcebox(...) end
45local getwhd  = function(...) return tex_getboxresourcedimensions(...) end
46
47local boxresources = {
48    save          = savebox,
49    use           = usebox,
50    getbox        = getbox,
51    getdimensions = getwhd,
52}
53
54tex.boxresources = boxresources
55
56-- local tex_saveimageresource = tex.saveimageresource
57-- local tex_useimageresource  = tex.useimageresource
58--
59-- updaters.register("backend.update",function()
60--     tex_saveimageresource = tex.saveimageresource
61--     tex_useimageresource  = tex.useimageresource
62-- end)
63--
64-- tex.imageresources = {
65--     save = function(...) return tex_saveimageresource(...) end,
66--     use  = function(...) return tex_useimageresource(...) end,
67-- }
68
69local lastindex = 0
70
71local function saveboxresource()
72    local immediate  = true
73    local kind       = scankeyword("type") and scaninteger() or 0
74    local attributes = scankeyword("attr") and scanstring() or nil
75    local resources  = scankeyword("resources") and scanstring() or nil
76    local margin     = scankeyword("margin") and scandimension() or 0 -- register
77    local boxnumber  = scaninteger()
78    --
79    lastindex = savebox(boxnumber,attributes,resources,immediate,kind,margin)
80    if trace then
81        report("\\saveboxresource: index %i",lastindex)
82    end
83end
84
85local function lastsavedboxresourceindex()
86    if trace then
87        report("\\lastsaveboxresource: index %i",lastindex)
88    end
89    context("%i",lastindex)
90end
91
92local function useboxresource()
93    local width, height, depth = scanwhd()
94    local index = scaninteger()
95    local node  = usebox(index,width,height,depth)
96    if trace then
97        report("\\useboxresource: index %i",index)
98    end
99    context(node)
100end
101
102implement { name = "saveboxresource",           actions = saveboxresource }
103implement { name = "lastsavedboxresourceindex", actions = lastsavedboxresourceindex }
104implement { name = "useboxresource",            actions = useboxresource }
105
106-- image resources
107
108local imageresources = { }
109local lastindex      = 0
110local lastpages      = 1
111
112local function saveimageresource()
113    local width, height, depth = scanwhd()
114    local page       = 1
115    local immediate  = true
116    local margin     = 0 -- or dimension
117    local attributes = scankeyword("attr") and scanstring() or nil
118    if scankeyword("named") then
119        scanstring() -- ignored
120    elseif scankeyword("page") then
121        page = scaninteger()
122    end
123    local userpassword    = scankeyword("userpassword") and scanstring() or nil
124    local ownerpassword   = scankeyword("ownerpassword") and scanstring() or nil
125    local visiblefilename = scankeyword("visiblefilename") and scanstring() or nil
126    local colorspace      = scankeyword("colorspace") and scaninteger() or nil
127    local pagebox         = scanword() or nil
128    local filename        = scanstring()
129-- pcall
130    context.getfiguredimensions( { filename }, {
131        [constants.userpassword]  = userpassword,
132        [constants.ownerpassword] = ownerpassword,
133        [constants.page]          = page or 1,
134        [constants.size]          = pagebox,
135    })
136    context.relax()
137    lastindex = lastindex + 1
138    lastpages = 1
139    imageresources[lastindex] = {
140        filename = filename,
141        page     = page or 1,
142        size     = pagebox,
143        width    = width,
144        height   = height,
145        depth    = depth,
146        attr     = attributes,
147     -- margin   = margin,
148     }
149end
150
151local function lastsavedimageresourceindex()
152    context("%i",lastindex or 0)
153end
154
155local function lastsavedimageresourcepages()
156    context("%i",lastpages or 0) -- todo
157end
158
159local function useimageresource()
160    local width, height, depth = scanwhd()
161    if scankeyword("keepopen") then
162        -- ignored
163    end
164    local index = scaninteger()
165    local l = imageresources[index]
166    if l then
167        if not (width or height or depth) then
168            width  = l.width
169            height = l.height
170            depth  = l.depth
171        end
172        -- pcall / we could use a dedicated call instead:
173        context.externalfigure( { l.filename }, {
174            [constants.userpassword]  = l.userpassword,
175            [constants.ownerpassword] = l.ownerpassword,
176            [constants.width]         = width and (width .. "sp") or nil,
177            [constants.height]        = height and (height .. "sp") or nil,
178            [constants.page]          = l.page or 1,
179            [constants.size]          = pagebox,
180        })
181        context.relax()
182    else
183        report("no valid image resource %a",index)
184    end
185end
186
187implement { name = "saveimageresource",           actions = saveimageresource }
188implement { name = "lastsavedimageresourceindex", actions = lastsavedimageresourceindex }
189implement { name = "lastsavedimageresourcepages", actions = lastsavedimageresourcepages }
190implement { name = "useimageresource",            actions = useimageresource }
191