Skip to content

Commit fc59b6d

Browse files
committed
Made parsing more robust
1 parent 458f808 commit fc59b6d

File tree

3 files changed

+65
-21
lines changed

3 files changed

+65
-21
lines changed

‎py2d/SVG.py‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ def transform_element(e, transform):
3434
m = re.match(r'translate\((?P<x>[0-9.-]+),(?P<y>[0-9.-]+)\)', new_t)
3535
if m:
3636
x, y = float(m.group("x")), float(m.group("y"))
37-
nt = Transform.move(x,y) * transform
37+
nt = Transform.move(x,y) * nt
38+
39+
m = re.match(r'matrix\((?P<a>[0-9.-]+),(?P<b>[0-9.-]+),(?P<c>[0-9.-]+),(?P<d>[0-9.-]+),(?P<e>[0-9.-]+),(?P<f>[0-9.-]+)\)', new_t)
40+
if m:
41+
a,b,c,d,e,f = ( float(m.group(l)) for l in "abcdef" )
42+
43+
import pdb; pdb.set_trace()
44+
nt = Transform([[ a, c, e ],
45+
[ b, d, f ],
46+
[ 0, 0, 1 ]]) * nt
3847

3948
return nt
4049

@@ -89,13 +98,13 @@ def parse_vec(s):
8998

9099
#print "cmd: %s, pars: %s" % (cmd, pars)
91100

92-
if cmd == "m":
101+
if cmd == "m" or cmd == "l":
93102
for p in pars:
94103
relative_pos += parse_vec(p)
95104
verts.append(relative_pos)
96105

97106

98-
elif cmd == "M":
107+
elif cmd == "M" or cmd == "L":
99108
for p in pars:
100109
relative_pos = parse_vec(p)
101110
verts.append(relative_pos)
@@ -119,6 +128,25 @@ def parse_vec(s):
119128

120129
verts.extend(bez)
121130

131+
elif cmd == "s" or cmd == "S":
132+
# shorthand / smooth cubic polybezier
133+
134+
for i in range(0, len(pars), 2):
135+
c2, b = parse_vec(pars[i]), parse_vec(pars[i+1])
136+
c1 = relative_pos + (relative_pos - last_control)
137+
138+
if cmd == "s":
139+
# convert to relative
140+
c2 += relative_pos
141+
b += relative_pos
142+
143+
bez = flatten_cubic_bezier(relative_pos, b, c1, c2, bezier_max_divisions, bezier_max_flatness)
144+
145+
last_control = c2
146+
relative_pos = b
147+
148+
verts.extend(bez)
149+
122150
elif cmd == "z":
123151
# close line by only moving relative_pos to first vertex
124152

@@ -131,7 +159,9 @@ def parse_vec(s):
131159
warnings.warn("Unrecognized SVG path command: %s - path skipped" % cmd)
132160
polys = []
133161
break
134-
162+
163+
if verts:
164+
polys.append(transform * Polygon.from_pointlist(verts))
135165
#print "----"
136166

137167
return id, polys

‎py2d/examples/SVG.py‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ def __init__(self, runner):
2626

2727
self.poly_colors = [ 0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff, 0x00ffff ]
2828

29-
print self.polys
30-
31-
3229
def render(self):
3330

3431
t = Transform.unit() #Transform.move(-22,-700)

‎py2d/examples/shapes.svg‎

Lines changed: 31 additions & 14 deletions
Loading

0 commit comments

Comments
 (0)