Prevent fonts from being removed inside a <defs>

This commit is contained in:
JSCHILL1 2009-04-19 08:08:37 -05:00
parent 72363ec24b
commit 8d0bb8fe55
4 changed files with 47 additions and 1 deletions

View file

@ -215,12 +215,13 @@ def removeUnreferencedIDs(referencedIDs, identifiedElements):
# returns the number of unreferenced children removed from defs elements
def vacuumDefs(doc):
global numElemsRemoved
keepTags = ['font', 'style', 'metadata' ]
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') == '' :
if elem.nodeType == 1 and elem.getAttribute('id') == '' and not elem.nodeName in keepTags:
elemsToRemove.append(elem)
for elem in elemsToRemove:
aDef.removeChild(elem)

View file

@ -195,6 +195,19 @@ class NoInkscapeAttributes(unittest.TestCase):
findInkscapeAttr), False,
'Found Inkscape attributes' )
class KeepReferencedFonts(unittest.TestCase):
def runTest(self):
doc = scour.scourXmlFile('unittests/referenced-font.svg')
fonts = doc.documentElement.getElementsByTagNameNS('http://www.w3.org/2000/svg','font')
self.assertEquals(len(fonts), 1,
"Font wrongly removed from <defs>" )
class RemoveUnreferencedFonts(unittest.TestCase):
def runTest(self):
doc = scour.scourXmlFile('unittests/unreferenced-font.svg')
fonts = doc.documentElement.getElementsByTagNameNS('http://www.w3.org/2000/svg','font')
self.assertEquals(len(fonts), 0,
"Font was not removed from <defs>" )
if __name__ == '__main__':
unittest.main()

View file

@ -0,0 +1,16 @@
<svg xmlns='http://www.w3.org/2000/svg'>
<defs>
<font id="Font1" horiz-adv-x="1000">
<font-face font-family="Super Sans" font-weight="bold" font-style="normal"
units-per-em="1000" cap-height="600" x-height="400"
ascent="700" descent="300"
alphabetic="0" mathematical="350" ideographic="400" hanging="500">
<font-face-src>
<font-face-name name="Super Sans Bold"/>
</font-face-src>
</font-face>
<missing-glyph><path d="M0,0h200v200h-200z"/></missing-glyph>
</font>
</defs>
<text x="100" y="100" style="font-family: 'Super Sans', Helvetica, sans-serif">Text</text>
</svg>

After

Width:  |  Height:  |  Size: 657 B

View file

@ -0,0 +1,16 @@
<svg xmlns='http://www.w3.org/2000/svg'>
<defs>
<font id="Font1" horiz-adv-x="1000">
<font-face font-family="Super Sans" font-weight="bold" font-style="normal"
units-per-em="1000" cap-height="600" x-height="400"
ascent="700" descent="300"
alphabetic="0" mathematical="350" ideographic="400" hanging="500">
<font-face-src>
<font-face-name name="Super Sans Bold"/>
</font-face-src>
</font-face>
<missing-glyph><path d="M0,0h200v200h-200z"/></missing-glyph>
</font>
</defs>
<text x="100" y="100" style="font-family: Helvetica, sans-serif">Text</text>
</svg>

After

Width:  |  Height:  |  Size: 643 B