back-res.lua /size: 6676 b    last modification: 2020-07-01 14:35
1
if
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 9
local
context
=
context
10 11
local
trace
=
false
trackers
.
register
(
"
backend
"
,
function
(
v
)
trace
=
v
end
)
12
local
report
=
logs
.
reporter
(
"
backend
"
)
13 14
local
scanners
=
tokens
.
scanners
15
local
scankeyword
=
scanners
.
keyword
16
local
scaninteger
=
scanners
.
integer
17
local
scanstring
=
scanners
.
string
18
local
scandimension
=
scanners
.
dimension
19
local
scanword
=
scanners
.
word
20
local
scanwhd
=
scanners
.
whd
21 22
local
implement
=
interfaces
.
implement
23
local
constants
=
interfaces
.
constants
24
local
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 30
local
tex_saveboxresource
=
tex
.
saveboxresource
31
local
tex_useboxresource
=
tex
.
useboxresource
32
local
tex_getboxresourcebox
=
tex
.
getboxresourcebox
33
local
tex_getboxresourcedimensions
=
tex
.
getboxresourcedimensions
34 35
updaters
.
register
(
"
backend.update
"
,
function
(
)
36
tex_saveboxresource
=
tex
.
saveboxresource
37
tex_useboxresource
=
tex
.
useboxresource
38
tex_getboxresourcebox
=
tex
.
getboxresourcebox
39
tex_getboxresourcedimensions
=
tex
.
getboxresourcedimensions
40
end
)
41 42
local
savebox
=
function
(
...
)
return
tex_saveboxresource
(
...
)
end
43
local
usebox
=
function
(
...
)
return
tex_useboxresource
(
...
)
end
44
local
getbox
=
function
(
...
)
return
tex_getboxresourcebox
(
...
)
end
45
local
getwhd
=
function
(
...
)
return
tex_getboxresourcedimensions
(
...
)
end
46 47
local
boxresources
=
{
48
save
=
savebox
,
49
use
=
usebox
,
50
getbox
=
getbox
,
51
getdimensions
=
getwhd
,
52
}
53 54
tex
.
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 69
local
lastindex
=
0
70 71
local
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
83
end
84 85
local
function
lastsavedboxresourceindex
(
)
86
if
trace
then
87
report
(
"
\\lastsaveboxresource: index %i
"
,
lastindex
)
88
end
89
context
(
"
%i
"
,
lastindex
)
90
end
91 92
local
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
)
100
end
101 102
implement
{
name
=
"
saveboxresource
"
,
actions
=
saveboxresource
}
103
implement
{
name
=
"
lastsavedboxresourceindex
"
,
actions
=
lastsavedboxresourceindex
}
104
implement
{
name
=
"
useboxresource
"
,
actions
=
useboxresource
}
105 106
-- image resources
107 108
local
imageresources
=
{
}
109
local
lastindex
=
0
110
local
lastpages
=
1
111 112
local
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
}
149
end
150 151
local
function
lastsavedimageresourceindex
(
)
152
context
(
"
%i
"
,
lastindex
or
0
)
153
end
154 155
local
function
lastsavedimageresourcepages
(
)
156
context
(
"
%i
"
,
lastpages
or
0
)
-- todo
157
end
158 159
local
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
185
end
186 187
implement
{
name
=
"
saveimageresource
"
,
actions
=
saveimageresource
}
188
implement
{
name
=
"
lastsavedimageresourceindex
"
,
actions
=
lastsavedimageresourceindex
}
189
implement
{
name
=
"
lastsavedimageresourcepages
"
,
actions
=
lastsavedimageresourcepages
}
190
implement
{
name
=
"
useimageresource
"
,
actions
=
useimageresource
}
191