Code

e37c816561b6992729ef2797ca64d1de85c795b8
[inkscape.git] / share / extensions / markers_strokepaint.py
1 #!/usr/bin/env python 
2 '''
3 Copyright (C) 2006 Aaron Spike, aaron@ekips.org
4 Copyright (C) 2010 Nicolas Dufour, nicoduf@yahoo.fr (color options)
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 '''
20 import random, inkex, simplestyle, copy
21 import gettext
22 _ = gettext.gettext
24 class MyEffect(inkex.Effect):
25     def __init__(self):
26         inkex.Effect.__init__(self)
27         self.OptionParser.add_option("-m", "--modify",
28                         action="store", type="inkbool", 
29                         dest="modify", default=False,
30                         help="Do not create a copy, modify the markers")
31         self.OptionParser.add_option("-t", "--type",
32                         action="store", type="string", 
33                         dest="fill_type", default="stroke",
34                         help="Replace the markers' fill with the object stroke or fill color")
35         self.OptionParser.add_option("-a", "--alpha",
36                         action="store", type="inkbool", 
37                         dest="assign_alpha", default=True,
38                         help="Assign the object fill and stroke alpha to the markers")
39         self.OptionParser.add_option("-i", "--invert",
40                         action="store", type="inkbool", 
41                         dest="invert", default=False,
42                         help="Invert fill and stroke colors")
43         self.OptionParser.add_option("--assign_fill",
44                         action="store", type="inkbool", 
45                         dest="assign_fill", default=True,
46                         help="Assign a fill color to the markers")
47         self.OptionParser.add_option("-f", "--fill_color",
48                         action="store", type="int", 
49                         dest="fill_color", default=1364325887,
50                         help="Choose a custom fill color")
51         self.OptionParser.add_option("--assign_stroke",
52                         action="store", type="inkbool", 
53                         dest="assign_stroke", default=True,
54                         help="Assign a stroke color to the markers")
55         self.OptionParser.add_option("-s", "--stroke_color",
56                         action="store", type="int", 
57                         dest="stroke_color", default=1364325887,
58                         help="Choose a custom fill color")
59         self.OptionParser.add_option("--tab",
60                         action="store", type="string",
61                         dest="tab",
62                         help="The selected UI-tab when OK was pressed")
63         self.OptionParser.add_option("--colortab",
64                         action="store", type="string",
65                         dest="colortab",
66                         help="The selected cutom color tab when OK was pressed")
68     def effect(self):
69         defs = self.xpathSingle('/svg:svg//svg:defs')
70         if defs == None:
71             defs = inkex.etree.SubElement(self.document.getroot(),inkex.addNS('defs','svg'))
72         
73         for id, node in self.selected.iteritems():
74             mprops = ['marker','marker-start','marker-mid','marker-end']
75             try:
76                 style = simplestyle.parseStyle(node.get('style'))
77             except:
78                 inkex.errormsg(_("No style attribute found for id: %s") % id)
79                 continue
81             # Use object colors
82             if self.options.tab == '"object"':
83                 temp_stroke = style.get('stroke', '#000000')
84                 temp_fill = style.get('fill', '#000000')
85                 if (self.options.invert):
86                     fill = temp_stroke
87                     stroke = temp_fill
88                 else:
89                     fill = temp_fill
90                     stroke = temp_stroke
91                 if (self.options.assign_alpha):
92                     temp_stroke_opacity = style.get('stroke-opacity', '1')
93                     temp_fill_opacity = style.get('fill-opacity', '1')
94                     if (self.options.invert):
95                         fill_opacity = temp_stroke_opacity
96                         stroke_opacity = temp_fill_opacity
97                     else:
98                         fill_opacity = temp_fill_opacity
99                         stroke_opacity = temp_stroke_opacity
100                 if (self.options.fill_type == "solid"):
101                     fill = stroke
102                     if (self.options.assign_alpha):
103                         fill_opacity = stroke_opacity
104             # Choose custom colors
105             elif self.options.tab == '"custom"':
106                 fill_red = ((self.options.fill_color >> 24) & 255)
107                 fill_green = ((self.options.fill_color >> 16) & 255)
108                 fill_blue = ((self.options.fill_color >>  8) & 255)
109                 fill = "rgb(%s,%s,%s)" % (fill_red, fill_green, fill_blue)
110                 fill_opacity = (((self.options.fill_color) & 255) / 255.)
111                 stroke_red = ((self.options.stroke_color >> 24) & 255)
112                 stroke_green = ((self.options.stroke_color >> 16) & 255)
113                 stroke_blue = ((self.options.stroke_color >>  8) & 255)
114                 stroke = "rgb(%s,%s,%s)" % (stroke_red, stroke_green, stroke_blue)
115                 stroke_opacity = (((self.options.stroke_color) & 255) / 255.)
116                 if (not(self.options.assign_fill)):
117                     fill = "none";
118                 if (not(self.options.assign_stroke)):
119                     stroke = "none";
121             for mprop in mprops:
122                 if style.has_key(mprop) and style[mprop] != 'none'and style[mprop][:5] == 'url(#':
123                     marker_id = style[mprop][5:-1]
125                     try:
126                         old_mnode = self.xpathSingle('/svg:svg//svg:marker[@id="%s"]' % marker_id)
127                         if not self.options.modify:
128                             mnode = copy.deepcopy(old_mnode)
129                         else:
130                             mnode = old_mnode
131                     except:
132                         inkex.errormsg(_("unable to locate marker: %s") % marker_id)
133                         continue
134                         
135                     new_id = self.uniqueId(marker_id, not self.options.modify)
136                     
137                     style[mprop] = "url(#%s)" % new_id
138                     mnode.set('id', new_id)
139                     mnode.set(inkex.addNS('stockid','inkscape'), new_id)
140                     defs.append(mnode)
141                     
142                     children = mnode.xpath('.//*[@style]', namespaces=inkex.NSS)
143                     for child in children:
144                         cstyle = simplestyle.parseStyle(child.get('style'))
145                         if (not(self.options.tab == '"object"' and cstyle['stroke'] == 'none' and self.options.fill_type == "filled")):
146                             cstyle['stroke'] = stroke
147                             if 'stroke_opacity' in locals():
148                                 cstyle['stroke-opacity'] = stroke_opacity
149                         if (not(self.options.tab == '"object"' and cstyle['fill'] == 'none' and self.options.fill_type == "solid")):
150                             cstyle['fill'] = fill
151                             if 'fill_opacity' in locals():
152                                 cstyle['fill-opacity'] = fill_opacity
153                         child.set('style',simplestyle.formatStyle(cstyle))
154             node.set('style',simplestyle.formatStyle(style))
156 if __name__ == '__main__':
157     e = MyEffect()
158     e.affect()
161 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99