-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg.lua
297 lines (257 loc) · 8.9 KB
/
svg.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
297
local driver = require"driver"
local style = require"style"
local arc = require"arc"
local xform = require"xform"
local _M = driver.new()
local unpack = table.unpack
local floor = math.floor
require("buffer")
local defs = { data = {}, nradial = 0, nlinear = 0 } -- is a buffer to save definitions
function defs.init(defs)
defs.data = {}
end
function defs.addradial(def, str)
local data = def.data
data[#data + 1] = str
defs.nradial = defs.nradial + 1
end
function defs.addlinear(def, str)
local data = def.data
data[#data + 1] = str
defs.nlinear = defs.nlinear + 1
end
function defs.size(defs,str)
return defs["n"..str]
end
function defs.concat(def)
return table.concat(def.data)
end
require("dump")
local function xformtostring(xform)
local a = xform[1]
local b = xform[1+3]
local c = xform[2]
local d = xform[2+3]
local e = xform[3]
local f = xform[3+3]
local t = ""
-- factor scale and translate
if b == 0 and c == 0 then
if e~= 0 or f ~= 0 then
if f ~= 0 then
t = string.format("translate(%g, %g)", e, f)
else
t = string.format("translate(%g)", e)
end
end
if a == d then
if a ~= 1 then
t = t ~= "" and " " .. t or t
t = t .. string.format(" scale(%g)", a)
end
else
t = t ~= "" and " " .. t or t
t = t .. string.format(" scale(%g, %g)", a, d)
end
else
-- could also try to factor rotate, but not worth it
t = string.format("matrix(%g, %g, %g, %g, %g, %g)", a, b, c, d, e, f)
end
return t
end
local write = {}
local rvgcommand = {
begin_open_contour = "M",
begin_closed_contour = "M",
linear_segment = "L",
quadratic_segment = "Q",
rational_quadratic_segment = "R",
cubic_segment = "C",
end_closed_contour = "Z",
}
function write.path(shape, buffer)
local previous = ""
local s = xformtostring(shape.xf)
ecount = ecount + 1
writeB(buffer, string.format("<path id=\"p%d\" d=\"",ecount))
for i,v in ipairs(shape.instructions) do
local o = shape.offsets[i]
local s = rvgcommand[v]
if s then
if v ~= previous then
writeB(buffer,s, " ")
end
if s == "M" then
writeB(buffer,shape.data[o+1].." "..shape.data[o+2].." ")
elseif s == "L" then
writeB(buffer,shape.data[o+2].." "..shape.data[o+3].." ")
elseif s == "Q" then
writeB(buffer,shape.data[o+2].." "..shape.data[o+3].. " ")
writeB(buffer,shape.data[o+4].." "..shape.data[o+5].. " ")
elseif s == "C" then
writeB(buffer,shape.data[o+2].." "..shape.data[o+3].." ")
writeB(buffer,shape.data[o+4].." "..shape.data[o+5].." ")
writeB(buffer,shape.data[o+6].." "..shape.data[o+7].." ")
elseif s == "R" then
writeB(buffer,shape.data[o+2].." "..shape.data[o+3].." ")
writeB(buffer,shape.data[o+4].." "..shape.data[o+5].." ")
writeB(buffer,shape.data[o+6].." ")
end
previous = v
end
end
writeB(buffer,"\" ")
if s ~= "" then
writeB(buffer,"transform=\""..s.."\" ")
end
end
function write.polygon(shape, file)
local s = xformtostring(shape.xf)
writeB(buffer,string.format("<polygon points=\"%s\" ",table.concat(shape.data, " ")))
if s ~= "" then
writeB(buffer,"transform=\""..s.."\" ")
end
end
function write.triangle(shape, file)
local s = xformtostring(shape.xf)
writeB(buffer,"<polygon ")
ecount = ecount + 1
writeB(buffer, string.format("id=\"p%d\" ",ecount))
writeB(buffer,"points=\"")
writeB(buffer,shape.x1.." " .. shape.y1.." ")
writeB(buffer,shape.x2.." "..shape.y2.." ")
writeB(buffer,shape.x3.." "..shape.y3.."\" ")
if s ~= "" then
writeB(buffer,"transform=\""..s.."\" ")
end
end
function write.circle(shape, buffer)
local s = xformtostring(shape.xf)
writeB(buffer,"<circle " )
ecount = ecount + 1
writeB(buffer, string.format("id=\"p%d\" ",ecount))
writeB(buffer, string.format("cx=\"%g\" ",shape.cx))
writeB(buffer, string.format("cy=\"%g\" ",shape.cy))
writeB(buffer, string.format("r=\"%g\" ",shape.r))
if s ~= "" then
writeB(buffer,"transform=\"" .. s .. "\" ")
end
end
function writecolor(color)
local r, g, b, a = unpack(color)
local color = ""
r = floor(r*255+.5) -- +.5
g = floor(g*255+.5)
b = floor(b*255+.5)
--a = floor(a*255+.5)
if a ~= 255 then
--color = "rgba8(", r, ",", g, ",", b, ",", a, ")")
color = string.format("rgb(%d,%d,%d)",r,g,b)
else
color = string.format("rgb(%d,%d,%d)",r,g,b)
end
return color
end
function write.radialgradient(paint, buffeR)
local s = xformtostring(paint.xf)
local data = paint.data
local ramp = paint.data.ramp
local index = defs:size("radial") + 1
writeB(buffer," fill=\"url(#radial" .. index .. ")\" ")
local focus = paint.data.focus
local radius = paint.data.radius
local center = paint.data.center
local def = string.format("<radialGradient id=\"radial%d\" ",index)
def = def .. "gradientUnits=\"userSpaceOnUse\" "
def = def .. string.format("cx=\"%g\" ",center[1])
def = def .. string.format("cy=\"%g\" ",center[2])
def = def .. string.format("fx=\"%g\" ",focus[1])
def = def .. string.format("fy=\"%g\" ",focus[2])
def = def .. string.format("r=\"%g\" ",radius)
def = def .. string.format("spreadMethod=\"%s\" ",ramp.spread)
def = def .. string.format("gradientTransform=\"%s\">\n", s)
local lim = #ramp - 1
for i = 1,lim,2 do
def = def .. string.format("<stop offset=\"%g\" ",ramp[i])
def = def .. string.format("stop-color=\"%s\" ",writecolor(ramp[i+1]))
def = def .. string.format("stop-opacity=\"%g\"/> \n",ramp[i+1][4]*paint.opacity)
end
def = def .. "</radialGradient>\n"
defs:addradial(def)
end
function write.lineargradient(paint, buffer)
local s = xformtostring(paint.xf)
local data = paint.data
local ramp = paint.data.ramp
local index = defs:size("linear") + 1
writeB(buffer, " fill=\"url(#linear" .. index .. ")\" ")
local def = string.format("<linearGradient id=\"linear%d\" ",index)
def = def .. "gradientUnits=\"userSpaceOnUse\" "
def = def .. string.format("x1=\"%g\" ",data.p1[1])
def = def .. string.format("y1=\"%g\" ",data.p1[2])
def = def .. string.format("x2=\"%g\" ",data.p2[1])
def = def .. string.format("y2=\"%g\" ",data.p2[2])
def = def .. string.format("spreadMethod=\"%s\" ",ramp.spread)
def = def .. string.format("gradientTransform=\"%s\" >\n", s)
local lim = #ramp - 1
for i = 1,lim,2 do
def = def .. string.format("<stop offset=\"%g\" ",ramp[i])
def = def .. string.format("stop-color=\"%s\" ",writecolor(ramp[i+1]))
def = def .. string.format("stop-opacity=\"%g\" /> \n",ramp[i+1][4]*paint.opacity)
end
def = def .. "</linearGradient>\n"
defs:addlinear(def)
end
function write.solid(paint, buffer)
writeB(buffer,"fill=\"")
writeB(buffer,writecolor(paint.data))
writeB(buffer,"\" ")
writeB(buffer,string.format("fill-opacity=\"%g\" ",paint.opacity*paint.data[4]))
end
function write.fill(element, buffer)
write[element.shape.type](element.shape, buffer)
write[element.paint.type](element.paint, buffer)
writeB(buffer," />\n")
end
function write.eofill(element, buffer)
write[element.shape.type](element.shape, buffer)
writeB(buffer," fill-rule=\"evenodd\" ")
write[element.paint.type](element.paint, buffer)
writeB(buffer," />\n")
end
function _M.render(scene, viewport, file)
local vxmin, vymin, vxmax, vymax = unpack(viewport, 1, 4)
defs:init()
buffer = newBuffer()
ecount = 0
writeB(buffer,"<?xml version=\"1.0\" standalone=\"no\"?>\n")
writeB(buffer,"<svg \n")
writeB(buffer," xmlns:xlink=\"http://www.w3.org/1999/xlink\" \n ")
writeB(buffer,"xmlns:dc=\"http://purl.org/dc/elements/1.1/\" \n ")
writeB(buffer,"xmlns:cc=\"http://creativecommons.org/ns#\" \n ")
writeB(buffer,"xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" \n ")
writeB(buffer,"xmlns:svg=\"http://www.w3.org/2000/svg\" \n ")
writeB(buffer,"xmlns=\"http://www.w3.org/2000/svg\" \n ")
writeB(buffer,"version=\"1.1\" \n ")
writeB(buffer," viewBox=\"" .. table.concat(viewport," ") .. "\" ")
writeB(buffer,">\n")
writeB(buffer,"<defs></defs>")
local xf = scene.xf -- alias for scene.xf
-- the body of svg file
local xfs = xform.scale(1,-1)
local xft = xform.translate(0,vymax)
xf = xft*xfs*xf
local s = xformtostring(xf)
writeB(buffer,"<g id=\"main\" transform=\"" .. s .. "\">\n")
for i,element in ipairs(scene.elements) do
local callback = assert(write[element.type],
"no handler for " .. element.type)
callback(element, buffer)
end
writeB(buffer,"</g>\n")
writeB(buffer,"</svg>")
result = table.concat(buffer)
result = result:gsub("<defs></defs>",string.format("<defs>\n%s</defs>\n",defs:concat()))
file:write(result)
end
return _M