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 *
22 import gettext
23 _ = gettext.gettext
25 class Project(inkex.Effect):
26 def __init__(self):
27 inkex.Effect.__init__(self)
28 def effect(self):
29 if len(self.options.ids) < 2:
30 inkex.errormsg(_("This extension requires two selected paths.")
31 + " "
32 + _("The second path must be exactly four nodes long."))
33 exit()
35 #obj is selected second
36 obj = self.selected[self.options.ids[0]]
37 trafo = self.selected[self.options.ids[1]]
38 if obj.tag == inkex.addNS('path','svg') and trafo.tag == inkex.addNS('path','svg'):
39 #distil trafo into four node points
40 trafo = cubicsuperpath.parsePath(trafo.get('d'))
41 trafo = [[Point(csp[1][0],csp[1][1]) for csp in subs] for subs in trafo][0][:4]
43 #vectors pointing away from the trafo origin
44 self.t1 = Segment(trafo[0],trafo[1])
45 self.t2 = Segment(trafo[1],trafo[2])
46 self.t3 = Segment(trafo[3],trafo[2])
47 self.t4 = Segment(trafo[0],trafo[3])
49 #query inkscape about the bounding box of obj
50 self.q = {'x':0,'y':0,'width':0,'height':0}
51 file = self.args[-1]
52 id = self.options.ids[0]
53 for query in self.q.keys():
54 f,err = os.popen3('inkscape --query-%s --query-id=%s "%s"' % (query,id,file))[1:]
55 self.q[query] = float(f.read())
56 f.close()
57 err.close()
59 #process path
60 d = obj.get('d')
61 p = cubicsuperpath.parsePath(d)
62 for subs in p:
63 for csp in subs:
64 csp[0] = self.trafopoint(csp[0])
65 csp[1] = self.trafopoint(csp[1])
66 csp[2] = self.trafopoint(csp[2])
67 obj.set('d',cubicsuperpath.formatPath(p))
69 def trafopoint(self,(x,y)):
70 #Transform algorithm thanks to Jose Hevia (freon)
71 vector = Segment(Point(self.q['x'],self.q['y']),Point(x,y))
72 xratio = abs(vector.delta_x())/self.q['width']
73 yratio = abs(vector.delta_y())/self.q['height']
75 horz = Segment(self.t1.pointAtRatio(xratio),self.t3.pointAtRatio(xratio))
76 vert = Segment(self.t4.pointAtRatio(yratio),self.t2.pointAtRatio(yratio))
78 p = intersectSegments(vert,horz)
79 return [p['x'],p['y']]
81 if __name__ == '__main__':
82 e = Project()
83 e.affect()
86 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99