Gracefully handle unreferenced gradients with --keep-unreferenced-defs
Closes: #156 Signed-off-by: Niels Thykier <niels@thykier.net>
This commit is contained in:
parent
0776d32179
commit
26f7eb6e28
1 changed files with 20 additions and 16 deletions
|
|
@ -1430,22 +1430,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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue