-
Notifications
You must be signed in to change notification settings - Fork 32
/
save_svg.py
275 lines (258 loc) · 12.5 KB
/
save_svg.py
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
# import torch
import pydiffvg
import xml.etree.ElementTree as etree
from xml.dom import minidom
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
rough_string = etree.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
def save_svg(filename, width, height, shapes, shape_groups, use_gamma=False):
root = etree.Element('svg')
root.set('version', '1.1')
root.set('xmlns', 'http://www.w3.org/2000/svg')
root.set('width', str(width))
root.set('height', str(height))
defs = etree.SubElement(root, 'defs')
g = etree.SubElement(root, 'g')
if use_gamma:
f = etree.SubElement(defs, 'filter')
f.set('id', 'gamma')
f.set('x', '0')
f.set('y', '0')
f.set('width', '100%')
f.set('height', '100%')
gamma = etree.SubElement(f, 'feComponentTransfer')
gamma.set('color-interpolation-filters', 'sRGB')
feFuncR = etree.SubElement(gamma, 'feFuncR')
feFuncR.set('type', 'gamma')
feFuncR.set('amplitude', str(1))
feFuncR.set('exponent', str(1/2.2))
feFuncG = etree.SubElement(gamma, 'feFuncG')
feFuncG.set('type', 'gamma')
feFuncG.set('amplitude', str(1))
feFuncG.set('exponent', str(1/2.2))
feFuncB = etree.SubElement(gamma, 'feFuncB')
feFuncB.set('type', 'gamma')
feFuncB.set('amplitude', str(1))
feFuncB.set('exponent', str(1/2.2))
feFuncA = etree.SubElement(gamma, 'feFuncA')
feFuncA.set('type', 'gamma')
feFuncA.set('amplitude', str(1))
feFuncA.set('exponent', str(1/2.2))
g.set('style', 'filter:url(#gamma)')
# Store color
for i, shape_group in enumerate(shape_groups):
def add_color(shape_color, name):
if isinstance(shape_color, pydiffvg.LinearGradient):
lg = shape_color
color = etree.SubElement(defs, 'linearGradient')
color.set('id', name)
color.set('x1', str(lg.begin[0].item()))
color.set('y1', str(lg.begin[1].item()))
color.set('x2', str(lg.end[0].item()))
color.set('y2', str(lg.end[1].item()))
offsets = lg.offsets.data.cpu().numpy()
# stop_colors = lg.stop_colors.data.cpu().numpy()
for j in range(offsets.shape[0]):
stop = etree.SubElement(color, 'stop')
stop.set('offset', offsets[j])
c = lg.stop_colors[j, :]
stop.set('stop-color', 'rgb({}, {}, {})'.format(
int(255 * c[0]), int(255 * c[1]), int(255 * c[2])))
stop.set('stop-opacity', '{}'.format(c[3]))
if shape_group.fill_color is not None:
add_color(shape_group.fill_color, 'shape_{}_fill'.format(i))
if shape_group.stroke_color is not None:
add_color(shape_group.stroke_color, 'shape_{}_stroke'.format(i))
for i, shape_group in enumerate(shape_groups):
shape = shapes[shape_group.shape_ids[0]]
if isinstance(shape, pydiffvg.Circle):
shape_node = etree.SubElement(g, 'circle')
shape_node.set('r', shape.radius.item())
shape_node.set('cx', shape.center[0].item())
shape_node.set('cy', shape.center[1].item())
elif isinstance(shape, pydiffvg.Polygon):
shape_node = etree.SubElement(g, 'polygon')
points = shape.points.data.cpu().numpy()
path_str = ''
for j in range(0, shape.points.shape[0]):
path_str += '{} {}'.format(points[j, 0], points[j, 1])
if j != shape.points.shape[0] - 1:
path_str += ' '
shape_node.set('points', path_str)
elif isinstance(shape, pydiffvg.Path):
shape_node = etree.SubElement(g, 'path')
num_segments = shape.num_control_points.shape[0]
num_control_points = shape.num_control_points.data.cpu().numpy()
points = shape.points.data.cpu().numpy()
num_points = shape.points.shape[0]
path_str = ' M {} {}'.format(points[0, 0], points[0, 1])
point_id = 1
for j in range(0, num_segments):
if num_control_points[j] == 0:
p = point_id % num_points
path_str += ' L {} {}'.format(
points[p, 0], points[p, 1])
point_id += 1
elif num_control_points[j] == 1:
p1 = (point_id + 1) % num_points
path_str += ' Q {} {} {} {}'.format(
points[point_id, 0], points[point_id, 1],
points[p1, 0], points[p1, 1])
point_id += 2
elif num_control_points[j] == 2:
p2 = (point_id + 2) % num_points
path_str += ' C {} {} {} {} {} {}'.format(
points[point_id, 0], points[point_id, 1],
points[point_id + 1, 0], points[point_id + 1, 1],
points[p2, 0], points[p2, 1])
point_id += 3
shape_node.set('d', path_str)
elif isinstance(shape, pydiffvg.Rect):
shape_node = etree.SubElement(g, 'rect')
shape_node.set('x', shape.p_min[0].item())
shape_node.set('y', shape.p_min[1].item())
shape_node.set('width', shape.p_max[0].item() - shape.p_min[0].item())
shape_node.set('height', shape.p_max[1].item() - shape.p_min[1].item())
else:
assert(False)
shape_node.set('stroke-width', str(2 * shape.stroke_width.data.cpu().item()))
if shape_group.fill_color is not None:
if isinstance(shape_group.fill_color, pydiffvg.LinearGradient):
shape_node.set('fill', 'url(#shape_{}_fill)'.format(i))
else:
c = shape_group.fill_color.data.cpu().numpy()
shape_node.set('fill', 'rgb({}, {}, {})'.format(
int(255 * c[0]), int(255 * c[1]), int(255 * c[2])))
shape_node.set('opacity', str(c[3]))
else:
shape_node.set('fill', 'none')
if shape_group.stroke_color is not None:
if isinstance(shape_group.stroke_color, pydiffvg.LinearGradient):
shape_node.set('stroke', 'url(#shape_{}_stroke)'.format(i))
else:
c = shape_group.stroke_color.data.cpu().numpy()
shape_node.set('stroke', 'rgb({}, {}, {})'.format(
int(255 * c[0]), int(255 * c[1]), int(255 * c[2])))
shape_node.set('stroke-opacity', str(c[3]))
shape_node.set('stroke-linecap', 'round')
shape_node.set('stroke-linejoin', 'round')
with open(filename, "w") as f:
f.write(prettify(root))
def save_svg_paths_only(filename, width, height, shapes, shape_groups, use_gamma=False):
root = etree.Element('svg')
root.set('version', '1.1')
root.set('xmlns', 'http://www.w3.org/2000/svg')
root.set('width', str(width))
root.set('height', str(height))
defs = etree.SubElement(root, 'defs')
g = etree.SubElement(root, 'g')
if use_gamma:
f = etree.SubElement(defs, 'filter')
f.set('id', 'gamma')
f.set('x', '0')
f.set('y', '0')
f.set('width', '100%')
f.set('height', '100%')
gamma = etree.SubElement(f, 'feComponentTransfer')
gamma.set('color-interpolation-filters', 'sRGB')
feFuncR = etree.SubElement(gamma, 'feFuncR')
feFuncR.set('type', 'gamma')
feFuncR.set('amplitude', str(1))
feFuncR.set('exponent', str(1/2.2))
feFuncG = etree.SubElement(gamma, 'feFuncG')
feFuncG.set('type', 'gamma')
feFuncG.set('amplitude', str(1))
feFuncG.set('exponent', str(1/2.2))
feFuncB = etree.SubElement(gamma, 'feFuncB')
feFuncB.set('type', 'gamma')
feFuncB.set('amplitude', str(1))
feFuncB.set('exponent', str(1/2.2))
feFuncA = etree.SubElement(gamma, 'feFuncA')
feFuncA.set('type', 'gamma')
feFuncA.set('amplitude', str(1))
feFuncA.set('exponent', str(1/2.2))
g.set('style', 'filter:url(#gamma)')
# Store color
for i, shape_group in enumerate(shape_groups):
def add_color(shape_color, name):
if isinstance(shape_color, pydiffvg.LinearGradient):
lg = shape_color
color = etree.SubElement(defs, 'linearGradient')
color.set('id', name)
color.set('x1', str(lg.begin[0].item()))
color.set('y1', str(lg.begin[1].item()))
color.set('x2', str(lg.end[0].item()))
color.set('y2', str(lg.end[1].item()))
offsets = lg.offsets.data.cpu().numpy()
# stop_colors = lg.stop_colors.data.cpu().numpy()
for j in range(offsets.shape[0]):
stop = etree.SubElement(color, 'stop')
stop.set('offset', offsets[j])
c = lg.stop_colors[j, :]
stop.set('stop-color', 'rgb({}, {}, {})'.format(
int(255 * c[0]), int(255 * c[1]), int(255 * c[2])))
stop.set('stop-opacity', '{}'.format(c[3]))
if shape_group.fill_color is not None:
add_color(shape_group.fill_color, 'shape_{}_fill'.format(i))
if shape_group.stroke_color is not None:
add_color(shape_group.stroke_color, 'shape_{}_stroke'.format(i))
for i, shape_group in enumerate(shape_groups):
shape_node = etree.SubElement(g, 'path')
path_str = ''
for shape_id in shape_group.shape_ids:
shape = shapes[shape_group.shape_ids[shape_id]]
if isinstance(shape, pydiffvg.Path):
num_segments = shape.num_control_points.shape[0]
num_control_points = shape.num_control_points.data.cpu().numpy()
points = shape.points.data.cpu().numpy()
num_points = shape.points.shape[0]
path_str += 'M {} {}'.format(points[0, 0], points[0, 1])
point_id = 1
for j in range(0, num_segments):
if num_control_points[j] == 0:
p = point_id % num_points
path_str += ' L {} {}'.format(
points[p, 0], points[p, 1])
point_id += 1
elif num_control_points[j] == 1:
p1 = (point_id + 1) % num_points
path_str += ' Q {} {} {} {}'.format(
points[point_id, 0], points[point_id, 1],
points[p1, 0], points[p1, 1])
point_id += 2
elif num_control_points[j] == 2:
p2 = (point_id + 2) % num_points
path_str += ' C {} {} {} {} {} {}'.format(
points[point_id, 0], points[point_id, 1],
points[point_id + 1, 0], points[point_id + 1, 1],
points[p2, 0], points[p2, 1])
point_id += 3
else:
assert(False)
shape_node.set('d', path_str)
shape_node.set('stroke-width', str(2 * shape.stroke_width.data.cpu().item()))
if shape_group.fill_color is not None:
if isinstance(shape_group.fill_color, pydiffvg.LinearGradient):
shape_node.set('fill', 'url(#shape_{}_fill)'.format(i))
else:
c = shape_group.fill_color.data.cpu().numpy()
shape_node.set('fill', 'rgb({}, {}, {})'.format(
int(255 * c[0]), int(255 * c[1]), int(255 * c[2])))
shape_node.set('opacity', str(c[3]))
else:
shape_node.set('fill', 'none')
if shape_group.stroke_color is not None:
if isinstance(shape_group.stroke_color, pydiffvg.LinearGradient):
shape_node.set('stroke', 'url(#shape_{}_stroke)'.format(i))
else:
c = shape_group.stroke_color.data.cpu().numpy()
shape_node.set('stroke', 'rgb({}, {}, {})'.format(
int(255 * c[0]), int(255 * c[1]), int(255 * c[2])))
shape_node.set('stroke-opacity', str(c[3]))
shape_node.set('stroke-linecap', 'round')
shape_node.set('stroke-linejoin', 'round')
with open(filename, "w") as f:
f.write(prettify(root))