Code

get the CORRECT Sodipodi namespace in there
[inkscape.git] / share / extensions / straightseg.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 math, inkex, simplepath, sys
21 def pointAtPercent((x1, y1), (x2, y2), percent):
22         percent /= 100.0
23         x = x1 + (percent * (x2 - x1))
24         y = y1 + (percent * (y2 - y1))
25         return [x,y]
27 class SegmentStraightener(inkex.Effect):
28         def __init__(self):
29                 inkex.Effect.__init__(self)
30                 self.OptionParser.add_option("-p", "--percent",
31                                                 action="store", type="float", 
32                                                 dest="percent", default=10.0,
33                                                 help="move curve handles PERCENT percent closer to a straight line")
34                 self.OptionParser.add_option("-b", "--behavior",
35                                                 action="store", type="int", 
36                                                 dest="behave", default=1,
37                                                 help="straightening behavior for cubic segments")
38         def effect(self):
39                 for id, node in self.selected.iteritems():
40                         if node.tagName == 'path':
41                                 d = node.attributes.getNamedItem('d')
42                                 p = simplepath.parsePath(d.value)
43                                 last = []
44                                 subPathStart = []
45                                 for cmd,params in p:
46                                         if cmd == 'C':
47                                                 if self.options.behave <= 1:
48                                                         #shorten handles towards end points
49                                                         params[:2] = pointAtPercent(params[:2],last[:],self.options.percent)    
50                                                         params[2:4] = pointAtPercent(params[2:4],params[-2:],self.options.percent)
51                                                 else:
52                                                         #shorten handles towards thirds of the segment                                                  
53                                                         dest1 = pointAtPercent(last[:],params[-2:],33.3)
54                                                         dest2 = pointAtPercent(params[-2:],last[:],33.3)
55                                                         params[:2] = pointAtPercent(params[:2],dest1[:],self.options.percent)   
56                                                         params[2:4] = pointAtPercent(params[2:4],dest2[:],self.options.percent)
57                                         elif cmd == 'Q':
58                                                 dest = pointAtPercent(last[:],params[-2:],50)
59                                                 params[:2] = pointAtPercent(params[:2],dest,self.options.percent)
60                                         if cmd == 'M':
61                                                 subPathStart = params[-2:]
62                                         if cmd == 'Z':
63                                                 last = subPathStart[:]
64                                         else:
65                                                 last = params[-2:]
66                                 d.value = simplepath.formatPath(p)
68 e = SegmentStraightener()
69 e.affect()