Code

noop: Add vim modeline for all share/extensions/*.py files that use four-space indent...
[inkscape.git] / share / extensions / markers_strokepaint.py
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
21 class MyEffect(inkex.Effect):
22     def __init__(self):
23         inkex.Effect.__init__(self)
24         self.OptionParser.add_option("-m", "--modify",
25                         action="store", type="inkbool", 
26                         dest="modify", default=False,
27                         help="do not create a copy, modify the markers")
28         
29     def effect(self):
30         defs = self.xpathSingle('/svg:svg//svg:defs')
31         if not defs:
32             defs = inkex.etree.SubElement(self.document.getroot(),inkex.addNS('defs','svg'))
33         
34         for id, node in self.selected.iteritems():
35             mprops = ['marker','marker-start','marker-mid','marker-end']
36             try:
37                 style = simplestyle.parseStyle(node.get('style'))
38             except:
39                 inkex.debug("No style attribute found for id: %s" % id)
40                 continue
41             
42             stroke = style.get('stroke', '#000000')
43             
44             for mprop in mprops:
45                 if style.has_key(mprop) and style[mprop] != 'none'and style[mprop][:5] == 'url(#':
46                     marker_id = style[mprop][5:-1]
47                     try:
48                         old_mnode = self.xpathSingle('/svg:svg//svg:marker[@id="%s"]' % marker_id)
49                         if not self.options.modify:
50                             mnode = copy.deepcopy(old_mnode)
51                         else:
52                             mnode = old_mnode
53                     except:
54                         inkex.debug("unable to locate marker: %s" % marker_id)
55                         continue
56                         
57                     new_id = self.uniqueId(marker_id, not self.options.modify)
58                     
59                     style[mprop] = "url(#%s)" % new_id
60                     mnode.set('id', new_id)
61                     mnode.set(inkex.addNS('stockid','inkscape'), new_id)
62                     defs.append(mnode)
63                     
64                     children = mnode.xpath('.//*[@style]', namespaces=inkex.NSS)
65                     for child in children:
66                         cstyle = simplestyle.parseStyle(child.get('style'))
67                         if ('stroke' in cstyle and cstyle['stroke'] != 'none') or 'stroke' not in cstyle:
68                             cstyle['stroke'] = stroke
69                         if ('fill' in cstyle and cstyle['fill'] != 'none') or 'fill' not in cstyle:
70                             cstyle['fill'] = stroke
71                         child.set('style',simplestyle.formatStyle(cstyle))
72             node.set('style',simplestyle.formatStyle(style))
74 e = MyEffect()
75 e.affect()
78 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99