Fix for Bug 395645, patch sent by Frederik Elwert

This commit is contained in:
JSCHILL1 2009-07-05 00:42:51 -05:00
parent e9f1e07a02
commit 6e68896d1f
3 changed files with 39 additions and 25 deletions

View file

@ -11,11 +11,12 @@
<section id="0.15"> <section id="0.15">
<header> <header>
<h2><a href="#0.14">Version 0.15</a></h2> <h2><a href="#0.15">Version 0.15</a></h2>
</header> </header>
<p>July 4th, 2009</p>
<ul> <ul>
<li>added --keep-editor-data command-line option</li> <li>added --keep-editor-data command-line option</li>
<li>Fix Bug 395645: Keep all identified children inside a defs (Thanks Frederik!)</li>
</ul> </ul>
</section> </section>
@ -23,7 +24,7 @@
<header> <header>
<h2><a href="#0.14">Version 0.14</a></h2> <h2><a href="#0.14">Version 0.14</a></h2>
</header> </header>
<p>June 10th, 2009</p>
<ul> <ul>
<li>Collapse adjacent commands of the same type</li> <li>Collapse adjacent commands of the same type</li>
<li>Convert straight curves into line commands</li> <li>Convert straight curves into line commands</li>
@ -38,7 +39,7 @@
<header> <header>
<h2><a href="#0.13">Version 0.13</a></h2> <h2><a href="#0.13">Version 0.13</a></h2>
</header> </header>
<p>May 19th, 2009</p>
<ul> <ul>
<li>properly deal with fill="url(&amp;quot;#foo&amp;quot;)"</li> <li>properly deal with fill="url(&amp;quot;#foo&amp;quot;)"</li>
<li>properly handle paths with more than 1 pair of coordinates in the first Move command</li> <li>properly handle paths with more than 1 pair of coordinates in the first Move command</li>
@ -53,7 +54,7 @@
<header> <header>
<h2><a href="#0.12">Version 0.12</a></h2> <h2><a href="#0.12">Version 0.12</a></h2>
</header> </header>
<p>May 17th, 2009</p>
<ul> <ul>
<li>upgraded enthought's path parser to handle scientific notation in path coordinates</li> <li>upgraded enthought's path parser to handle scientific notation in path coordinates</li>
<li>convert colors to #RRGGBB format</li> <li>convert colors to #RRGGBB format</li>
@ -65,7 +66,7 @@
<header> <header>
<h2><a href="#0.11">Version 0.11</a></h2> <h2><a href="#0.11">Version 0.11</a></h2>
</header> </header>
<p>April 28th, 2009</p>
<ul> <ul>
<li>convert gradient stop offsets from percentages to float</li> <li>convert gradient stop offsets from percentages to float</li>
<li>convert gradient stop offsets to integers if possible (0 or 1)</li> <li>convert gradient stop offsets to integers if possible (0 or 1)</li>
@ -83,7 +84,7 @@
<header> <header>
<h2><a href="#0.10">Version 0.10</a></h2> <h2><a href="#0.10">Version 0.10</a></h2>
</header> </header>
<p>April 27th, 2009</p>
<ul> <ul>
<li>Remove path with empty d attributes</li> <li>Remove path with empty d attributes</li>
<li>Sanitize path data (remove unnecessary whitespace)</li> <li>Sanitize path data (remove unnecessary whitespace)</li>
@ -101,7 +102,7 @@
<header> <header>
<h2><a href="#0.09">Version 0.09</a></h2> <h2><a href="#0.09">Version 0.09</a></h2>
</header> </header>
<p>April 25th, 2009</p>
<ul> <ul>
<li>Fix bug when removing stroke styles</li> <li>Fix bug when removing stroke styles</li>
<li>Remove gradients that are only referenced by one other gradient</li> <li>Remove gradients that are only referenced by one other gradient</li>
@ -115,7 +116,7 @@
<header> <header>
<h2><a href="#0.08">Version 0.08</a></h2> <h2><a href="#0.08">Version 0.08</a></h2>
</header> </header>
<p>April 22nd, 2009</p>
<ul> <ul>
<li>Remove unnecessary nested &lt;g&gt; elements</li> <li>Remove unnecessary nested &lt;g&gt; elements</li>
<li>Remove duplicate gradient stops (same offset, stop-color, stop-opacity)</li> <li>Remove duplicate gradient stops (same offset, stop-color, stop-opacity)</li>
@ -128,7 +129,7 @@
<header> <header>
<h2><a href="#0.07">Version 0.07</a></h2> <h2><a href="#0.07">Version 0.07</a></h2>
</header> </header>
<p>April 15th, 2009</p>
<ul> <ul>
<li>moved all functionality into a module level function named 'scour' and began adding unit tests</li> <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>prevent metadata from being removed if they contain only text nodes</li>
@ -141,7 +142,7 @@
<header> <header>
<h2><a href="#0.06">Version 0.06</a></h2> <h2><a href="#0.06">Version 0.06</a></h2>
</header> </header>
<p>April 13th, 2009</p>
<ul> <ul>
<li>Prevent error when stroke-width property value has a unit</li> <li>Prevent error when stroke-width property value has a unit</li>
<li>Convert width/height into a viewBox where possible</li> <li>Convert width/height into a viewBox where possible</li>
@ -153,7 +154,7 @@
<header> <header>
<h2><a href="#0.05">Version 0.05 and earlier</a></h2> <h2><a href="#0.05">Version 0.05 and earlier</a></h2>
</header> </header>
<p>April 7th, 2009</p>
<ul> <ul>
<li>Removes unreferenced elements in a &lt;defs&gt;</li> <li>Removes unreferenced elements in a &lt;defs&gt;</li>
<li>Removes all inkscape, sodipodi, adobe elements</li> <li>Removes all inkscape, sodipodi, adobe elements</li>

View file

@ -471,6 +471,24 @@ numBytesSavedInPathData = 0
numBytesSavedInColors = 0 numBytesSavedInColors = 0
numPointsRemovedFromPolygon = 0 numPointsRemovedFromPolygon = 0
def removeUnusedDefs(doc, defElem, elemsToRemove=None):
if elemsToRemove is None:
elemsToRemove = []
identifiedElements = findElementsWithId(doc.documentElement)
referencedIDs = findReferencedElements(doc.documentElement)
keepTags = ['font', 'style', 'metadata', 'script', 'title', 'desc']
for elem in defElem.childNodes:
if elem.nodeName == 'g' and elem.namespaceURI == NS['SVG']:
elemsToRemove = removeUnusedDefs(doc, elem, elemsToRemove)
continue
if elem.nodeType == 1 and (elem.getAttribute('id') == '' or \
(not elem.getAttribute('id') in referencedIDs)) and \
not elem.nodeName in keepTags:
elemsToRemove.append(elem)
return elemsToRemove
def removeUnreferencedElements(doc): def removeUnreferencedElements(doc):
""" """
Removes all unreferenced elements except for <svg>, <font>, <metadata>, <title>, and <desc>. Removes all unreferenced elements except for <svg>, <font>, <metadata>, <title>, and <desc>.
@ -494,26 +512,15 @@ def removeUnreferencedElements(doc):
numElemsRemoved += 1 numElemsRemoved += 1
# TODO: should also go through defs and vacuum it # TODO: should also go through defs and vacuum it
identifiedElements = findElementsWithId(doc.documentElement)
referencedIDs = findReferencedElements(doc.documentElement)
keepTags = ['font', 'style', 'metadata', 'script', 'title', 'desc']
num = 0 num = 0
defs = doc.documentElement.getElementsByTagNameNS(NS['SVG'], 'defs') defs = doc.documentElement.getElementsByTagNameNS(NS['SVG'], 'defs')
for aDef in defs: for aDef in defs:
elemsToRemove = [] elemsToRemove = removeUnusedDefs(doc, aDef)
for elem in aDef.childNodes:
if elem.nodeType == 1 and (elem.getAttribute('id') == '' or \
(not elem.getAttribute('id') in referencedIDs)) and \
not elem.nodeName in keepTags:
elemsToRemove.append(elem)
for elem in elemsToRemove: for elem in elemsToRemove:
aDef.removeChild(elem) elem.parentNode.removeChild(elem)
numElemsRemoved += 1 numElemsRemoved += 1
num += 1 num += 1
return num return num
return num
def removeUnreferencedIDs(referencedIDs, identifiedElements): def removeUnreferencedIDs(referencedIDs, identifiedElements):
""" """

View file

@ -596,6 +596,12 @@ class DoNotRemovePolgonLastPoint(unittest.TestCase):
self.assertEquals(p.getAttribute('points'), '200,50 300,50 300,150 200,150', self.assertEquals(p.getAttribute('points'), '200,50 300,50 300,150 200,150',
'Last point of polygon removed' ) 'Last point of polygon removed' )
class DoNotRemoveGroupsWithIDsInDefs(unittest.TestCase):
def runTest(self):
f = scour.scourXmlFile('unittests/important-groups-in-defs.svg')
self.assertEquals(len(f.getElementsByTagNameNS(SVGNS, 'linearGradient')), 1,
'Group in defs with id\'ed element removed');
# TODO: write tests for --set-precision for path data, for polygon data, for attributes # TODO: write tests for --set-precision for path data, for polygon data, for attributes
# TODO; write a test for embedding rasters # TODO; write a test for embedding rasters
# TODO: write a test for --disable-embed-rasters # TODO: write a test for --disable-embed-rasters