Fix path parsing for all path subcommand types. Remove unnecessary whitespace.

This commit is contained in:
JSCHILL1 2009-04-27 00:04:51 -05:00
parent d6d4d3d027
commit 90e3ce1a02
4 changed files with 80 additions and 34 deletions

View file

@ -1,3 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="200px" viewBox="-0,0 300 200">
<rect width="200" height="100" fill="blue"/>
</svg>

Before

Width:  |  Height:  |  Size: 144 B

View file

@ -604,11 +604,68 @@ def repairStyle(node):
# This method will do the following:
# - parse the path data and reserialize
def cleanPath(element) :
path = svg_parser.parse(element.getAttribute('d'))
for (cmd,dataset) in path:
if not dataset == None:
for data in dataset:
pass
pathObj = svg_parser.parse(element.getAttribute('d'))
path = []
for (cmd,dataset) in pathObj:
if cmd in ['M','m','L','l','T','t']:
# one or more tuples, each containing two numbers
nums = []
for t in dataset:
nums.append(t[0])
nums.append(t[1])
path.append( (cmd, nums) )
elif cmd in ['V','v','H','h']:
# one or more numbers
nums = []
for n in dataset:
nums.append(n)
path.append( (cmd, nums) )
elif cmd in ['C','c']:
# one or more tuples, each containing three tuples of two numbers each
nums = []
for t in dataset:
for pair in t:
nums.append(pair[0])
nums.append(pair[1])
path.append( (cmd, nums) )
elif cmd in ['S','s','Q','q']:
# one or more tuples, each containing two tuples of two numbers each
nums = []
for t in dataset:
for pair in t:
nums.append(pair[0])
nums.append(pair[1])
path.append( (cmd, nums) )
elif cmd in ['A','a']:
# one or more tuples, each containing a tuple of two numbers, a number, a boolean,
# another boolean, and a tuple of two numbers
nums = []
for t in dataset:
nums.append( t[0][0] )
nums.append( t[0][1] )
nums.append( t[1] )
if t[2]: nums.append( 1 )
else: nums.append( 0 )
if t[3]: nums.append( 1 )
else: nums.append( 0 )
nums.append( t[4][0] )
nums.append( t[4][1] )
path.append( (cmd, nums) )
elif cmd in ['Z','z']:
path.append( (cmd, []) )
# for (cmd,dataset) in path:
# if not dataset == None:
# for data in dataset:
# pass
element.setAttribute('d', serializePath(path))
# - reserialize the path data with some cleanups:
@ -618,24 +675,20 @@ def cleanPath(element) :
# - adds commas between all values in a subcommand
def serializePath(pathObj):
pathStr = ""
# print pathObj
for (cmd,dataset) in pathObj:
for (cmd,data) in pathObj:
pathStr += cmd
if not dataset == None:
for data in dataset:
try:
c = 0
for coord in data:
# if coord can be an integer without loss of precision, go for it
if int(coord) == coord: pathStr += str(int(coord))
else: pathStr += str(coord)
# only need the comma if the next number if non-negative
if c < len(data)-1 and data[c+1] >= 0:
pathStr += ','
c += 1
except TypeError:
pathStr += str(data)
pathStr += ' '
if data != None:
c = 0
for coord in data:
# if coord can be an integer without loss of precision, go for it
if int(coord) == coord: pathStr += str(int(coord))
else: pathStr += str(coord)
# only need the comma if the next number if non-negative
if c < len(data)-1 and data[c+1] >= 0:
pathStr += ','
c += 1
# pathStr += ' '
return pathStr
# converts raster references to inline images

View file

@ -434,14 +434,7 @@ class RemoveDelimiterBeforeNegativeCoordsInPath(unittest.TestCase):
doc = scour.scourXmlFile('unittests/path-truncate-zeroes.svg')
path = doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('d')
self.assertEquals(path[4], '-',
'Delimiters not removed before negative coordinate in path data' )
#class RemoveUnreferencedFonts(unittest.TestCase):
# def runTest(self):
# doc = scour.scourXmlFile('unittests/unreferenced-font.svg')
# fonts = doc.documentElement.getElementsByTagNameNS('http://www.w3.org/2000/svg','font')
# self.assertEquals(len(fonts), 0,
# "Font was not removed from <defs>" )
'Delimiters not removed before negative coordinates in path data' )
if __name__ == '__main__':
unittest.main()

View file

@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg">
<path d="M 385.88362,201.47812 v-20 l100,-50 h20 C 505.43501,223.44223 659.42238,164.82405 714.32160,-0.0015300000 C 649.90356,227.13187 497.48814,312.46353 371.30643,277.40123 C 245.12472,242.33893 157.17674,250.88268 121.69357,12.440270 C 211.69357,149.44027 323.87473,190.08578 385.88362,201.47812 z " fill="blue"/>
</svg>

After

Width:  |  Height:  |  Size: 372 B