7e1ea58686e29118710947f3e7dd39a78c2cdee5
1 #!/usr/bin/env python
2 """
3 Copyright (C) 2005 Aaron Spike, aaron@ekips.org
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 """
20 import inkex, os, simplepath, cubicsuperpath
21 from ffgeom import *
23 class Project(inkex.Effect):
24 def __init__(self):
25 inkex.Effect.__init__(self)
26 def effect(self):
27 if len(self.options.ids) < 2:
28 inkex.debug("Requires two selected paths. The second must be exactly four nodes long.")
29 exit()
31 #obj is selected second
32 obj = self.selected[self.options.ids[0]]
33 trafo = self.selected[self.options.ids[1]]
34 if obj.tag == inkex.addNS('path','svg') and trafo.tag == inkex.addNS('path','svg'):
35 #distil trafo into four node points
36 trafo = cubicsuperpath.parsePath(trafo.get('d'))
37 trafo = [[Point(csp[1][0],csp[1][1]) for csp in subs] for subs in trafo][0][:4]
39 #vectors pointing away from the trafo origin
40 self.t1 = Segment(trafo[0],trafo[1])
41 self.t2 = Segment(trafo[1],trafo[2])
42 self.t3 = Segment(trafo[3],trafo[2])
43 self.t4 = Segment(trafo[0],trafo[3])
45 #query inkscape about the bounding box of obj
46 self.q = {'x':0,'y':0,'width':0,'height':0}
47 file = self.args[-1]
48 id = self.options.ids[0]
49 for query in self.q.keys():
50 _,f,err = os.popen3("inkscape --query-%s --query-id=%s %s" % (query,id,file))
51 self.q[query] = float(f.read())
52 f.close()
53 err.close()
55 #process path
56 d = obj.get('d')
57 p = cubicsuperpath.parsePath(d)
58 for subs in p:
59 for csp in subs:
60 csp[0] = self.trafopoint(csp[0])
61 csp[1] = self.trafopoint(csp[1])
62 csp[2] = self.trafopoint(csp[2])
63 obj.set('d',cubicsuperpath.formatPath(p))
65 def trafopoint(self,(x,y)):
66 #Transform algorithm thanks to Jose Hevia (freon)
67 vector = Segment(Point(self.q['x'],self.q['y']),Point(x,y))
68 xratio = abs(vector.delta_x())/self.q['width']
69 yratio = abs(vector.delta_y())/self.q['height']
71 horz = Segment(self.t1.pointAtRatio(xratio),self.t3.pointAtRatio(xratio))
72 vert = Segment(self.t4.pointAtRatio(yratio),self.t2.pointAtRatio(yratio))
74 p = intersectSegments(vert,horz)
75 return [p['x'],p['y']]
77 e = Project()
78 e.affect()