Add unit tests to reduce to shorthand curve path segments. Updated test script

This commit is contained in:
JSCHILL1 2009-08-05 17:05:27 -05:00
parent 94a1e28a57
commit 61a7265ecf
4 changed files with 30 additions and 1 deletions

View file

@ -65,6 +65,8 @@
# - prevent elements from being stripped if they are referenced in a <style> element # - prevent elements from being stripped if they are referenced in a <style> element
# (for instance, filter, marker, pattern) - need a crude CSS parser # (for instance, filter, marker, pattern) - need a crude CSS parser
# - Remove any unused glyphs from font elements? # - Remove any unused glyphs from font elements?
# - Convert path segment c into s where possible: http://www.w3.org/TR/SVG11/paths.html#PathDataCubicBezierCommands
# - Convert path segment q into t where possible: http://www.w3.org/TR/SVG11/paths.html#PathDataQuadraticBezierCommands
# necessary to get true division # necessary to get true division
from __future__ import division from __future__ import division
@ -1500,9 +1502,10 @@ def cleanPath(element) :
newPath.append( (prevCmd, prevData) ) newPath.append( (prevCmd, prevData) )
path = newPath path = newPath
# convert line segments into h,v where possible # convert to shorthand path segments where possible
newPath = [path[0]] newPath = [path[0]]
for (cmd,data) in path[1:]: for (cmd,data) in path[1:]:
# convert line segments into h,v where possible
if cmd == 'l': if cmd == 'l':
i = 0 i = 0
lineTuples = [] lineTuples = []
@ -1529,6 +1532,12 @@ def cleanPath(element) :
i += 2 i += 2
if lineTuples: if lineTuples:
newPath.append( ('l', lineTuples) ) newPath.append( ('l', lineTuples) )
# convert Bézier curve segments into s where possible
elif cmd == 'c':
newPath.append( (cmd, data) )
# convert quadratic curve segments into t where possible
elif cmd == 'q':
newPath.append( (cmd, data) )
else: else:
newPath.append( (cmd, data) ) newPath.append( (cmd, data) )
path = newPath path = newPath

View file

@ -548,6 +548,18 @@ class ChangeLineToVerticalLineSegmentInPath(unittest.TestCase):
self.assertEquals(path[2][1][0], 100.0, self.assertEquals(path[2][1][0], 100.0,
'Did not calculate vertical line segment in path correctly' ) 'Did not calculate vertical line segment in path correctly' )
class ChangeBezierToShorthandInPath(unittest.TestCase):
def runTest(self):
path = scour.scourXmlFile('unittests/path-bez-optimize.svg').getElementsByTagNameNS(SVGNS, 'path')[0]
self.assertEquals(path.getAttribute('d'), 'm10,100c50-50,50,50,100,0s50,50,100,0',
'Did not change bezier curves into shorthand curve segments in path')
class ChangeQuadToShorthandInPath(unittest.TestCase):
def runTest(self):
path = scour.scourXmlFile('unittests/path-quad-optimize.svg').getElementsByTagNameNS(SVGNS, 'path')[0]
self.assertEquals(path.getAttribute('d'), 'm10,100q50-50,100,0t100,0',
'Did not change quadratic curves into shorthand curve segments in path')
class HandleNonAsciiUtf8(unittest.TestCase): class HandleNonAsciiUtf8(unittest.TestCase):
def runTest(self): def runTest(self):
doc = scour.scourXmlFile('unittests/utf8.svg') doc = scour.scourXmlFile('unittests/utf8.svg')

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg version="1.1" viewBox="0 0 400 400" xmlns="http://www.w3.org/2000/svg">
<path d="m10,100c50-50,50,50,100,0,50-50,50,50,100,0" fill="none" stroke="blue" stroke-width="5"/>
</svg>

After

Width:  |  Height:  |  Size: 239 B

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg version="1.1" viewBox="0 0 400 400" xmlns="http://www.w3.org/2000/svg">
<path d="m10,100q50-50,100,0,50,50,100,0" fill="none" stroke="blue" stroke-width="5"/>
</svg>

After

Width:  |  Height:  |  Size: 227 B