-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg2pat.py
executable file
·557 lines (494 loc) · 15.2 KB
/
svg2pat.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#!/usr/bin/env python3
#read in a file in 'svg' format
#write an innova '.pat' format file (lines only)
import argparse, sys, re, xml.parsers.expat
argparser = argparse.ArgumentParser(description="Convert SVG to Innova Autopilot pattern (.pat).")
argparser.add_argument('svgfile', help="input svg file", nargs='?', type=argparse.FileType('rb'), default=sys.stdin.buffer)
argparser.add_argument('patfile', help="output pat file", nargs='?', type=argparse.FileType('w'), default=sys.stdout)
argparser.add_argument('--clip', help="discard path outside svg page [default]", action='store_true', dest='clip')
argparser.add_argument('--no-clip', help="keep path outside svg page", action='store_false', dest='clip')
argparser.set_defaults(clip=True)
args = argparser.parse_args()
#svg page is [0, width]x[0, height]:
width = 1
height = 1
#current transform matrix ( in order: [ a, c, e, b, d, f ] )
transform = [ 1, 0, 0,
0, 1, 0 ]
transform_stack = []
def parse_length(length):
#as per https://www.w3.org/TR/SVG/coords.html#Units
m = re.match(r"^(\d+|\d*.\d+|\d+.\d*)(em|ex|px|pt|pc|cm|mm|in|)$", length)
if m == None:
print("Failed to parse length '" + length + "'.", file=sys.stderr)
sys.exit(1)
l = float(m.group(1))
if m.group(2) == 'em' or m.group(2) == 'ex':
print("Length in em/ex units not supported.", file=sys.stderr)
sys.exit(1)
elif m.group(2) == 'px' or m.group(2) == '':
l *= 1.0 / 90.0
elif m.group(2) == 'pt':
l *= 1.25 / 90.0
elif m.group(2) == 'pc':
l *= 15.0 / 90.0
elif m.group(2) == 'cm':
l *= 35.43307 / 90.0
elif m.group(2) == 'mm':
l *= 3.543307 / 90.0
elif m.group(2) == 'in':
l *= 90.0 / 90.0
else:
assert(False) #"unit not supported"
return l
def multiply_transforms(a, b):
return [
a[0] * b[0] + a[1] * b[3] + 0.0 * a[2],
a[0] * b[1] + a[1] * b[4] + 0.0 * a[2],
a[0] * b[2] + a[1] * b[5] + 1.0 * a[2],
a[3] * b[0] + a[4] * b[3] + 0.0 * a[5],
a[3] * b[1] + a[4] * b[4] + 0.0 * a[5],
a[3] * b[2] + a[4] * b[5] + 1.0 * a[5]
]
def apply_transform(xf, pt):
return (
xf[0] * pt[0] + xf[1] * pt[1] + xf[2],
xf[3] * pt[0] + xf[4] * pt[1] + xf[5]
)
def parse_transform(attrib):
#handle empty transform:
m = re.match(r"^\s*$", attrib)
if m != None:
return [1.0, 0.0, 0.0, 0.0, 1.0, 0.0]
#parse the first transform in the list:
m = re.match(r"^\s*(matrix|translate|scale|rotate|skewX|skewY)\s*\(\s*([\s\d+.,-]+)\s*\)\s*(|[\s,](.*))$", attrib)
if m == None:
print("Failed to parse first transform in '" + attrib + "'", file=sys.stderr)
sys.exit(1)
op = m.group(1)
args = re.split(r"\s*[\s,]\s*", m.group(2))
remain = m.group(4)
#print(op, args, remain, file=sys.stderr) #DEBUG
xf = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0]
if op == 'matrix':
if len(args) != 6:
print("matrix transform requires six arguments, have only ", args, file=sys.stderr)
sys.exit(1)
xf[0] = float(args[0])
xf[1] = float(args[2])
xf[2] = float(args[4])
xf[3] = float(args[1])
xf[4] = float(args[3])
xf[5] = float(args[5])
elif op == 'translate':
if len(args) < 1 or len(args) > 2:
print("translate transform requires one or two arguments, have ", args, file=sys.stderr)
sys.exit(1)
xf[2] = float(args[0])
if len(args) > 1:
xf[5] = float(args[1])
elif op == 'scale':
if len(args) < 1 or len(args) > 2:
print("translate scale requires one or two arguments, have ", args, file=sys.stderr)
sys.exit(1)
xf[0] = float(args[0])
if len(args) > 1:
xf[4] = float(args[1])
else:
xf[4] = float(args[0])
#elif op == 'rotate':
#elif op == 'skewX':
#elif op == 'skewY':
else:
print("Unimplemented transformation '" + op + "'.", file=sys.stderr)
sys.exit(1)
if remain != None:
xf = multiply_transforms(xf, parse_transform(remain))
return xf
#handle_path will fill in paths:
paths = []
def handle_path(data):
#print("Path: " + data, file=sys.stderr)
#print(" transform: " + str(transform), file=sys.stderr)
#helpers:
def trim_wsp():
nonlocal data
#was: data = re.sub("^[\x20\x09\x0D\x0A]*", "", data)
i = 0
while i < len(data) and (data[i] == '\x20' or data[i] == '\x09' or data[i] == '\x0D' or data[i] == '\x0A'):
i += 1
data = data[i:]
def trim_wsp_comma_wsp():
nonlocal data
#was: data = re.sub("^"
# + "[\x20\x09\x0D\x0A]*"
# + "[\x20\x09\x0D\x0A,]?"
# + "[\x20\x09\x0D\x0A]*",
# "", data)
i = 0
while i < len(data) and (data[i] == '\x20' or data[i] == '\x09' or data[i] == '\x0D' or data[i] == '\x0A'):
i += 1
if i < len(data) and data[i] == ',':
i += 1
while i < len(data) and (data[i] == '\x20' or data[i] == '\x09' or data[i] == '\x0D' or data[i] == '\x0A'):
i += 1
data = data[i:]
def read_number():
nonlocal data
m = re.match(r"^([+-]?(?:\d*\.\d+|\d+\.\d*|\d+)(?:[eE][+-]?\d+)?)(.*)", data)
if m == None:
raise ValueError("data does not start with number")
#print("Number:" + m.group(1) + " remain:" + m.group(2), file=sys.stderr) #DEBUG
data = m.group(2)
return float(m.group(1))
def read_coordinate_pair():
x = read_number()
trim_wsp_comma_wsp()
y = read_number()
return [x, y]
#current point (for relative commands):
current = (0.0, 0.0)
prev_xy2 = (0.0, 0.0)
#current path segment:
seg = None
def xf(pt):
return apply_transform(transform, pt)
def moveto(pt):
#print("moveto " + str(pt), file=sys.stderr)
nonlocal current, prev_xy2
nonlocal seg
if seg != None:
paths.append(list(map(xf, seg)))
#print(" path:" + str(paths[-1]))
current = pt
prev_xy2 = pt
seg = [pt]
def lineto(pt):
#print("lineto " + str(pt), file=sys.stderr)
nonlocal current, prev_xy2
nonlocal seg
current = pt
prev_xy2 = pt
seg.append(pt)
def smoothcurveto(xy2, xy):
curveto((2*current[0]-prev_xy2[0], 2*current[1]-prev_xy2[1]),xy2, xy)
def curveto(xy1, xy2, xy):
#print("curveto " + str(xy1) + " " + str(xy2) + " " + str(xy), file=sys.stderr)
nonlocal current, prev_xy2
nonlocal seg
def subcurveto(xy0, xy1, xy2, xy):
xf_xy0 = xf(xy0)
xf_xy1 = xf(xy1)
xf_xy2 = xf(xy2)
xf_xy = xf(xy)
length = 0.0
length += ((xf_xy1[0] - xf_xy0[0]) ** 2 + (xf_xy1[1] - xf_xy0[1]) ** 2) ** 0.5
length += ((xf_xy2[0] - xf_xy1[0]) ** 2 + (xf_xy2[1] - xf_xy1[1]) ** 2) ** 0.5
length += ((xf_xy[0] - xf_xy2[0]) ** 2 + (xf_xy[1] - xf_xy2[1]) ** 2) ** 0.5
if length > 0.125:
m_c1 = (
0.5 * (xy1[0] - xy0[0]) + xy0[0],
0.5 * (xy1[1] - xy0[1]) + xy0[1]
)
m_12 = (
0.5 * (xy2[0] - xy1[0]) + xy1[0],
0.5 * (xy2[1] - xy1[1]) + xy1[1]
)
m_2e = (
0.5 * (xy[0] - xy2[0]) + xy2[0],
0.5 * (xy[1] - xy2[1]) + xy2[1]
)
m_c1_12 = (
0.5 * (m_12[0] - m_c1[0]) + m_c1[0],
0.5 * (m_12[1] - m_c1[1]) + m_c1[1]
)
m_12_2e = (
0.5 * (m_2e[0] - m_12[0]) + m_12[0],
0.5 * (m_2e[1] - m_12[1]) + m_12[1]
)
m = (
0.5 * (m_12_2e[0] - m_c1_12[0]) + m_c1_12[0],
0.5 * (m_12_2e[1] - m_c1_12[1]) + m_c1_12[1]
)
subcurveto(xy0, m_c1, m_c1_12, m)
subcurveto(m, m_12_2e, m_2e, xy)
else:
lineto(xy)
subcurveto(current, xy1, xy2, xy)
current = xy
prev_xy2 = xy2
def closepath():
nonlocal current, prev_xy2
nonlocal seg
if seg != None:
lineto(seg[0])
while True:
#trim leftmost whitespace:
trim_wsp()
if data == "": break
#command is first character:
cmd = data[0]
data = data[1:]
if cmd == 'm' or cmd == 'M' or cmd == 'l' or cmd == 'L':
trim_wsp()
pt = read_coordinate_pair()
if cmd.islower(): pt = (pt[0] + current[0], pt[1] + current[1])
if cmd == 'm' or cmd == 'M': moveto(pt)
else: lineto(pt)
while True:
trim_wsp_comma_wsp()
if data == "": break
try:
pt = read_coordinate_pair()
except ValueError:
break
if cmd.islower(): pt = (pt[0] + current[0], pt[1] + current[1])
lineto(pt)
elif cmd == 'z' or cmd == 'Z':
closepath()
elif cmd == 'h' or cmd == 'H':
trim_wsp()
x = read_number()
if cmd.islower(): x += current[0]
y = current[1]
lineto((x,y))
while True:
trim_wsp_comma_wsp()
if data == "": break
try:
x = read_number()
except ValueError:
break
if cmd.islower(): x += current[0]
lineto((x,y))
elif cmd == 'v' or cmd == 'V':
trim_wsp()
x = current[0]
y = read_number()
if cmd.islower(): y += current[1]
lineto((x,y))
while True:
trim_wsp_comma_wsp()
if data == "": break
try:
y = read_number()
except ValueError:
break
if cmd.islower(): y += current[1]
lineto((x,y))
elif cmd == 's' or cmd == 'S':
trim_wsp()
xy2 = read_coordinate_pair()
trim_wsp_comma_wsp()
xy = read_coordinate_pair()
if cmd.islower():
xy2 = (xy2[0] + current[0], xy2[1] + current[1])
xy = (xy[0] + current[0], xy[1] + current[1])
smoothcurveto(xy2, xy)
while True:
trim_wsp_comma_wsp()
if data == "": break
try:
xy2 = read_coordinate_pair()
trim_wsp_comma_wsp()
xy = read_coordinate_pair()
except ValueError:
break
if cmd.islower():
xy2 = (xy2[0] + current[0], xy2[1] + current[1])
xy = (xy[0] + current[0], xy[1] + current[1])
smoothcurveto(xy2, xy)
elif cmd == 'c' or cmd == 'C':
trim_wsp()
xy1 = read_coordinate_pair()
trim_wsp_comma_wsp()
xy2 = read_coordinate_pair()
trim_wsp_comma_wsp()
xy = read_coordinate_pair()
if cmd.islower():
xy1 = (xy1[0] + current[0], xy1[1] + current[1])
xy2 = (xy2[0] + current[0], xy2[1] + current[1])
xy = (xy[0] + current[0], xy[1] + current[1])
curveto(xy1, xy2, xy)
while True:
trim_wsp_comma_wsp()
if data == "": break
try:
xy1 = read_coordinate_pair()
trim_wsp_comma_wsp()
xy2 = read_coordinate_pair()
trim_wsp_comma_wsp()
xy = read_coordinate_pair()
except ValueError:
break
if cmd.islower():
xy1 = (xy1[0] + current[0], xy1[1] + current[1])
xy2 = (xy2[0] + current[0], xy2[1] + current[1])
xy = (xy[0] + current[0], xy[1] + current[1])
curveto(xy1, xy2, xy)
else:
print("Unhandled command '" + cmd + "' in path data.", file=sys.stderr)
sys.exit(1)
#moveto() will emit path segment if non-empty, so use a last moveto to do that:
moveto((0.0, 0.0))
def start_element(name, attribs):
global transform, transform_stack, width, height
transform_stack.append(transform)
if 'transform' in attribs:
#TODO: do transform
parsed = parse_transform(attribs['transform'])
transform = multiply_transforms(transform, parsed)
if name == 'svg':
if not 'width' in attribs and not 'height' in attribs and 'viewBox' in attribs:
#svgs from illustrator don't have a width or height but do have a viewbox
print("WARNING: your svg has a viewBox but no width or height set. (This happens if you export from Illustrator with 'Responsive' checked in the 'More Options' section.) Estimating size from the viewBox (an inexact science).")
vals = re.split(r"\s*[\s,]\s*", attribs['viewBox'])
assert(len(vals) == 4)
x = float(vals[0])
y = float(vals[1])
w = float(vals[2])
h = float(vals[3])
attribs['width'] = str(w) + 'px'
attribs['height'] = str(h) + 'px'
if 'width' in attribs:
width = parse_length(attribs['width'])
print("Width: " + str(width) + "in", file=sys.stderr)
else:
width = parse_length("1in")
print("No width in svg element, assuming 1in", file=sys.stderr)
if 'height' in attribs:
height = parse_length(attribs['height'])
print("Height: " + str(height) + "in", file=sys.stderr)
else:
height = parse_length("1in")
print("No height in svg element, assuming 1in", file=sys.stderr)
if 'viewBox' in attribs:
#print("viewBox: ", attribs['viewBox'], file=sys.stderr)
vals = re.split(r"\s*[\s,]\s*", attribs['viewBox'])
assert(len(vals) == 4)
x = float(vals[0])
y = float(vals[1])
w = float(vals[2])
h = float(vals[3])
#set transform for viewBox:
transform = [
width / w, 0.0, -width / w * x,
0.0,-height / h, height / h * y + height
]
else:
#transform maps one user unit to one px:
x = 0.0
y = 0.0
w = 90.0 * width
h = 90.0 * height
transform = [
width / w, 0.0, -width / w * x,
0.0, height / h, -height / h * y
]
elif name == 'g':
pass
elif name == 'path':
if 'd' in attribs:
handle_path(attribs['d'])
elif name == 'rect':
x = float(attribs['x'])
y = float(attribs['y'])
#the '* 90.0' converts from inches back into user units:
w = parse_length(attribs['width']) * 90.0
h = parse_length(attribs['height']) * 90.0
handle_path('M' + str(x) + ',' + str(y)
+ ' ' + str(x+w) + ',' + str(y)
+ ' ' + str(x+w) + ',' + str(y+h)
+ ' ' + str(x) + ',' + str(y+h)
+ 'z')
elif name == 'circle':
print("TODO: circle element", file=sys.stderr)
pass
elif name == 'line':
print("TODO: line element", file=sys.stderr)
pass
elif name == 'polyline':
print("WARNING: polyine element handling is experimental; try using a path instead.", file=sys.stderr)
handle_path('M ' + attribs['points'])
pass
#ignore other elements:
else:
pass #print("Unhandled SVG element: '" + name + "'")
def end_element(name):
global transform, transform_stack
transform = transform_stack.pop()
parser = xml.parsers.expat.ParserCreate()
parser.StartElementHandler = start_element
parser.EndElementHandler = end_element
parser.ParseFile(args.svgfile)
#clip paths so that eqn[0] * pt[0] + eqn[1] * pt[1] + eqn[2] >= 0.0:
def clip(paths, eqn):
out = []
for path in paths:
prev = None
prev_val = None
for pt in path:
val = pt[0] * eqn[0] + pt[1] * eqn[1] + eqn[2]
if val >= 0.0:
if prev == None:
out.append([pt])
elif prev_val >= 0.0:
out[-1].append(pt)
else: #prev_val < 0.0
amt = (0.0 - prev_val) / (val - prev_val)
mid = (
amt * (pt[0] - prev[0]) + prev[0],
amt * (pt[1] - prev[1]) + prev[1]
)
out.append([mid, pt])
else: #val < 0.0
if prev_val != None and prev_val >= 0.0:
amt = (0.0 - prev_val) / (val - prev_val)
mid = (
amt * (pt[0] - prev[0]) + prev[0],
amt * (pt[1] - prev[1]) + prev[1]
)
out[-1].append(mid)
prev = pt
prev_val = val
return out
if args.clip:
points = 0
for path in paths:
points += len(path)
print("Before clipping, had " + str(len(paths)) + " paths with " + str(points) + " total points.", file=sys.stderr)
epsilon = 1e-4
paths = clip(paths, (1, 0, epsilon))
paths = clip(paths, (0, 1, epsilon))
paths = clip(paths, (-1, 0, width + epsilon))
paths = clip(paths, (0,-1, height + epsilon))
else:
print("Not clipping paths.", file=sys.stderr)
points = 0
for path in paths:
points += len(path)
print("Have " + str(len(paths)) + " paths with " + str(points) + " total points.", file=sys.stderr)
line = 1
out_paths = 0
out_points = 0
CRLF = '\r\n'
for path in paths:
#remove duplicate coordinates after rounding:
simple = []
for pt in path:
pt = "X{:.3f}Y{:.3f}".format(
round(pt[0] * 1000.0) / 1000.0,
round(pt[1] * 1000.0) / 1000.0
)
if len(simple) == 0 or pt != simple[-1]:
simple.append(pt)
if len(simple) > 1:
out_paths += 1
out_points += len(simple)
for i in range(0, len(simple)):
if i == 0: print("N" + str(line) + "G00" + simple[i], file=args.patfile, end=CRLF)
else: print("N" + str(line) + "G01" + simple[i], file=args.patfile, end=CRLF)
line += 1
print("N" + str(line) + "M02", file=args.patfile, end=CRLF)
line += 1
print("After rounding for output, had " + str(out_paths) + " paths with " + str(out_points) + " total points.", file=sys.stderr)