1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-12-01 13:50:30 +00:00
8bitworkshop/tools/svg2vector.py

72 lines
2.8 KiB
Python
Raw Normal View History

2017-03-31 17:25:18 +00:00
#!/usr/bin/python
import sys
from xml.dom import minidom
def textToFloat(name):
if name:
return float(name)
else:
return None
def textToColor(name,opacity):
if name is None or not name[0] == '#':
return None
color = int(name[1:],16) << 8
if opacity:
2018-11-22 19:11:56 +00:00
print(('opacity',opacity))
2017-03-31 17:25:18 +00:00
return color
svg_file = sys.argv[1]
doc = minidom.parse(svg_file) # parseString also exists
groups = doc.getElementsByTagName('g')
for grp in groups:
groupID = grp.getAttribute('id')
paths = grp.getElementsByTagName('path')
2018-11-22 19:11:56 +00:00
print((groupID,paths))
2017-03-31 17:25:18 +00:00
if len(paths):
for path in paths:
shape = {}
d = path.getAttribute('d')
styleAttrs = {}
style = path.getAttribute('style')
sarr = style.split(';')
for s in sarr:
nvarr = s.split(':', 2)
if len(nvarr) == 2:
styleAttrs[nvarr[0]] = nvarr[1]
2018-11-22 19:11:56 +00:00
print((path,d,styleAttrs))
2017-03-31 17:25:18 +00:00
shape['pts'] = []
shape['currentColor'] = textToColor(styleAttrs.get("stroke"), styleAttrs.get("stroke-opacity"))
shape['currentWidth'] = textToFloat(styleAttrs.get("strokeWidth"))
hidden = "none" == styleAttrs.get("display")
cmds = d.split(' ')
x = 0
y = 0
i0 = 0
start = True
for cmd in cmds:
2018-11-22 19:11:56 +00:00
print(cmd)
2017-03-31 17:25:18 +00:00
ch = cmd[0]
if ch == 'm' or ch == 'l':
start = True
elif ch == 'z':
origin = shape['pts'][i0]
shape.lineTo(origin.x, origin.y)
elif ch == 'c':
skip = True
else:
xy = cmd.split(',')
2018-11-22 19:11:56 +00:00
print(xy)
2017-03-31 17:25:18 +00:00
x += float(xy[0])
y -= float(xy[1])
if start or hidden:
shape.moveTo(x,y)
else:
shape.lineTo(x,y)
start = False
doc.unlink()