From: acspike Date: Sun, 24 Jun 2007 13:17:46 +0000 (+0000) Subject: more pyxml to lxml conversion. X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=60bbcba041e80a4b29118269c0897df5c068563e;p=inkscape.git more pyxml to lxml conversion. I'd like to get someone else to look at Barcode and the TeX extension. Some of the big name extensions like pathalongpath.py still don't work but I have started converting (and somehow broke them). committing in case someone wants to help fix. :-) --- diff --git a/share/extensions/lorem_ipsum.py b/share/extensions/lorem_ipsum.py index d246fe0b4..2de7b0e60 100644 --- a/share/extensions/lorem_ipsum.py +++ b/share/extensions/lorem_ipsum.py @@ -205,37 +205,26 @@ class MyEffect(inkex.Effect): def addText(self, node): for i in range(self.options.num): - para=self.document.createElement('flowPara') - text=self.document.createTextNode(self.makePara()) - para.appendChild(text) - node.appendChild(para) - node.appendChild(self.document.createElement('flowPara')) + para=inkex.etree.SubElement(node,inkex.addNS('flowPara','svg')) + para.text = self.makePara() + inkex.etree.SubElement(node,inkex.addNS('flowPara','svg')) def effect(self): found=0 for id, node in self.selected.iteritems(): - if node.tagName == 'flowRoot': + if node.tag == inkex.addNS('flowRoot','svg'): found+=1 if found==1: self.addText(node) if found==0: #inkex.debug('No "flowRoot" elements selected. Unable to add text.') - svg=self.document.getElementsByTagName('svg')[0] - g=self.document.createElement('g') - g.setAttribute('inkscape:label','fill text') - g.setAttribute('inkscape:groupmode','layer') - flowRoot=self.document.createElement('flowRoot') - flowRoot.setAttribute('xml:space','preserve') - flowRegion=self.document.createElement('flowRegion') - rect=self.document.createElement('rect') - rect.setAttribute('x','0') - rect.setAttribute('y','0') - rect.setAttribute('width',svg.getAttribute('width')) - rect.setAttribute('height',svg.getAttribute('height')) - svg.appendChild(g) - g.appendChild(flowRoot) - flowRoot.appendChild(flowRegion) - flowRegion.appendChild(rect) + svg=self.document.getroot() + gattribs = {inkex.addNS('label','inkscape'):'fill text',inkex.addNS('groupmode','inkscape'):'layer'} + g=inkex.etree.SubElement(svg,inkex.addNS('g','svg'),gattribs) + flowRoot=inkex.etree.SubElement(g,inkex.addNS('flowRoot','svg'),{'xml:space':'preserve'}) + flowRegion=inkex.etree.SubElement(flowRoot,inkex.addNS('flowRegion','svg')) + rattribs = {'x':'0','y':'0','width':svg.get('width'),'height':svg.get('height')} + rect=inkex.etree.SubElement(flowRegion,inkex.addNS('rect','svg'),rattribs) self.addText(flowRoot) e = MyEffect() diff --git a/share/extensions/markers_strokepaint.py b/share/extensions/markers_strokepaint.py index 6d6331cdb..a6928f5e4 100644 --- a/share/extensions/markers_strokepaint.py +++ b/share/extensions/markers_strokepaint.py @@ -27,15 +27,14 @@ class MyEffect(inkex.Effect): help="do not create a copy, modify the markers") def effect(self): - defs = self.xpathSingle('/svg//defs') + defs = self.xpathSingle('/svg:svg//svg:defs') if not defs: - defs = self.document.createElement('svg:defs') - self.document.documentElement.appendChile(defs) + 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.attributes.getNamedItem('style').value) + style = simplestyle.parseStyle(node.get('style')) except: inkex.debug("No style attribute found for id: %s" % id) continue @@ -46,9 +45,9 @@ class MyEffect(inkex.Effect): 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//marker[@id="%s"]' % marker_id) + old_mnode = self.xpathSingle('/svg:svg//svg:marker[@id="%s"]' % marker_id) if not self.options.modify: - mnode = old_mnode.cloneNode(True) + mnode = inkex.etree.fromstring(inkex.etree.tostring(old_mnode)) else: mnode = old_mnode except: @@ -58,18 +57,18 @@ class MyEffect(inkex.Effect): 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) + mnode.set('id', new_id) + mnode.set(inkex.addNS('stockid','inkscape'), new_id) + defs.append(mnode) - children = inkex.xml.xpath.Evaluate('/svg//marker[@id="%s"]//*[@style]' % new_id,self.document,context=self.ctx) + children = self.document.getroot().xpath('/svg:svg//svg:marker[@id="%s"]//*[@style]' % new_id,inkex.NSS) for child in children: - cstyle = simplestyle.parseStyle(child.attributes.getNamedItem('style').value) + cstyle = simplestyle.parseStyle(child.get('style')) if ('stroke' in cstyle and cstyle['stroke'] != 'none') or 'stroke' not in cstyle: cstyle['stroke'] = stroke if ('fill' in cstyle and cstyle['fill'] != 'none') or 'fill' not in cstyle: cstyle['fill'] = stroke - child.attributes.getNamedItem('style').value = simplestyle.formatStyle(cstyle) - node.attributes.getNamedItem('style').value = simplestyle.formatStyle(style) + child.set('style',simplestyle.formatStyle(cstyle)) + node.set('style',simplestyle.formatStyle(style)) e = MyEffect() e.affect() diff --git a/share/extensions/measure.py b/share/extensions/measure.py index 4a4435ceb..b37303f1b 100644 --- a/share/extensions/measure.py +++ b/share/extensions/measure.py @@ -110,21 +110,18 @@ class Length(inkex.Effect): def effect(self): # get number of digits prec = int(self.options.precision) - # loop over all selected pathes + # loop over all selected paths for id, node in self.selected.iteritems(): - if node.tagName == 'path': - # self.group = self.document.createElement('svg:g') - self.group = self.document.createElement('svg:text') - node.parentNode.appendChild(self.group) + if node.tag == inkex.addNS('path','svg'): + self.group = inkex.etree.SubElement(node.getparent(),inkex.addNS('text','svg')) - try: - t = node.attributes.getNamedItem('transform').value - self.group.setAttribute('transform', t) - except AttributeError: - pass + t = node.get('transform') + if t: + self.group.set('transform', t) + a =[] - p = cubicsuperpath.parsePath(node.attributes.getNamedItem('d').value) + p = cubicsuperpath.parsePath(node.get('d')) num = 1 slengths, stotal = csplength(p) ''' Wio: Umrechnung in unit ''' @@ -155,22 +152,20 @@ class Length(inkex.Effect): def addTextOnPath(self,node,x,y,text, id,dy=0): - #new = self.document.createElement('svg:text') - new = self.document.createElement('svg:textPath') + new = inkex.etree.SubElement(node,inkex.addNS('textPath','svg')) s = {'text-align': 'center', 'vertical-align': 'bottom', 'text-anchor': 'middle', 'font-size': str(self.options.fontsize), 'fill-opacity': '1.0', 'stroke': 'none', 'font-weight': 'normal', 'font-style': 'normal', 'fill': '#000000'} - new.setAttribute('style', simplestyle.formatStyle(s)) - node.setAttribute('x', str(x)) - node.setAttribute('y', str(y)) - #node.setAttribute('transform','rotate(180,'+str(-x)+','+str(-y)+')') - new.setAttributeNS('http://www.w3.org/1999/xlink','xlink:href', '#'+id) - new.setAttribute('startOffset', "50%") - new.setAttribute('dy', str(dy)) # dubious merit - #new.appendChild(tp) - new.appendChild(self.document.createTextNode(str(text))) - node.appendChild(new) + new.set('style', simplestyle.formatStyle(s)) + new.set(inkex.addNS('href','xlink'), '#'+id) + new.set('startOffset', "50%") + new.set('dy', str(dy)) # dubious merit + #new.append(tp) + new.text = str(text) + #node.set('transform','rotate(180,'+str(-x)+','+str(-y)+')') + node.set('x', str(x)) + node.set('y', str(y)) e = Length() e.affect() diff --git a/share/extensions/motion.py b/share/extensions/motion.py index 3834c7f2b..f9533cd50 100755 --- a/share/extensions/motion.py +++ b/share/extensions/motion.py @@ -53,33 +53,26 @@ class Motion(inkex.Effect): a.append([cmd,np[:]]) a.append(['Z',[]]) - face = self.document.createElement('svg:path') - self.facegroup.appendChild(face) - face.setAttribute('d', simplepath.formatPath(a)) - + face = inkex.etree.SubElement(self.facegroup,inkex.addNS('path','svg'),{'d':simplepath.formatPath(a)}) def effect(self): self.vx = math.cos(math.radians(self.options.angle))*self.options.magnitude self.vy = math.sin(math.radians(self.options.angle))*self.options.magnitude for id, node in self.selected.iteritems(): - if node.tagName == 'path': - group = self.document.createElement('svg:g') - self.facegroup = self.document.createElement('svg:g') - node.parentNode.appendChild(group) - group.appendChild(self.facegroup) - group.appendChild(node) + if node.tag == inkex.addNS('path','svg'): + group = inkex.etree.SubElement(node.getparent(),inkex.addNS('g','svg')) + self.facegroup = inkex.etree.SubElement(group, inkex.addNS('g','svg')) + group.append(node) - try: - t = node.attributes.getNamedItem('transform').value - group.setAttribute('transform', t) - node.attributes.getNamedItem('transform').value="" - except AttributeError: - pass - - s = node.attributes.getNamedItem('style').value - self.facegroup.setAttribute('style', s) + t = node.get('transform') + if t: + group.set('transform', t) + node.set('transform','') + + s = node.get('style') + self.facegroup.set('style', s) - p = simplepath.parsePath(node.attributes.getNamedItem('d').value) + p = simplepath.parsePath(node.get('d')) for cmd,params in p: tees = [] if cmd == 'C': diff --git a/share/extensions/pathalongpath.py b/share/extensions/pathalongpath.py index af42a2dee..3901069c9 100644 --- a/share/extensions/pathalongpath.py +++ b/share/extensions/pathalongpath.py @@ -34,7 +34,7 @@ they move and rotate, deforming the pattern. import inkex, cubicsuperpath, bezmisc import pathmodifier -import copy, math, re, random, xml.xpath +import copy, math, re, random def flipxy(path): for pathcomp in path: @@ -212,15 +212,15 @@ class PathAlongPath(pathmodifier.Diffeo): dx=width+self.options.space for id, node in self.patterns.iteritems(): - if node.tagName == 'path': - d = node.attributes.getNamedItem('d') - p0 = cubicsuperpath.parsePath(d.value) + if node.tag == inkex.addNS('path','svg'): + d = node.get('d') + p0 = cubicsuperpath.parsePath(d) if self.options.vertical: flipxy(p0) newp=[] for skelnode in self.skeletons.itervalues(): - self.curSekeleton=cubicsuperpath.parsePath(skelnode.getAttribute('d')) + self.curSekeleton=cubicsuperpath.parsePath(skelnode.get('d')) if self.options.vertical: flipxy(self.curSekeleton) for comp in self.curSekeleton: @@ -261,7 +261,7 @@ class PathAlongPath(pathmodifier.Diffeo): flipxy(p) newp+=p - d.value = cubicsuperpath.formatPath(newp) + node.set('d', cubicsuperpath.formatPath(newp)) e = PathAlongPath() e.affect() diff --git a/share/extensions/pathmodifier.py b/share/extensions/pathmodifier.py index a27c547c1..f10a86b49 100644 --- a/share/extensions/pathmodifier.py +++ b/share/extensions/pathmodifier.py @@ -18,7 +18,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA barraud@math.univ-lille1.fr ''' import inkex, cubicsuperpath, bezmisc, simplestyle -import copy, math, re, random, xml.xpath +import copy, math, re, random def parseTransform(transf,mat=[[1.0,0.0,0.0],[0.0,1.0,0.0]]): if transf=="": @@ -84,9 +84,9 @@ def composeTransform(M1,M2): return [[a11,a12,v1],[a21,a22,v2]] def applyTransformToNode(mat,node): - m=parseTransform(node.getAttributeNS(None,"transform")) + m=parseTransform(node.get("transform")) newtransf=formatTransform(composeTransform(mat,m)) - node.setAttributeNS(None,"transform", newtransf) + node.set("transform", newtransf) def applyTransformToPoint(mat,pt): x=mat[0][0]*pt[0]+mat[0][1]*pt[1]+mat[0][2] @@ -95,15 +95,18 @@ def applyTransformToPoint(mat,pt): pt[1]=y def fuseTransform(node): - m=parseTransform(node.getAttributeNS(None,"transform")) - d = node.getAttributeNS(None,'d') + t = node.get("transform") + if t == None: + return + m=parseTransform(t) + d = node.get('d') p=cubicsuperpath.parsePath(d) for comp in p: for ctl in comp: for pt in ctl: applyTransformToPoint(m,pt) - node.setAttributeNS(None,'d', cubicsuperpath.formatPath(p)) - node.removeAttributeNS(None,"transform") + node.set('d', cubicsuperpath.formatPath(p)) + del node.attribs["transform"] def boxunion(b1,b2): @@ -134,83 +137,83 @@ class PathModifier(inkex.Effect): #-- Selectionlists manipulation -- ################################## def computeBBox(self, aList): - bbox=None - for id, node in aList.iteritems(): - if node.tagName == 'path': - d = node.attributes.getNamedItem('d') - p = cubicsuperpath.parsePath(d.value) - bbox=boxunion(roughBBox(p),bbox) - return bbox + bbox=None + for id, node in aList.iteritems(): + if node.tag == inkex.addNS('path','svg') or node.tag == 'path': + d = node.get('d') + p = cubicsuperpath.parsePath(d) + bbox=boxunion(roughBBox(p),bbox) + return bbox def duplicateNodes(self, aList): clones={} for id,node in aList.iteritems(): - clone=node.cloneNode(True) - #!!!--> should it be given an id? - #seems to work without this!?! - clone.setAttributeNS(None,"id", self.uniqueId(node.tagName)) - node.parentNode.appendChild(clone) - clones[clone.getAttributeNS(None,"id")]=clone + clone=inkex.etree.fromstring(inkex.etree.tostring(node)) + #!!!--> should it be given an id? + #seems to work without this!?! + clone.set("id", self.uniqueId(node.tag)) + node.getparent().append(clone) + clones[clone.get("id")]=clone return(clones) def uniqueId(self, prefix): id="%s%04i"%(prefix,random.randint(0,9999)) - while len(xml.xpath.Evaluate('//*[@id="%s"]' % id,self.document)): + while len(self.document.getroot().xpath('//*[@id="%s"]' % id,inkex.NSS)): id="%s%04i"%(prefix,random.randint(0,9999)) return(id) def expandGroups(self,aList,transferTransform=True): for id, node in aList.items(): - if node.tagName == 'g': - mat=parseTransform(node.getAttributeNS(None,"transform")) - for child in node.childNodes: - if child.nodeType==child.ELEMENT_NODE: - if transferTransform: - applyTransformToNode(mat,child) - aList.update(self.expandGroups({child.getAttribute('id'):child})) + if node.tag == inkex.addNS('g','svg'): + mat=parseTransform(node.get("transform")) + for child in node: + if transferTransform: + applyTransformToNode(mat,child) + aList.update(self.expandGroups({child.get('id'):child})) if transferTransform: - node.removeAttribute("transform") + del node.attribs["transform"] del aList[id] - return(aList) + return(aList) def expandGroupsUnlinkClones(self,aList,transferTransform=True,doReplace=True): for id in aList.keys()[:]: node=aList[id] - if node.tagName == 'g': + if node.tag == inkex.addNS('g','svg'): self.expandGroups(aList,transferTransform) self.expandGroupsUnlinkClones(aList,transferTransform,doReplace) - #Hum... not very efficient if there are many clones of groups... - elif node.tagName == 'use': - refid=node.getAttributeNS(inkex.NSS[u'xlink'],'href') + #Hum... not very efficient if there are many clones of groups... + elif node.tag == inkex.addNS('use','svg'): + refid=node.get(inkex.addNS('href','xlink')) path = '//*[@id="%s"]' % refid[1:] - refnode = xml.xpath.Evaluate(path,self.document)[0] - newnode=refnode.cloneNode(True) - self.recursNewIds(newnode) - - if node.hasAttributeNS(None,u'style'): - style=simplestyle.parseStyle(node.getAttributeNS(None,u'style')) - refstyle=simplestyle.parseStyle(refnode.getAttributeNS(None,u'style')) - style.update(refstyle) - newnode.setAttributeNS(None,'style',simplestyle.formatStyle(style)) - applyTransformToNode(parseTransform(node.getAttributeNS(None,'transform')),newnode) - if doReplace: - parent=node.parentNode - parent.insertBefore(newnode,node) - parent.removeChild(node) - del aList[id] - newid=newnode.getAttributeNS(None,'id') + refnode = self.document.getroot().xpath(path,inkex.NSS) + newnode=inkex.etree.fromstring(inkex.etree.tostring(refnode)) + self.recursNewIds(newnode) + + s = node.get('style') + if s: + style=simplestyle.parseStyle(s) + refstyle=simplestyle.parseStyle(refnode.get('style')) + style.update(refstyle) + newnode.set('style',simplestyle.formatStyle(style)) + applyTransformToNode(parseTransform(node.get('transform')),newnode) + if doReplace: + parent=node.getparent() + parent.insert(node.index,newnode) + parent.remove(node) + del aList[id] + newid=newnode.get('id') aList.update(self.expandGroupsUnlinkClones({newid:newnode},transferTransform,doReplace)) return aList def recursNewIds(self,node): - if node.nodeType==node.ELEMENT_NODE and node.hasAttributeNS(None,u'id'): - node.setAttributeNS(None,u'id',self.uniqueId(node.tagName)) - for child in node.childNodes: - self.recursNewIds(child) - + if node.get('id'): + node.set('id',self.uniqueId(node.tag)) + for child in node: + self.recursNewIds(child) + - +# Had not been rewritten for ElementTree # def makeClonesReal(self,aList,doReplace=True,recursivelytransferTransform=True): # for id in aList.keys(): # node=aList[id] @@ -246,17 +249,18 @@ class PathModifier(inkex.Effect): ################################ def rectToPath(self,node,doReplace=True): - if node.tagName == 'rect': - x =float(node.getAttributeNS(None,u'x')) - y =float(node.getAttributeNS(None,u'y')) + if node.tag == inkex.addNS('rect','svg'): + x =float(node.get('x')) + y =float(node.get('y')) + #FIXME: no exception anymore and sometimes just one try: - rx=float(node.getAttributeNS(None,u'rx')) - ry=float(node.getAttributeNS(None,u'ry')) + rx=float(node.get('rx')) + ry=float(node.get('ry')) except: rx=0 ry=0 - w =float(node.getAttributeNS(None,u'width' )) - h =float(node.getAttributeNS(None,u'height')) + w =float(node.get('width' )) + h =float(node.get('height')) d ='M %f,%f '%(x+rx,y) d+='L %f,%f '%(x+w-rx,y) d+='A %f,%f,%i,%i,%i,%f,%f '%(rx,ry,0,0,1,x+w,y+ry) @@ -267,33 +271,39 @@ class PathModifier(inkex.Effect): d+='L %f,%f '%(x,y+ry) d+='A %f,%f,%i,%i,%i,%f,%f '%(rx,ry,0,0,1,x+rx,y) - newnode=self.document.createElement('path') - newnode.setAttributeNS(None,'d',d) - newnode.setAttributeNS(None,'id', self.uniqueId('path')) - newnode.setAttributeNS(None,'style',node.getAttributeNS(None,u'style')) - newnode.setAttributeNS(None,'transform',node.getAttributeNS(None,u'transform')) - fuseTransform(newnode) + newnode=inkex.etree.Element('path') + newnode.set('d',d) + newnode.set('id', self.uniqueId('path')) + newnode.set('style',node.get('style')) + nnt = node.get('transform') + if nnt: + newnode.set('transform',nnt) + fuseTransform(newnode) if doReplace: - parent=node.parentNode - parent.insertBefore(newnode,node) - parent.removeChild(node) + parent=node.getparent() + parent.insert(node.index,newnode) + parent.remove(node) return newnode def objectToPath(self,node,doReplace=True): - #--TODO: support other object types!!!! - #--TODO: make sure cubicsuperpath supports A and Q commands... - if node.tagName == 'rect': - return(self.rectToPath(node,doReplace)) - elif node.tagName == 'path': - attributes = node.attributes.keys() - for uri,attName in attributes: - if uri in [inkex.NSS[u'sodipodi'],inkex.NSS[u'inkscape']]: -# if attName not in ["d","id","style","transform"]: - node.removeAttributeNS(uri,attName) + #--TODO: support other object types!!!! + #--TODO: make sure cubicsuperpath supports A and Q commands... + if node.tag == inkex.addNS('rect','svg'): + return(self.rectToPath(node,doReplace)) + elif node.tag == inkex.addNS('path','svg') or node.tag == 'path': + attributes = node.keys() + for attName in attributes: + uri = None + if attName[0] == '{': + uri,attName = attName.split('}') + uri = uri[1:] + if uri in [inkex.NSS[u'sodipodi'],inkex.NSS[u'inkscape']]: + #if attName not in ["d","id","style","transform"]: + del node.attribs[inkex.addNS(attName,uri)] fuseTransform(node) return node else: - inkex.debug("Please first convert objects to paths!...(got '%s')"%node.tagName) + inkex.debug("Please first convert objects to paths!...(got '%s')"%node.tag) return None def objectsToPaths(self,aList,doReplace=True): @@ -301,27 +311,27 @@ class PathModifier(inkex.Effect): for id,node in aList.items(): newnode=self.objectToPath(node,self.document) del aList[id] - aList[newnode.getAttributeNS(None,u'id')]=newnode + aList[newnode.get('id')]=newnode ################################ #-- Action ---------- ################################ - + #-- overwrite this method in subclasses... def effect(self): #self.duplicateNodes(self.selected) self.expandGroupsUnlinkClones(self.selected, True) - self.objectsToPaths(self.selected, True) + self.objectsToPaths(self.selected, True) self.bbox=self.computeBBox(self.selected) for id, node in self.selected.iteritems(): - if node.tagName == 'path': - d = node.attributes.getNamedItem('d') - p = cubicsuperpath.parsePath(d.value) + if node.tag == inkex.addNS('path','svg'): + d = node.get('d') + p = cubicsuperpath.parsePath(d) #do what ever you want with p! - d.value = cubicsuperpath.formatPath(p) + node.set('d',cubicsuperpath.formatPath(p)) class Diffeo(PathModifier): @@ -355,20 +365,20 @@ class Diffeo(PathModifier): def effect(self): #self.duplicateNodes(self.selected) - self.expandGroupsUnlinkClones(self.selected, True) + self.expandGroupsUnlinkClones(self.selected, True) self.expandGroups(self.selected, True) - self.objectsToPaths(self.selected, True) + self.objectsToPaths(self.selected, True) self.bbox=self.computeBBox(self.selected) for id, node in self.selected.iteritems(): - if node.tagName == 'path': - d = node.attributes.getNamedItem('d') - p = cubicsuperpath.parsePath(d.value) + if node.tag == inkex.addNS('path','svg'): + d = node.get('d') + p = cubicsuperpath.parsePath(d) for sub in p: for ctlpt in sub: self.applyDiffeo(ctlpt[1],(ctlpt[0],ctlpt[2])) - d.value = cubicsuperpath.formatPath(p) + node.set('d',cubicsuperpath.formatPath(p)) #e = Diffeo() #e.affect() diff --git a/share/extensions/perspective.py b/share/extensions/perspective.py index 014a7b779..84bd9c768 100755 --- a/share/extensions/perspective.py +++ b/share/extensions/perspective.py @@ -56,8 +56,8 @@ class Project(inkex.Effect): #obj is selected second obj = self.selected[self.options.ids[0]] envelope = self.selected[self.options.ids[1]] - if (obj.tagName == 'path' or obj.tagName == 'g') and envelope.tagName == 'path': - path = cubicsuperpath.parsePath(envelope.attributes.getNamedItem('d').value) + if (obj.tag == inkex.addNS('path','svg') or obj.tag == inkex.addNS('g','svg')) and envelope.tag == inkex.addNS('path','svg'): + path = cubicsuperpath.parsePath(envelope.get('d')) dp = zeros((4,2), dtype=float64) for i in range(4): dp[i][0] = path[0][i][1][0] @@ -71,7 +71,7 @@ class Project(inkex.Effect): _,f,err = os.popen3("inkscape --query-%s --query-id=%s %s" % (query,id,file)) q[query] = float(f.read()) f.close() - err.close() + err.close() sp = array([[q['x'], q['y']+q['height']],[q['x'], q['y']],[q['x']+q['width'], q['y']],[q['x']+q['width'], q['y']+q['height']]], dtype=float64) solmatrix = zeros((8,8), dtype=float64) @@ -92,30 +92,29 @@ class Project(inkex.Effect): res = solve(solmatrix, free_term) projmatrix = array([[res[0],res[1],res[2]],[res[3],res[4],res[5]],[res[6],res[7],1.0]],dtype=float64) - if obj.tagName == "path": + if obj.tag == inkex.addNS("path",'svg'): self.process_path(obj,projmatrix) - if obj.tagName == "g": + if obj.tag == inkex.addNS("g",'svg'): self.process_group(obj,projmatrix) def process_group(self,group,m): - for node in group.childNodes: - if node.nodeType==node.ELEMENT_NODE: - if node.tagName == 'path': - self.process_path(node,m) - if node.tagName == 'g': - self.process_group(node,m) + for node in group: + if node.tag == inkex.addNS('path','svg'): + self.process_path(node,m) + if node.tagName == inkex.addNS('g','svg'): + self.process_group(node,m) def process_path(self,path,m): - d = path.attributes.getNamedItem('d') - p = cubicsuperpath.parsePath(d.value) + d = path.get('d') + p = cubicsuperpath.parsePath(d) for subs in p: for csp in subs: csp[0] = self.project_point(csp[0],m) csp[1] = self.project_point(csp[1],m) csp[2] = self.project_point(csp[2],m) - d.value = cubicsuperpath.formatPath(p) + path.set('d',cubicsuperpath.formatPath(p)) diff --git a/share/extensions/radiusrand.py b/share/extensions/radiusrand.py index 0dcba40cf..5eb73965b 100755 --- a/share/extensions/radiusrand.py +++ b/share/extensions/radiusrand.py @@ -50,9 +50,9 @@ class RadiusRandomize(inkex.Effect): help="Use normal distribution") def effect(self): for id, node in self.selected.iteritems(): - if node.tagName == 'path': - d = node.attributes.getNamedItem('d') - p = cubicsuperpath.parsePath(d.value) + if node.tag == inkex.addNS('path','svg'): + d = node.get('d') + p = cubicsuperpath.parsePath(d) for subpath in p: for csp in subpath: if self.options.end: @@ -66,7 +66,7 @@ class RadiusRandomize(inkex.Effect): if self.options.ctrl: csp[0]=randomize(csp[0], self.options.radius, self.options.norm) csp[2]=randomize(csp[2], self.options.radius, self.options.norm) - d.value = cubicsuperpath.formatPath(p) + node.set('d',cubicsuperpath.formatPath(p)) e = RadiusRandomize() e.affect() diff --git a/share/extensions/render_barcode.py b/share/extensions/render_barcode.py index c35507a95..590e55b86 100644 --- a/share/extensions/render_barcode.py +++ b/share/extensions/render_barcode.py @@ -48,7 +48,7 @@ class InsertBarcode(inkex.Effect): if object: barcode = object.generate() if barcode: - self.current_layer.appendChild(barcode) + self.current_layer.append(barcode) else: sys.stderr.write("No barcode was generated\n") else: diff --git a/share/extensions/rtree.py b/share/extensions/rtree.py index 5d8ac9f3d..a70356557 100755 --- a/share/extensions/rtree.py +++ b/share/extensions/rtree.py @@ -44,19 +44,18 @@ class RTreeTurtle(inkex.Effect): dest="minimum", default=4.0, help="minimum branch size") def effect(self): - new = self.document.createElement('svg:path') s = {'stroke-linejoin': 'miter', 'stroke-width': '1.0px', 'stroke-opacity': '1.0', 'fill-opacity': '1.0', 'stroke': '#000000', 'stroke-linecap': 'butt', 'fill': 'none'} - new.setAttribute('style', simplestyle.formatStyle(s)) t = pturtle.pTurtle() t.pu() t.setpos(self.view_center) t.pd() rtree(t, self.options.size, self.options.minimum) - new.setAttribute('d', t.getPath()) - self.current_layer.appendChild(new) + + attribs = {'d':t.getPath(),'style':simplestyle.formatStyle(s)} + inkex.etree.SubElement(self.current_layer, inkex.addNS('path','svg'), attribs) e = RTreeTurtle() e.affect() diff --git a/share/extensions/rubberstretch.py b/share/extensions/rubberstretch.py index ae52d1c84..1ea533cf0 100644 --- a/share/extensions/rubberstretch.py +++ b/share/extensions/rubberstretch.py @@ -33,45 +33,45 @@ class RubberStretch(pathmodifier.Diffeo): dest="curve", default=0.5) def applyDiffeo(self,bpt,vects=()): - for v in vects: - v[0]-=bpt[0] - v[1]-=bpt[1] - v[1]*=-1 - bpt[1]*=-1 - a=self.options.ratio/100 - b=min(self.options.curve/100,0.99) - x0= (self.bbox[0]+self.bbox[1])/2 - y0=-(self.bbox[2]+self.bbox[3])/2 - w,h=(self.bbox[1]-self.bbox[0])/2,(self.bbox[3]-self.bbox[2])/2 - - x,y=(bpt[0]-x0),(bpt[1]-y0) - sx=(1+b*(x/w+1)*(x/w-1))*2**(-a) - sy=(1+b*(y/h+1)*(y/h-1))*2**(-a) - bpt[0]=x0+x*sy - bpt[1]=y0+y/sx - for v in vects: - dx,dy=v - dXdx=sy - dXdy= x*2*b*y/h/h*2**(-a) - dYdx=-y*2*b*x/w/w*2**(-a)/sx/sx - dYdy=1/sx - v[0]=dXdx*dx+dXdy*dy - v[1]=dYdx*dx+dYdy*dy - - #--spherify -# s=((x*x+y*y)/(w*w+h*h))**(-a/2) -# bpt[0]=x0+s*x -# bpt[1]=y0+s*y -# for v in vects: -# dx,dy=v -# v[0]=(1-a/2/(x*x+y*y)*2*x*x)*s*dx+( -a/2/(x*x+y*y)*2*y*x)*s*dy -# v[1]=( -a/2/(x*x+y*y)*2*x*y)*s*dx+(1-a/2/(x*x+y*y)*2*y*y)*s*dy - - for v in vects: - v[0]+=bpt[0] - v[1]+=bpt[1] - v[1]*=-1 - bpt[1]*=-1 + for v in vects: + v[0]-=bpt[0] + v[1]-=bpt[1] + v[1]*=-1 + bpt[1]*=-1 + a=self.options.ratio/100 + b=min(self.options.curve/100,0.99) + x0= (self.bbox[0]+self.bbox[1])/2 + y0=-(self.bbox[2]+self.bbox[3])/2 + w,h=(self.bbox[1]-self.bbox[0])/2,(self.bbox[3]-self.bbox[2])/2 + + x,y=(bpt[0]-x0),(bpt[1]-y0) + sx=(1+b*(x/w+1)*(x/w-1))*2**(-a) + sy=(1+b*(y/h+1)*(y/h-1))*2**(-a) + bpt[0]=x0+x*sy + bpt[1]=y0+y/sx + for v in vects: + dx,dy=v + dXdx=sy + dXdy= x*2*b*y/h/h*2**(-a) + dYdx=-y*2*b*x/w/w*2**(-a)/sx/sx + dYdy=1/sx + v[0]=dXdx*dx+dXdy*dy + v[1]=dYdx*dx+dYdy*dy + + #--spherify + #s=((x*x+y*y)/(w*w+h*h))**(-a/2) + #bpt[0]=x0+s*x + #bpt[1]=y0+s*y + #for v in vects: + # dx,dy=v + # v[0]=(1-a/2/(x*x+y*y)*2*x*x)*s*dx+( -a/2/(x*x+y*y)*2*y*x)*s*dy + # v[1]=( -a/2/(x*x+y*y)*2*x*y)*s*dx+(1-a/2/(x*x+y*y)*2*y*y)*s*dy + + for v in vects: + v[0]+=bpt[0] + v[1]+=bpt[1] + v[1]*=-1 + bpt[1]*=-1 e = RubberStretch() e.affect() diff --git a/share/extensions/spirograph.py b/share/extensions/spirograph.py index 82b83a31b..8ed297849 100644 --- a/share/extensions/spirograph.py +++ b/share/extensions/spirograph.py @@ -66,9 +66,9 @@ class Spirograph(inkex.Effect): rotation = - math.pi * self.options.rotation / 180; - new = self.document.createElement('svg:path') + new = inkex.etree.Element(inkex.addNS('path','svg')) s = { 'stroke': '#000000', 'fill': 'none' } - new.setAttribute('style', simplestyle.formatStyle(s)) + new.set('style', simplestyle.formatStyle(s)) pathString = '' maxPointCount = 1000 @@ -104,8 +104,8 @@ class Spirograph(inkex.Effect): pathString += ' C ' + str(x + dx) + ',' + str(y + dy) + ' ' - new.setAttribute('d', pathString) - self.current_layer.appendChild(new) + new.set('d', pathString) + self.current_layer.append(new) e = Spirograph() e.affect() diff --git a/share/extensions/straightseg.py b/share/extensions/straightseg.py index dcec88e2c..071768e27 100755 --- a/share/extensions/straightseg.py +++ b/share/extensions/straightseg.py @@ -37,9 +37,9 @@ class SegmentStraightener(inkex.Effect): help="straightening behavior for cubic segments") def effect(self): for id, node in self.selected.iteritems(): - if node.tagName == 'path': - d = node.attributes.getNamedItem('d') - p = simplepath.parsePath(d.value) + if node.tag == inkex.addNS('path', 'svg'): + d = node.get('d') + p = simplepath.parsePath(d) last = [] subPathStart = [] for cmd,params in p: @@ -63,7 +63,7 @@ class SegmentStraightener(inkex.Effect): last = subPathStart[:] else: last = params[-2:] - d.value = simplepath.formatPath(p) + node.set('d',simplepath.formatPath(p)) e = SegmentStraightener() e.affect() diff --git a/share/extensions/summersnight.py b/share/extensions/summersnight.py index 803b2b372..1e283e10d 100755 --- a/share/extensions/summersnight.py +++ b/share/extensions/summersnight.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +dfa#!/usr/bin/env python """ Copyright (C) 2005 Aaron Spike, aaron@ekips.org @@ -31,9 +31,9 @@ class Project(inkex.Effect): #obj is selected second obj = self.selected[self.options.ids[0]] trafo = self.selected[self.options.ids[1]] - if obj.tagName == 'path' and trafo.tagName == 'path': + if obj.tag == inkex.addNS('path','svg) and trafo.tag == inkex.addNS('path','svg'): #distil trafo into four node points - trafo = cubicsuperpath.parsePath(trafo.attributes.getNamedItem('d').value) + trafo = cubicsuperpath.parsePath(trafo.get('d')) trafo = [[Point(csp[1][0],csp[1][1]) for csp in subs] for subs in trafo][0][:4] #vectors pointing away from the trafo origin @@ -53,14 +53,14 @@ class Project(inkex.Effect): err.close() #process path - d = obj.attributes.getNamedItem('d') - p = cubicsuperpath.parsePath(d.value) + d = obj.get('d') + p = cubicsuperpath.parsePath(d) for subs in p: for csp in subs: csp[0] = self.trafopoint(csp[0]) csp[1] = self.trafopoint(csp[1]) csp[2] = self.trafopoint(csp[2]) - d.value = cubicsuperpath.formatPath(p) + obj.set('d',cubicsuperpath.formatPath(p)) def trafopoint(self,(x,y)): #Transform algorithm thanks to Jose Hevia (freon) diff --git a/share/extensions/whirl.py b/share/extensions/whirl.py index 3e9df6c16..0be38e0d5 100644 --- a/share/extensions/whirl.py +++ b/share/extensions/whirl.py @@ -35,9 +35,9 @@ class Whirl(inkex.Effect): if self.options.rotation == True: rotation = 1 whirl = self.options.whirl / 1000 - if node.tagName == 'path': - d = node.attributes.getNamedItem('d') - p = cubicsuperpath.parsePath(d.value) + if node.tag == inkex.addNS('path','svg'): + d = node.get('d') + p = cubicsuperpath.parsePath(d) for sub in p: for csp in sub: for point in csp: @@ -51,7 +51,7 @@ class Whirl(inkex.Effect): point[1] = (dist * math.sin(theta)) point[0] += self.view_center[0] point[1] += self.view_center[1] - d.value = cubicsuperpath.formatPath(p) + node.set('d',cubicsuperpath.formatPath(p)) e = Whirl() e.affect()