Unit test added for transformations.

Transformation bugs fixed.
pull/20/merge^2
alphanoob1337 2017-04-09 16:13:15 +02:00
parent 8542afb77c
commit 3a2cd2c7a0
3 changed files with 108 additions and 12 deletions

View File

@ -104,18 +104,19 @@ def svg2paths(svg_file_location,
y = values[1] if (len(values) > 1) else 0.
return [x, 0., 0., y, 0., 0.]
elif 'rotate' in trafo_str:
a = values[0]
a = values[0]*np.pi/180.
x = values[1] if (len(values) > 1) else 0.
y = values[2] if (len(values) > 2) else 0.
am = np.dot(np.array([np.cos(a), np.sin(a), -np.sin(a), np.cos(a), 0., 0., 0., 0., 1.]).reshape((3, 3)),
np.array([1., 0., 0., 1., -x, -y, 0., 0., 1.]).reshape((3, 3)))
am = list(np.dot(np.array([1., 0., 0., 1., x, y, 0., 0., 1.]).reshape((3, 3)), am).reshape((9, ))[:6])
am = np.dot(np.array([np.cos(a), -np.sin(a), 0., np.sin(a), np.cos(a), 0., 0., 0., 1.]).reshape((3, 3)),
np.array([1., 0., -x, 0., 1., -y, 0., 0., 1.]).reshape((3, 3)))
am = list(np.dot(np.array([1., 0., x, 0., 1., y, 0., 0., 1.]).reshape((3, 3)), am).reshape((9, ))[:6])
am = am[::3]+am[1::3]+am[2::3]
return am
elif 'skewX' in trafo_str:
a = values[0]
a = values[0]*np.pi/180.
return [1., 0., np.tan(a), 1., 0., 0.]
elif 'skewY' in trafo_str:
a = values[0]
a = values[0]*np.pi/180.
return [1., np.tan(a), 0., 1., 0., 0.]
else:
while len(values) < 6:
@ -166,23 +167,23 @@ def svg2paths(svg_file_location,
# Path found; parsing it
path = dom2dict(node)
d_string = path['d']
return [parse_path(d_string)]+ret_list, [path]+attribute_dictionary_list
return [parse_path(d_string)]+ret_list, [path]+attribute_dictionary_list_int
elif convert_polylines_to_paths and node.nodeName == 'polyline':
attrs = dom2dict(node)
path = parse_path(polyline2pathd(node['points']))
return [path]+ret_list, [attrs]+attribute_dictionary_list
return [path]+ret_list, [attrs]+attribute_dictionary_list_int
elif convert_polygons_to_paths and node.nodeName == 'polygon':
attrs = dom2dict(node)
path = parse_path(polygon2pathd(node['points']))
return [path]+ret_list, [attrs]+attribute_dictionary_list
path = parse_path(polygon2pathd(attrs['points']))
return [path]+ret_list, [attrs]+attribute_dictionary_list_int
elif convert_lines_to_paths and node.nodeName == 'line':
line = dom2dict(node)
d_string = ('M' + line['x1'] + ' ' + line['y1'] +
'L' + line['x2'] + ' ' + line['y2'])
path = parse_path(d_string)
return [path]+ret_list, [line]+attribute_dictionary_list
return [path]+ret_list, [line]+attribute_dictionary_list_int
else:
return ret_list, attribute_dictionary_list
return ret_list, attribute_dictionary_list_int
path_list, attribute_dictionary_list = parse_node(doc)
if return_svg_attributes:

81
test/groups.svg Normal file
View File

@ -0,0 +1,81 @@
<?xml version="1.0" ?>
<svg id="rootn" version="1.1" baseProfile="full" width="1100" height="200" xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink">
<path
id="p0"
style="stroke:#ff0000;stroke-width:2."
d="m 50.,100. 100.,0." />
<g id="translate" transform="translate(100.,200.)">
<path
id="p1"
style="stroke:#ff0000;stroke-width:2."
d="m 0.,-150. 0,100." />
</g>
<path
id="p2"
style="stroke:#ff0000;stroke-width:2."
d="m 200.,100. 100.,0." />
<g id="scale" transform="scale(1.,10.)">
<path
id="p3"
style="stroke:#ff0000;stroke-width:.20"
d="m 250.,5. 0,10." />
</g>
<path
id="p4"
style="stroke:#ff0000;stroke-width:2."
d="m 350.,100. 100.,0." />
<g id="rotate" transform="rotate(-90.,50.,0.)">
<path
id="p5"
style="stroke:#ff0000;stroke-width:2."
d="m -100.,350. 100.,0." />
</g>
<path
id="p6"
style="stroke:#ff0000;stroke-width:2."
d="m 500.,100. 100.,0." />
<g id="skewX" transform="skewX(-45.)">
<path
id="p7"
style="stroke:#ff0000;stroke-width:2."
d="m 600.,50. 100.,100." />
</g>
<g id="skewY" transform="skewY(-45.)">
<path
id="p8"
style="stroke:#ff0000;stroke-width:2."
d="m 650.,750. 100.,100." />
</g>
<path
id="p9"
style="stroke:#ff0000;stroke-width:2."
d="m 700.,50. 0.,100." />
<path
id="p10"
style="stroke:#ff0000;stroke-width:2."
d="m 800.,100. 100.,0." />
<g id="matrix" transform="matrix(0.,-1.,1.,0.,850.,150.)">
<path
id="p11"
style="stroke:#ff0000;stroke-width:2."
d="m 0.,0. 100.,0." />
</g>
<path
id="p12"
style="stroke:#ff0000;stroke-width:2."
d="m 950.,100. 100.,0." />
<g id="translatenested" transform="translate(1000.,150.)">
<g id="rotatenested" transform="rotate(-90.)">
<path
id="p13"
style="stroke:#ff0000;stroke-width:2."
d="m 0.,0. 100.,0." />
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

@ -0,0 +1,14 @@
from __future__ import division, absolute_import, print_function
from unittest import TestCase
from svgpathtools import *
from os.path import join, dirname
class TestSvg2pathsGroups(TestCase):
def test_svg2paths(self):
paths, _ = svg2paths(join(dirname(__file__), 'groups.svg'))
# the paths should form crosses after being transformed
self.assertTrue((len(paths) % 2) == 0)
for i in range(len(paths)//2):
self.assertTrue(len(paths[i * 2].intersect(paths[i * 2 + 1])) > 0, 'Path '+str(i * 2)+' does not intersect path '+str(i * 2 + 1)+'!')