grph-mem.lua /size: 3250 b    last modification: 2020-07-01 14:35
1if 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
18local gsub = string.gsub
19
20local report = logs.reporter("memstream")
21local trace  = false  trackers.register  ("graphics.memstreams", function(v) trace = v end)
22local data   = { }
23
24local 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
49end
50
51resolvers.setmemstream = setmemstream
52
53function 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()
70end
71
72local flush = { }
73
74function resolvers.resetmemstream(name,afterpage)
75    if afterpage then
76        flush[#flush+1] = name
77    end
78end
79
80luatex.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
91end)
92
93figures.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
112end
113
114figures.setmemstream = resolvers.setmemstream
115