1 #!/usr/bin/env python
2 """
3 Copyright (C) 2007,2008 Rob Antonishen; rob.antonishen@gmail.com
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
23 """
24 import inkex, os, csv, math
26 class Restack(inkex.Effect):
27 def __init__(self):
28 inkex.Effect.__init__(self)
29 self.OptionParser.add_option("-d", "--direction",
30 action="store", type="string",
31 dest="direction", default="tb",
32 help="direction to restack")
33 self.OptionParser.add_option("-a", "--angle",
34 action="store", type="float",
35 dest="angle", default=0.0,
36 help="arbitrary angle")
37 self.OptionParser.add_option("-x", "--xanchor",
38 action="store", type="string",
39 dest="xanchor", default="m",
40 help="horizontal point to compare")
41 self.OptionParser.add_option("-y", "--yanchor",
42 action="store", type="string",
43 dest="yanchor", default="m",
44 help="vertical point to compare")
45 def effect(self):
46 if len( self.selected ) > 0:
47 objlist = []
48 svg = self.document.getroot()
50 file = self.args[ -1 ]
51 #get all bounding boxes in file by calling inkscape again with the --querry-all command line option
52 #it returns a comma seperated list structured id,x,y,w,h
53 _,f,err = os.popen3( "inkscape --query-all %s" % ( file ) )
54 reader=csv.reader( f.readlines() )
55 f.close()
56 err.close()
58 #build a dictionary with id as the key
59 dimen = dict()
60 for line in reader:
61 dimen[line[0]] = map( float, line[1:])
63 #find the center of all selected objects **Not the average!
64 x,y,w,h = dimen[self.selected.keys()[0]]
65 minx = x
66 miny = y
67 maxx = x + w
68 maxy = y + h
70 for id, node in self.selected.iteritems():
71 # get the bounding box
72 x,y,w,h = dimen[id]
73 if x < minx:
74 minx = x
75 if (x + w) > maxx:
76 maxx = x + w
77 if y < miny:
78 miny = y
79 if (y + h) > maxy:
80 maxy = y + h
82 midx = (minx + maxx) / 2
83 midy = (miny + maxy) / 2
85 #calculate distances fro each selected object
86 for id, node in self.selected.iteritems():
87 # get the bounding box
88 x,y,w,h = dimen[id]
90 # calc the comparison coords
91 if self.options.xanchor == "l":
92 cx = x
93 elif self.options.xanchor == "r":
94 cx = x + w
95 else: # middle
96 cx = x + w / 2
98 if self.options.yanchor == "t":
99 cy = y
100 elif self.options.yanchor == "b":
101 cy = y + h
102 else: # middle
103 cy = y + h / 2
105 #direction chosen
106 if self.options.direction == "tb" or self.options.angle == 270:
107 objlist.append([cy,id])
108 elif self.options.direction == "bt" or self.options.angle == 90:
109 objlist.append([-cy,id])
110 elif self.options.direction == "lr" or self.options.angle == 0 or self.options.angle == 360:
111 objlist.append([cx,id])
112 elif self.options.direction == "rl" or self.options.angle == 180:
113 objlist.append([-cx,id])
114 elif self.options.direction == "aa":
115 distance = math.hypot(cx,cy)*(math.cos(math.radians(-self.options.angle)-math.atan2(cy, cx)))
116 objlist.append([distance,id])
117 elif self.options.direction == "ro":
118 distance = math.hypot(midx - cx, midy - cy)
119 objlist.append([distance,id])
120 elif self.options.direction == "ri":
121 distance = -math.hypot(midx - cx, midy - cy)
122 objlist.append([distance,id])
124 objlist.sort()
125 #move them to the top of the object stack in this order.
126 for item in objlist:
127 svg.append( self.selected[item[1]])
129 e = Restack()
130 e.affect()