-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsvg_tools.py
432 lines (412 loc) · 15.1 KB
/
svg_tools.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
"""
svg_to_paths()
Can parse basic SVG's and generate coordinate paths
to fill them appropriately. It generates simple
[[[[c_coord_1],[c_coord_2]], [c_path_2,... ] ... ] , ... ]
cymk plotter paths.
Those paths can be made for example in using https://picsvg.com
"""
import os, re
from math import sqrt, pow, cos, sin, pi
import copy, pickle, random
from xml.dom import minidom
import numpy as np
from scipy import interpolate
import imageio
from PIL import Image
import svgwrite
from lineifiers import *
def scale_xform(X,scl = 1.0):
return [scl*X[0], scl*X[1]]
def ident_xform(X):
return [X[0],X[1]]
def parse_transform(t):
if 'scale' in t and not 'translate' in t:
scl = float(t.replace('scale(','').replace(')',''))
tore = lambda X: scale_xform(X, scl = scl)
elif 'scale' in t and 'translate' in t:
tokens = re.split('\(|\)|,| ', t)
scalei = tokens.index('scale')
ss = (float(tokens[scalei+1]),float(tokens[scalei+2]))
scalei = tokens.index('translate')
tt = (float(tokens[scalei+1]),float(tokens[scalei+2]))
return lambda X: [(X[0]+tt[0])*ss[0],(X[1]+tt[1])*ss[1]]
elif 'matrix' in t:
meles = t.replace('matrix(','').replace(')','').replace(',',' ').split(' ')
a,b,c,d,e,f = list(map(float,meles))
return lambda X: [a*X[0]+c*X[1]+e, b*X[0]+d*X[1]+f]
else:
tore = ident_xform
return tore
def parse_fill(S):
"""
Returns a CYMK (0,1) color intensity
"""
fi = S.index('#')
hexes = S[fi+1:fi+1+6]
R = eval('0x'+hexes[:2])
G = eval('0x'+hexes[2:4])
B = eval('0x'+hexes[4:])
c = 1. - R/255
m = 1. - G/255
y = 1. - B/255
min_cmy = min(c,m,y)
if (R==0 and G==0 and B==0):
return 0., 0., 0., 1.
else:
k = min_cmy
c = (c - min_cmy) / (1. - min_cmy+1e-9)
m = (m - min_cmy) / (1. - min_cmy+1e-9)
y = (y - min_cmy) / (1. - min_cmy+1e-9)
return c,m,y,k
def parse_group_into_lines(g, lines, x_form_ = ident_xform,
fill_style ='outline', fill_color=[0,0,0,0],
bezier_steps=20):
if ('transform' in g.attributes):
XF = parse_transform(g.attributes['transform'].value)
x_form = lambda X: XF(x_form_(X))
else:
x_form = x_form_
if ('style' in g.attributes and not fill_style is 'outline'):
style = g.attributes['style'].value
if (style.count('fill:none')>0):
fill_style = 'outline'
elif ('style' in g.attributes and not fill_style is 'outline'):
fill_color = parse_fill(style)
fill_style = 'hatch'
elif ('fill' in g.attributes and not fill_style is 'outline'):
fill_color = parse_fill(g.attributes['fill'].value)
fill_style = 'hatch'
try:
for child in g.childNodes:
if (child.nodeName=='g'):
parse_group_into_lines(child, lines, x_form,
fill_style=fill_style,
bezier_steps=bezier_steps,
fill_color=fill_color)
elif (child.nodeName=='path'):
parse_path_into_lines(child, lines, x_form,
fill_style=fill_style,
bezier_steps=bezier_steps,
fill_color=fill_color)
except Exception as Ex:
print(fill_style)
raise Ex
def path_bounds(path):
A = np.array(path)
if (len(A.shape) != 2):
print(A.shape)
raise Exception("Bad Path")
if (A.shape[1]!=2):
print(A.shape)
raise Exception("Bad Path")
return A.min(0).tolist()+A.max(0).tolist()
def paths_bounds(paths):
L = [path_bounds(X) for X in paths if len(X)>=2]
A = np.array(L)
return A[:,:2].min(0).tolist()+A[:,2:].max(0).tolist()
def interior_hatches(a_path, ys):
tore = []
for y in ys:
xs = []
for I,s in enumerate(a_path[:-1]):
s2 = a_path[I+1]
YS = sorted([s[1],s2[1]])
if y >= YS[0] and y <= YS[1]:
rise = (s2[1]-s[1])
run = (s2[0]-s[0])
if (rise == 0):
continue
if (run == 0):
xs.append(s[0])
continue
slope = rise/run
int = s[1] - slope*s[0]
x = (y-int)/slope
xs.append(x)
# Now sort the x's
SX = sorted(xs)
if (len(SX)%2==1):
print("WARNING ODD INT")
NS = len(SX)//2
for I in range(NS):
tore.append([[SX[2*I],y],[SX[2*I+1],y]])
return tore
def interior_hatches_paths(paths, ys):
tore = []
for y in ys:
xs = []
for a_path in paths:
for I,s in enumerate(a_path[:-1]):
s2 = a_path[I+1]
YS = sorted([s[1],s2[1]])
if y >= YS[0] and y <= YS[1]:
rise = (s2[1]-s[1])
run = (s2[0]-s[0])
if (rise == 0):
continue
if (run == 0):
xs.append(s[0])
continue
slope = rise/run
int = s[1] - slope*s[0]
x = (y-int)/slope
xs.append(x)
# Now sort the x's
SX = sorted(xs)
if (len(SX)%2==1):
print("WARNING ODD INT")
raise Exception('odd int')
NS = len(SX)//2
for I in range(NS):
tore.append([[SX[2*I],y],[SX[2*I+1],y]])
return tore
def hatch_paths_within_path(a_path, cymk, linewidth=2., slope = 0.):
"""
Creates horiz hatches in a curve.
Args:
a_path: a list of coordinate tuples
cymk: sequence-like of c,m,y,k 0-1 weights.
Returns:
[c_paths, ... ]
which you could append to cymk to get the desired hatching.
"""
xmin, ymin, xmax, ymax = path_bounds(a_path)
ydist = ymax - ymin
nc = ydist*cymk[0]/linewidth
ny = ydist*cymk[1]/linewidth
nm = ydist*cymk[2]/linewidth
nk = ydist*cymk[3]/linewidth
cys = np.linspace(ymin,ymax,nc)
chs = interior_hatches(a_path, cys)
yys = np.linspace(ymin,ymax,ny)
yhs = interior_hatches(a_path, yys)
mys = np.linspace(ymin,ymax,nm)
mhs = interior_hatches(a_path, mys)
kys = np.linspace(ymin,ymax,nk)
khs = interior_hatches(a_path, kys)
return [chs,yhs,mhs,khs]
def hatch_paths_within_paths(paths, cymk, linewidth=4., slope = 0.):
"""
Creates horiz hatches in a curve.
Args:
a_path: a list of coordinate tuples
cymk: sequence-like of c,m,y,k 0-1 weights.
Returns:
[c_paths, ... ]
which you could append to cymk to get the desired hatching.
"""
if len(paths)<1:
return [[],[],[],[]]
xmin, ymin, xmax, ymax = paths_bounds(paths)
ydist = ymax - ymin
nc = ydist*cymk[0]/linewidth
ny = ydist*cymk[1]/linewidth
nm = ydist*cymk[2]/linewidth
nk = ydist*cymk[3]/linewidth
cys = np.linspace(ymin,ymax,nc)
chs = interior_hatches_paths(paths, cys)
yys = np.linspace(ymin,ymax,ny)
yhs = interior_hatches_paths(paths, yys)
mys = np.linspace(ymin,ymax,nm)
mhs = interior_hatches_paths(paths, mys)
kys = np.linspace(ymin,ymax,nk)
khs = interior_hatches_paths(paths, kys)
return [chs,yhs,mhs,khs]
def parse_path_into_lines(a_path_, lines, x_form_ = ident_xform,
fill_style ='outline', fill_color=[0,0,0,0],
X=0 , Y=0, bezier_steps = 20 ):
"""
Parses most of the commands found in an SVG path.
"""
# Check for any xform of the path
if(not 'd' in a_path_.attributes):
return
else:
a_path = a_path_.attributes['d'].value
# Get any required transformations.
if ('style' in a_path_.attributes):
fill_color = parse_fill(a_path_.attributes['style'].value)
fill_style = 'hatch'
else:
fill_color=[0.,0.,0.,1.]
if ('transform' in a_path_.attributes):
XF = parse_transform(a_path_.attributes['transform'].value)
x_form = lambda X: XF(x_form_(X))
else:
x_form = x_form_
X0, Y0 = X,Y
# TODO deal with contiguous tokens.
# insert spaces after any char if needed.
p = re.compile("([mcMClLzcChHvV ,])")
tokens=[X for X in p.split(a_path) if len(X)>0 and not X in [' ',',']]
out_paths = []
current_path = []
token = None
while (len(tokens)>0):
token = tokens.pop(0)
#TODO parse E
if (token == 'm'):
if (len(current_path)>0):
out_paths.append(copy.copy(current_path))
current_path = []
while (len(tokens)>0):
if tokens[0].isalpha():
break
X += float(tokens.pop(0))
Y += float(tokens.pop(0))
X0,Y0 = X,Y
continue
elif (token == 'M'):
if (len(current_path)>0):
out_paths.append(copy.copy(current_path))
current_path = []
while (len(tokens)>0):
if tokens[0].isalpha():
break
X = float(tokens.pop(0))
Y = float(tokens.pop(0))
X0,Y0 = X,Y
continue
elif (token == 'l'):
while (len(tokens)>0):
if tokens[0].isalpha():
break
# Simple line
current_path.append(x_form([X,Y]))
X += float(tokens.pop(0))
Y += float(tokens.pop(0))
current_path.append(x_form([X,Y]))
elif (token == 'L'):
while (len(tokens)>0):
if tokens[0].isalpha():
break
# Simple line
current_path.append(x_form([X,Y]))
X = float(tokens.pop(0))
Y = float(tokens.pop(0))
current_path.append(x_form([X,Y]))
elif (token == 'z'):
# close the contour
current_path.append(x_form([X,Y]))
current_path.append(x_form([X0,Y0]))
X,Y = X0, Y0
if (len(current_path)>0):
out_paths.append(copy.copy(current_path))
current_path = []
elif (token == 'h'):
current_path.append(x_form([X,Y]))
while (len(tokens)>0):
if tokens[0].isalpha():
break
X += float(tokens.pop(0))
current_path.append(x_form([X,Y]))
elif (token == 'H'):
current_path.append(x_form([X,Y]))
while (len(tokens)>0):
if tokens[0].isalpha():
break
X = float(tokens.pop(0))
current_path.append(x_form([X,Y]))
elif (token == 'v'):
current_path.append(x_form([X,Y]))
while (len(tokens)>0):
if tokens[0].isalpha():
break
Y += float(tokens.pop(0))
current_path.append(x_form([X,Y]))
elif (token == 'V'):
current_path.append(x_form([X,Y]))
while (len(tokens)>0):
if tokens[0].isalpha():
break
Y = float(tokens.pop(0))
current_path.append(x_form([X,Y]))
elif (token == 'C'):
try:
while (len(tokens)>0):
if tokens[0].isalpha():
break
# Simple line
current_path.append(x_form([X,Y]))
p0x = float(tokens.pop(0))
p0y = float(tokens.pop(0))
C1x = float(tokens.pop(0))
C1y = float(tokens.pop(0))
C2x = X = float(tokens.pop(0))
C2y = Y = float(tokens.pop(0))
for t in [1.*X/bezier_steps for X in range(1,bezier_steps+1)]:
omt = 1.-t
Xp = pow(omt,3)*p0x + 3*pow(omt,2.)*t*C1x + 3*pow(t,2.)*omt*C2x+pow(t,3)*X
Yp = pow(omt,3)*p0y + 3*pow(omt,2.)*t*C1y + 3*pow(t,2.)*omt*C2y+pow(t,3)*Y
current_path.append(x_form([Xp,Yp]))
except Exception as Ex:
print(token)
print(tokens)
raise Ex
elif (token == 'c'):
try:
while (len(tokens)>0):
if tokens[0].isalpha():
break
# Simple line
current_path.append(x_form([X,Y]))
p0x = X
p0y = Y
C1x = X + float(tokens.pop(0))
C1y = Y + float(tokens.pop(0))
C2x = X + float(tokens.pop(0))
C2y = Y + float(tokens.pop(0))
X += float(tokens.pop(0))
Y += float(tokens.pop(0))
for t in [1.*X/bezier_steps for X in range(1,bezier_steps+1)]:
omt = 1.-t
Xp = pow(omt,3)*p0x + 3*pow(omt,2.)*t*C1x + 3*pow(t,2.)*omt*C2x+pow(t,3)*X
Yp = pow(omt,3)*p0y + 3*pow(omt,2.)*t*C1y + 3*pow(t,2.)*omt*C2y+pow(t,3)*Y
current_path.append(x_form([Xp,Yp]))
except Exception as Ex:
print(token)
print(tokens)
raise Ex
else:
print('BAD TOKEN', token)
raise Exception('Bad Token')
if (len(current_path)>0):
out_paths.append(copy.copy(current_path))
if (fill_style=='hatch'):
c_hs, y_hs, m_hs, k_hs = hatch_paths_within_paths(out_paths, fill_color)
lines[0].extend(c_hs)
lines[1].extend(y_hs)
lines[2].extend(m_hs)
lines[3].extend(k_hs)
if (fill_color[-1] > .99):
lines[-1].extend(out_paths)
else:
return lines[-1].extend(out_paths)
def svg_to_paths(filename = 'drawing.svg', fill_style = 'outline',
bezier_steps=10):
"""
Args:
filename: an svg filename
fill_style: 'outline', None
Returns:
[cpaths, ypaths... ]
where cpaths is a list of lists of coordinate pairs [[[x,y]]]
"""
tore = [[],[],[],[]] # Cymk lines.
xmldoc = minidom.parse(filename)
itemlist = xmldoc.getElementsByTagName('path')
print("Converting", len(itemlist),"paths")
lines = [[],[],[],[]]
for child in xmldoc.childNodes[0].childNodes:
if (child.nodeName=='g'):
parse_group_into_lines(child, lines, x_form_ = ident_xform,
bezier_steps=bezier_steps,
fill_style =fill_style,
fill_color=[0,0,0,0])
elif (child.nodeName=='path'):
parse_path_into_lines(child, lines, x_form_ = ident_xform,
bezier_steps=bezier_steps,
fill_style =fill_style,
fill_color=[0,0,0,0])
return lines