Prevent error on stroke-width property value with units

This commit is contained in:
JSCHILL1 2009-04-11 07:08:24 -05:00
parent 48602f0dca
commit 6dec9c84cd

View file

@ -240,6 +240,18 @@ def removeNamespacedElements(node, namespaces):
removeNamespacedElements(child, namespaces) removeNamespacedElements(child, namespaces)
return num return num
# returns the length of a property
def getSVGLength(value):
# QRegExp coord("\\-?\\d+\\.?\\d*");
# QRegExp pct("(\\-?\\d+\\.?\\d*)\\%");
# QRegExp unit("(em|ex|px|pt|pc|cm|mm|in|\\%){1}");
try:
v = string.atof(value)
except ValueError:
# TODO: do unit parsing here
v = value
return value
def repairStyle(node): def repairStyle(node):
num = 0 num = 0
if node.nodeType == 1 and len(node.getAttribute('style')) > 0 : if node.nodeType == 1 and len(node.getAttribute('style')) > 0 :
@ -264,7 +276,7 @@ def repairStyle(node):
# opacity:1 # opacity:1
if styleMap.has_key('opacity') : if styleMap.has_key('opacity') :
# opacity='1.0' is useless, remove it # opacity='1.0' is useless, remove it
if string.atof(styleMap['opacity']) == 1.0 : if getSVGLength(styleMap['opacity']) == 1.0 :
del styleMap['opacity'] del styleMap['opacity']
# if opacity='0' then all fill and stroke properties are useless, remove them # if opacity='0' then all fill and stroke properties are useless, remove them
@ -326,7 +338,7 @@ def repairStyle(node):
# stroke-width: 0 # stroke-width: 0
if styleMap.has_key('stroke-width') : if styleMap.has_key('stroke-width') :
strokeWidth = string.atof(styleMap['stroke-width']) strokeWidth = getSVGLength(styleMap['stroke-width'])
if strokeWidth == 0.0 : if strokeWidth == 0.0 :
for uselessStrokeStyle in [ 'stroke', 'stroke-linejoin', 'stroke-linecap', for uselessStrokeStyle in [ 'stroke', 'stroke-linejoin', 'stroke-linecap',
'stroke-dasharray', 'stroke-dashoffset', 'stroke-opacity' ] : 'stroke-dasharray', 'stroke-dashoffset', 'stroke-opacity' ] :
@ -361,6 +373,11 @@ def repairStyle(node):
def cleanPath(element) : def cleanPath(element) :
path = element.getAttribute('d') path = element.getAttribute('d')
def properlySizeDoc(docElement):
w = docElement.getAttribute('width')
h = docElement.getAttribute('height')
vb = docElement.getAttribute('viewBox')
# parse command-line arguments # parse command-line arguments
args = sys.argv[1:] args = sys.argv[1:]
@ -426,6 +443,9 @@ while bContinueLooping:
referencedIDs = findReferencedElements(doc.documentElement, {}) referencedIDs = findReferencedElements(doc.documentElement, {})
bContinueLooping = ((removeUnreferencedIDs(referencedIDs, identifiedElements) + vacuumDefs(doc)) > 0) bContinueLooping = ((removeUnreferencedIDs(referencedIDs, identifiedElements) + vacuumDefs(doc)) > 0)
# repair style (remove unnecessary style properties and change them into XML attributes)
numStylePropsFixed = repairStyle(doc.documentElement) numStylePropsFixed = repairStyle(doc.documentElement)
# remove empty defs, metadata, g # remove empty defs, metadata, g