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