Code

Added measure extension.
[inkscape.git] / share / extensions / measure.py
1 #!/usr/bin/env python 
2 '''
3 Copyright (C) 2006 Nathan Hurst
4 Copyright (C) 2005 Aaron Spike, aaron@ekips.org
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 '''
20 import inkex, simplestyle, simplepath,sys,cubicsuperpath, bezmisc
22 def numsegs(csp):
23         return sum([len(p)-1 for p in csp])
24 def interpcoord(v1,v2,p):
25         return v1+((v2-v1)*p)
26 def interppoints(p1,p2,p):
27         return [interpcoord(p1[0],p2[0],p),interpcoord(p1[1],p2[1],p)]
28 def pointdistance((x1,y1),(x2,y2)):
29         return math.sqrt(((x2 - x1) ** 2) + ((y2 - y1) ** 2))
30 def bezlenapprx(sp1, sp2):
31         return pointdistance(sp1[1], sp1[2]) + pointdistance(sp1[2], sp2[0]) + pointdistance(sp2[0], sp2[1])
32 def tpoint((x1,y1), (x2,y2), t = 0.5):
33         return [x1+t*(x2-x1),y1+t*(y2-y1)]
34 def cspbezsplit(sp1, sp2, t = 0.5):
35         m1=tpoint(sp1[1],sp1[2],t)
36         m2=tpoint(sp1[2],sp2[0],t)
37         m3=tpoint(sp2[0],sp2[1],t)
38         m4=tpoint(m1,m2,t)
39         m5=tpoint(m2,m3,t)
40         m=tpoint(m4,m5,t)
41         return [[sp1[0][:],sp1[1][:],m1], [m4,m,m5], [m3,sp2[1][:],sp2[2][:]]]
42 def cspbezsplitatlength(sp1, sp2, l = 0.5, tolerance = 0.001):
43         bez = (sp1[1][:],sp1[2][:],sp2[0][:],sp2[1][:])
44         t = bezmisc.beziertatlength(bez, l, tolerance)
45         return cspbezsplit(sp1, sp2, t)
46 def cspseglength(sp1,sp2, tolerance = 0.001):
47         bez = (sp1[1][:],sp1[2][:],sp2[0][:],sp2[1][:])
48         return bezmisc.bezierlength(bez, tolerance)     
49 def csplength(csp):
50         total = 0
51         lengths = []
52         for sp in csp:
53                 lengths.append([])
54                 for i in xrange(1,len(sp)):
55                         l = cspseglength(sp[i-1],sp[i])
56                         lengths[-1].append(l)
57                         total += l                      
58         return lengths, total
60 class Length(inkex.Effect):
61         def __init__(self):
62                 inkex.Effect.__init__(self)
63                 self.OptionParser.add_option("-f", "--fontsize",
64                                                 action="store", type="string", 
65                                                 dest="fontsize", default="20",
66                                                 help="Size of node label numbers")      
67                 self.OptionParser.add_option("-o", "--offset",
68                                                 action="store", type="string", 
69                                                 dest="offset", default="-4",
70                                                 help="The distance above the curve")    
71         def effect(self):
72                 for id, node in self.selected.iteritems():
73                         if node.tagName == 'path':
74                                 self.group = self.document.createElement('svg:g')
75                                 node.parentNode.appendChild(self.group)
76                                 
77                                 try:
78                                         t = node.attributes.getNamedItem('transform').value
79                                         self.group.setAttribute('transform', t)
80                                 except AttributeError:
81                                         pass
83                                 a =[]
84                                 p = cubicsuperpath.parsePath(node.attributes.getNamedItem('d').value)
85                                 num = 1
86                                 slengths, stotal = csplength(p)
87                                 self.addTextOnPath(self.group,0, 0,str(stotal), id, self.options.offset)
89                                 
90         def addTextOnPath(self,node,x,y,text, id,dy=0):
91                                 new = self.document.createElement('svg:text')
92                                 tp = self.document.createElement('svg:textPath')
93                                 s = {'font-size': self.options.fontsize, 'fill-opacity': '1.0', 'stroke': 'none',
94                                         'font-weight': 'normal', 'font-style': 'normal', 'fill': '#000000'}
95                                 new.setAttribute('style', simplestyle.formatStyle(s))
96                                 new.setAttribute('x', str(x))
97                                 new.setAttribute('y', str(y))
98                                 tp.setAttributeNS('http://www.w3.org/1999/xlink','xlink:href', '#'+id)
99                                 tp.setAttribute('startOffset', "50%")
100                                 #tp.setAttribute('dy', dy) # dubious merit
101                                 new.appendChild(tp)
102                                 tp.appendChild(self.document.createTextNode(str(text)))
103                                 node.appendChild(new)
105 e = Length()
106 e.affect()