Fix for Bug 395645, patch sent by Frederik Elwert
This commit is contained in:
parent
e9f1e07a02
commit
6e68896d1f
3 changed files with 39 additions and 25 deletions
|
|
@ -11,11 +11,12 @@
|
|||
|
||||
<section id="0.15">
|
||||
<header>
|
||||
<h2><a href="#0.14">Version 0.15</a></h2>
|
||||
<h2><a href="#0.15">Version 0.15</a></h2>
|
||||
</header>
|
||||
|
||||
<p>July 4th, 2009</p>
|
||||
<ul>
|
||||
<li>added --keep-editor-data command-line option</li>
|
||||
<li>Fix Bug 395645: Keep all identified children inside a defs (Thanks Frederik!)</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
|
|
@ -23,7 +24,7 @@
|
|||
<header>
|
||||
<h2><a href="#0.14">Version 0.14</a></h2>
|
||||
</header>
|
||||
|
||||
<p>June 10th, 2009</p>
|
||||
<ul>
|
||||
<li>Collapse adjacent commands of the same type</li>
|
||||
<li>Convert straight curves into line commands</li>
|
||||
|
|
@ -38,7 +39,7 @@
|
|||
<header>
|
||||
<h2><a href="#0.13">Version 0.13</a></h2>
|
||||
</header>
|
||||
|
||||
<p>May 19th, 2009</p>
|
||||
<ul>
|
||||
<li>properly deal with fill="url(&quot;#foo&quot;)"</li>
|
||||
<li>properly handle paths with more than 1 pair of coordinates in the first Move command</li>
|
||||
|
|
@ -53,7 +54,7 @@
|
|||
<header>
|
||||
<h2><a href="#0.12">Version 0.12</a></h2>
|
||||
</header>
|
||||
|
||||
<p>May 17th, 2009</p>
|
||||
<ul>
|
||||
<li>upgraded enthought's path parser to handle scientific notation in path coordinates</li>
|
||||
<li>convert colors to #RRGGBB format</li>
|
||||
|
|
@ -65,7 +66,7 @@
|
|||
<header>
|
||||
<h2><a href="#0.11">Version 0.11</a></h2>
|
||||
</header>
|
||||
|
||||
<p>April 28th, 2009</p>
|
||||
<ul>
|
||||
<li>convert gradient stop offsets from percentages to float</li>
|
||||
<li>convert gradient stop offsets to integers if possible (0 or 1)</li>
|
||||
|
|
@ -83,7 +84,7 @@
|
|||
<header>
|
||||
<h2><a href="#0.10">Version 0.10</a></h2>
|
||||
</header>
|
||||
|
||||
<p>April 27th, 2009</p>
|
||||
<ul>
|
||||
<li>Remove path with empty d attributes</li>
|
||||
<li>Sanitize path data (remove unnecessary whitespace)</li>
|
||||
|
|
@ -101,7 +102,7 @@
|
|||
<header>
|
||||
<h2><a href="#0.09">Version 0.09</a></h2>
|
||||
</header>
|
||||
|
||||
<p>April 25th, 2009</p>
|
||||
<ul>
|
||||
<li>Fix bug when removing stroke styles</li>
|
||||
<li>Remove gradients that are only referenced by one other gradient</li>
|
||||
|
|
@ -115,7 +116,7 @@
|
|||
<header>
|
||||
<h2><a href="#0.08">Version 0.08</a></h2>
|
||||
</header>
|
||||
|
||||
<p>April 22nd, 2009</p>
|
||||
<ul>
|
||||
<li>Remove unnecessary nested <g> elements</li>
|
||||
<li>Remove duplicate gradient stops (same offset, stop-color, stop-opacity)</li>
|
||||
|
|
@ -128,7 +129,7 @@
|
|||
<header>
|
||||
<h2><a href="#0.07">Version 0.07</a></h2>
|
||||
</header>
|
||||
|
||||
<p>April 15th, 2009</p>
|
||||
<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>
|
||||
|
|
@ -141,7 +142,7 @@
|
|||
<header>
|
||||
<h2><a href="#0.06">Version 0.06</a></h2>
|
||||
</header>
|
||||
|
||||
<p>April 13th, 2009</p>
|
||||
<ul>
|
||||
<li>Prevent error when stroke-width property value has a unit</li>
|
||||
<li>Convert width/height into a viewBox where possible</li>
|
||||
|
|
@ -153,7 +154,7 @@
|
|||
<header>
|
||||
<h2><a href="#0.05">Version 0.05 and earlier</a></h2>
|
||||
</header>
|
||||
|
||||
<p>April 7th, 2009</p>
|
||||
<ul>
|
||||
<li>Removes unreferenced elements in a <defs></li>
|
||||
<li>Removes all inkscape, sodipodi, adobe elements</li>
|
||||
|
|
|
|||
33
scour.py
33
scour.py
|
|
@ -471,6 +471,24 @@ numBytesSavedInPathData = 0
|
|||
numBytesSavedInColors = 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):
|
||||
"""
|
||||
Removes all unreferenced elements except for <svg>, <font>, <metadata>, <title>, and <desc>.
|
||||
|
|
@ -494,27 +512,16 @@ def removeUnreferencedElements(doc):
|
|||
numElemsRemoved += 1
|
||||
|
||||
# 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
|
||||
defs = doc.documentElement.getElementsByTagNameNS(NS['SVG'], 'defs')
|
||||
for aDef in defs:
|
||||
elemsToRemove = []
|
||||
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)
|
||||
elemsToRemove = removeUnusedDefs(doc, aDef)
|
||||
for elem in elemsToRemove:
|
||||
aDef.removeChild(elem)
|
||||
elem.parentNode.removeChild(elem)
|
||||
numElemsRemoved += 1
|
||||
num += 1
|
||||
return num
|
||||
|
||||
return num
|
||||
|
||||
def removeUnreferencedIDs(referencedIDs, identifiedElements):
|
||||
"""
|
||||
Removes the unreferenced ID attributes.
|
||||
|
|
|
|||
|
|
@ -596,6 +596,12 @@ class DoNotRemovePolgonLastPoint(unittest.TestCase):
|
|||
self.assertEquals(p.getAttribute('points'), '200,50 300,50 300,150 200,150',
|
||||
'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 a test for embedding rasters
|
||||
# TODO: write a test for --disable-embed-rasters
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue