Removal of stroke:none. Prevent groups from being collapsed if title/desc present. Add option to prevent groups from being collapsed. Unit tests. Started release notes HTML.
This commit is contained in:
parent
8e9683f648
commit
9f38caabaa
4 changed files with 145 additions and 9 deletions
88
release-notes.html
Normal file
88
release-notes.html
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Scour Release Notes</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<section id="0.09">
|
||||
<header>
|
||||
<h3><a href="#0.09">Version 0.09</a></h3>
|
||||
</header>
|
||||
|
||||
<ul>
|
||||
<li>Fix bug when removing stroke styles</li>
|
||||
<li>Remove gradients that are only referenced by one other gradient</li>
|
||||
<li>Added option to prevent group collapsing</li>
|
||||
<li>Prevent groups with title/desc children from being collapsed</li>
|
||||
<li>Remove stroke="none"</li>
|
||||
</ul>
|
||||
|
||||
</section>
|
||||
|
||||
<section id="0.08">
|
||||
<header>
|
||||
<h3><a href="#0.08">Version 0.08</a></h3>
|
||||
</header>
|
||||
|
||||
<ul>
|
||||
<li>Remove unnecessary nested <g> elements</li>
|
||||
<li>Remove duplicate gradient stops (same offset, stop-color, stop-opacity)</li>
|
||||
<li>Always keep fonts inside <defs>, always keep ids on fonts</li>
|
||||
<li>made ID stripping optional (disabled by default)</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<section id="0.07">
|
||||
<header>
|
||||
<h3><a href="#0.07">Version 0.07</a></h3>
|
||||
</header>
|
||||
|
||||
<ul>
|
||||
<li>moved all functionality into a module level function named 'scour' and began adding unit tests</li>
|
||||
<li>prevent metadata from being removed if they contain only text nodes</li>
|
||||
<li>Remove unreferenced pattern and gradient elements outside of defs</li>
|
||||
<li>Removal of extra whitespace, pretty printing of XML</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<section id="0.06">
|
||||
<header>
|
||||
<h3><a href="#0.06">Version 0.06</a></h3>
|
||||
</header>
|
||||
|
||||
<ul>
|
||||
<li>Prevent error when stroke-width property value has a unit</li>
|
||||
<li>Convert width/height into a viewBox where possible</li>
|
||||
<li>Convert all referenced rasters into base64 encoded URLs if the files can be found</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<section id="0.05">
|
||||
<header>
|
||||
<h3><a href="#0.05">Version 0.05 and earlier</a></h3>
|
||||
</header>
|
||||
|
||||
<ul>
|
||||
<li>Removes unreferenced elements in a <defs></li>
|
||||
<li>Removes all inkscape, sodipodi, adobe elements</li>
|
||||
<li>Removes all inkscape, sodipodi, adobe attributes</li>
|
||||
<li>Remove all unused namespace declarations on the document element</li>
|
||||
<li>Removes any empty <defs>, <metadata>, or <g> elements</li>
|
||||
<li>Style fix-ups:
|
||||
<ul><li>Fixes any style properties like this: <code>style="fill: url(#linearGradient1000) rgb(0, 0, 0);"</code></li>
|
||||
<li>Removes any style property of: <code>opacity: 1;</code></li>
|
||||
<li>Removes any stroke properties when stroke=none or stroke-opacity=0 or stroke-width=0</li>
|
||||
<li>Removes any fill properties when fill=none or fill-opacity=0</li>
|
||||
<li>Removes all fill/stroke properties when opacity=0</li>
|
||||
<li>Removes any <code>stop-opacity: 1</code></li>
|
||||
<li>Removes any <code>fill-opacity: 1</code></li>
|
||||
<li>Removes any <code>stroke-opacity: 1</code></li>
|
||||
</ul></li>
|
||||
<li>Convert style properties into SVG attributes</li>
|
||||
</ul>
|
||||
|
||||
</section>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
28
scour.py
28
scour.py
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
# Scour
|
||||
# Version 0.08
|
||||
# Version 0.09
|
||||
#
|
||||
# Copyright 2009 Jeff Schiller
|
||||
#
|
||||
|
|
@ -30,7 +30,6 @@
|
|||
# * Specify a limit to the precision of all positional elements.
|
||||
# * Clean up Definitions
|
||||
# * Collapse duplicate gradient definitions
|
||||
# * Remove gradients that are only referenced by one other gradient
|
||||
# * Clean up CSS
|
||||
# * Convert RGB colours from RGB(r,g,b) to #RRGGBB format
|
||||
# * Convert RGB colours from #RRGGBB to #RGB if possible
|
||||
|
|
@ -50,6 +49,9 @@
|
|||
# Next Up:
|
||||
# + fix bug when removing stroke styles
|
||||
# + Remove gradients that are only referenced by one other gradient
|
||||
# + added option to prevent group collapsing
|
||||
# + prevent groups with title/desc children from being collapsed
|
||||
# + remove stroke=none attribute
|
||||
# - Remove unnecessary units of precision on attributes
|
||||
# - Remove unnecessary units of precision on path coordinates
|
||||
# - Convert all colors to #RRGGBB format
|
||||
|
|
@ -295,8 +297,8 @@ def removeNamespacedElements(node, namespaces):
|
|||
return num
|
||||
|
||||
# this walks further and further down the tree, removing groups
|
||||
# which do not have any attributes and promoting their children
|
||||
# up one level
|
||||
# which do not have any attributes or a title/desc child and
|
||||
# promoting their children up one level
|
||||
def removeNestedGroups(node):
|
||||
global numElemsRemoved
|
||||
num = 0
|
||||
|
|
@ -304,7 +306,13 @@ def removeNestedGroups(node):
|
|||
groupsToRemove = []
|
||||
for child in node.childNodes:
|
||||
if child.nodeName == 'g' and child.namespaceURI == NS['SVG'] and len(child.attributes) == 0:
|
||||
groupsToRemove.append(child)
|
||||
# only collapse group if it does not have a title or desc as a direct descendant
|
||||
for grandchild in child.childNodes:
|
||||
if grandchild.nodeType == 1 and grandchild.namespaceURI == NS['SVG'] and \
|
||||
grandchild.nodeName in ['title','desc']:
|
||||
break
|
||||
else:
|
||||
groupsToRemove.append(child)
|
||||
|
||||
for g in groupsToRemove:
|
||||
while g.childNodes.length > 0:
|
||||
|
|
@ -530,6 +538,7 @@ def repairStyle(node):
|
|||
if styleMap.has_key(strokestyle) :
|
||||
del styleMap[strokestyle]
|
||||
num += 1
|
||||
del styleMap['stroke']
|
||||
|
||||
# if fill:none, then remove all fill-related properties (fill-rule, etc)
|
||||
# TODO: should also detect if fill-opacity=0
|
||||
|
|
@ -757,8 +766,9 @@ def scourString(in_string, options=[]):
|
|||
referencedIDs = findReferencedElements(doc.documentElement, {})
|
||||
bContinueLooping = (removeUnreferencedIDs(referencedIDs, identifiedElements) > 0)
|
||||
|
||||
while removeNestedGroups(doc.documentElement) > 0:
|
||||
pass
|
||||
if not '--disable-group-collapsing' in options:
|
||||
while removeNestedGroups(doc.documentElement) > 0:
|
||||
pass
|
||||
|
||||
while removeDuplicateGradientStops(doc) > 0:
|
||||
pass
|
||||
|
|
@ -812,7 +822,8 @@ def printSyntaxAndQuit():
|
|||
print 'If the output file is not specified, stdout is used.\n'
|
||||
print 'If an option is not available below that means it occurs automatically'
|
||||
print 'when scour is invoked. Available OPTIONS:\n'
|
||||
print ' --enable-id-stripping : Scour will remove all un-referenced ID attributes'
|
||||
print ' --enable-id-stripping : Scour will remove all un-referenced ID attributes'
|
||||
print ' --disable-group-collapsing : Scour will not collapse <g> elements'
|
||||
print ''
|
||||
quit()
|
||||
|
||||
|
|
@ -826,6 +837,7 @@ def parseCLA():
|
|||
output = sys.stdout
|
||||
options = []
|
||||
validOptions = [
|
||||
'--disable-group-collapsing',
|
||||
'--enable-id-stripping',
|
||||
]
|
||||
|
||||
|
|
|
|||
26
testscour.py
26
testscour.py
|
|
@ -163,6 +163,24 @@ class RemoveUselessNestedGroups(unittest.TestCase):
|
|||
self.assertEquals(len(doc.getElementsByTagNameNS(SVGNS, 'g')), 1,
|
||||
'Useless nested groups not removed' )
|
||||
|
||||
class DoNotRemoveUselessNestedGroups(unittest.TestCase):
|
||||
def runTest(self):
|
||||
doc = scour.scourXmlFile('unittests/nested-useless-groups.svg', ['--disable-group-collapsing'])
|
||||
self.assertEquals(len(doc.getElementsByTagNameNS(SVGNS, 'g')), 2,
|
||||
'Useless nested groups were removed despite --disable-group-collapsing' )
|
||||
|
||||
class DoNotRemoveNestedGroupsWithTitle(unittest.TestCase):
|
||||
def runTest(self):
|
||||
doc = scour.scourXmlFile('unittests/groups-with-title-desc.svg')
|
||||
self.assertEquals(len(doc.getElementsByTagNameNS(SVGNS, 'g')), 2,
|
||||
'Nested groups with title was removed' )
|
||||
|
||||
class DoNotRemoveNestedGroupsWithDesc(unittest.TestCase):
|
||||
def runTest(self):
|
||||
doc = scour.scourXmlFile('unittests/groups-with-title-desc.svg')
|
||||
self.assertEquals(len(doc.getElementsByTagNameNS(SVGNS, 'g')), 2,
|
||||
'Nested groups with desc was removed' )
|
||||
|
||||
class RemoveDuplicateLinearGradientStops(unittest.TestCase):
|
||||
def runTest(self):
|
||||
doc = scour.scourXmlFile('unittests/duplicate-gradient-stops.svg')
|
||||
|
|
@ -303,8 +321,14 @@ class RemoveStrokeDashoffsetWhenStrokeWidthZero(unittest.TestCase):
|
|||
doc = scour.scourXmlFile('unittests/stroke-nowidth.svg')
|
||||
self.assertEquals(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-dashoffset'), '',
|
||||
'stroke-dashoffset attribute not emptied when width zero' )
|
||||
|
||||
|
||||
class RemoveStrokeWhenStrokeNone(unittest.TestCase):
|
||||
def runTest(self):
|
||||
doc = scour.scourXmlFile('unittests/stroke-none.svg')
|
||||
self.assertEquals(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke'), '',
|
||||
'stroke attribute not emptied when no stroke' )
|
||||
|
||||
class RemoveStrokeWidthWhenStrokeNone(unittest.TestCase):
|
||||
def runTest(self):
|
||||
doc = scour.scourXmlFile('unittests/stroke-none.svg')
|
||||
self.assertEquals(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-width'), '',
|
||||
|
|
|
|||
12
unittests/groups-with-title-desc.svg
Normal file
12
unittests/groups-with-title-desc.svg
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg">
|
||||
<g>
|
||||
<title>Group 1</title>
|
||||
<rect width="300" height="200" fill="green" />
|
||||
<circle cx="200" cy="100" r="50" fill="yellow" />
|
||||
</g>
|
||||
<g>
|
||||
<desc>Group 1</desc>
|
||||
<rect width="300" height="200" fill="green" />
|
||||
<circle cx="200" cy="100" r="50" fill="yellow" />
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 309 B |
Loading…
Add table
Add a link
Reference in a new issue