#!/usr/bin/env python """ Copyright (C) 2007 Rob Antonishen; rob.antonishen@gmail.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import inkex, os class Restack(inkex.Effect): def __init__(self): inkex.Effect.__init__(self) self.OptionParser.add_option("-d", "--direction", action="store", type="string", dest="direction", default="tb", help="direction to restack") self.OptionParser.add_option("-x", "--xanchor", action="store", type="string", dest="xanchor", default="m", help="horizontal point to compare") self.OptionParser.add_option("-y", "--yanchor", action="store", type="string", dest="yanchor", default="m", help="vertical point to compare") def effect(self): if len( self.selected ) > 0: objlist = [] svg = self.document.getElementsByTagName( 'svg' )[ 0 ] for id, node in self.selected.iteritems(): # get the bounding box attrs = { 'x' : 0, 'y' : 0, 'width' : 0, 'height' : 0 } file = self.args[ -1 ] for query in attrs.keys(): f = os.popen( "inkscape --query-%s --query-id=%s %s" % ( query, id, file ) ) attrs[ query ] = float( f.read() ) f.close() # calc the center coords if self.options.xanchor == "l": cx = attrs["x"] elif self.options.xanchor == "r": cx = attrs["x"] + attrs["width"] else: # middle cx = attrs["x"] + attrs["width"] / 2 if self.options.yanchor == "t": cy = attrs["y"] elif self.options.yanchor == "b": cy = attrs["y"] + attrs["height"] else: # middle cy = attrs["y"] + attrs["height"] / 2 #direction chosen if self.options.direction == "tb": objlist.append([cy,id]) elif self.options.direction == "bt": objlist.append([-cy,id]) elif self.options.direction == "lr": objlist.append([cx,id]) else: #right to left objlist.append([-cx,id]) objlist.sort() for item in objlist: #inkex.debug( '%s : %s ' % ( item[0], item[1] ) ) svg.appendChild( self.selected[item[1]]) e = Restack() e.affect()