Fix replacement of duplicate gradients if "fill/stroke" contains fallbacks.

(fixes #79)
This commit is contained in:
Eduard Braun 2016-09-05 22:40:37 +02:00
parent 564367f886
commit 9dd0d2ab49

View file

@ -1381,22 +1381,27 @@ def removeDuplicateGradients(doc):
for dupGrad in gradientsToRemove[masterGrad]:
# if the duplicate gradient no longer has a parent that means it was
# already re-mapped to another master gradient
if not dupGrad.parentNode: continue
if not dupGrad.parentNode:
continue
# for each element that referenced the gradient we are going to replace dup_id with master_id
dup_id = dupGrad.getAttribute('id')
# for each element that referenced the gradient we are going to remove
funcIRI = re.compile('url\([\'"]?#' + dup_id + '[\'"]?\)') # matches url(#a), url('#a') and url("#a")
for elem in referencedIDs[dup_id][1]:
# find out which attribute referenced the duplicate gradient
for attr in ['fill', 'stroke']:
v = elem.getAttribute(attr)
if v == 'url(#'+dup_id+')' or v == 'url("#'+dup_id+'")' or v == "url('#"+dup_id+"')":
elem.setAttribute(attr, 'url(#'+master_id+')')
(v_new, n) = funcIRI.subn('url(#'+master_id+')', v)
if n > 0:
elem.setAttribute(attr, v_new)
if elem.getAttributeNS(NS['XLINK'], 'href') == '#'+dup_id:
elem.setAttributeNS(NS['XLINK'], 'href', '#'+master_id)
styles = _getStyle(elem)
for style in styles:
v = styles[style]
if v == 'url(#'+dup_id+')' or v == 'url("#'+dup_id+'")' or v == "url('#"+dup_id+"')":
styles[style] = 'url(#'+master_id+')'
(v_new, n) = funcIRI.subn('url(#'+master_id+')', v)
if n > 0:
styles[style] = v_new
_setStyle(elem, styles)
# now that all referencing elements have been re-mapped to the master