Code

fix an old bug in the style splitting routine and add to the makefile
[inkscape.git] / share / extensions / markers_strokepaint.py
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 copy, 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             retval = None\r
29         return retval\r
30         \r
31     def effect(self):\r
32         self.ctx = inkex.xml.xpath.Context.Context(self.document,processorNss=inkex.NSS)\r
33         defs = self.xpathSingle('/svg/defs')  \r
34         for id, node in self.selected.iteritems():\r
35             mprops = ['marker','marker-start','marker-mid','marker-end']\r
36             style = simplestyle.parseStyle(node.attributes.getNamedItem('style').value)\r
37             \r
38             try:\r
39                 stroke = style['stroke']\r
40             except:\r
41                 stroke = '#000000'\r
42             \r
43             for mprop in mprops:\r
44                 if style.has_key(mprop) and style[mprop] != 'none'and style[mprop][:5] == 'url(#':\r
45                     marker_id = style[mprop][5:-1]\r
46                     old_mnode = self.xpathSingle('/svg//marker[@id="%s"]' % marker_id)\r
47                     mnode = old_mnode.cloneNode(True)\r
48                     new_id = "%s%s" % (marker_id,2)\r
49                     style[mprop] = "url(#%s)" % new_id\r
50                     mnode.attributes.getNamedItem('id').value = new_id\r
51                     defs.appendChild(mnode)\r
52                     \r
53                     children = inkex.xml.xpath.Evaluate('/svg//marker[@id="%s"]//*[@style]' % new_id,self.document,context=self.ctx)\r
54                     for child in children:\r
55                         inkex.debug(child.attributes.getNamedItem('style').value)\r
56                         cstyle = simplestyle.parseStyle(child.attributes.getNamedItem('style').value)\r
57                         if (cstyle.has_key('stroke') and cstyle['stroke'] != 'none') or not cstyle.has_key('stroke'):\r
58                                 cstyle['stroke'] = stroke\r
59                         if (cstyle.has_key('fill') and cstyle['fill'] != 'none') or not cstyle.has_key('fill'):\r
60                                 cstyle['fill'] = stroke\r
61                         child.attributes.getNamedItem('style').value = simplestyle.formatStyle(cstyle)\r
62             node.attributes.getNamedItem('style').value = simplestyle.formatStyle(style)\r
63 e = MyEffect()\r
64 e.affect()\r