Code

add some convenience methods to inkex and allow the possiblity of replacing the marke...
[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 random, inkex, simplestyle\r
20 \r
21 class MyEffect(inkex.Effect):\r
22     def __init__(self):\r
23         inkex.Effect.__init__(self)\r
24         self.OptionParser.add_option("-m", "--modify",\r
25                         action="store", type="inkbool", \r
26                         dest="modify", default=False,\r
27                         help="do not create a copy, modify the markers")\r
28         \r
29     def effect(self):\r
30         defs = self.xpathSingle('/svg//defs')\r
31         if not defs:\r
32             defs = self.document.createElement('svg:defs')\r
33             self.document.documentElement.appendChile(defs)\r
34         \r
35         for id, node in self.selected.iteritems():\r
36             mprops = ['marker','marker-start','marker-mid','marker-end']\r
37             try:\r
38                 style = simplestyle.parseStyle(node.attributes.getNamedItem('style').value)\r
39             except:\r
40                 inkex.debug("No style attribute found for id: %s" % id)\r
41                 continue\r
42             \r
43             stroke = style.get('stroke', '#000000')\r
44             \r
45             for mprop in mprops:\r
46                 if style.has_key(mprop) and style[mprop] != 'none'and style[mprop][:5] == 'url(#':\r
47                     marker_id = style[mprop][5:-1]\r
48                     try:\r
49                         old_mnode = self.xpathSingle('/svg//marker[@id="%s"]' % marker_id)\r
50                         if not self.options.modify:\r
51                             mnode = old_mnode.cloneNode(True)\r
52                         else:\r
53                             mnode = old_mnode\r
54                     except:\r
55                         inkex.debug("unable to locate marker: %s" % marker_id)\r
56                         continue\r
57                         \r
58                     new_id = self.uniqueId(marker_id, not self.options.modify)\r
59                     \r
60                     style[mprop] = "url(#%s)" % new_id\r
61                     mnode.attributes.getNamedItem('id').value = new_id\r
62                     mnode.attributes.getNamedItemNS(inkex.NSS['inkscape'],'stockid').value = new_id\r
63                     defs.appendChild(mnode)\r
64                     \r
65                     children = inkex.xml.xpath.Evaluate('/svg//marker[@id="%s"]//*[@style]' % new_id,self.document,context=self.ctx)\r
66                     for child in children:\r
67                         cstyle = simplestyle.parseStyle(child.attributes.getNamedItem('style').value)\r
68                         if ('stroke' in cstyle and cstyle['stroke'] != 'none') or 'stroke' not in cstyle:\r
69                                 cstyle['stroke'] = stroke\r
70                         if ('fill' in cstyle and cstyle['fill'] != 'none') or 'fill' not in cstyle:\r
71                                 cstyle['fill'] = stroke\r
72                         child.attributes.getNamedItem('style').value = simplestyle.formatStyle(cstyle)\r
73             node.attributes.getNamedItem('style').value = simplestyle.formatStyle(style)\r
74 e = MyEffect()\r
75 e.affect()\r