From f6387b1f221772393f6b3db2e610498f5fa01633 Mon Sep 17 00:00:00 2001 From: JSCHILL1 Date: Fri, 17 Apr 2009 08:18:36 -0500 Subject: [PATCH] Remove duplicate gradient stops and update unit tests --- scour.py | 37 ++++++++++++++++++++++---- testscour.py | 16 ++++++++--- unittests/duplicate-gradient-stops.svg | 11 ++++++-- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/scour.py b/scour.py index 577fc74..f06c4f7 100755 --- a/scour.py +++ b/scour.py @@ -29,7 +29,6 @@ # # * Specify a limit to the precision of all positional elements. # * Clean up Definitions -# * Remove duplicate gradient stops # * Collapse duplicate gradient definitions # * Remove gradients that are only referenced by one other gradient # * Clean up CSS @@ -47,11 +46,11 @@ # Next Up: # + Remove unnecessary nested elements -# - Remove duplicate gradient stops (same offset, stop-color, stop-opacity) +# + Remove duplicate gradient stops (same offset, stop-color, stop-opacity) # - Convert all colors to #RRGGBB format # - Reduce #RRGGBB format to #RGB format when possible # - rework command-line argument processing so that options are configurable -# - remove unreferenced patterns? https://bugs.edge.launchpad.net/ubuntu/+source/human-icon-theme/+bug/361667/ +https://bugs.edge.launchpad.net/ubuntu/+source/human-icon-theme/+bug/361667/ # Some notes to not forget: # - removing unreferenced IDs loses some semantic information @@ -309,8 +308,33 @@ def removeNestedGroups(node): # now recurse for children for child in node.childNodes: if child.nodeType == 1: - num += removeNestedGroups(child) - + num += removeNestedGroups(child) + return num + +def removeDuplicateGradientStops(doc): + global numElemsRemoved + num = 0 + + for gradType in ['linearGradient', 'radialGradient']: + for grad in doc.getElementsByTagNameNS(NS['SVG'], gradType): + stops = {} + stopsToRemove = [] + for stop in grad.getElementsByTagNameNS(NS['SVG'], 'stop'): + offset = string.atof(stop.getAttribute('offset')) + color = stop.getAttribute('stop-color') + opacity = stop.getAttribute('stop-opacity') + if stops.has_key(offset) : + oldStop = stops[offset] + if oldStop[0] == color and oldStop[1] == opacity: + stopsToRemove.append(stop) + stops[offset] = [color, opacity] + + for stop in stopsToRemove: + stop.parentNode.removeChild(stop) + num += 1 + numElemsRemoved += 1 + + # linear gradients return num coord = re.compile("\\-?\\d+\\.?\\d*") @@ -668,6 +692,9 @@ def scourString(in_string): while removeNestedGroups(doc.documentElement) > 0: pass + while removeDuplicateGradientStops(doc) > 0: + pass + # clean path data for elem in doc.documentElement.getElementsByTagNameNS(NS['SVG'], 'path') : cleanPath(elem) diff --git a/testscour.py b/testscour.py index 2b7cef4..56bb296 100755 --- a/testscour.py +++ b/testscour.py @@ -139,13 +139,21 @@ class RemoveUselessNestedGroups(unittest.TestCase): self.assertEquals(len(doc.getElementsByTagNameNS(SVGNS, 'g')), 1, 'Useless nested groups not removed' ) -# These tests will fail at present -class RemoveDuplicateGradientStops(unittest.TestCase): +class RemoveDuplicateLinearGradientStops(unittest.TestCase): def runTest(self): doc = scour.scourXmlFile('unittests/duplicate-gradient-stops.svg') - self.assertEquals(len(doc.getElementsByTagNameNS(SVGNS, 'stop')), 3, - 'Duplicate gradient stops not removed' ) + grad = doc.getElementsByTagNameNS(SVGNS, 'linearGradient') + self.assertEquals(len(grad[0].getElementsByTagNameNS(SVGNS, 'stop')), 3, + 'Duplicate linear gradient stops not removed' ) +class RemoveDuplicateRadialGradientStops(unittest.TestCase): + def runTest(self): + doc = scour.scourXmlFile('unittests/duplicate-gradient-stops.svg') + grad = doc.getElementsByTagNameNS(SVGNS, 'radialGradient') + self.assertEquals(len(grad[0].getElementsByTagNameNS(SVGNS, 'stop')), 3, + 'Duplicate radial gradient stops not removed' ) + +# These tests will fail at present #class NoInkscapeAttributes(unittest.TestCase): # def runTest(self): # self.assertNotEquals(walkTree(scour.scourXmlFile('unittests/inkscape.svg').documentElement, diff --git a/unittests/duplicate-gradient-stops.svg b/unittests/duplicate-gradient-stops.svg index 010bb95..a5acfe4 100644 --- a/unittests/duplicate-gradient-stops.svg +++ b/unittests/duplicate-gradient-stops.svg @@ -1,11 +1,18 @@ - + + + + + + + - + +