Code

moving trunk for module inkscape
[inkscape.git] / share / extensions / addnodes.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 inkex, cubicsuperpath, simplestyle, copy, math, re, bezmisc
21 def numsegs(csp):
22         return sum([len(p)-1 for p in csp])
23 def tpoint((x1,y1), (x2,y2), t = 0.5):
24         return [x1+t*(x2-x1),y1+t*(y2-y1)]
25 def cspbezsplit(sp1, sp2, t = 0.5):
26         m1=tpoint(sp1[1],sp1[2],t)
27         m2=tpoint(sp1[2],sp2[0],t)
28         m3=tpoint(sp2[0],sp2[1],t)
29         m4=tpoint(m1,m2,t)
30         m5=tpoint(m2,m3,t)
31         m=tpoint(m4,m5,t)
32         return [[sp1[0][:],sp1[1][:],m1], [m4,m,m5], [m3,sp2[1][:],sp2[2][:]]]
33 def cspbezsplitatlength(sp1, sp2, l = 0.5, tolerance = 0.001):
34         bez = (sp1[1][:],sp1[2][:],sp2[0][:],sp2[1][:])
35         t = bezmisc.beziertatlength(bez, l, tolerance)
36         return cspbezsplit(sp1, sp2, t)
37 def cspseglength(sp1,sp2, tolerance = 0.001):
38         bez = (sp1[1][:],sp1[2][:],sp2[0][:],sp2[1][:])
39         return bezmisc.bezierlength(bez, tolerance)     
40 def csplength(csp):
41         total = 0
42         lengths = []
43         for sp in csp:
44                 lengths.append([])
45                 for i in xrange(1,len(sp)):
46                         l = cspseglength(sp[i-1],sp[i])
47                         lengths[-1].append(l)
48                         total += l                      
49         return lengths, total
50 def numlengths(csplen):
51         retval = 0
52         for sp in csplen:
53                 for l in sp:
54                         if l > 0:
55                                 retval += 1
56         return retval
58 class SplitIt(inkex.Effect):
59         def __init__(self):
60                 inkex.Effect.__init__(self)
61                 self.OptionParser.add_option("-m", "--max",
62                                                 action="store", type="float", 
63                                                 dest="max", default=0.0,
64                                                 help="maximum segment length")
65         def effect(self):
66                 for id, node in self.selected.iteritems():
67                         if node.tagName == 'path':
68                                 d = node.attributes.getNamedItem('d')
69                                 p = cubicsuperpath.parsePath(d.value)
70                                 
71                                 #lens, total = csplength(p)
72                                 #avg = total/numlengths(lens)
73                                 #inkex.debug("average segment length: %s" % avg)
75                                 new = []
76                                 for sub in p:
77                                         new.append([sub[0][:]])
78                                         i = 1
79                                         while i <= len(sub)-1:
80                                                 length = cspseglength(new[-1][-1], sub[i])
81                                                 if length > self.options.max:
82                                                         splits = math.ceil(length/self.options.max)
83                                                         for s in xrange(int(splits),1,-1):
84                                                                 new[-1][-1], next, sub[i] = cspbezsplitatlength(new[-1][-1], sub[i], 1.0/s)
85                                                                 new[-1].append(next[:])
86                                                 new[-1].append(sub[i])
87                                                 i+=1
88                                         
89                                 d.value = cubicsuperpath.formatPath(new)
91 e = SplitIt()
92 e.affect()