1#!/usr/local/bin/python3.8 2# 3# Copyright (C) 2007-2011 Rob Antonishen; rob.antonishen@gmail.com 4# 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: 11# 12# The above copyright notice and this permission notice shall be included in 13# all copies or substantial portions of the Software. 14# 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. 22# 23 24import random 25 26import inkex 27from inkex.utils import KeyDict 28from inkex import SvgDocumentElement 29 30# Old settings, supported because users click 'ok' without looking. 31XAN = KeyDict({'l': 'left', 'r': 'right', 'm': 'center_x'}) 32YAN = KeyDict({'t': 'top', 'b': 'bottom', 'm': 'center_y'}) 33CUSTOM_DIRECTION = {270: 'tb', 90: 'bt', 0: 'lr', 360: 'lr', 180: 'rl'} 34 35class Restack(inkex.EffectExtension): 36 """Change the z-order of objects based on their position on the canvas""" 37 restack_help = staticmethod(lambda: None) 38 39 def add_arguments(self, pars): 40 pars.add_argument("--tab", type=self.arg_method('restack'), default=self.restack_positional) 41 pars.add_argument("--direction", default="tb", help="direction to restack") 42 pars.add_argument("--angle", type=float, default=0.0, help="arbitrary angle") 43 pars.add_argument("--xanchor", default="m", help="horizontal point to compare") 44 pars.add_argument("--yanchor", default="m", help="vertical point to compare") 45 pars.add_argument("--zsort", default="rev", help="Restack mode based on Z-Order") 46 pars.add_argument("--nb_direction", default='', help='Direction tab') 47 48 def effect(self): 49 if not self.svg.selected: 50 raise inkex.AbortExtension("There is no selection to restack.") 51 52 # process selection to get list of objects to be arranged 53 parentnode = None 54 for node in self.svg.selection.filter(SvgDocumentElement): 55 parentnode = node 56 self.svg.set_selection(*list(node)) 57 58 if parentnode is None: 59 parentnode = self.svg.get_current_layer() 60 61 self.options.tab(parentnode) 62 63 def restack_positional(self, parentnode): 64 """Restack based on canvas position""" 65 # move them to the top of the object stack in this order. 66 for node in sorted(self.svg.selected.values(), key=self._sort): 67 parentnode.append(node) 68 return True 69 70 def _sort(self, node): 71 x, y = self.options.xanchor, self.options.yanchor 72 selbox = self.svg.selection.bounding_box() 73 direction = self.options.direction 74 if 'custom' in self.options.nb_direction: 75 direction = self.options.angle 76 return node.bounding_box().get_anchor(x, y, direction, selbox) 77 78 def restack_z_order(self, parentnode): 79 """Restack based on z-order""" 80 objects = list(self.svg.selected.paint_order()) 81 if self.options.zsort == "rev": 82 objects.reverse() 83 elif self.options.zsort == "rand": 84 random.shuffle(objects) 85 if parentnode is not None: 86 for item in objects: 87 parentnode.append(item) 88 return True 89 90if __name__ == '__main__': 91 Restack().run() 92