Code

Extensions. Color marker improvements (Bug #692582, Color markers to match stroke...
[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
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")
30         self.OptionParser.add_option("-t", "--type",
31                         action="store", type="string", 
32                         dest="fill_type", default="stroke",
33                         help="Replace the marker fill with the object stroke or fill color")
34         self.OptionParser.add_option("-a", "--alpha",
35                         action="store", type="inkbool", 
36                         dest="assign_alpha", default=True,
37                         help="Assign the object fill and stroke alpha to the marker")
39     def effect(self):
40         defs = self.xpathSingle('/svg:svg//svg:defs')
41         if defs == None:
42             defs = inkex.etree.SubElement(self.document.getroot(),inkex.addNS('defs','svg'))
43         
44         for id, node in self.selected.iteritems():
45             mprops = ['marker','marker-start','marker-mid','marker-end']
46             try:
47                 style = simplestyle.parseStyle(node.get('style'))
48             except:
49                 inkex.errormsg(_("No style attribute found for id: %s") % id)
50                 continue
51             
52             stroke = style.get('stroke', '#000000')
53             stroke_opacity = style.get('stroke-opacity', '1')
54             if (self.options.fill_type == "white"):
55                 fill = "#FFFFFF"
56                 fill_opacity = "1"
57             else:
58                 fill = style.get('fill', '#000000')
59                 fill_opacity = style.get('fill-opacity', '1')
61             for mprop in mprops:
62                 if style.has_key(mprop) and style[mprop] != 'none'and style[mprop][:5] == 'url(#':
63                     marker_id = style[mprop][5:-1]
65                     try:
66                         old_mnode = self.xpathSingle('/svg:svg//svg:marker[@id="%s"]' % marker_id)
67                         if not self.options.modify:
68                             mnode = copy.deepcopy(old_mnode)
69                         else:
70                             mnode = old_mnode
71                     except:
72                         inkex.errormsg(_("unable to locate marker: %s") % marker_id)
73                         continue
74                         
75                     new_id = self.uniqueId(marker_id, not self.options.modify)
76                     
77                     style[mprop] = "url(#%s)" % new_id
78                     mnode.set('id', new_id)
79                     mnode.set(inkex.addNS('stockid','inkscape'), new_id)
80                     defs.append(mnode)
81                     
82                     children = mnode.xpath('.//*[@style]', namespaces=inkex.NSS)
83                     for child in children:
84                         cstyle = simplestyle.parseStyle(child.get('style'))
85                         if ('stroke' in cstyle and cstyle['stroke'] != 'none') or 'stroke' not in cstyle:
86                             cstyle['stroke'] = stroke
87                             if (self.options.assign_alpha):
88                                 cstyle['stroke-opacity'] = stroke_opacity
89                         if ('fill' in cstyle and cstyle['fill'] != 'none') or 'fill' not in cstyle:
90                             if (self.options.fill_type == "fill" or self.options.fill_type == "white" ):
91                                 cstyle['fill'] = fill
92                                 if (self.options.assign_alpha):
93                                     cstyle['fill-opacity'] = fill_opacity
94                             elif (self.options.fill_type == "stroke"):
95                                 cstyle['fill'] = stroke
96                                 if (self.options.assign_alpha):
97                                     cstyle['fill-opacity'] = stroke_opacity
98                             else:
99                                 cstyle['fill'] = "none";
100                                 cstyle['fill-opacity'] = "0"
101                         child.set('style',simplestyle.formatStyle(cstyle))
102             node.set('style',simplestyle.formatStyle(style))
104 if __name__ == '__main__':
105     e = MyEffect()
106     e.affect()
109 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99