Code

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