Code

patch to fix multiple ps and pdf problems by Ulf Erikson
[inkscape.git] / share / extensions / kochify.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, tempfile, copy, cPickle, inkex, simplepath
21 def findend(d):
22     end = []
23     subPathStart = []
24     for cmd,params in d:
25         if cmd == 'M':
26             subPathStart = params[-2:]
27         if cmd == 'Z':
28             end = subPathStart[:]
29         else:
30             end = params[-2:]
31     return end
33 class Kochify(inkex.Effect):
34     def effect(self):
35         try:
36             f = open(tempfile.gettempdir() + '/kochify.bin', 'r')
37             proto = cPickle.load(f)
38             f.close()                
39         except:
40             return False
41         for id, node in self.selected.iteritems():
42             if node.tagName == 'path':
43                 d = node.attributes.getNamedItem('d')
44                 p = simplepath.parsePath(d.value)
45                 last = p[0][1][-2:]
46                 subPathStart = []
47                 cur = []
48                 new = []
49                 for i in range(len(p)):
50                     rep = copy.deepcopy(proto['path'])
51                     cmd, params = p[i]
52                     if cmd == 'M':
53                         subPathStart = params[-2:]
54                         new.append(copy.deepcopy(p[i]))
55                         continue
56                     if cmd == 'Z':
57                         cur = subPathStart[:]
58                     else:
59                         cur = params[-2:]
61                     if last == cur:
62                         continue
64                     dx = cur[0]-last[0]
65                     dy = cur[1]-last[1]
66                     length = math.sqrt((dx**2) + (dy**2))
67                     angle = math.atan2(dy,dx)
68         
69                     scale = length / proto['length']
70                     rotate = angle - proto['angle']
71                     simplepath.scalePath(rep,scale,scale)
72                     simplepath.rotatePath(rep, rotate)
73                     repend = findend(rep)
74                     transx = cur[0] - repend[0]
75                     transy = cur[1] - repend[1]
76                     simplepath.translatePath(rep, transx, transy)
77                     
78                     if proto['endsinz']:
79                         new.extend(rep[:])
80                     else:
81                         new.extend(rep[1:])
82                     last = cur[:]
83                 
84                 d.value = simplepath.formatPath(new)
85 e = Kochify()
86 e.affect()