Added unit tests for color conversion

This commit is contained in:
JSCHILL1 2009-05-17 09:27:33 -05:00
parent 56ebc67491
commit 823c8c45e3
6 changed files with 81 additions and 2 deletions

22
fulltests/dragonfly.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 663 KiB

View file

@ -44,10 +44,13 @@
# * Put id attributes first in the serialization (or make the d attribute last) # * Put id attributes first in the serialization (or make the d attribute last)
# Next Up: # Next Up:
# + Up-revved enthought's path parser for integer coords with sci notation:
# https://svn.enthought.com/enthought/changeset/23583?format=diff&new=23583
# - prevent elements from being stripped if they are referenced in a <style> element # - prevent elements from being stripped if they are referenced in a <style> element
# (for instance, filter, marker, pattern) - need a crude CSS parser # (for instance, filter, marker, pattern) - need a crude CSS parser
# - Convert all colors to #RRGGBB format # - Convert all colors to #RRGGBB format
# - Reduce #RRGGBB format to #RGB format when possible # - Reduce #RRGGBB format to #RGB format when possible
# - Remove any unused glyphs from font elements?
# necessary to get true division # necessary to get true division
from __future__ import division from __future__ import division
@ -69,7 +72,7 @@ import gzip
getcontext().prec = 6 getcontext().prec = 6
APP = 'scour' APP = 'scour'
VER = '0.11' VER = '0.12'
COPYRIGHT = 'Copyright Jeff Schiller, 2009' COPYRIGHT = 'Copyright Jeff Schiller, 2009'
NS = { 'SVG': 'http://www.w3.org/2000/svg', NS = { 'SVG': 'http://www.w3.org/2000/svg',
@ -1002,6 +1005,9 @@ def scourString(in_string, options=[]):
# repair style (remove unnecessary style properties and change them into XML attributes) # repair style (remove unnecessary style properties and change them into XML attributes)
numStylePropsFixed = repairStyle(doc.documentElement, options) numStylePropsFixed = repairStyle(doc.documentElement, options)
# TODO: change color properties to #RRGGBB format
# TODO: change color properties to #RGB where possible
# remove empty defs, metadata, g # remove empty defs, metadata, g
# NOTE: these elements will be removed even if they have (invalid) text nodes # NOTE: these elements will be removed even if they have (invalid) text nodes
elemsToRemove = [] elemsToRemove = []

View file

@ -52,7 +52,7 @@ class _EOF(object):
EOF = _EOF() EOF = _EOF()
lexicon = [ lexicon = [
('float', r'[-\+]?(?:(?:[0-9]*\.[0-9]+)|(?:[0-9]+\.))(?:[Ee][-\+]?[0-9]+)?'), ('float', r'[-\+]?(?:(?:[0-9]*\.[0-9]+)|(?:[0-9]+\.?))(?:[Ee][-\+]?[0-9]+)?'),
('int', r'[-\+]?[0-9]+'), ('int', r'[-\+]?[0-9]+'),
('command', r'[AaCcHhLlMmQqSsTtVvZz]'), ('command', r'[AaCcHhLlMmQqSsTtVvZz]'),
] ]

View file

@ -501,5 +501,42 @@ class HandleNonAsciiUtf8(unittest.TestCase):
self.assertEquals( desc, u'ú', self.assertEquals( desc, u'ú',
'Did not handle non-ASCII characters' ) 'Did not handle non-ASCII characters' )
class HandleSciNoInPathData(unittest.TestCase):
def runTest(self):
doc = scour.scourXmlFile('unittests/path-sn.svg')
self.assertEquals( len(doc.getElementsByTagNameNS(SVGNS, 'path')), 1,
'Did not handle scientific notation in path data' )
class TranslateRGBIntoHex(unittest.TestCase):
def runTest(self):
elem = scour.scourXmlFile('unittests/color-formats.svg').getElementsByTagNameNS(SVGNS, 'rect')[0]
self.assertEquals( elem.getAttribute('fill'), '#010203',
'Not converting rgb into hex')
class TranslateRGBPctIntoHex(unittest.TestCase):
def runTest(self):
elem = scour.scourXmlFile('unittests/color-formats.svg').getElementsByTagNameNS(SVGNS, 'stop')[0]
self.assertEquals( elem.getAttribute('stop-color'), '#FF0000',
'Not converting rgb pct into hex')
class TranslateColorNamesIntoHex(unittest.TestCase):
def runTest(self):
elem = scour.scourXmlFile('unittests/color-formats.svg').getElementsByTagNameNS(SVGNS, 'rect')[0]
self.assertEquals( elem.getAttribute('stroke'), '#FF0000',
'Not converting standard color names into hex')
class TranslateExtendedColorNamesIntoHex(unittest.TestCase):
def runTest(self):
elem = scour.scourXmlFile('unittests/color-formats.svg').getElementsByTagNameNS(SVGNS, 'solidColor')[0]
self.assertEquals( elem.getAttribute('solid-color'), '#00007F',
'Not converting extended color names into hex')
class TranslateLongHexColorIntoShortHex(unittest.TestCase):
def runTest(self):
elem = scour.scourXmlFile('unittests/color-formats.svg').getElementsByTagNameNS(SVGNS, 'ellipse')[0]
self.assertEquals( elem.getAttribute('fill'), '#FFF',
'Not converting extended color names into hex')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -0,0 +1,11 @@
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<linearGradient id="g1" x1="0" y1="0" x2="1" y2="0">
<stop offset="0.5" stop-color="rgb(100%, 0%, 0%)" />
</linearGradient>
<solidColor id="c1" solid-color="navy"/>
</defs>
<rect id="rect" width="100" height="100" fill="rgb(1,2,3)" stroke="red" />
<circle id="circle" cx="100" cy="100" r="30" fill="url(#g1)" stroke="url(#c1)" />
<ellipse id="ellipse" cx="100" cy="100" rx="30" ry="30" style="fill:#ffffff" fill="black" />
</svg>

After

Width:  |  Height:  |  Size: 508 B

3
unittests/path-sn.svg Normal file
View file

@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1781 1142">
<path d="m 0,0 l 2.e-4,0 z"/>
</svg>

After

Width:  |  Height:  |  Size: 103 B