Debian Codegraph @

texmf-dist/tex/luatex/minim/minim-pdfresources.lua

raw
1
2 local M = { }
3
4 local alloc = require 'minim-alloc'
5 local cb = require 'minim-callbacks'
6
7 -- the standard table design
8 local function init_resources()
9 return {
10 ExtGState = { },
11 ColorSpace = { },
12 Pattern = { },
13 Shading = { },
14 }
15 end
16
17 -- the module will use optex’s functions if present
18 M.self_destruct = pdf.add_page_resource or false
19
20 -- resource_data is a table that can contain any data. only the following keys
21 -- are used by this module:
22 --
23 -- _entry_ internal value; do not use
24 -- value value in the resource dictionary
25 -- write() called for writing the resource to the pdf; returns value
26 --
27 -- of the last two, only one needs be present; if the value is missing it will
28 -- be set at first use to the result of write().
29 --
30 local resource_data = init_resources() -- name ↦ { [write = ... | value = ...], ... }
31 --
32 function M.add_resource(kind, name, resource)
33 resource_data[kind][name] = resource
34 end
35 function M.get_resource(kind, name)
36 return resource_data[kind][name]
37 end
38 function M.use_resource(kind, name)
39 local res = resource_data[kind][name]
40 if not res then
41 return alloc.err('Unknown %s resource: %s', kind, name)
42 end
43 res.value = res.value or res:write()
44 if not res._entry_ then -- first-use
45 res._entry_ = string.format('%s %s', alloc.pdf_name(name), res.value)
46 if M.self_destruct then
47 M.self_destruct(kind, name, res.value)
48 end
49 end
50 return res._entry_
51 end
52
53 -- global resources are mainly for pgf compatibility: it contains entries to
54 -- the resource dictionaries that will be added for every page.
55 --
56 local global_resources = init_resources() -- name ↦ '/Key <value>'
57 --
58 function M.add_global_resource(kind, name, value)
59 local r = global_resources[kind]
60 if M.self_destruct then
61 M.self_destruct(kind, name, value)
62 elseif name == nil then -- used for pgf compatibility
63 if value ~= '' then
64 r[''] = r[''] and string.format('%s %s', global_resources[kind][''], value) or value
65 end
66 else
67 r[name] = string.format('%s %s', alloc.pdf_name(name), value)
68 end
69 end
70
71 -- we can now provide optex’s interface
72 if not M.self_destruct then
73 pdf.add_page_resource = M.add_global_resource
74 end
75
76 -- for nonglobal resources, every use must be marked with a late_lua node. from
77 -- those, the M.use_resouce() function will be called automatically.
78 --
79 local page_resources = init_resources() -- name ↦ '/Key <value>'
80 -- global, for use in latelua
81 function _with_pdf_resource_(kind, name) --luacheck: ignore 111
82 page_resources[kind][name] = M.use_resource(kind, name)
83 end
84 function M.use_resource_node(kind, name)
85 local n = node.new('whatsit', 'late_lua')
86 n.data = string.format('_with_pdf_resource_("%s", "%s")', kind, name)
87 return n
88 end
89
90 alloc.luadef('withpdfresource', function()
91 node.write(M.use_resource_node(token.scan_string(), token.scan_string()))
92 end, 'protected')
93
94 -- construction and caching of resource dictionaries.
95 --
96 local previous_dicts = init_resources() -- pdf dict ↦ objnum
97 --
98 local function make_resource_entry(kind)
99 local arr = { }
100 for _, entry in pairs(page_resources[kind]) do table.insert(arr, entry) end
101 for _, entry in pairs(global_resources[kind]) do table.insert(arr, entry) end
102 if #arr == 0 then return false end
103 table.sort(arr)
104 arr[0] = '<<'; table.insert(arr, '>>')
105 local dict = table.concat(arr, ' ', 0)
106 local objnum = previous_dicts[kind][dict]
107 if not objnum then
108 objnum = pdf.obj(dict)
109 pdf.refobj(objnum)
110 previous_dicts[kind][dict] = objnum
111 end
112 return string.format('/%s %d 0 R', kind, objnum)
113 end
114
115 local removal_regex = {
116 ExtGState = ' */ExtGState *%d+ +0 *R',
117 ColorSpace = ' */ColorSpace *%d+ +0 *R',
118 Pattern = ' */Pattern *%d+ +0 *R',
119 Shading = ' */Shading *%d+ +0 *R' }
120
121 local function update_page_resources(shippingout)
122 if M.self_destruct then return end
123 local resources = shippingout and pdf.getpageresources() or pdf.getxformresources() or ''
124 for kind, _ in pairs(page_resources) do
125 resources = string.gsub(resources, removal_regex[kind], '')
126 local entry = make_resource_entry(kind)
127 if entry then
128 resources = resources == '' and entry or string.format('%s %s', resources, entry)
129 end
130 end
131 -- update page resources
132 if shippingout then
133 pdf.setpageresources(resources)
134 else
135 pdf.setxformresources(resources)
136 end
137 page_resources = init_resources()
138 end
139
140 cb.register('finish_pdfpage', update_page_resources)
141
142 -- for pgf compatibility, reroute the pgf resource registration functions to
143 -- our global resource pool.
144 --
145 alloc.luadef('minim:pgf:fix:luaside', function()
146 alloc.luadef('pgf@sys@addpdfresource@extgs@plain', function()
147 M.add_global_resource('ExtGState', nil, token.scan_string())
148 end)
149 alloc.luadef('pgf@sys@addpdfresource@patterns@plain', function()
150 M.add_global_resource('Pattern', nil, token.scan_string())
151 end)
152 alloc.luadef('pgf@sys@addpdfresource@colorspaces@plain', function()
153 M.add_global_resource('ColorSpace', nil, token.scan_string())
154 end)
155 end)
156
157 return M
158