File indexing completed on 2024-11-17 05:00:19
0001 #!/usr/bin/env python 0002 # -*- coding: utf-8 -*- 0003 0004 ''' 0005 SPDX-FileCopyrightText: 2009 Marco Martin <notmart@gmail.com> 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 ''' 0009 0010 0011 import sys 0012 sys.path.append('/usr/share/inkscape/extensions') 0013 0014 import inkex 0015 0016 from simplestyle import * 0017 from simpletransform import * 0018 import pathmodifier 0019 0020 class PlasmaNamesEffect(pathmodifier.PathModifier): 0021 """ 0022 Renames 9 selected elements as a plasma theme frame 0023 """ 0024 def __init__(self): 0025 pathmodifier.PathModifier.__init__(self) 0026 0027 # Define string option "--prefix" with "-p" shortcut and default value "World". 0028 self.OptionParser.add_option('-p', '--prefix', action = 'store', 0029 type = 'string', dest = 'prefix', default = '', 0030 help = 'Prefix of the svg elements') 0031 0032 def nodeBBox(self, node): 0033 path = node 0034 return computeBBox([path]) 0035 0036 0037 def effect(self): 0038 # Get script's "--prefix" option value. 0039 prefix = self.options.prefix 0040 0041 #9 elements: is a frame. 4 elements: is a border hint 0042 positions = [] 0043 if len(self.selected) == 9: 0044 positions = ['topleft', 'left', 'bottomleft', 'top', 'center', 'bottom', 'topright', 'right', 'bottomright'] 0045 elif len(self.selected) == 4: 0046 positions = ['hint-left-margin', 'hint-top-margin', 'hint-bottom-margin', 'hint-right-margin'] 0047 else: 0048 return 0049 0050 #some heuristics to normalize the values, find the least coords and size 0051 minX = 9999 0052 minY = 9999 0053 minWidth = 9999 0054 minHeight = 9999 0055 for id, node in self.selected.iteritems(): 0056 nodeBox = self.nodeBBox(node) 0057 minX = min(minX, int(nodeBox[0])) 0058 minY = min(minY, int(nodeBox[2])) 0059 minWidth = min(minWidth, int(nodeBox[1] - nodeBox[0])) 0060 minHeight = min(minHeight, int(nodeBox[3] - nodeBox[2])) 0061 0062 0063 nodedictionary = {} 0064 for id, node in self.selected.iteritems(): 0065 nodeBox = self.nodeBBox(node) 0066 x = int(nodeBox[0])/minWidth - minX 0067 y = int(nodeBox[2])/minHeight - minY 0068 nodedictionary[x*1000 + y] = node 0069 0070 keys = nodedictionary.keys(); 0071 keys.sort(); 0072 i = 0 0073 0074 for (k) in keys: 0075 name = '' 0076 if prefix: 0077 name = '%s-%s' % (prefix, positions[i]) 0078 else: 0079 name = '%s' % (positions[i]) 0080 nodedictionary[k].set('id', name) 0081 i = i+1 0082 0083 # Create effect instance and apply it. 0084 effect = PlasmaNamesEffect() 0085 effect.affect()