Remove fill-X when fill-opacity=0, remove stroke-X when stroke-opacity=0 or stroke-width=0

This commit is contained in:
JSCHILL1 2009-04-07 22:44:36 -05:00
parent 0376ddd83f
commit 40dda68a45

View file

@ -1,6 +1,6 @@
#!/usr/local/bin/python #!/usr/local/bin/python
# Scour # Scour
# Version 0.03 # Version 0.05
# #
# Copyright 2009 Jeff Schiller # Copyright 2009 Jeff Schiller
# #
@ -20,10 +20,12 @@
# Notes: # Notes:
# Path-crunching ideas here: http://intertwingly.net/code/svgtidy/spec.rb # rubys path-crunching ideas here: http://intertwingly.net/code/svgtidy/spec.rb
# (and implemented here: http://intertwingly.net/code/svgtidy/svgtidy.rb ) # (and implemented here: http://intertwingly.net/code/svgtidy/svgtidy.rb )
# Yet more ideas here: http://wiki.inkscape.org/wiki/index.php/Save_Cleaned_SVG # Yet more ideas here: http://wiki.inkscape.org/wiki/index.php/Save_Cleaned_SVG
# TODO: Adapt this script into an Inkscape python plugin
#
# * Specify a limit to the precision of all positional elements. # * Specify a limit to the precision of all positional elements.
# * Clean up XML Elements # * Clean up XML Elements
# * Collapse multiple redundent groups # * Collapse multiple redundent groups
@ -50,6 +52,8 @@
# Next Up: # Next Up:
# + Convert style to recognized XML attributes # + Convert style to recognized XML attributes
# + Remove any fill-X styles when fill="none" or fill-opacity="0"
# + Remove any stroke-X styles when stroke-opacity="0" or stroke-width="0"
# - Removed duplicate gradient stops # - Removed duplicate gradient stops
# - Convert all colors to #RRGGBB format # - Convert all colors to #RRGGBB format
@ -262,10 +266,22 @@ def repairStyle(node):
# Here is where we can weed out unnecessary styles like: # Here is where we can weed out unnecessary styles like:
# opacity:1 # opacity:1
if styleMap.has_key('opacity') and string.atof(styleMap['opacity']) == 1.0 : if styleMap.has_key('opacity') :
del styleMap['opacity'] # opacity='1.0' is useless, remove it
if string.atof(styleMap['opacity']) == 1.0 :
# if stroke:none, then remove all stroke properties (stroke-width, etc) del styleMap['opacity']
# if opacity='0' then all fill and stroke properties are useless, remove them
elif string.atof(styleMap['opacity']) == 0.0 :
for uselessStyle in ['fill', 'fill-opacity', 'fill-rule', 'stroke', 'stroke-linejoin',
'stroke-opacity', 'stroke-miterlimit', 'stroke-linecap', 'stroke-dasharray',
'stroke-dashoffset', 'stroke-opacity'] :
del styleMap[uselessStyle]
num += 1
# if stroke:none, then remove all stroke-related properties (stroke-width, etc)
# TODO: should also detect if the computed value of this element is fill="none"
# TODO: should also detect if stroke-width=0 or stroke-opacity=0
if styleMap.has_key('stroke') and styleMap['stroke'] == 'none' : if styleMap.has_key('stroke') and styleMap['stroke'] == 'none' :
for strokestyle in [ 'stroke-width', 'stroke-linejoin', 'stroke-miterlimit', for strokestyle in [ 'stroke-width', 'stroke-linejoin', 'stroke-miterlimit',
'stroke-linecap', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-opacity'] : 'stroke-linecap', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-opacity'] :
@ -273,22 +289,53 @@ def repairStyle(node):
del styleMap[strokestyle] del styleMap[strokestyle]
num += 1 num += 1
# if fill:none, then remove all fill-related properties (fill-rule, etc)
# TODO: should also detect if fill-opacity=0
if styleMap.has_key('fill') and styleMap['fill'] == 'none' :
for fillstyle in [ 'fill-rule', 'fill-opacity' ] :
if styleMap.has_key(fillstyle) :
del styleMap[fillstyle]
num += 1
# stop-opacity: 1 # stop-opacity: 1
if styleMap.has_key('stop-opacity') and string.atof(styleMap['stop-opacity']) == 1.0 : if styleMap.has_key('stop-opacity') :
del styleMap['stop-opacity'] if string.atof(styleMap['stop-opacity']) == 1.0 :
num += 1 del styleMap['stop-opacity']
num += 1
# fill-opacity: 1 # fill-opacity: 1 or 0
# TODO: This is actually a problem is a parent element has a fill-opacity != 1 if styleMap.has_key('fill-opacity') :
if styleMap.has_key('fill-opacity') and string.atof(styleMap['fill-opacity']) == 1.0 : fillOpacity = string.atof(styleMap['fill-opacity'])
del styleMap['fill-opacity'] # TODO: This is actually a problem is the parent element does not have fill-opacity = 1
num += 1 if fillOpacity == 1.0 :
del styleMap['fill-opacity']
num += 1
elif fillOpacity == 0.0 :
for uselessFillStyle in [ 'fill', 'fill-rule' ] :
del styleMap[uselessFillStyle]
num += 1
# stroke-opacity: 1 # stroke-opacity: 1 or 0
# TODO: This is actually a problem is a parent element has a stroke-opacity != 1 if styleMap.has_key('stroke-opacity') :
if styleMap.has_key('stroke-opacity') and string.atof(styleMap['stroke-opacity']) == 1.0 : strokeOpacity = string.atof(styleMap['stroke-opacity'])
del styleMap['stroke-opacity'] # TODO: This is actually a problem is the parent element does not have stroke-opacity = 1
num += 1 if strokeOpacity == 1.0 :
del styleMap['stroke-opacity']
num += 1
elif strokeOpacity == 0.0 :
for uselessStrokeStyle in [ 'stroke', 'stroke-width', 'stroke-linejoin', 'stroke-linecap',
'stroke-dasharray', 'stroke-dashoffset' ] :
del styleMap[uselessStrokeStyle]
num += 1
# stroke-width: 0
if styleMap.has_key('stroke-width') :
strokeWidth = string.atof(styleMap['stroke-width'])
if strokeWidth == 0.0 :
for uselessStrokeStyle in [ 'stroke', 'stroke-linejoin', 'stroke-linecap',
'stroke-dasharray', 'stroke-dashoffset', 'stroke-opacity' ] :
del styleMap[uselessStrokeStyle]
num += 1
# TODO: what else? # TODO: what else?