265b4d4338f1beeb7e5c625ef9e8b5b732a41bb2
1 #!/usr/bin/env python
2 '''
3 Copyright (C) 2006 Aaron Spike, aaron@ekips.org
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 '''
19 import random, inkex, simplestyle, copy
20 import gettext
21 _ = gettext.gettext
23 class MyEffect(inkex.Effect):
24 def __init__(self):
25 inkex.Effect.__init__(self)
26 self.OptionParser.add_option("-m", "--modify",
27 action="store", type="inkbool",
28 dest="modify", default=False,
29 help="do not create a copy, modify the markers")
31 def effect(self):
32 defs = self.xpathSingle('/svg:svg//svg:defs')
33 if defs == None:
34 defs = inkex.etree.SubElement(self.document.getroot(),inkex.addNS('defs','svg'))
36 for id, node in self.selected.iteritems():
37 mprops = ['marker','marker-start','marker-mid','marker-end']
38 try:
39 style = simplestyle.parseStyle(node.get('style'))
40 except:
41 inkex.errormsg(_("No style attribute found for id: %s") % id)
42 continue
44 stroke = style.get('stroke', '#000000')
46 for mprop in mprops:
47 if style.has_key(mprop) and style[mprop] != 'none'and style[mprop][:5] == 'url(#':
48 marker_id = style[mprop][5:-1]
49 try:
50 old_mnode = self.xpathSingle('/svg:svg//svg:marker[@id="%s"]' % marker_id)
51 if not self.options.modify:
52 mnode = copy.deepcopy(old_mnode)
53 else:
54 mnode = old_mnode
55 except:
56 inkex.errormsg(_("unable to locate marker: %s") % marker_id)
57 continue
59 new_id = self.uniqueId(marker_id, not self.options.modify)
61 style[mprop] = "url(#%s)" % new_id
62 mnode.set('id', new_id)
63 mnode.set(inkex.addNS('stockid','inkscape'), new_id)
64 defs.append(mnode)
66 children = mnode.xpath('.//*[@style]', namespaces=inkex.NSS)
67 for child in children:
68 cstyle = simplestyle.parseStyle(child.get('style'))
69 if ('stroke' in cstyle and cstyle['stroke'] != 'none') or 'stroke' not in cstyle:
70 cstyle['stroke'] = stroke
71 if ('fill' in cstyle and cstyle['fill'] != 'none') or 'fill' not in cstyle:
72 cstyle['fill'] = stroke
73 child.set('style',simplestyle.formatStyle(cstyle))
74 node.set('style',simplestyle.formatStyle(style))
76 if __name__ == '__main__':
77 e = MyEffect()
78 e.affect()
81 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99