grph-mem.lua /size: 3250 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
grph-mem
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to grph-inc.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
-- very experimental and likely to change
10
--
11
-- \startluacode
12
-- figures.setmemstream("whatever",io.loaddata("t:/sources/cow.pdf"))
13
-- \stopluacode
14
--
15
-- \externalfigure[memstream:///t:/sources/cow.pdf]
16
-- \externalfigure[memstream:///whatever]
17 18
local
gsub
=
string
.
gsub
19 20
local
report
=
logs
.
reporter
(
"
memstream
"
)
21
local
trace
=
false
trackers
.
register
(
"
graphics.memstreams
"
,
function
(
v
)
trace
=
v
end
)
22
local
data
=
{
}
23 24
local
function
setmemstream
(
name
,
stream
,
once
)
25
if
once
and
data
[
name
]
then
26
if
trace
then
27
report
(
"
not overloading %a
"
,
name
)
--
28
end
29
return
data
[
name
]
30
end
31
local
kind
=
figures
.
guessfromstring
(
stream
)
32
local
identifier
=
false
33
if
kind
=
=
"
pdf
"
then
34
identifier
=
pdfe
.
new
(
stream
,
#
stream
,
name
)
35
if
not
identifier
then
36
report
(
"
no valid pdf stream %a
"
,
name
)
37
elseif
trace
then
38
report
(
"
setting %a with identifier %a
"
,
name
,
identifier
)
39
end
40
else
41
identifier
=
"
m_k_i_v_memstream_
"
.
.
name
.
.
"
.
"
.
.
kind
42
io
.
savedata
(
identifier
,
stream
)
43
end
44
if
not
identifier
then
45
identifier
=
"
invalid-memstream
"
46
end
47
data
[
name
]
=
identifier
48
return
identifier
49
end
50 51
resolvers
.
setmemstream
=
setmemstream
52 53
function
resolvers
.
finders
.
memstream
(
specification
)
54
local
name
=
specification
.
path
55
local
identifier
=
data
[
name
]
56
if
identifier
then
57
if
trace
then
58
report
(
"
reusing %a with identifier %a
"
,
name
,
identifier
)
59
end
60
return
identifier
61
end
62
local
stream
=
io
.
loaddata
(
name
)
63
if
stream
and
stream
~
=
"
"
then
64
return
setmemstream
(
name
,
stream
)
65
end
66
if
trace
then
67
report
(
"
no valid memstream %a
"
,
name
)
68
end
69
return
resolvers
.
finders
.
notfound
(
)
70
end
71 72
local
flush
=
{
}
73 74
function
resolvers
.
resetmemstream
(
name
,
afterpage
)
75
if
afterpage
then
76
flush
[
#
flush
+
1
]
=
name
77
end
78
end
79 80
luatex
.
registerpageactions
(
function
(
)
81
if
#
flush
>
0
then
82
for
i
=
1
,
#
flush
do
83
local
identifier
=
data
[
name
]
84
if
identifier
then
85
os
.
remove
(
identifier
)
86
data
[
name
]
=
nil
87
end
88
end
89
flush
=
{
}
90
end
91
end
)
92 93
figures
.
identifiers
.
list
[
#
figures
.
identifiers
.
list
+
1
]
=
function
(
specification
)
94
local
name
=
specification
.
request
.
name
95
if
name
then
96
local
base
=
gsub
(
name
,
"
^memstream:///
"
,
"
"
)
97
if
base
~
=
name
then
98
local
identifier
=
data
[
base
]
99
if
identifier
then
100
if
trace
then
101
report
(
"
requested %a has identifier %s
"
,
name
,
identifier
)
102
end
103
specification
.
status
.
status
=
1
104
specification
.
used
.
fullname
=
name
105
else
106
if
trace
then
107
report
(
"
requested %a is not found
"
,
name
)
108
end
109
end
110
end
111
end
112
end
113 114
figures
.
setmemstream
=
resolvers
.
setmemstream
115