From 659d437ab1fa196d010c1e92b009b608b8e8b086 Mon Sep 17 00:00:00 2001 From: acspike Date: Wed, 20 Jun 2007 03:31:47 +0000 Subject: [PATCH] Another round of extension conversion (from pyxml to lxml) and some corrections --- share/extensions/coloreffect.py | 2 +- share/extensions/dots.py | 2 +- share/extensions/edge3d.py | 4 +- share/extensions/extractimage.py | 17 ++-- share/extensions/flatten.py | 8 +- share/extensions/fractalize.py | 136 ++++++++++++++----------------- share/extensions/funcplot.py | 28 +++---- share/extensions/handles.py | 12 ++- share/extensions/interp.py | 15 ++-- 9 files changed, 103 insertions(+), 121 deletions(-) diff --git a/share/extensions/coloreffect.py b/share/extensions/coloreffect.py index fcb20dde4..76887faf0 100644 --- a/share/extensions/coloreffect.py +++ b/share/extensions/coloreffect.py @@ -83,7 +83,7 @@ class ColorEffect(inkex.Effect): #inkex.debug("visited: " + str(self.visited)) newnode = inkex.etree.fromstring(inkex.etree.tostring(node)) newnode.set('id', newid) - node.xpath('..')[0].append(newnode) + node.getparent().append(newnode) self.changeStyle(newnode) for child in newnode: self.changeStyle(child) diff --git a/share/extensions/dots.py b/share/extensions/dots.py index ca235861c..7350cae8d 100755 --- a/share/extensions/dots.py +++ b/share/extensions/dots.py @@ -32,7 +32,7 @@ class Dots(inkex.Effect): def effect(self): for id, node in self.selected.iteritems(): if node.tag == inkex.addNS('path','svg'): - self.group = inkex.etree.SubElement(node.xpath('..')[0],inkex.addNS('g','svg')) + self.group = inkex.etree.SubElement(node.getparent(),inkex.addNS('g','svg')) new = inkex.etree.SubElement(self.group,inkex.addNS('path','svg')) try: diff --git a/share/extensions/edge3d.py b/share/extensions/edge3d.py index bcb69237a..0192da022 100644 --- a/share/extensions/edge3d.py +++ b/share/extensions/edge3d.py @@ -121,7 +121,7 @@ class Edge3d(inkex.Effect): clip.append(inkex.etree.fromstring(inkex.etree.tostring(node))) clipId = self.uniqueId('clipPath') clip.set('id', clipId) - clipG = inkex.etree.SubElement(node.xpath('..')[0],inkex.addNS('g','svg')) + clipG = inkex.etree.SubElement(node.getparent(),inkex.addNS('g','svg')) g = inkex.etree.SubElement(clipG,inkex.addNS('g','svg')) clipG.set('clip-path', 'url(#'+clipId+')') # make a blur filter reference by the style of each path @@ -136,7 +136,7 @@ class Edge3d(inkex.Effect): fe.set('stdDeviation', str(self.options.stddev)) else: # can't find defs, just group paths - g = inkex.etree.SubElement(node.xpath('..')[0],inkex.addNS('g','svg')) + g = inkex.etree.SubElement(node.getparent(),inkex.addNS('g','svg')) g.append(node) return g diff --git a/share/extensions/extractimage.py b/share/extensions/extractimage.py index f0540ac50..188cb5d60 100644 --- a/share/extensions/extractimage.py +++ b/share/extensions/extractimage.py @@ -36,31 +36,30 @@ class MyEffect(inkex.Effect): 'icon':'.ico', 'gif' :'.gif' } - #ctx = inkex.xml.xpath.Context.Context(self.document,processorNss=inkex.NSS) # exbed the first embedded image path = self.options.filepath if (path != ''): if (self.options.ids): for id, node in self.selected.iteritems(): - if node.tagName == 'image': - xlink = node.attributes.getNamedItemNS(inkex.NSS[u'xlink'],'href') - if (xlink.value[:4]=='data'): - comma = xlink.value.find(',') + if node.tag == inkex.addNS('image','svg'): + xlink = node.get(inkex.addNS('href','xlink')) + if (xlink[:4]=='data'): + comma = xlink.find(',') if comma>0: #get extension fileext='' - semicolon = xlink.value.find(';') + semicolon = xlink.find(';') if semicolon>0: for sub in mimesubext.keys(): - if sub in xlink.value[5:semicolon]: + if sub in xlink[5:semicolon]: fileext=mimesubext[sub] path=path+fileext; break #save - data = base64.decodestring(xlink.value[comma:]) + data = base64.decodestring(xlink[comma:]) open(path,'wb').write(data) - xlink.value = os.path.realpath(path) #absolute for making in-mem cycles work + node.set(inkex.addNS('href','xlink'),os.path.realpath(path)) #absolute for making in-mem cycles work else: inkex.debug('Difficulty finding the image data.') break diff --git a/share/extensions/flatten.py b/share/extensions/flatten.py index 8dc4e393b..2e1a491cd 100755 --- a/share/extensions/flatten.py +++ b/share/extensions/flatten.py @@ -27,9 +27,9 @@ class MyEffect(inkex.Effect): help="Minimum flatness of the subdivided curves") 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) cspsubdiv.cspsubdiv(p, self.options.flat) np = [] for sp in p: @@ -40,7 +40,7 @@ class MyEffect(inkex.Effect): cmd = 'M' first = False np.append([cmd,[csp[1][0],csp[1][1]]]) - d.value = simplepath.formatPath(np) + node.set('d',simplepath.formatPath(np)) e = MyEffect() e.affect() \ No newline at end of file diff --git a/share/extensions/fractalize.py b/share/extensions/fractalize.py index 7b4dc2ea3..8ec9f80ae 100755 --- a/share/extensions/fractalize.py +++ b/share/extensions/fractalize.py @@ -16,86 +16,74 @@ 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, math, inkex, simplestyle, simplepath +import random, math, inkex, simplepath def calculateSubdivision(x1,y1,x2,y2,smoothness): - """ Calculate the vector from (x1,y1) to (x2,y2) """ - x3 = x2 - x1 - y3 = y2 - y1 - """ Calculate the point half-way between the two points """ - hx = x1 + x3/2 - hy = y1 + y3/2 - """ Calculate normalized vector perpendicular to the vector (x3,y3) """ - length = math.sqrt(x3*x3 + y3*y3) - nx = -y3/length - ny = x3/length - """ Scale perpendicular vector by random factor """ - r = random.uniform(-length/(1+smoothness),length/(1+smoothness)) - nx = nx * r - ny = ny * r - """ add scaled perpendicular vector to the half-way point to get the final - displaced subdivision point """ - x = hx + nx - y = hy + ny - return [x, y] + """ Calculate the vector from (x1,y1) to (x2,y2) """ + x3 = x2 - x1 + y3 = y2 - y1 + """ Calculate the point half-way between the two points """ + hx = x1 + x3/2 + hy = y1 + y3/2 + """ Calculate normalized vector perpendicular to the vector (x3,y3) """ + length = math.sqrt(x3*x3 + y3*y3) + nx = -y3/length + ny = x3/length + """ Scale perpendicular vector by random factor """ + r = random.uniform(-length/(1+smoothness),length/(1+smoothness)) + nx = nx * r + ny = ny * r + """ add scaled perpendicular vector to the half-way point to get the final + displaced subdivision point """ + x = hx + nx + y = hy + ny + return [x, y] class PathFractalize(inkex.Effect): - def __init__(self): - inkex.Effect.__init__(self) - self.OptionParser.add_option("-s", "--subdivs", - action="store", type="int", - dest="subdivs", default="6", - help="Number of subdivisons") - self.OptionParser.add_option("-f", "--smooth", - action="store", type="float", - dest="smooth", default="4.0", - help="Smoothness of the subdivision") - def effect(self): - for id, node in self.selected.iteritems(): - if node.tagName == 'path': - d = node.attributes.getNamedItem('d') - p = simplepath.parsePath(d.value) - new = self.document.createElement('svg:path') - try: - t = node.attributes.getNamedItem('transform').value - new.setAttribute('transform', t) - except AttributeError: - pass - - s = simplestyle.parseStyle(node.attributes.getNamedItem('style').value) - new.setAttribute('style', simplestyle.formatStyle(s)) - - a = [] - p = simplepath.parsePath(node.attributes.getNamedItem('d').value) - first = 1 - for cmd,params in p: - if cmd != 'Z': - if first == 1: - x1 = params[-2] - y1 = params[-1] - a.append(['M',params[-2:]]) - first = 2 - else : - x2 = params[-2] - y2 = params[-1] - self.fractalize(a,x1,y1,x2,y2,self.options.subdivs,self.options.smooth) - x1 = x2 - y1 = y2 - a.append(['L',params[-2:]]) + def __init__(self): + inkex.Effect.__init__(self) + self.OptionParser.add_option("-s", "--subdivs", + action="store", type="int", + dest="subdivs", default="6", + help="Number of subdivisons") + self.OptionParser.add_option("-f", "--smooth", + action="store", type="float", + dest="smooth", default="4.0", + help="Smoothness of the subdivision") + def effect(self): + for id, node in self.selected.iteritems(): + if node.tag == inkex.addNS('path','svg'): + d = node.get('d') + p = simplepath.parsePath(d) + + a = [] + first = 1 + for cmd,params in p: + if cmd != 'Z': + if first == 1: + x1 = params[-2] + y1 = params[-1] + a.append(['M',params[-2:]]) + first = 2 + else : + x2 = params[-2] + y2 = params[-1] + self.fractalize(a,x1,y1,x2,y2,self.options.subdivs,self.options.smooth) + x1 = x2 + y1 = y2 + a.append(['L',params[-2:]]) - new.setAttribute('d', simplepath.formatPath(a)) - node.parentNode.appendChild(new) - node.parentNode.removeChild(node) + node.set('d', simplepath.formatPath(a)) - def fractalize(self,a,x1,y1,x2,y2,s,f): - subdivPoint = calculateSubdivision(x1,y1,x2,y2,f) - - if s > 0 : - """ recursively subdivide the segment left of the subdivision point """ - self.fractalize(a,x1,y1,subdivPoint[-2],subdivPoint[-1],s-1,f) - a.append(['L',subdivPoint]) - """ recursively subdivide the segment right of the subdivision point """ - self.fractalize(a,subdivPoint[-2],subdivPoint[-1],x2,y2,s-1,f) + def fractalize(self,a,x1,y1,x2,y2,s,f): + subdivPoint = calculateSubdivision(x1,y1,x2,y2,f) + + if s > 0 : + """ recursively subdivide the segment left of the subdivision point """ + self.fractalize(a,x1,y1,subdivPoint[-2],subdivPoint[-1],s-1,f) + a.append(['L',subdivPoint]) + """ recursively subdivide the segment right of the subdivision point """ + self.fractalize(a,subdivPoint[-2],subdivPoint[-1],x2,y2,s-1,f) e = PathFractalize() e.affect() diff --git a/share/extensions/funcplot.py b/share/extensions/funcplot.py index fb0a9ecb6..f0e84d6f6 100644 --- a/share/extensions/funcplot.py +++ b/share/extensions/funcplot.py @@ -174,25 +174,25 @@ class FuncPlot(inkex.Effect): def effect(self): for id, node in self.selected.iteritems(): - if node.tagName == 'rect': + if node.tag == inkex.addNS('rect','svg'): # create new path with basic dimensions of selected rectangle - newpath = self.document.createElement('svg:path') - x = float(node.attributes.getNamedItem('x').value) - y = float(node.attributes.getNamedItem('y').value) - w = float(node.attributes.getNamedItem('width').value) - h = float(node.attributes.getNamedItem('height').value) + newpath = inkex.etree.Element(inkex.addNS('path','svg')) + x = float(node.get('x')) + y = float(node.get('y')) + w = float(node.get('width')) + h = float(node.get('height')) #copy attributes of rect - s = node.attributes.getNamedItem('style').value - newpath.setAttribute('style', s) + s = node.get('style') + newpath.set('style', s) try: - t = node.attributes.getNamedItem('transform').value - newpath.setAttribute('transform', t) + t = node.get('transform') + newpath.set('transform', t) except AttributeError: pass # top and bottom where exchanhged - newpath.setAttribute('d', simplepath.formatPath( + newpath.set('d', simplepath.formatPath( drawfunction(self.options.xstart, self.options.xend, self.options.ybottom, @@ -205,7 +205,7 @@ class FuncPlot(inkex.Effect): self.options.times2pi, self.options.isoscale, self.options.drawaxis))) - newpath.setAttribute('title', self.options.fofx) + newpath.set('title', self.options.fofx) #newpath.setAttribute('desc', '!func;' + self.options.fofx + ';' # + self.options.fpofx + ';' @@ -215,10 +215,10 @@ class FuncPlot(inkex.Effect): # + `self.options.samples`) # add path into SVG structure - node.parentNode.appendChild(newpath) + node.getparent().append(newpath) # option wether to remove the rectangle or not. if self.options.remove: - node.parentNode.removeChild(node) + node.getparent().remove(node) e = FuncPlot() e.affect() diff --git a/share/extensions/handles.py b/share/extensions/handles.py index 5ce41b734..83d7f1969 100755 --- a/share/extensions/handles.py +++ b/share/extensions/handles.py @@ -21,8 +21,8 @@ import inkex, simplepath, simplestyle class Handles(inkex.Effect): def effect(self): for id, node in self.selected.iteritems(): - if node.tagName == 'path': - p = simplepath.parsePath(node.attributes.getNamedItem('d').value) + if node.tag == inkex.addNS('path','svg'): + p = simplepath.parsePath(node.get('d')) a =[] pen = None subPathStart = None @@ -43,14 +43,12 @@ class Handles(inkex.Effect): pen = params[-2:] if len(a) > 0: - 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)) - new.setAttribute('d', simplepath.formatPath(a)) - node.parentNode.appendChild(new) - + attribs = {'style':simplestyle.formatStyle(s),'d':simplepath.formatPath(a)} + inkex.etree.SubElement(node.getparent(), inkex.addNS('path','svg'), attribs) + e = Handles() e.affect() diff --git a/share/extensions/interp.py b/share/extensions/interp.py index f0ba8f26c..1b28f1bff 100755 --- a/share/extensions/interp.py +++ b/share/extensions/interp.py @@ -122,9 +122,9 @@ class Interp(inkex.Effect): styles = {} for id in self.options.ids: node = self.selected[id] - if node.tagName =='path': - paths[id] = cubicsuperpath.parsePath(node.attributes.getNamedItem('d').value) - styles[id] = simplestyle.parseStyle(node.attributes.getNamedItem('style').value) + if node.tag ==inkex.addNS('path','svg'): + paths[id] = cubicsuperpath.parsePath(node.get('d')) + styles[id] = simplestyle.parseStyle(node.get('style')) else: self.options.ids.remove(id) @@ -272,8 +272,7 @@ class Interp(inkex.Effect): if self.options.dup: steps = [0] + steps + [1] #create an interpolated path for each interval - group = self.document.createElement('svg:g') - self.current_layer.appendChild(group) + group = inkex.etree.SubElement(self.current_layer,inkex.addNS('g','svg')) for time in steps: interp = [] #process subpaths @@ -295,7 +294,6 @@ class Interp(inkex.Effect): #remove final subpath if empty. if not interp[-1]: del interp[-1] - new = self.document.createElement('svg:path') #basic style tweening if self.options.style: @@ -307,9 +305,8 @@ class Interp(inkex.Effect): if dofill: basestyle['fill-opacity'] = tweenstylefloat('fill-opacity',sst,est,time) basestyle['fill'] = tweenstylecolor('fill',sst,est,time) - new.setAttribute('style', simplestyle.formatStyle(basestyle)) - new.setAttribute('d', cubicsuperpath.formatPath(interp)) - group.appendChild(new) + attribs = {'style':simplestyle.formatStyle(basestyle),'d':cubicsuperpath.formatPath(interp)} + new = inkex.etree.SubElement(group,inkex.addNS('path','svg'), attribs) e = Interp() e.affect() -- 2.30.2