Code

standardize indents on 4 spaces
[inkscape.git] / share / extensions / motion.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
18 '''
19 import math, inkex, simplestyle, simplepath, bezmisc
21 class Motion(inkex.Effect):
22     def __init__(self):
23         inkex.Effect.__init__(self)
24         self.OptionParser.add_option("-a", "--angle",
25                         action="store", type="float", 
26                         dest="angle", default=45.0,
27                         help="direction of the motion vector")
28         self.OptionParser.add_option("-m", "--magnitude",
29                         action="store", type="float", 
30                         dest="magnitude", default=100.0,
31                         help="magnitude of the motion vector")    
33     def makeface(self,last,(cmd, params)):
34         a = []
35         a.append(['M',last[:]])
36         a.append([cmd, params[:]])
38         #translate path segment along vector
39         np = params[:]
40         defs = simplepath.pathdefs[cmd]
41         for i in range(defs[1]):
42             if defs[3][i] == 'x':
43                 np[i] += self.vx
44             elif defs[3][i] == 'y':
45                 np[i] += self.vy
47         a.append(['L',[np[-2],np[-1]]])
48         
49         #reverse direction of path segment
50         np[-2:] = last[0]+self.vx,last[1]+self.vy
51         if cmd == 'C':
52             c1 = np[:2], np[2:4] = np[2:4], np[:2]
53         a.append([cmd,np[:]])
54             
55         a.append(['Z',[]])
56         face = self.document.createElement('svg:path')
57         self.facegroup.appendChild(face)
58         face.setAttribute('d', simplepath.formatPath(a))
59         
60         
61     def effect(self):
62         self.vx = math.cos(math.radians(self.options.angle))*self.options.magnitude
63         self.vy = math.sin(math.radians(self.options.angle))*self.options.magnitude
64         for id, node in self.selected.iteritems():
65             if node.tagName == 'path':
66                 group = self.document.createElement('svg:g')
67                 self.facegroup = self.document.createElement('svg:g')
68                 node.parentNode.appendChild(group)
69                 group.appendChild(self.facegroup)
70                 group.appendChild(node)
71                 
72                 try:
73                     t = node.attributes.getNamedItem('transform').value
74                     group.setAttribute('transform', t)
75                     node.attributes.getNamedItem('transform').value=""
76                 except AttributeError:
77                     pass
79                 s = node.attributes.getNamedItem('style').value
80                 self.facegroup.setAttribute('style', s)
82                 p = simplepath.parsePath(node.attributes.getNamedItem('d').value)
83                 for cmd,params in p:
84                     tees = []
85                     if cmd == 'C':
86                         bez = (last,params[:2],params[2:4],params[-2:])
87                         tees = [t for t in bezmisc.beziertatslope(bez,(self.vy,self.vx)) if 0<t<1]
88                         tees.sort()
90                     segments = []
91                     if len(tees) == 0 and cmd in ['L','C']:
92                             segments.append([cmd,params[:]])
93                     elif len(tees) == 1:
94                             one,two = bezmisc.beziersplitatt(bez,tees[0])
95                             segments.append([cmd,list(one[1]+one[2]+one[3])])
96                             segments.append([cmd,list(two[1]+two[2]+two[3])])
97                     elif len(tees) == 2:
98                             one,two = bezmisc.beziersplitatt(bez,tees[0])
99                             two,three = bezmisc.beziersplitatt(two,tees[1])
100                             segments.append([cmd,list(one[1]+one[2]+one[3])])
101                             segments.append([cmd,list(two[1]+two[2]+two[3])])
102                             segments.append([cmd,list(three[1]+three[2]+three[3])])
104                     for seg in segments:
105                         self.makeface(last,seg)
106                         last = seg[1][-2:]
107                     
108                     if cmd == 'M':
109                         subPathStart = params[-2:]
110                     if cmd == 'Z':
111                         last = subPathStart
112                     else:
113                         last = params[-2:]
115 e = Motion()
116 e.affect()