Add a check to prevent we make path data longer by "optimization"

This commit is contained in:
Eduard Braun 2016-08-31 07:06:42 +02:00
parent 21e6c7491b
commit 0c63344ea4
3 changed files with 18 additions and 2 deletions

View file

@ -2286,6 +2286,10 @@ def cleanPath(element, options) :
path = newPath
newPathStr = serializePath(path, options)
# if for whatever reason we actually made the path longer don't use it
# TODO: maybe we could compare path lengths after each optimization step and use the shortest
if len(newPathStr) <= len(oldPathStr):
numBytesSavedInPathData += ( len(oldPathStr) - len(newPathStr) )
element.setAttribute('d', newPathStr)

View file

@ -685,6 +685,14 @@ class ChangeQuadToShorthandInPath(unittest.TestCase):
self.assertEqual(path.getAttribute('d'), 'm10 100q50-50 100 0t100 0',
'Did not change quadratic curves into shorthand curve segments in path')
class DoNotOptimzePathIfLarger(unittest.TestCase):
def runTest(self):
p = scour.scourXmlFile('unittests/path-no-optimize.svg').getElementsByTagNameNS(SVGNS, 'path')[0];
self.assertTrue(len(p.getAttribute('d')) <= len("M100,100 L200.12345,200.12345 C215,205 185,195 200.12,200.12 Z"),
'Made path data longer during optimization')
# this was the scoured path data as of 2016-08-31 without the length check in cleanPath():
# d="m100 100l100.12 100.12c14.877 4.8766-15.123-5.1234-0.00345-0.00345z"
class HandleEncodingUTF8(unittest.TestCase):
def runTest(self):
doc = scour.scourXmlFile('unittests/encoding-utf8.svg')

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="210" height="210">
<path stroke="yellow" fill="red" d="M100,100 L200.12345,200.12345 C215,205 185,195 200.12,200.12 Z"/>
</svg>

After

Width:  |  Height:  |  Size: 275 B