Fix path parsing for all path subcommand types. Remove unnecessary whitespace.
This commit is contained in:
parent
d6d4d3d027
commit
90e3ce1a02
4 changed files with 80 additions and 34 deletions
|
|
@ -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 |
99
scour.py
99
scour.py
|
|
@ -604,11 +604,68 @@ def repairStyle(node):
|
||||||
# This method will do the following:
|
# This method will do the following:
|
||||||
# - parse the path data and reserialize
|
# - parse the path data and reserialize
|
||||||
def cleanPath(element) :
|
def cleanPath(element) :
|
||||||
path = svg_parser.parse(element.getAttribute('d'))
|
pathObj = svg_parser.parse(element.getAttribute('d'))
|
||||||
for (cmd,dataset) in path:
|
path = []
|
||||||
if not dataset == None:
|
for (cmd,dataset) in pathObj:
|
||||||
for data in dataset:
|
if cmd in ['M','m','L','l','T','t']:
|
||||||
pass
|
# 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))
|
element.setAttribute('d', serializePath(path))
|
||||||
|
|
||||||
# - reserialize the path data with some cleanups:
|
# - reserialize the path data with some cleanups:
|
||||||
|
|
@ -618,24 +675,20 @@ def cleanPath(element) :
|
||||||
# - adds commas between all values in a subcommand
|
# - adds commas between all values in a subcommand
|
||||||
def serializePath(pathObj):
|
def serializePath(pathObj):
|
||||||
pathStr = ""
|
pathStr = ""
|
||||||
# print pathObj
|
for (cmd,data) in pathObj:
|
||||||
for (cmd,dataset) in pathObj:
|
pathStr += cmd
|
||||||
pathStr += cmd
|
if data != None:
|
||||||
if not dataset == None:
|
c = 0
|
||||||
for data in dataset:
|
for coord in data:
|
||||||
try:
|
# if coord can be an integer without loss of precision, go for it
|
||||||
c = 0
|
if int(coord) == coord: pathStr += str(int(coord))
|
||||||
for coord in data:
|
else: pathStr += str(coord)
|
||||||
# if coord can be an integer without loss of precision, go for it
|
|
||||||
if int(coord) == coord: pathStr += str(int(coord))
|
# only need the comma if the next number if non-negative
|
||||||
else: pathStr += str(coord)
|
if c < len(data)-1 and data[c+1] >= 0:
|
||||||
# only need the comma if the next number if non-negative
|
pathStr += ','
|
||||||
if c < len(data)-1 and data[c+1] >= 0:
|
c += 1
|
||||||
pathStr += ','
|
# pathStr += ' '
|
||||||
c += 1
|
|
||||||
except TypeError:
|
|
||||||
pathStr += str(data)
|
|
||||||
pathStr += ' '
|
|
||||||
return pathStr
|
return pathStr
|
||||||
|
|
||||||
# converts raster references to inline images
|
# converts raster references to inline images
|
||||||
|
|
|
||||||
|
|
@ -434,14 +434,7 @@ class RemoveDelimiterBeforeNegativeCoordsInPath(unittest.TestCase):
|
||||||
doc = scour.scourXmlFile('unittests/path-truncate-zeroes.svg')
|
doc = scour.scourXmlFile('unittests/path-truncate-zeroes.svg')
|
||||||
path = doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('d')
|
path = doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('d')
|
||||||
self.assertEquals(path[4], '-',
|
self.assertEquals(path[4], '-',
|
||||||
'Delimiters not removed before negative coordinate in path data' )
|
'Delimiters not removed before negative coordinates 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>" )
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
3
unittests/path-abs-to-rel.svg
Normal file
3
unittests/path-abs-to-rel.svg
Normal 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 |
Loading…
Add table
Add a link
Reference in a new issue