1 #!/usr/bin/env python \r
2 '''\r
3 Copyright (C) 2006 Aaron Spike, aaron@ekips.org\r
4 \r
5 This program is free software; you can redistribute it and/or modify\r
6 it under the terms of the GNU General Public License as published by\r
7 the Free Software Foundation; either version 2 of the License, or\r
8 (at your option) any later version.\r
9 \r
10 This program is distributed in the hope that it will be useful,\r
11 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
13 GNU General Public License for more details.\r
14 \r
15 You should have received a copy of the GNU General Public License\r
16 along with this program; if not, write to the Free Software\r
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
18 '''\r
19 import random, inkex, simplestyle\r
20 \r
21 class MyEffect(inkex.Effect):\r
22 def __init__(self):\r
23 inkex.Effect.__init__(self)\r
24 def xpathSingle(self, path):\r
25 try:\r
26 retval = inkex.xml.xpath.Evaluate(path,self.document,context=self.ctx)[0]\r
27 except:\r
28 inkex.debug("No matching node for expression: %s" % path)\r
29 retval = None\r
30 return retval\r
31 \r
32 def effect(self):\r
33 self.ctx = inkex.xml.xpath.Context.Context(self.document,processorNss=inkex.NSS)\r
34 id_characters = '0123456789abcdefghijklmnopqrstuvwkyzABCDEFGHIJKLMNOPQRSTUVWXYZ'\r
35 \r
36 defs = self.xpathSingle('/svg//defs')\r
37 if not defs:\r
38 defs = self.document.createElement('svg:defs')\r
39 self.document.documentElement.appendChile(defs)\r
40 \r
41 doc_ids = {}\r
42 docIdNodes = inkex.xml.xpath.Evaluate('//@id',self.document,context=self.ctx)\r
43 for m in docIdNodes:\r
44 doc_ids[m.value] = 1\r
45 \r
46 for id, node in self.selected.iteritems():\r
47 mprops = ['marker','marker-start','marker-mid','marker-end']\r
48 try:\r
49 style = simplestyle.parseStyle(node.attributes.getNamedItem('style').value)\r
50 except:\r
51 inkex.debug("No style attribute found for id: %s" % id)\r
52 continue\r
53 \r
54 stroke = style.get('stroke', '#000000')\r
55 \r
56 for mprop in mprops:\r
57 if style.has_key(mprop) and style[mprop] != 'none'and style[mprop][:5] == 'url(#':\r
58 marker_id = style[mprop][5:-1]\r
59 try:\r
60 old_mnode = self.xpathSingle('/svg//marker[@id="%s"]' % marker_id)\r
61 mnode = old_mnode.cloneNode(True)\r
62 except:\r
63 inkex.debug("unable to locate marker: %s" % marker_id)\r
64 continue\r
65 \r
66 #generate a unique id\r
67 new_id = marker_id\r
68 while new_id in doc_ids:\r
69 new_id = "%s%s" % (new_id,random.choice(id_characters))\r
70 doc_ids[new_id] = 1\r
71 \r
72 style[mprop] = "url(#%s)" % new_id\r
73 mnode.attributes.getNamedItem('id').value = new_id\r
74 defs.appendChild(mnode)\r
75 \r
76 children = inkex.xml.xpath.Evaluate('/svg//marker[@id="%s"]//*[@style]' % new_id,self.document,context=self.ctx)\r
77 for child in children:\r
78 cstyle = simplestyle.parseStyle(child.attributes.getNamedItem('style').value)\r
79 if ('stroke' in cstyle and cstyle['stroke'] != 'none') or 'stroke' not in cstyle:\r
80 cstyle['stroke'] = stroke\r
81 if ('fill' in cstyle and cstyle['fill'] != 'none') or 'fill' not in cstyle:\r
82 cstyle['fill'] = stroke\r
83 child.attributes.getNamedItem('style').value = simplestyle.formatStyle(cstyle)\r
84 node.attributes.getNamedItem('style').value = simplestyle.formatStyle(style)\r
85 e = MyEffect()\r
86 e.affect()\r