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
|
if not modules then modules = { } end modules ['grph-mem'] = {
version = 1.001,
comment = "companion to grph-inc.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
-- very experimental and likely to change
--
-- \startluacode
-- figures.setmemstream("whatever",io.loaddata("t:/sources/cow.pdf"))
-- \stopluacode
--
-- \externalfigure[memstream:///t:/sources/cow.pdf]
-- \externalfigure[memstream:///whatever]
local gsub = string.gsub
local report = logs.reporter("memstream")
local trace = false trackers.register ("graphics.memstreams", function(v) trace = v end)
local data = { }
if pdfe then
local function setmemstream(name,stream,once)
if once and data[name] then
if trace then
report("not overloading %a",name) --
end
return data[name]
end
local kind = figures.guessfromstring(stream)
local identifier = false
if kind == "pdf" then
identifier = pdfe.new(stream,#stream,name)
if not identifier then
report("no valid pdf stream %a",name)
elseif trace then
report("setting %a with identifier %a",name,identifier)
end
else
identifier = "m_k_i_v_memstream_" .. name .. "." .. kind
io.savedata(identifier,stream)
end
if not identifier then
identifier = "invalid-memstream"
end
data[name] = identifier
return identifier
end
resolvers.setmemstream = setmemstream
function resolvers.finders.memstream(specification)
local name = specification.path
local identifier = data[name]
if identifier then
if trace then
report("reusing %a with identifier %a",name,identifier)
end
return identifier
end
local stream = io.loaddata(name)
if stream and stream ~= "" then
return setmemstream(name,stream)
end
if trace then
report("no valid memstream %a",name)
end
return resolvers.finders.notfound()
end
local flush = { }
function resolvers.resetmemstream(name,afterpage)
if afterpage then
flush[#flush+1] = name
end
end
luatex.registerpageactions(function()
if #flush > 0 then
for i=1,#flush do
local identifier = data[name]
if identifier then
os.remove(identifier)
data[name] = nil
end
end
flush = { }
end
end)
else
local opened = { }
local function setmemstream(name,stream,once)
if once and data[name] then
if trace then
report("not overloading %a",name) --
end
return data[name]
end
local memstream, identifier = epdf.openMemStream(stream,#stream,name)
if not identifier then
report("no valid stream %a",name)
identifier = "invalid-memstream"
elseif trace then
report("setting %a with identifier %a",name,identifier)
end
data [name] = identifier
opened[name] = memstream
return identifier
end
resolvers.setmemstream = setmemstream
function resolvers.finders.memstream(specification)
local name = specification.path
local identifier = data[name]
if identifier then
if trace then
report("reusing %a with identifier %a",name,identifier)
end
return identifier
end
local stream = io.loaddata(name)
if not stream or stream == "" then
if trace then
report("no valid file %a",name)
end
return resolvers.finders.notfound()
else
return setmemstream(name,stream)
end
end
local flush = { }
function resolvers.resetmemstream(name,afterpage)
if afterpage then
flush[#flush+1] = name
else
opened[name] = nil
end
end
luatex.registerpageactions(function()
if #flush > 0 then
for i=1,#flush do
opened[flush[i]] = nil -- we keep of course data[name] because of reuse
end
flush = { }
end
end)
end
figures.identifiers.list[#figures.identifiers.list+1] = function(specification)
local name = specification.request.name
if name then
local base = gsub(name,"^memstream:///","")
if base ~= name then
local identifier = data[base]
if identifier then
if trace then
report("requested %a has identifier %s",name,identifier)
end
specification.status.status = 1
specification.used.fullname = name
else
if trace then
report("requested %a is not found",name)
end
end
end
end
end
figures.setmemstream = resolvers.setmemstream
|