Add unittests for collapsing of straight path segments

This commit is contained in:
Eduard Braun 2017-05-17 22:07:04 +02:00
parent 8d7220c222
commit 9712b71b86
3 changed files with 66 additions and 24 deletions

View file

@ -1224,28 +1224,44 @@ class RemoveFontStylesFromNonTextShapes(unittest.TestCase):
'font-size not removed from rect') 'font-size not removed from rect')
class CollapseConsecutiveHLinesSegments(unittest.TestCase): class CollapseStraightPathSegments(unittest.TestCase):
def runTest(self): def runTest(self):
p = scourXmlFile('unittests/consecutive-hlines.svg').getElementsByTagNameNS(SVGNS, 'path')[0] doc = scourXmlFile('unittests/collapse-straight-path-segments.svg', parse_args(['--disable-style-to-xml']))
self.assertEqual(p.getAttribute('d'), 'm100 100h200v100h-200z', paths = doc.getElementsByTagNameNS(SVGNS, 'path')
'Did not collapse consecutive hlines segments') path_data = [path.getAttribute('d') for path in paths]
path_data_expected = ['m0 0h30',
'm0 0v30',
'm0 0h10.5v10.5',
'm0 0h10-1v10-1',
'm0 0h30',
'm0 0h30',
'm0 0h10 20',
'm0 0h10 20',
'm0 0h10 20',
'm0 0h10 20',
'm0 0 20 40',
'm0 0 10 10-20-20 10 10-20-20',
'm0 0 1 2m1 2 2 4m1 2 2 4',
'm6.3228 7.1547 81.198 45.258']
self.assertEqual(path_data[0:3], path_data_expected[0:3],
'Did not collapse h/v commands into a single h/v commands')
self.assertEqual(path_data[3], path_data_expected[3],
'Collapsed h/v commands with different direction')
self.assertEqual(path_data[4:6], path_data_expected[4:6],
'Did not collapse h/v commands with only start/end markers present')
self.assertEqual(path_data[6:10], path_data_expected[6:10],
'Did not preserve h/v commands with intermediate markers present')
class CollapseConsecutiveHLinesCoords(unittest.TestCase): self.assertEqual(path_data[10], path_data_expected[10],
'Did not collapse lineto commands into a single (implicit) lineto command')
def runTest(self): self.assertEqual(path_data[11], path_data_expected[11],
p = scourXmlFile('unittests/consecutive-hlines.svg').getElementsByTagNameNS(SVGNS, 'path')[1] 'Collapsed lineto commands with different direction')
self.assertEqual(p.getAttribute('d'), 'm100 300h200v100h-200z', self.assertEqual(path_data[12], path_data_expected[12],
'Did not collapse consecutive hlines coordinates') 'Collapsed first parameter pair of a moveto subpath')
self.assertEqual(path_data[13], path_data_expected[13],
'Did not collapse the nodes of a straight real world path')
class DoNotCollapseConsecutiveHLinesSegsWithDifferingSigns(unittest.TestCase):
def runTest(self):
p = scourXmlFile('unittests/consecutive-hlines.svg').getElementsByTagNameNS(SVGNS, 'path')[2]
self.assertEqual(p.getAttribute('d'), 'm100 500h300-100v100h-200z',
'Collapsed consecutive hlines segments with differing signs')
class ConvertStraightCurvesToLines(unittest.TestCase): class ConvertStraightCurvesToLines(unittest.TestCase):

View file

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<defs>
<marker id="dot">
<circle r="5px"/>
</marker>
</defs>
<!-- h/v commands should be collapsed into a single h/v commands -->
<path d="m0 0h10 20"/>
<path d="m0 0v10 20"/>
<path d="m0 0h10 0.5v10 0.5"/>
<!-- h/v commands should not be collapsed if they have different direction -->
<path d="m0 0h10 -1v10 -1"/>
<!-- h/v commands should also be collapsed if only start/end markers are present -->
<path d="m0 0h10 20" marker-start="url(#dot)" marker-end="url(#dot)"/>
<path d="m0 0h10 20" style="marker-start:url(#dot);marker-end:url(#dot)"/>
<!-- h/v commands should be preserved if intermediate markers are present -->
<path d="m0 0h10 20" marker="url(#dot)"/>
<path d="m0 0h10 20" marker-mid="url(#dot)"/>
<path d="m0 0h10 20" style="marker:url(#dot)"/>
<path d="m0 0h10 20" style="marker-mid:url(#dot)"/>
<!-- all lineto commands should be collapsed into a single (implicit if possible) lineto command -->
<path d="m 0 0 l 10 20 0.25 0.5 l 0.75 1.5 l 5 10 0.2 0.4 l 3 6 0.8 1.6"/>
<!-- must not be collapsed (same slope, but different direction) -->
<path d="m 0 0 10 10 -20 -20 l 10 10 -20 -20"/>
<!-- first parameter pair of a moveto subpath must not be collapsed as it's not drawn on canvas -->
<path d="m0 0 1 2 m 1 2 1 2l 1 2 m 1 2 1 2 1 2"/>
<!-- real world example of straight path with multiple nodes -->
<path d="m 6.3227953,7.1547422 10.6709787,5.9477588 9.20334,5.129731 22.977448,12.807101 30.447251,16.970601 7.898986,4.402712"/>
</svg>

After

Width:  |  Height:  |  Size: 1.6 KiB

View file

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg">
<path fill="#F00" stroke="#0F0" d="M100,100h100h100v100h-200z"/>
<path fill="#F00" stroke="#0F0" d="M100,300h100,100v100h-200z"/>
<path fill="#F00" stroke="#0F0" d="M100,500h300h-100v100h-200z"/>
</svg>

Before

Width:  |  Height:  |  Size: 299 B