summaryrefslogtreecommitdiffstats
path: root/share/extensions/motion.py
blob: 95100e62987894f1aaba4e2be228292718889fbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python 
'''
Copyright (C) 2005 Aaron Spike, aaron@ekips.org

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'''
import math, inkex, simplestyle, simplepath, bezmisc

class Motion(inkex.Effect):
	def __init__(self):
		inkex.Effect.__init__(self)
		self.OptionParser.add_option("-a", "--angle",
						action="store", type="float", 
						dest="angle", default=45.0,
						help="direction of the motion vector")
		self.OptionParser.add_option("-m", "--magnitude",
						action="store", type="float", 
						dest="magnitude", default=100.0,
						help="magnitude of the motion vector")	

	def makeface(self,last,(cmd, params)):
		a = []
		a.append(['M',last[:]])
		a.append([cmd, params[:]])

		#translate path segment along vector
		np = params[:]
		defs = simplepath.pathdefs[cmd]
		for i in range(defs[1]):
			if defs[3][i] == 'x':
				np[i] += self.vx
			elif defs[3][i] == 'y':
				np[i] += self.vy

		a.append(['L',[np[-2],np[-1]]])
		
		#reverse direction of path segment
		np[-2:] = last[0]+self.vx,last[1]+self.vy
		if cmd == 'C':
			c1 = np[:2], np[2:4] = np[2:4], np[:2]
		a.append([cmd,np[:]])
			
		a.append(['Z',[]])
		face = self.document.createElement('svg:path')
		self.facegroup.appendChild(face)
		face.setAttribute('d', simplepath.formatPath(a))
		
		
	def effect(self):
		self.vx = math.cos(math.radians(self.options.angle))*self.options.magnitude
		self.vy = math.sin(math.radians(self.options.angle))*self.options.magnitude
		for id, node in self.selected.iteritems():
			if node.tagName == 'path':
				group = self.document.createElement('svg:g')
				self.facegroup = self.document.createElement('svg:g')
				node.parentNode.appendChild(group)
				group.appendChild(self.facegroup)
				group.appendChild(node)
				
				try:
					t = node.attributes.getNamedItem('transform').value
					group.setAttribute('transform', t)
					node.attributes.getNamedItem('transform').value=""
				except AttributeError:
					pass

				s = node.attributes.getNamedItem('style').value
				self.facegroup.setAttribute('style', s)

				p = simplepath.parsePath(node.attributes.getNamedItem('d').value)
				for cmd,params in p:
					tees = []
					if cmd == 'C':
						bez = (last,params[:2],params[2:4],params[-2:])
						tees = [t for t in bezmisc.beziertatslope(bez,(self.vy,self.vx)) if 0<t<1]
						tees.sort()

					segments = []
					if len(tees) == 0 and cmd in ['L','C']:
							segments.append([cmd,params[:]])
					elif len(tees) == 1:
							one,two = bezmisc.beziersplitatt(bez,tees[0])
							segments.append([cmd,list(one[1]+one[2]+one[3])])
							segments.append([cmd,list(two[1]+two[2]+two[3])])
					elif len(tees) == 2:
							one,two = bezmisc.beziersplitatt(bez,tees[0])
							two,three = bezmisc.beziersplitatt(two,tees[1])
							segments.append([cmd,list(one[1]+one[2]+one[3])])
							segments.append([cmd,list(two[1]+two[2]+two[3])])
							segments.append([cmd,list(three[1]+three[2]+three[3])])

					for seg in segments:
						self.makeface(last,seg)
						last = seg[1][-2:]
					
					if cmd == 'M':
						subPathStart = params[-2:]
					if cmd == 'Z':
						last = subPathStart
					else:
						last = params[-2:]


				

e = Motion()
e.affect()