Merge pull request #53 from codedread/detect_flowtext

add option to check and warn or bail out on flowtext
This commit is contained in:
Tobias Oberstein 2016-04-18 19:01:56 +02:00
commit 1a8ece216d
5 changed files with 230 additions and 51 deletions

View file

@ -17,3 +17,9 @@ test_version:
test_help: test_help:
PYTHONPATH=. python -m scour.scour --help PYTHONPATH=. python -m scour.scour --help
test_error_on_flowtext:
# this is fine ..
PYTHONPATH=. scour --error-on-flowtext unittests/flowtext-less.svg /dev/null
# .. and this should bail out!
PYTHONPATH=. scour --error-on-flowtext unittests/flowtext.svg /dev/null

View file

@ -2880,6 +2880,16 @@ def scourString(in_string, options=None):
global numBytesSavedInTransforms global numBytesSavedInTransforms
doc = xml.dom.minidom.parseString(in_string) doc = xml.dom.minidom.parseString(in_string)
# determine number of flowRoot elements in input document
# flowRoot elements don't render at all on current browsers (04/2016)
cnt_flowText_el = len(doc.getElementsByTagName('flowRoot'))
if cnt_flowText_el:
errmsg = "SVG input document uses {} flow text elements, which won't render on browsers!".format(cnt_flowText_el)
if options.error_on_flowtext:
raise Exception(errmsg)
else:
print("WARNING: {}".format(errmsg))
# remove <metadata> if the user wants to # remove <metadata> if the user wants to
if options.remove_metadata: if options.remove_metadata:
removeMetadataElements(doc) removeMetadataElements(doc)
@ -3225,6 +3235,11 @@ _option_group_ids.add_option("--protect-ids-prefix",
help="don't remove IDs starting with the given prefix") help="don't remove IDs starting with the given prefix")
_options_parser.add_option_group(_option_group_ids) _options_parser.add_option_group(_option_group_ids)
_option_group_compatibility = optparse.OptionGroup(_options_parser, "SVG compatibility checks")
_option_group_compatibility.add_option("--error-on-flowtext",
action="store_true", dest="error_on_flowtext", default=False,
help="In case the input SVG uses flow text, bail out with error. Otherwise only warn. (default: False)")
_options_parser.add_option_group(_option_group_compatibility)
def maybe_gziped_file(filename, mode="r"): def maybe_gziped_file(filename, mode="r"):

View file

@ -1416,6 +1416,20 @@ class DuplicateGradientsUpdateStyle(unittest.TestCase):
self.assertEqual('fill:url(#' + gradientTag.getAttribute('id') + ')', rectTag1.getAttribute('style'), self.assertEqual('fill:url(#' + gradientTag.getAttribute('id') + ')', rectTag1.getAttribute('style'),
'Either of #duplicate-one or #duplicate-two was removed, but style="fill:" was not updated to reflect this') 'Either of #duplicate-one or #duplicate-two was removed, but style="fill:" was not updated to reflect this')
class DocWithFlowtext(unittest.TestCase):
def runTest(self):
with self.assertRaises(Exception):
scour.scourXmlFile('unittests/flowtext.svg',
scour.parse_args(['--error-on-flowtext'])[0])
class DocWithNoFlowtext(unittest.TestCase):
def runTest(self):
try:
scour.scourXmlFile('unittests/flowtext-less.svg',
scour.parse_args(['--error-on-flowtext'])[0])
except Exception as e:
self.fail("exception '{}' was raised, and we didn't expect that!".format(e))
# TODO: write tests for --enable-viewboxing # TODO: write tests for --enable-viewboxing
# 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

View file

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="210mm"
height="297mm"
id="svg2"
version="1.1"
inkscape:version="0.48.4 r9939"
sodipodi:docname="flowtext-less.svg">
<defs
id="defs4" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="0.35"
inkscape:cx="350"
inkscape:cy="520"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showgrid="false"
inkscape:window-width="1559"
inkscape:window-height="876"
inkscape:window-x="41"
inkscape:window-y="24"
inkscape:window-maximized="1" />
<metadata
id="metadata7">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title />
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Ebene 1"
inkscape:groupmode="layer"
id="layer1">
<text
xml:space="preserve"
style="font-size:40px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans"
x="142.85715"
y="638.07648"
id="text2997"
sodipodi:linespacing="125%"><tspan
sodipodi:role="line"
id="tspan2999"
x="142.85715"
y="638.07648">abcd</tspan></text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.9 KiB

78
unittests/flowtext.svg Normal file
View file

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="210mm"
height="297mm"
id="svg2"
version="1.1"
inkscape:version="0.48.4 r9939"
sodipodi:docname="Neues Dokument 1">
<defs
id="defs4" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="0.35"
inkscape:cx="350"
inkscape:cy="520"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showgrid="false"
inkscape:window-width="936"
inkscape:window-height="631"
inkscape:window-x="41"
inkscape:window-y="24"
inkscape:window-maximized="0" />
<metadata
id="metadata7">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Ebene 1"
inkscape:groupmode="layer"
id="layer1">
<flowRoot
xml:space="preserve"
id="flowRoot2985"
style="font-size:40px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans"><flowRegion
id="flowRegion2987"><rect
id="rect2989"
width="480"
height="262.85715"
x="45.714287"
y="218.07646" /></flowRegion><flowPara
id="flowPara2991">sfdadasd</flowPara><flowPara
id="flowPara2993">asdasd</flowPara><flowPara
id="flowPara2995">adsa</flowPara></flowRoot> <text
xml:space="preserve"
style="font-size:40px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans"
x="142.85715"
y="638.07648"
id="text2997"
sodipodi:linespacing="125%"><tspan
sodipodi:role="line"
id="tspan2999"
x="142.85715"
y="638.07648">abcd</tspan></text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.5 KiB