Code

Extensions. Color marker improvements (Bug #692582, Color markers to match stroke...
[inkscape.git] / share / extensions / markers_strokepaint.py
index 163767f1aaa4f5d423c16055eba39c4e5fa5dfd2..bf5b530351f690b817f6c7fb4ff22f5d166e0d1d 100644 (file)
-#!/usr/bin/env python \r
-'''\r
-Copyright (C) 2006 Aaron Spike, aaron@ekips.org\r
-\r
-This program is free software; you can redistribute it and/or modify\r
-it under the terms of the GNU General Public License as published by\r
-the Free Software Foundation; either version 2 of the License, or\r
-(at your option) any later version.\r
-\r
-This program is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License\r
-along with this program; if not, write to the Free Software\r
-Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
-'''\r
-import copy, inkex, simplestyle\r
-\r
-class MyEffect(inkex.Effect):\r
-    def __init__(self):\r
-        inkex.Effect.__init__(self)\r
-    def xpathSingle(self, path):\r
-        try:\r
-            retval = inkex.xml.xpath.Evaluate(path,self.document,context=self.ctx)[0]\r
-        except:\r
-            retval = None\r
-        return retval\r
-        \r
-    def effect(self):\r
-        self.ctx = inkex.xml.xpath.Context.Context(self.document,processorNss=inkex.NSS)\r
-        defs = self.xpathSingle('/svg/defs')  \r
-        for id, node in self.selected.iteritems():\r
-            mprops = ['marker','marker-start','marker-mid','marker-end']\r
-            style = simplestyle.parseStyle(node.attributes.getNamedItem('style').value)\r
-            \r
-            try:\r
-                stroke = style['stroke']\r
-            except:\r
-                stroke = '#000000'\r
-            \r
-            for mprop in mprops:\r
-                if style.has_key(mprop) and style[mprop] != 'none'and style[mprop][:5] == 'url(#':\r
-                    marker_id = style[mprop][5:-1]\r
-                    old_mnode = self.xpathSingle('/svg//marker[@id="%s"]' % marker_id)\r
-                    mnode = old_mnode.cloneNode(True)\r
-                    new_id = "%s%s" % (marker_id,2)\r
-                    style[mprop] = "url(#%s)" % new_id\r
-                    mnode.attributes.getNamedItem('id').value = new_id\r
-                    defs.appendChild(mnode)\r
-                    \r
-                    children = inkex.xml.xpath.Evaluate('/svg//marker[@id="%s"]//*[@style]' % new_id,self.document,context=self.ctx)\r
-                    for child in children:\r
-                        inkex.debug(child.attributes.getNamedItem('style').value)\r
-                        cstyle = simplestyle.parseStyle(child.attributes.getNamedItem('style').value)\r
-                        if (cstyle.has_key('stroke') and cstyle['stroke'] != 'none') or not cstyle.has_key('stroke'):\r
-                                cstyle['stroke'] = stroke\r
-                        if (cstyle.has_key('fill') and cstyle['fill'] != 'none') or not cstyle.has_key('fill'):\r
-                                cstyle['fill'] = stroke\r
-                        child.attributes.getNamedItem('style').value = simplestyle.formatStyle(cstyle)\r
-            node.attributes.getNamedItem('style').value = simplestyle.formatStyle(style)\r
-e = MyEffect()\r
-e.affect()\r
+#!/usr/bin/env python 
+'''
+Copyright (C) 2006 Aaron Spike, aaron@ekips.org
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+'''
+import random, inkex, simplestyle, copy
+import gettext
+_ = gettext.gettext
+
+class MyEffect(inkex.Effect):
+    def __init__(self):
+        inkex.Effect.__init__(self)
+        self.OptionParser.add_option("-m", "--modify",
+                        action="store", type="inkbool", 
+                        dest="modify", default=False,
+                        help="Do not create a copy, modify the markers")
+        self.OptionParser.add_option("-t", "--type",
+                        action="store", type="string", 
+                        dest="fill_type", default="stroke",
+                        help="Replace the marker fill with the object stroke or fill color")
+        self.OptionParser.add_option("-a", "--alpha",
+                        action="store", type="inkbool", 
+                        dest="assign_alpha", default=True,
+                        help="Assign the object fill and stroke alpha to the marker")
+
+    def effect(self):
+        defs = self.xpathSingle('/svg:svg//svg:defs')
+        if defs == None:
+            defs = inkex.etree.SubElement(self.document.getroot(),inkex.addNS('defs','svg'))
+        
+        for id, node in self.selected.iteritems():
+            mprops = ['marker','marker-start','marker-mid','marker-end']
+            try:
+                style = simplestyle.parseStyle(node.get('style'))
+            except:
+                inkex.errormsg(_("No style attribute found for id: %s") % id)
+                continue
+            
+            stroke = style.get('stroke', '#000000')
+            stroke_opacity = style.get('stroke-opacity', '1')
+            if (self.options.fill_type == "white"):
+                fill = "#FFFFFF"
+                fill_opacity = "1"
+            else:
+                fill = style.get('fill', '#000000')
+                fill_opacity = style.get('fill-opacity', '1')
+
+            for mprop in mprops:
+                if style.has_key(mprop) and style[mprop] != 'none'and style[mprop][:5] == 'url(#':
+                    marker_id = style[mprop][5:-1]
+
+                    try:
+                        old_mnode = self.xpathSingle('/svg:svg//svg:marker[@id="%s"]' % marker_id)
+                        if not self.options.modify:
+                            mnode = copy.deepcopy(old_mnode)
+                        else:
+                            mnode = old_mnode
+                    except:
+                        inkex.errormsg(_("unable to locate marker: %s") % marker_id)
+                        continue
+                        
+                    new_id = self.uniqueId(marker_id, not self.options.modify)
+                    
+                    style[mprop] = "url(#%s)" % new_id
+                    mnode.set('id', new_id)
+                    mnode.set(inkex.addNS('stockid','inkscape'), new_id)
+                    defs.append(mnode)
+                    
+                    children = mnode.xpath('.//*[@style]', namespaces=inkex.NSS)
+                    for child in children:
+                        cstyle = simplestyle.parseStyle(child.get('style'))
+                        if ('stroke' in cstyle and cstyle['stroke'] != 'none') or 'stroke' not in cstyle:
+                            cstyle['stroke'] = stroke
+                            if (self.options.assign_alpha):
+                                cstyle['stroke-opacity'] = stroke_opacity
+                        if ('fill' in cstyle and cstyle['fill'] != 'none') or 'fill' not in cstyle:
+                            if (self.options.fill_type == "fill" or self.options.fill_type == "white" ):
+                                cstyle['fill'] = fill
+                                if (self.options.assign_alpha):
+                                    cstyle['fill-opacity'] = fill_opacity
+                            elif (self.options.fill_type == "stroke"):
+                                cstyle['fill'] = stroke
+                                if (self.options.assign_alpha):
+                                    cstyle['fill-opacity'] = stroke_opacity
+                            else:
+                                cstyle['fill'] = "none";
+                                cstyle['fill-opacity'] = "0"
+                        child.set('style',simplestyle.formatStyle(cstyle))
+            node.set('style',simplestyle.formatStyle(style))
+
+if __name__ == '__main__':
+    e = MyEffect()
+    e.affect()
+
+
+# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99