Skip to content

Instantly share code, notes, and snippets.

@cdeletre
Created February 15, 2020 21:04
Show Gist options
  • Select an option

  • Save cdeletre/268992209c58d814f6d76294985c0606 to your computer and use it in GitHub Desktop.

Select an option

Save cdeletre/268992209c58d814f6d76294985c0606 to your computer and use it in GitHub Desktop.
SVG to x,y
#!/usr/bin/env python
from sys import argv
MMARK = 0
MLINE = 1
MCURVE = 2
def sint(n):
if n<0:
return 256 + n
else:
return n
print("opening %s" % argv[1])
fsvg = open(argv[1],'r')
fx = open(argv[1].split('.')[0] + '-x.byte','wb+')
fy = open(argv[1].split('.')[0] + '-y.byte','wb+')
line = fsvg.readline()
x = []
y = []
j = -1
while line != "":
line = line.strip()
if len(line) > 0 and line[0] == 'd':
values = line.split('"')[1].split(' ')
i = 0
length = len(values)
while i < length:
if values[i] == 'm':
mode = MMARK
i += 1
xy = values[i].split(',')
x.append(float(xy[0]))
y.append(float(xy[1]))
j += 1
elif values[i] == 'l':
mode = MLINE
elif values[i] == 'c':
mode = MCURVE
elif values[i] == 'z':
xmin = min(x)
ymin = min(y)
xsize = max(x) - xmin
ysize = max(y) - ymin
# calculate ratio to fit scale (0-255)
xratio = 255.0 / (xsize)
yratio = 255.0 / (ysize)
if yratio > xratio:
yratio = xratio
else:
xratio = yratio
# resize, center and convert to signed integer
x = map(lambda v:sint(int((v-xmin)*xratio)-128),x)
y = map(lambda v:sint(int((v-ymin)*yratio)-128),y)
# write
for i,v in enumerate(x):
fx.write(chr(v))
for i,v in enumerate(y):
fy.write(chr(v))
fx.close()
fy.close()
fsvg.close()
print("SVG converted to xyplot (%s plots)" % len(x))
exit()
else:
if mode == MLINE:
xy = values[i].split(',')
x.append(x[j]+float(xy[0]))
y.append(y[j]+float(xy[1]))
j += 1
elif mode == MCURVE:
i += 2
xy = values[i].split(',')
x.append(x[j]+float(xy[0]))
y.append(y[j]+float(xy[1]))
j += 1
i += 1
exit()
line = fsvg.readline()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment