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 try:
27 from subprocess import Popen, PIPE
28 bsubprocess = True
29 except:
30 bsubprocess = False
32 class Restack(inkex.Effect):
33 def __init__(self):
34 inkex.Effect.__init__(self)
35 self.OptionParser.add_option("-d", "--direction",
36 action="store", type="string",
37 dest="direction", default="tb",
38 help="direction to restack")
39 self.OptionParser.add_option("-a", "--angle",
40 action="store", type="float",
41 dest="angle", default=0.0,
42 help="arbitrary angle")
43 self.OptionParser.add_option("-x", "--xanchor",
44 action="store", type="string",
45 dest="xanchor", default="m",
46 help="horizontal point to compare")
47 self.OptionParser.add_option("-y", "--yanchor",
48 action="store", type="string",
49 dest="yanchor", default="m",
50 help="vertical point to compare")
51 def effect(self):
52 if len( self.selected ) > 0:
53 objlist = []
54 svg = self.document.getroot()
56 file = self.args[ -1 ]
57 #get all bounding boxes in file by calling inkscape again with the --querry-all command line option
58 #it returns a comma seperated list structured id,x,y,w,h
59 if bsubprocess:
60 p = Popen('inkscape --query-all "%s"' % (file), shell=True, stdout=PIPE, stderr=PIPE)
61 rc = p.wait()
62 f = p.stdout
63 err = p.stderr
64 else:
65 _,f,err = os.popen3( "inkscape --query-all %s" % ( file ) )
66 reader=csv.reader( f.readlines() )
67 f.close()
68 err.close()
70 #build a dictionary with id as the key
71 dimen = dict()
72 for line in reader:
73 dimen[line[0]] = map( float, line[1:])
75 #find the center of all selected objects **Not the average!
76 x,y,w,h = dimen[self.selected.keys()[0]]
77 minx = x
78 miny = y
79 maxx = x + w
80 maxy = y + h
82 for id, node in self.selected.iteritems():
83 # get the bounding box
84 x,y,w,h = dimen[id]
85 if x < minx:
86 minx = x
87 if (x + w) > maxx:
88 maxx = x + w
89 if y < miny:
90 miny = y
91 if (y + h) > maxy:
92 maxy = y + h
94 midx = (minx + maxx) / 2
95 midy = (miny + maxy) / 2
97 #calculate distances for each selected object
98 for id, node in self.selected.iteritems():
99 # get the bounding box
100 x,y,w,h = dimen[id]
102 # calc the comparison coords
103 if self.options.xanchor == "l":
104 cx = x
105 elif self.options.xanchor == "r":
106 cx = x + w
107 else: # middle
108 cx = x + w / 2
110 if self.options.yanchor == "t":
111 cy = y
112 elif self.options.yanchor == "b":
113 cy = y + h
114 else: # middle
115 cy = y + h / 2
117 #direction chosen
118 if self.options.direction == "tb" or self.options.angle == 270:
119 objlist.append([cy,id])
120 elif self.options.direction == "bt" or self.options.angle == 90:
121 objlist.append([-cy,id])
122 elif self.options.direction == "lr" or self.options.angle == 0 or self.options.angle == 360:
123 objlist.append([cx,id])
124 elif self.options.direction == "rl" or self.options.angle == 180:
125 objlist.append([-cx,id])
126 elif self.options.direction == "aa":
127 distance = math.hypot(cx,cy)*(math.cos(math.radians(-self.options.angle)-math.atan2(cy, cx)))
128 objlist.append([distance,id])
129 elif self.options.direction == "ro":
130 distance = math.hypot(midx - cx, midy - cy)
131 objlist.append([distance,id])
132 elif self.options.direction == "ri":
133 distance = -math.hypot(midx - cx, midy - cy)
134 objlist.append([distance,id])
136 objlist.sort()
137 #move them to the top of the object stack in this order.
138 for item in objlist:
139 svg.append( self.selected[item[1]])
141 if __name__ == '__main__':
142 e = Restack()
143 e.affect()
146 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99