Code

avoid id collisions and add more error checking
authoracspike <acspike@users.sourceforge.net>
Thu, 20 Jul 2006 12:46:35 +0000 (12:46 +0000)
committeracspike <acspike@users.sourceforge.net>
Thu, 20 Jul 2006 12:46:35 +0000 (12:46 +0000)
share/extensions/markers_strokepaint.py

index 163767f1aaa4f5d423c16055eba39c4e5fa5dfd2..4b261cba6046180fd7a3df037fac26b802143d5d 100644 (file)
@@ -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\r
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
 '''\r
-import copy, inkex, simplestyle\r
+import random, inkex, simplestyle\r
 \r
 class MyEffect(inkex.Effect):\r
     def __init__(self):\r
@@ -25,38 +25,60 @@ class MyEffect(inkex.Effect):
         try:\r
             retval = inkex.xml.xpath.Evaluate(path,self.document,context=self.ctx)[0]\r
         except:\r
+            inkex.debug("No matching node for expression: %s" % path)\r
             retval = None\r
         return retval\r
         \r
     def effect(self):\r
         self.ctx = inkex.xml.xpath.Context.Context(self.document,processorNss=inkex.NSS)\r
-        defs = self.xpathSingle('/svg/defs')  \r
+        id_characters = '0123456789abcdefghijklmnopqrstuvwkyzABCDEFGHIJKLMNOPQRSTUVWXYZ'\r
+        \r
+        defs = self.xpathSingle('/svg//defs')\r
+        if not defs:\r
+            defs = self.document.createElement('svg:defs')\r
+            self.document.documentElement.appendChile(defs)\r
+        \r
+        doc_ids = {}\r
+        docIdNodes = inkex.xml.xpath.Evaluate('//@id',self.document,context=self.ctx)\r
+        for m in docIdNodes:\r
+            doc_ids[m.value] = 1\r
+        \r
         for id, node in self.selected.iteritems():\r
             mprops = ['marker','marker-start','marker-mid','marker-end']\r
-            style = simplestyle.parseStyle(node.attributes.getNamedItem('style').value)\r
-            \r
             try:\r
-                stroke = style['stroke']\r
+                style = simplestyle.parseStyle(node.attributes.getNamedItem('style').value)\r
             except:\r
-                stroke = '#000000'\r
+                inkex.debug("No style attribute found for id: %s" % id)\r
+                continue\r
+            \r
+            stroke = style.get('stroke', '#000000')\r
             \r
             for mprop in mprops:\r
                 if style.has_key(mprop) and style[mprop] != 'none'and style[mprop][:5] == 'url(#':\r
                     marker_id = style[mprop][5:-1]\r
-                    old_mnode = self.xpathSingle('/svg//marker[@id="%s"]' % marker_id)\r
-                    mnode = old_mnode.cloneNode(True)\r
-                    new_id = "%s%s" % (marker_id,2)\r
+                    try:\r
+                        old_mnode = self.xpathSingle('/svg//marker[@id="%s"]' % marker_id)\r
+                        mnode = old_mnode.cloneNode(True)\r
+                    except:\r
+                        inkex.debug("unable to locate marker: %s" % marker_id)\r
+                        continue\r
+                        \r
+                    #generate a unique id\r
+                    new_id = marker_id\r
+                    while new_id in doc_ids:\r
+                        new_id = "%s%s" % (new_id,random.choice(id_characters))\r
+                    doc_ids[new_id] = 1\r
+                    \r
                     style[mprop] = "url(#%s)" % new_id\r
                     mnode.attributes.getNamedItem('id').value = new_id\r
                     defs.appendChild(mnode)\r
                     \r
                     children = inkex.xml.xpath.Evaluate('/svg//marker[@id="%s"]//*[@style]' % new_id,self.document,context=self.ctx)\r
                     for child in children:\r
-                        inkex.debug(child.attributes.getNamedItem('style').value)\r
                         cstyle = simplestyle.parseStyle(child.attributes.getNamedItem('style').value)\r
-                        if (cstyle.has_key('stroke') and cstyle['stroke'] != 'none') or not cstyle.has_key('stroke'):\r
+                        if ('stroke' in cstyle and cstyle['stroke'] != 'none') or 'stroke' not in cstyle:\r
                                 cstyle['stroke'] = stroke\r
-                        if (cstyle.has_key('fill') and cstyle['fill'] != 'none') or not cstyle.has_key('fill'):\r
+                        if ('fill' in cstyle and cstyle['fill'] != 'none') or 'fill' not in cstyle:\r
                                 cstyle['fill'] = stroke\r
                         child.attributes.getNamedItem('style').value = simplestyle.formatStyle(cstyle)\r
             node.attributes.getNamedItem('style').value = simplestyle.formatStyle(style)\r