-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathprocess.lua
executable file
·296 lines (277 loc) · 8.99 KB
/
process.lua
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/local/bin/luapp
-- Stroke-to-fill conversion program and test harness
-- Copyright (C) 2020 Diego Nehab
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as published
-- by the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- Contact information: diego.nehab@gmail.com
--
local chronos = require"chronos"
local total = chronos.chronos()
local util = require"util"
local quiet = false
local stroker = nil
local function stderr(...)
if not quiet then
io.stderr:write(string.format(...))
end
end
-- print help and exit
local function help()
io.stderr:write([=[
Usage:
lua process.lua [options] <driver> [<input.rvg> [<output-name>]]
where options are:
-profile:<output> write profiling info to <output>
-stroker:<method> transform strokes to fills using <method>
-stroker-repeats:<n> repeat stroking <n> times
-accelerate-repeats:<n> repeat acceleration <n> times
-render-repeats:<n> repeat rendering <n> times
-width:<number> set viewport width (and height proportionally if not set)
-height:<number> set viewport height (and width proportionally if not set)
]=])
os.exit()
end
local width, height
local drivername, inputname, outputname, profilename
local strokerrepeats = 1
local renderrepeats = 1
local acceleraterepeats = 1
-- list of supported options
-- in each option,
-- first entry is the pattern to match
-- second entry is a callback
-- if callback returns true, the option is accepted.
-- if callback returns false, the option is rejected.
local options = {
{ "^%-help$", function(w)
if w then
help()
return true
else
return false
end
end },
{ "^%-quiet$", function(d)
if not d then return false end
quiet = true;
return true
end },
{ "^%-stroker%:(.*)$", function(o)
if not o or #o < 1 then return false end
strokername = o
return true
end },
{ "^%-profile%:(.*)$", function(o)
if not o or #o < 1 then return false end
profilename = o
return true
end },
{ "^(%-width%:(%d*)(.*))$", function(all, n, e)
if not n then return false end
assert(e == "", "invalid option " .. all)
n = assert(tonumber(n), "invalid option " .. all)
assert(n >= 1, "invalid option " .. all)
width = math.floor(n)
return true
end },
{ "^(%-stroker%-repeats%:(%d*)(.*))$", function(all, n, e)
if not n then return false end
assert(e == "", "invalid option " .. all)
n = assert(tonumber(n), "invalid option " .. all)
assert(n >= 1, "invalid option " .. all)
strokerrepeats = math.floor(n)
return true
end },
{ "^(%-render%-repeats%:(%d*)(.*))$", function(all, n, e)
if not n then return false end
assert(e == "", "invalid option " .. all)
n = assert(tonumber(n), "invalid option " .. all)
assert(n >= 1, "invalid option " .. all)
renderrepeats = math.floor(n)
return true
end },
{ "^(%-accel%-repeats%:(%d*)(.*))$", function(all, n, e)
if not n then return false end
assert(e == "", "invalid option " .. all)
n = assert(tonumber(n), "invalid option " .. all)
assert(n >= 1, "invalid option " .. all)
acceleraterepeats = math.floor(n)
return true
end },
{ "^(%-height%:(%d+)(.*))$", function(all, n, e)
if not n then return false end
assert(e == "", "invalid option " .. all)
n = assert(tonumber(n), "invalid option " .. all)
assert(n >= 1, "invalid option " .. all)
height = math.floor(n)
return true
end },
}
-- rejected options are passed to driver
local rejected = {}
local nrejected = 0
-- value do not start with -
local values = {}
local nvalues = 0
-- go over command-line arguments
-- processes recognized options
-- collect unrecognized ones into rejected list,
-- collect values into another list
for i, argument in ipairs({...}) do
if argument:sub(1,1) == "-" then
local recognized = false
for j, option in ipairs(options) do
if option[2](argument:match(option[1])) then
recognized = true
break
end
end
if not recognized then
nrejected = nrejected + 1
rejected[nrejected] = argument
end
else
nvalues = nvalues + 1
values[nvalues] = argument
end
end
drivername = values[1]
inputname = values[2]
outputname = values[3]
-- load driver
assert(drivername, "missing <driver> argument")
if not package.searchpath(drivername, package.cpath) and
not package.searchpath(drivername, package.path) then
drivername = "driver." .. drivername
end
local driver = require(drivername)
assert(type(driver) == "table", "invalid driver")
if strokername == "list" then
for i,v in pairs(driver.stroke_method) do
if i ~= "native" then
print(i)
end
end
os.exit()
end
-- load and run the Lua program that defines the scene, window, and viewport
-- the only globals visible are the ones exported by the
stderr("processing %s\n", inputname)
local time = chronos.chronos()
local input
if _VERSION == "Lua 5.1" then
input = assert(setfenv(assert(loadfile(inputname)), driver)())
else
input = assert(assert(loadfile(inputname, "bt", driver))())
end
stderr("loaded in %gs\n", time:elapsed())
-- by default, dump to stadard out
local output = io.stdout
-- if another argument was given, replace with the open file
if outputname then
output = assert(io.open(outputname, "wb"))
end
-- print options that will be passed down to driver
if #rejected > 0 then
stderr("options passed down to driver\n")
for i,v in ipairs(rejected) do
stderr(" %s\n", v)
end
end
-- update viewport if width or height were given
local viewport = input.viewport
local vxmin, vymin, vxmax, vymax = table.unpack(viewport)
local vwidth = vxmax-vxmin
local vheight = vymax-vymin
if width or height then
vxmin = 0
vymin = 0
end
if width and not height then
assert(vwidth > 0, "empty viewport")
vheight = math.floor(vheight*width/vwidth+0.5)
assert(vheight > 0, "empty viewport")
vwidth = width
end
if height and not width then
assert(vheight > 0, "empty viewport")
vwidth = math.floor(vwidth*height/vheight+0.5)
assert(vwidth > 0, "empty viewport")
vheight = height
end
if height and width then
vwidth = width
vheight = height
end
viewport = driver.viewport(vxmin, vymin, vxmin+vwidth, vymin+vheight)
if strokername and strokername ~= "native" then
local filter = require"filter"
local method = driver.stroke_method[strokername]
assert(method, "stroker not available")
if profilename then
stderr("profiling stroker...\n")
util.profiler_start("stroker-" .. profilename)
end
local stroked
time:reset()
for i = 1, strokerrepeats do
stderr("mock stroker pass %d\n", i)
stroked = driver.scene_data()
input.scene:get_scene_data():iterate(filter.make_scene_f_stroke(method, true, stroked))
end
local mocktime = time:elapsed()/strokerrepeats
time:reset()
for i = 1, strokerrepeats do
stderr("stroker pass %d\n", i)
stroked = driver.scene_data()
input.scene:get_scene_data():iterate(filter.make_scene_f_stroke(method, stroked))
end
stderr("stroke in %gs\n", time:elapsed()/strokerrepeats - mocktime)
if profilename then
util.profiler_stop()
end
input.scene = driver.scene(stroked):transformed(input.scene:get_xf())
end
-- invoke driver-defined accelerate() function on scene
-- pass rejected options as last argument
if profilename then
stderr("profiling accelerate...\n")
util.profiler_start("accelerate-" .. profilename)
end
time:reset()
local accel
for i = 1, acceleraterepeats do
stderr("accelerate pass %d\n", i)
accel = driver.accelerate(input.scene, input.window, viewport, rejected)
end
stderr("accelerate in %gs\n", time:elapsed()/acceleraterepeats)
if profilename then
util.profiler_stop()
end
-- invoke driver-defined render() on result of accelerate()
-- pass rejected options as last argument
if profilename then
stderr("profiling render...\n")
util.profiler_start("render-" .. profilename)
end
time:reset()
for i = 1, renderrepeats do
stderr("render pass %d\n", i)
driver.render(accel, input.window, viewport, output, rejected)
end
stderr("render in %gs\n", time:elapsed()/renderrepeats)
if profilename then
util.profiler_stop()
end
-- close output file if we created it
if outputname then output:close() end
stderr("done in %gs\n", total:elapsed())