From: acspike Date: Thu, 20 Jul 2006 12:46:35 +0000 (+0000) Subject: avoid id collisions and add more error checking X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=d2ff38ac1578390f13f83a165188ca9ba0fffcc7;p=inkscape.git avoid id collisions and add more error checking --- diff --git a/share/extensions/markers_strokepaint.py b/share/extensions/markers_strokepaint.py index 163767f1a..4b261cba6 100644 --- a/share/extensions/markers_strokepaint.py +++ b/share/extensions/markers_strokepaint.py @@ -16,7 +16,7 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ''' -import copy, inkex, simplestyle +import random, inkex, simplestyle class MyEffect(inkex.Effect): def __init__(self): @@ -25,38 +25,60 @@ class MyEffect(inkex.Effect): try: retval = inkex.xml.xpath.Evaluate(path,self.document,context=self.ctx)[0] except: + inkex.debug("No matching node for expression: %s" % path) retval = None return retval def effect(self): self.ctx = inkex.xml.xpath.Context.Context(self.document,processorNss=inkex.NSS) - defs = self.xpathSingle('/svg/defs') + id_characters = '0123456789abcdefghijklmnopqrstuvwkyzABCDEFGHIJKLMNOPQRSTUVWXYZ' + + defs = self.xpathSingle('/svg//defs') + if not defs: + defs = self.document.createElement('svg:defs') + self.document.documentElement.appendChile(defs) + + doc_ids = {} + docIdNodes = inkex.xml.xpath.Evaluate('//@id',self.document,context=self.ctx) + for m in docIdNodes: + doc_ids[m.value] = 1 + for id, node in self.selected.iteritems(): mprops = ['marker','marker-start','marker-mid','marker-end'] - style = simplestyle.parseStyle(node.attributes.getNamedItem('style').value) - try: - stroke = style['stroke'] + style = simplestyle.parseStyle(node.attributes.getNamedItem('style').value) except: - stroke = '#000000' + inkex.debug("No style attribute found for id: %s" % id) + continue + + stroke = style.get('stroke', '#000000') for mprop in mprops: if style.has_key(mprop) and style[mprop] != 'none'and style[mprop][:5] == 'url(#': marker_id = style[mprop][5:-1] - old_mnode = self.xpathSingle('/svg//marker[@id="%s"]' % marker_id) - mnode = old_mnode.cloneNode(True) - new_id = "%s%s" % (marker_id,2) + try: + old_mnode = self.xpathSingle('/svg//marker[@id="%s"]' % marker_id) + mnode = old_mnode.cloneNode(True) + except: + inkex.debug("unable to locate marker: %s" % marker_id) + continue + + #generate a unique id + new_id = marker_id + while new_id in doc_ids: + new_id = "%s%s" % (new_id,random.choice(id_characters)) + doc_ids[new_id] = 1 + style[mprop] = "url(#%s)" % new_id mnode.attributes.getNamedItem('id').value = new_id defs.appendChild(mnode) children = inkex.xml.xpath.Evaluate('/svg//marker[@id="%s"]//*[@style]' % new_id,self.document,context=self.ctx) for child in children: - inkex.debug(child.attributes.getNamedItem('style').value) cstyle = simplestyle.parseStyle(child.attributes.getNamedItem('style').value) - if (cstyle.has_key('stroke') and cstyle['stroke'] != 'none') or not cstyle.has_key('stroke'): + if ('stroke' in cstyle and cstyle['stroke'] != 'none') or 'stroke' not in cstyle: cstyle['stroke'] = stroke - if (cstyle.has_key('fill') and cstyle['fill'] != 'none') or not cstyle.has_key('fill'): + if ('fill' in cstyle and cstyle['fill'] != 'none') or 'fill' not in cstyle: cstyle['fill'] = stroke child.attributes.getNamedItem('style').value = simplestyle.formatStyle(cstyle) node.attributes.getNamedItem('style').value = simplestyle.formatStyle(style)