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, copy\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:svg//svg:defs')\r
31 if not defs:\r
32 defs = inkex.etree.SubElement(self.document.getroot(),inkex.addNS('defs','svg'))\r
33 \r
34 for id, node in self.selected.iteritems():\r
35 mprops = ['marker','marker-start','marker-mid','marker-end']\r
36 try:\r
37 style = simplestyle.parseStyle(node.get('style'))\r
38 except:\r
39 inkex.debug("No style attribute found for id: %s" % id)\r
40 continue\r
41 \r
42 stroke = style.get('stroke', '#000000')\r
43 \r
44 for mprop in mprops:\r
45 if style.has_key(mprop) and style[mprop] != 'none'and style[mprop][:5] == 'url(#':\r
46 marker_id = style[mprop][5:-1]\r
47 try:\r
48 old_mnode = self.xpathSingle('/svg:svg//svg:marker[@id="%s"]' % marker_id)\r
49 if not self.options.modify:\r
50 mnode = copy.deepcopy(old_mnode)\r
51 else:\r
52 mnode = old_mnode\r
53 except:\r
54 inkex.debug("unable to locate marker: %s" % marker_id)\r
55 continue\r
56 \r
57 new_id = self.uniqueId(marker_id, not self.options.modify)\r
58 \r
59 style[mprop] = "url(#%s)" % new_id\r
60 mnode.set('id', new_id)\r
61 mnode.set(inkex.addNS('stockid','inkscape'), new_id)\r
62 defs.append(mnode)\r
63 \r
64 children = mnode.xpath('.//*[@style]', namespaces=inkex.NSS)\r
65 for child in children:\r
66 cstyle = simplestyle.parseStyle(child.get('style'))\r
67 if ('stroke' in cstyle and cstyle['stroke'] != 'none') or 'stroke' not in cstyle:\r
68 cstyle['stroke'] = stroke\r
69 if ('fill' in cstyle and cstyle['fill'] != 'none') or 'fill' not in cstyle:\r
70 cstyle['fill'] = stroke\r
71 child.set('style',simplestyle.formatStyle(cstyle))\r
72 node.set('style',simplestyle.formatStyle(style))\r
73 \r
74 e = MyEffect()\r
75 e.affect()\r