Add unit tests to reduce to shorthand curve path segments. Updated test script
This commit is contained in:
parent
94a1e28a57
commit
61a7265ecf
4 changed files with 30 additions and 1 deletions
11
scour.py
11
scour.py
|
|
@ -65,6 +65,8 @@
|
|||
# - prevent elements from being stripped if they are referenced in a <style> element
|
||||
# (for instance, filter, marker, pattern) - need a crude CSS parser
|
||||
# - 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
|
||||
from __future__ import division
|
||||
|
|
@ -1500,9 +1502,10 @@ def cleanPath(element) :
|
|||
newPath.append( (prevCmd, prevData) )
|
||||
path = newPath
|
||||
|
||||
# convert line segments into h,v where possible
|
||||
# convert to shorthand path segments where possible
|
||||
newPath = [path[0]]
|
||||
for (cmd,data) in path[1:]:
|
||||
# convert line segments into h,v where possible
|
||||
if cmd == 'l':
|
||||
i = 0
|
||||
lineTuples = []
|
||||
|
|
@ -1529,6 +1532,12 @@ def cleanPath(element) :
|
|||
i += 2
|
||||
if 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:
|
||||
newPath.append( (cmd, data) )
|
||||
path = newPath
|
||||
|
|
|
|||
12
testscour.py
12
testscour.py
|
|
@ -548,6 +548,18 @@ class ChangeLineToVerticalLineSegmentInPath(unittest.TestCase):
|
|||
self.assertEquals(path[2][1][0], 100.0,
|
||||
'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):
|
||||
def runTest(self):
|
||||
doc = scour.scourXmlFile('unittests/utf8.svg')
|
||||
|
|
|
|||
4
unittests/path-bez-optimize.svg
Normal file
4
unittests/path-bez-optimize.svg
Normal 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 |
4
unittests/path-quad-optimize.svg
Normal file
4
unittests/path-quad-optimize.svg
Normal 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 |
Loading…
Add table
Add a link
Reference in a new issue