1 #!/usr/bin/env python
2 '''
3 This extension converts a path into a dashed line using 'stroke-dasharray'
4 It is a modification of the file addnodes.py
6 Copyright (C) 2005,2007 Aaron Spike, aaron@ekips.org
7 Copyright (C) 2009 Alvin Penner, penner@vaxxine.com
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 '''
24 import inkex, cubicsuperpath, bezmisc, simplestyle
26 def tpoint((x1,y1), (x2,y2), t = 0.5):
27 return [x1+t*(x2-x1),y1+t*(y2-y1)]
28 def cspbezsplit(sp1, sp2, t = 0.5):
29 m1=tpoint(sp1[1],sp1[2],t)
30 m2=tpoint(sp1[2],sp2[0],t)
31 m3=tpoint(sp2[0],sp2[1],t)
32 m4=tpoint(m1,m2,t)
33 m5=tpoint(m2,m3,t)
34 m=tpoint(m4,m5,t)
35 return [[sp1[0][:],sp1[1][:],m1], [m4,m,m5], [m3,sp2[1][:],sp2[2][:]]]
36 def cspbezsplitatlength(sp1, sp2, l = 0.5, tolerance = 0.001):
37 bez = (sp1[1][:],sp1[2][:],sp2[0][:],sp2[1][:])
38 t = bezmisc.beziertatlength(bez, l, tolerance)
39 return cspbezsplit(sp1, sp2, t)
40 def cspseglength(sp1,sp2, tolerance = 0.001):
41 bez = (sp1[1][:],sp1[2][:],sp2[0][:],sp2[1][:])
42 return bezmisc.bezierlength(bez, tolerance)
44 class SplitIt(inkex.Effect):
45 def __init__(self):
46 inkex.Effect.__init__(self)
48 def effect(self):
49 for id, node in self.selected.iteritems():
50 if node.tag == inkex.addNS('path','svg'):
51 dashes = []
52 style = simplestyle.parseStyle(node.get('style'))
53 if style.has_key('stroke-dasharray'):
54 if style['stroke-dasharray'].find(',') > 0:
55 dashes = [float (dash) for dash in style['stroke-dasharray'].split(',')]
56 if dashes:
57 p = cubicsuperpath.parsePath(node.get('d'))
58 new = []
59 for sub in p:
60 idash = 0
61 dash = dashes[0]
62 length = 0
63 new.append([sub[0][:]])
64 i = 1
65 while i < len(sub):
66 dash = dash - length
67 length = cspseglength(new[-1][-1], sub[i])
68 while dash < length:
69 new[-1][-1], next, sub[i] = cspbezsplitatlength(new[-1][-1], sub[i], dash/length)
70 if idash % 2: # create a gap
71 new.append([next[:]])
72 else: # splice the curve
73 new[-1].append(next[:])
74 length = length - dash
75 idash = (idash + 1) % len(dashes)
76 dash = dashes[idash]
77 if idash % 2:
78 new.append([sub[i]])
79 else:
80 new[-1].append(sub[i])
81 i+=1
82 node.set('d',cubicsuperpath.formatPath(new))
84 if __name__ == '__main__':
85 e = SplitIt()
86 e.affect()
89 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99