index 6f39170ebdc617a4b6d46f5c25f1ffdb7b4d015a..7e1ea58686e29118710947f3e7dd39a78c2cdee5 100755 (executable)
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
-import inkex, os, re, simplepath, cubicsuperpath
+import inkex, os, simplepath, cubicsuperpath
from ffgeom import *
-uuconv = {'in':90.0, 'pt':1.25, 'px':1, 'mm':3.5433070866, 'cm':35.433070866, 'pc':15.0}
-def unittouu(string):
- unit = re.compile('(%s)$' % '|'.join(uuconv.keys()))
- param = re.compile(r'(([-+]?[0-9]+(\.[0-9]*)?|[-+]?\.[0-9]+)([eE][-+]?[0-9]+)?)')
-
- p = param.match(string)
- u = unit.search(string)
- if p:
- retval = float(p.string[p.start():p.end()])
- else:
- retval = 0.0
- if u:
- try:
- return retval * uuconv[u.string[u.start():u.end()]]
- except KeyError:
- pass
- return retval
-
class Project(inkex.Effect):
- def __init__(self):
- inkex.Effect.__init__(self)
- def effect(self):
- if len(self.options.ids) < 2:
- inkex.debug("Requires two selected paths. The second must be exctly four nodes long.")
- exit()
+ def __init__(self):
+ inkex.Effect.__init__(self)
+ def effect(self):
+ if len(self.options.ids) < 2:
+ inkex.debug("Requires two selected paths. The second must be exactly four nodes long.")
+ exit()
- #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':
- #distil trafo into four node points
- trafo = cubicsuperpath.parsePath(trafo.attributes.getNamedItem('d').value)
- trafo = [[Point(csp[1][0],csp[1][1]) for csp in subs] for subs in trafo][0][:4]
+ #obj is selected second
+ obj = self.selected[self.options.ids[0]]
+ trafo = self.selected[self.options.ids[1]]
+ if obj.tag == inkex.addNS('path','svg') and trafo.tag == inkex.addNS('path','svg'):
+ #distil trafo into four node points
+ 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
- self.t1 = Segment(trafo[0],trafo[1])
- self.t2 = Segment(trafo[1],trafo[2])
- self.t3 = Segment(trafo[3],trafo[2])
- self.t4 = Segment(trafo[0],trafo[3])
+ #vectors pointing away from the trafo origin
+ self.t1 = Segment(trafo[0],trafo[1])
+ self.t2 = Segment(trafo[1],trafo[2])
+ self.t3 = Segment(trafo[3],trafo[2])
+ self.t4 = Segment(trafo[0],trafo[3])
- #query inkscape about the bounding box of obj
- self.q = {'x':0,'y':0,'width':0,'height':0}
- file = self.args[-1]
- id = self.options.ids[0]
- for query in self.q.keys():
- f = os.popen("inkscape --query-%s --query-id=%s %s" % (query,id,file))
- self.q[query] = float(f.read())
- f.close()
- #glean document height from the SVG
- docheight = unittouu(inkex.xml.xpath.Evaluate('/svg/@height',self.document)[0].value)
- #Flip inkscapes transposed renderer coords
- self.q['y'] = docheight - self.q['y'] - self.q['height']
+ #query inkscape about the bounding box of obj
+ self.q = {'x':0,'y':0,'width':0,'height':0}
+ file = self.args[-1]
+ id = self.options.ids[0]
+ for query in self.q.keys():
+ _,f,err = os.popen3("inkscape --query-%s --query-id=%s %s" % (query,id,file))
+ self.q[query] = float(f.read())
+ f.close()
+ err.close()
- #process path
- d = obj.attributes.getNamedItem('d')
- p = cubicsuperpath.parsePath(d.value)
- 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)
+ #process path
+ 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])
+ obj.set('d',cubicsuperpath.formatPath(p))
def trafopoint(self,(x,y)):
#Transform algorithm thanks to Jose Hevia (freon)