2c08c35c4569c874f3f6e88526da360cdc2d6364
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.get(inkex.addNS('type','sodipodi')):
39 inkex.errormsg(_("The first selected object is of type '%s'.\nTry using the procedure Path | Object to Path." % obj.get(inkex.addNS('type','sodipodi'))))
40 exit()
41 if obj.tag == inkex.addNS('path','svg') or obj.tag == inkex.addNS('g','svg'):
42 if trafo.tag == inkex.addNS('path','svg'):
43 #distil trafo into four node points
44 trafo = cubicsuperpath.parsePath(trafo.get('d'))
45 if len(trafo[0]) < 4:
46 inkex.errormsg(_("This extension requires that the second selected path be four nodes long."))
47 exit()
48 trafo = [[Point(csp[1][0],csp[1][1]) for csp in subs] for subs in trafo][0][:4]
50 #vectors pointing away from the trafo origin
51 self.t1 = Segment(trafo[0],trafo[1])
52 self.t2 = Segment(trafo[1],trafo[2])
53 self.t3 = Segment(trafo[3],trafo[2])
54 self.t4 = Segment(trafo[0],trafo[3])
56 #query inkscape about the bounding box of obj
57 self.q = {'x':0,'y':0,'width':0,'height':0}
58 file = self.args[-1]
59 id = self.options.ids[0]
60 for query in self.q.keys():
61 f,err = os.popen3('inkscape --query-%s --query-id=%s "%s"' % (query,id,file))[1:]
62 self.q[query] = float(f.read())
63 f.close()
64 err.close()
66 if obj.tag == inkex.addNS("path",'svg'):
67 self.process_path(obj)
68 if obj.tag == inkex.addNS("g",'svg'):
69 self.process_group(obj)
70 else:
71 if trafo.tag == inkex.addNS('g','svg'):
72 inkex.errormsg(_("The second selected object is a group, not a path.\nTry using the procedure Object | Ungroup."))
73 else:
74 inkex.errormsg(_("The second selected object is not a path.\nTry using the procedure Path | Object to Path."))
75 exit()
76 else:
77 inkex.errormsg(_("The first selected object is not a path.\nTry using the procedure Path | Object to Path."))
78 exit()
80 def process_group(self,group):
81 for node in group:
82 if node.tag == inkex.addNS('path','svg'):
83 self.process_path(node)
84 if node.tag == inkex.addNS('g','svg'):
85 self.process_group(node)
87 def process_path(self,path):
88 d = path.get('d')
89 p = cubicsuperpath.parsePath(d)
90 for subs in p:
91 for csp in subs:
92 csp[0] = self.trafopoint(csp[0])
93 csp[1] = self.trafopoint(csp[1])
94 csp[2] = self.trafopoint(csp[2])
95 path.set('d',cubicsuperpath.formatPath(p))
97 def trafopoint(self,(x,y)):
98 #Transform algorithm thanks to Jose Hevia (freon)
99 vector = Segment(Point(self.q['x'],self.q['y']),Point(x,y))
100 xratio = abs(vector.delta_x())/self.q['width']
101 yratio = abs(vector.delta_y())/self.q['height']
103 horz = Segment(self.t1.pointAtRatio(xratio),self.t3.pointAtRatio(xratio))
104 vert = Segment(self.t4.pointAtRatio(yratio),self.t2.pointAtRatio(yratio))
106 p = intersectSegments(vert,horz)
107 return [p['x'],p['y']]
109 if __name__ == '__main__':
110 e = Project()
111 e.affect()
114 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99