Gracefully handle unreferenced gradients with --keep-unreferenced-defs (#173)

Closes: #156
Signed-off-by: Niels Thykier <niels@thykier.net>
This commit is contained in:
Niels Thykier 2018-03-10 16:06:50 +01:00 committed by Eduard Braun
parent cae0faefa0
commit 6ea126d290

View file

@ -1421,22 +1421,26 @@ def removeDuplicateGradients(doc):
# for each element that referenced the gradient we are going to replace dup_id with master_id # for each element that referenced the gradient we are going to replace dup_id with master_id
dup_id = dupGrad.getAttribute('id') dup_id = dupGrad.getAttribute('id')
funcIRI = re.compile('url\\([\'"]?#' + dup_id + '[\'"]?\\)') # matches url(#a), url('#a') and url("#a") funcIRI = re.compile('url\\([\'"]?#' + dup_id + '[\'"]?\\)') # matches url(#a), url('#a') and url("#a")
for elem in referencedIDs[dup_id]:
# find out which attribute referenced the duplicate gradient # With --keep-unreferenced-defs, we can end up with
for attr in ['fill', 'stroke']: # unreferenced gradients. See GH#156.
v = elem.getAttribute(attr) if dup_id in referencedIDs:
(v_new, n) = funcIRI.subn('url(#' + master_id + ')', v) for elem in referencedIDs[dup_id]:
if n > 0: # find out which attribute referenced the duplicate gradient
elem.setAttribute(attr, v_new) for attr in ['fill', 'stroke']:
if elem.getAttributeNS(NS['XLINK'], 'href') == '#' + dup_id: v = elem.getAttribute(attr)
elem.setAttributeNS(NS['XLINK'], 'href', '#' + master_id) (v_new, n) = funcIRI.subn('url(#' + master_id + ')', v)
styles = _getStyle(elem) if n > 0:
for style in styles: elem.setAttribute(attr, v_new)
v = styles[style] if elem.getAttributeNS(NS['XLINK'], 'href') == '#' + dup_id:
(v_new, n) = funcIRI.subn('url(#' + master_id + ')', v) elem.setAttributeNS(NS['XLINK'], 'href', '#' + master_id)
if n > 0: styles = _getStyle(elem)
styles[style] = v_new for style in styles:
_setStyle(elem, styles) v = styles[style]
(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 # now that all referencing elements have been re-mapped to the master
# it is safe to remove this gradient from the document # it is safe to remove this gradient from the document