Code

Fixed const/non-const mismatch loop.
[inkscape.git] / share / extensions / summersnight.py
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 try:
26     from subprocess import Popen, PIPE
27     bsubprocess = True
28 except:
29     bsubprocess = False
31 class Project(inkex.Effect):
32     def __init__(self):
33             inkex.Effect.__init__(self)
34     def effect(self):
35         if len(self.options.ids) < 2:
36             inkex.errormsg(_("This extension requires two selected paths. \nThe second path must be exactly four nodes long."))
37             exit()
39         #obj is selected second
40         obj = self.selected[self.options.ids[0]]
41         trafo = self.selected[self.options.ids[1]]
42         if obj.get(inkex.addNS('type','sodipodi')):
43             inkex.errormsg(_("The first selected object is of type '%s'.\nTry using the procedure Path->Object to Path." % obj.get(inkex.addNS('type','sodipodi'))))
44             exit()
45         if obj.tag == inkex.addNS('path','svg') or obj.tag == inkex.addNS('g','svg'):
46             if trafo.tag == inkex.addNS('path','svg'):
47                 #distil trafo into four node points
48                 trafo = cubicsuperpath.parsePath(trafo.get('d'))
49                 if len(trafo[0]) < 4:
50                     inkex.errormsg(_("This extension requires that the second selected path be four nodes long."))
51                     exit()
52                 trafo = [[Point(csp[1][0],csp[1][1]) for csp in subs] for subs in trafo][0][:4]
54                 #vectors pointing away from the trafo origin
55                 self.t1 = Segment(trafo[0],trafo[1])
56                 self.t2 = Segment(trafo[1],trafo[2])
57                 self.t3 = Segment(trafo[3],trafo[2])
58                 self.t4 = Segment(trafo[0],trafo[3])
60                 #query inkscape about the bounding box of obj
61                 self.q = {'x':0,'y':0,'width':0,'height':0}
62                 file = self.args[-1]
63                 id = self.options.ids[0]
64                 for query in self.q.keys():
65                     if bsubprocess:
66                         p = Popen('inkscape --query-%s --query-id=%s "%s"' % (query,id,file), shell=True, stdout=PIPE, stderr=PIPE)
67                         rc = p.wait()
68                         self.q[query] = float(p.stdout.read())
69                         err = p.stderr.read()
70                     else:
71                         f,err = os.popen3('inkscape --query-%s --query-id=%s "%s"' % (query,id,file))[1:]
72                         self.q[query] = float(f.read())
73                         f.close()
74                         err.close()
76                 if obj.tag == inkex.addNS("path",'svg'):
77                     self.process_path(obj)
78                 if obj.tag == inkex.addNS("g",'svg'):
79                     self.process_group(obj)
80             else:
81                 if trafo.tag == inkex.addNS('g','svg'):
82                     inkex.errormsg(_("The second selected object is a group, not a path.\nTry using the procedure Object->Ungroup."))
83                 else:
84                     inkex.errormsg(_("The second selected object is not a path.\nTry using the procedure Path->Object to Path."))
85                 exit()
86         else:
87             inkex.errormsg(_("The first selected object is not a path.\nTry using the procedure Path->Object to Path."))
88             exit()
90     def process_group(self,group):
91         for node in group:
92             if node.tag == inkex.addNS('path','svg'):
93                 self.process_path(node)
94             if node.tag == inkex.addNS('g','svg'):
95                 self.process_group(node)
97     def process_path(self,path):
98         d = path.get('d')
99         p = cubicsuperpath.parsePath(d)
100         for subs in p:
101             for csp in subs:
102                 csp[0] = self.trafopoint(csp[0])
103                 csp[1] = self.trafopoint(csp[1])
104                 csp[2] = self.trafopoint(csp[2])
105         path.set('d',cubicsuperpath.formatPath(p))
107     def trafopoint(self,(x,y)):
108         #Transform algorithm thanks to Jose Hevia (freon)
109         vector = Segment(Point(self.q['x'],self.q['y']),Point(x,y))
110         xratio = abs(vector.delta_x())/self.q['width']
111         yratio = abs(vector.delta_y())/self.q['height']
112     
113         horz = Segment(self.t1.pointAtRatio(xratio),self.t3.pointAtRatio(xratio))
114         vert = Segment(self.t4.pointAtRatio(yratio),self.t2.pointAtRatio(yratio))
116         p = intersectSegments(vert,horz)
117         return [p['x'],p['y']]    
119 if __name__ == '__main__':
120     e = Project()
121     e.affect()
124 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99