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, simplestyle, simplepath, math
21 class Dots(inkex.Effect):
23 def __init__(self):
24 inkex.Effect.__init__(self)
25 self.OptionParser.add_option("-d", "--dotsize",
26 action="store", type="string",
27 dest="dotsize", default="10px",
28 help="Size of the dots placed at path nodes")
29 self.OptionParser.add_option("-f", "--fontsize",
30 action="store", type="string",
31 dest="fontsize", default="20",
32 help="Size of node label numbers")
35 def separateLastAndFirst(self, p):
36 # Separate the last and first dot if they are togheter
37 lastDot = -1
38 if p[lastDot][1] == []: lastDot = -2
39 if round(p[lastDot][1][-2]) == round(p[0][1][-2]) and \
40 round(p[lastDot][1][-1]) == round(p[0][1][-1]):
41 x1 = p[lastDot][1][-2]
42 y1 = p[lastDot][1][-1]
43 x2 = p[lastDot-1][1][-2]
44 y2 = p[lastDot-1][1][-1]
45 dx = abs( max(x1,x2) - min(x1,x2) )
46 dy = abs( max(y1,y2) - min(y1,y2) )
47 dist = math.sqrt( dx**2 + dy**2 )
48 x = dx/dist
49 y = dy/dist
50 if x1 > x2: x *= -1
51 if y1 > y2: y *= -1
52 p[lastDot][1][-2] += x * inkex.unittouu(self.options.dotsize)
53 p[lastDot][1][-1] += y * inkex.unittouu(self.options.dotsize)
56 def effect(self):
57 for id, node in self.selected.iteritems():
58 if node.tag == inkex.addNS('path','svg'):
59 self.group = inkex.etree.SubElement( node.getparent(), inkex.addNS('g','svg') )
60 self.dotGroup = inkex.etree.SubElement( self.group, inkex.addNS('g','svg') )
61 self.numGroup = inkex.etree.SubElement( self.group, inkex.addNS('g','svg') )
63 try:
64 t = node.get('transform')
65 self.group.set('transform', t)
66 except:
67 pass
69 style = simplestyle.formatStyle({ 'stroke': 'none', 'fill': '#000' })
70 a = []
71 p = simplepath.parsePath(node.get('d'))
73 self.separateLastAndFirst(p)
75 num = 1
76 for cmd,params in p:
77 if cmd != 'Z' and cmd != 'z':
78 dot_att = {
79 'style': style,
80 'r': str( inkex.unittouu(self.options.dotsize) / 2 ),
81 'cx': str( params[-2] ),
82 'cy': str( params[-1] )
83 }
84 inkex.etree.SubElement(
85 self.dotGroup,
86 inkex.addNS('circle','svg'),
87 dot_att )
88 self.addText(
89 self.numGroup,
90 params[-2] + ( inkex.unittouu(self.options.dotsize) / 2 ),
91 params[-1] - ( inkex.unittouu(self.options.dotsize) / 2 ),
92 num )
93 num += 1
94 node.getparent().remove( node )
97 def addText(self,node,x,y,text):
98 new = inkex.etree.SubElement(node,inkex.addNS('text','svg'))
99 s = {'font-size': self.options.fontsize, 'fill-opacity': '1.0', 'stroke': 'none',
100 'font-weight': 'normal', 'font-style': 'normal', 'fill': '#999'}
101 new.set('style', simplestyle.formatStyle(s))
102 new.set('x', str(x))
103 new.set('y', str(y))
104 new.text = str(text)
106 if __name__ == '__main__':
107 e = Dots()
108 e.affect()
111 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99