From: acspike Date: Thu, 20 Jul 2006 14:54:57 +0000 (+0000) Subject: add some convenience methods to inkex and allow the possiblity of replacing the marke... X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=635bc6783b4541149e6a1693e50eed4df4eb85ff;p=inkscape.git add some convenience methods to inkex and allow the possiblity of replacing the markers in place. --- diff --git a/share/extensions/inkex.py b/share/extensions/inkex.py index e17111057..e210e90c2 100755 --- a/share/extensions/inkex.py +++ b/share/extensions/inkex.py @@ -19,7 +19,7 @@ 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 sys, copy, optparse +import sys, copy, optparse, random #a dictionary of all of the xmlns prefixes in a standard inkscape doc NSS = { @@ -60,8 +60,11 @@ class InkOption(optparse.Option): class Effect: """A class for creating Inkscape SVG Effects""" def __init__(self): + self.id_characters = '0123456789abcdefghijklmnopqrstuvwkyzABCDEFGHIJKLMNOPQRSTUVWXYZ' self.document=None + self.ctx=None self.selected={} + self.doc_ids={} self.options=None self.args=None self.OptionParser = optparse.OptionParser(usage="usage: %prog [options] SVGfile",option_class=InkOption) @@ -84,6 +87,7 @@ class Effect: except: stream = sys.stdin self.document = reader.fromStream(stream) + self.ctx = xml.xpath.Context.Context(self.document,processorNss=NSS) stream.close() def getposinlayer(self): ctx = xml.xpath.Context.Context(self.document,processorNss=NSS) @@ -111,6 +115,10 @@ class Effect: path = '//*[@id="%s"]' % id for node in xml.xpath.Evaluate(path,self.document): self.selected[id] = node + def getdocids(self): + docIdNodes = xml.xpath.Evaluate('//@id',self.document,context=self.ctx) + for m in docIdNodes: + self.doc_ids[m.value] = 1 def output(self): """Serialize document into XML on stdout""" xml.dom.ext.Print(self.document) @@ -120,5 +128,22 @@ class Effect: self.parse() self.getposinlayer() self.getselected() + self.getdocids() self.effect() self.output() + + def uniqueId(self, old_id, make_new_id = True): + new_id = old_id + if make_new_id: + while new_id in self.doc_ids: + new_id = "%s%s" % (new_id,random.choice(self.id_characters)) + self.doc_ids[new_id] = 1 + return new_id + def xpathSingle(self, path): + try: + retval = xml.xpath.Evaluate(path,self.document,context=self.ctx)[0] + except: + debug("No matching node for expression: %s" % path) + retval = None + return retval + diff --git a/share/extensions/markers_strokepaint.py b/share/extensions/markers_strokepaint.py index 4b261cba6..6d6331cdb 100644 --- a/share/extensions/markers_strokepaint.py +++ b/share/extensions/markers_strokepaint.py @@ -21,28 +21,17 @@ import random, inkex, simplestyle class MyEffect(inkex.Effect): def __init__(self): inkex.Effect.__init__(self) - def xpathSingle(self, path): - try: - retval = inkex.xml.xpath.Evaluate(path,self.document,context=self.ctx)[0] - except: - inkex.debug("No matching node for expression: %s" % path) - retval = None - return retval + self.OptionParser.add_option("-m", "--modify", + action="store", type="inkbool", + dest="modify", default=False, + help="do not create a copy, modify the markers") def effect(self): - self.ctx = inkex.xml.xpath.Context.Context(self.document,processorNss=inkex.NSS) - id_characters = '0123456789abcdefghijklmnopqrstuvwkyzABCDEFGHIJKLMNOPQRSTUVWXYZ' - defs = self.xpathSingle('/svg//defs') if not defs: defs = self.document.createElement('svg:defs') self.document.documentElement.appendChile(defs) - doc_ids = {} - docIdNodes = inkex.xml.xpath.Evaluate('//@id',self.document,context=self.ctx) - for m in docIdNodes: - doc_ids[m.value] = 1 - for id, node in self.selected.iteritems(): mprops = ['marker','marker-start','marker-mid','marker-end'] try: @@ -58,19 +47,19 @@ class MyEffect(inkex.Effect): marker_id = style[mprop][5:-1] try: old_mnode = self.xpathSingle('/svg//marker[@id="%s"]' % marker_id) - mnode = old_mnode.cloneNode(True) + if not self.options.modify: + mnode = old_mnode.cloneNode(True) + else: + mnode = old_mnode except: inkex.debug("unable to locate marker: %s" % marker_id) continue - #generate a unique id - new_id = marker_id - while new_id in doc_ids: - new_id = "%s%s" % (new_id,random.choice(id_characters)) - doc_ids[new_id] = 1 + new_id = self.uniqueId(marker_id, not self.options.modify) style[mprop] = "url(#%s)" % new_id mnode.attributes.getNamedItem('id').value = new_id + mnode.attributes.getNamedItemNS(inkex.NSS['inkscape'],'stockid').value = new_id defs.appendChild(mnode) children = inkex.xml.xpath.Evaluate('/svg//marker[@id="%s"]//*[@style]' % new_id,self.document,context=self.ctx)