Code

957d6a8b20ace566e2601b251558041faf253b07
[inkscape.git] / share / extensions / webslicer-create-rect.py
1 #!/usr/bin/env python
2 '''
3 Copyright (C) 2010 Aurelio A. Heckert, aurium (a) gmail dot com
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 '''
20 import inkex
21 import gettext
23 _ = gettext.gettext
25 def is_empty(val):
26     if val is None:
27         return True
28     else:
29         return len(str(val)) == 0
31 class WebSlicer_CreateRect(inkex.Effect):
33     def __init__(self):
34         inkex.Effect.__init__(self)
35         self.OptionParser.add_option("--name",
36                                      action="store", type="string",
37                                      dest="name",
38                                      help="")
39         self.OptionParser.add_option("--format",
40                                      action="store", type="string",
41                                      dest="format",
42                                      help="")
43         self.OptionParser.add_option("--dpi",
44                                      action="store", type="int",
45                                      dest="dpi",
46                                      help="")
47         self.OptionParser.add_option("--dimension",
48                                      action="store", type="string",
49                                      dest="dimension",
50                                      help="")
51         self.OptionParser.add_option("--bg-color",
52                                      action="store", type="string",
53                                      dest="bg_color",
54                                      help="")
55         self.OptionParser.add_option("--quality",
56                                      action="store", type="int",
57                                      dest="quality",
58                                      help="")
59         self.OptionParser.add_option("--gif-type",
60                                      action="store", type="string",
61                                      dest="gif_type",
62                                      help="")
63         self.OptionParser.add_option("--palette-size",
64                                      action="store", type="int",
65                                      dest="palette_size",
66                                      help="")
67         self.OptionParser.add_option("--html-id",
68                                      action="store", type="string",
69                                      dest="html_id",
70                                      help="")
71         self.OptionParser.add_option("--html-class",
72                                      action="store", type="string",
73                                      dest="html_class",
74                                      help="")
75         self.OptionParser.add_option("--layout-disposition",
76                                      action="store", type="string",
77                                      dest="layout_disposition",
78                                      help="")
79         self.OptionParser.add_option("--layout-position-anchor",
80                                      action="store", type="string",
81                                      dest="layout_position_anchor",
82                                      help="")
83         # inkscape param workarround
84         self.OptionParser.add_option("--tab")
87     def unique_slice_name(self):
88         name = self.options.name
89         el = self.document.xpath( '//*[@id="'+name+'"]', namespaces=inkex.NSS )
90         if len(el) > 0:
91             if name[-3:] == '-00': name = name[:-3]
92             num = 0
93             num_s = '00'
94             while len(el) > 0:
95                 num += 1
96                 num_s = str(num)
97                 if len(num_s)==1 : num_s = '0'+num_s
98                 el = self.document.xpath( '//*[@id="'+name+'-'+num_s+'"]',
99                                           namespaces=inkex.NSS )
100             self.options.name = name+'-'+num_s
103     def validate_options(self):
104         self.options.format = self.options.ensure_value('format', 'png').lower()
105         if not is_empty( self.options.dimension ):
106             self.options.dimension
108     def effect(self):
109         self.validate_options()
110         layer = self.get_slicer_layer()
111         #TODO: get selected elements to define location and size
112         rect = inkex.etree.SubElement(layer, 'rect')
113         if is_empty(self.options.name):
114             self.options.name = 'slice-00'
115         self.unique_slice_name()
116         rect.set('id', self.options.name)
117         rect.set('fill', 'red')
118         rect.set('opacity', '0.5')
119         rect.set('x', '-100')
120         rect.set('y', '-100')
121         rect.set('width', '200')
122         rect.set('height', '200')
123         desc = inkex.etree.SubElement(rect, 'desc')
124         conf_txt = "format:"+ self.options.format +"\n"
125         if not is_empty(self.options.dpi):
126             conf_txt += "dpi:"     + str(self.options.dpi) +"\n"
127         if not is_empty(self.options.html_id):
128             conf_txt += "html-id:" + self.options.html_id
129         desc.text = "\n".join( self.get_full_conf_list() )
133     def get_conf_from_list(self, conf_atts):
134         conf_list = []
135         for att in conf_atts:
136             if not is_empty(getattr(self.options, att)):
137                 conf_list.append( att +':'+ str(getattr(self.options, att)) )
138         return conf_list
141     def get_full_conf_list(self):
142         conf_list = [ 'format:'+self.options.format ]
143         if self.options.format == 'gif':
144             conf_list.extend( get_conf_from_list([ 'gif_type', 'palette_size' ]) )
145         if self.options.format == 'jpg':
146             conf_list.extend( get_conf_from_list([ 'quality' ]) )
147         conf_general_atts = [
148                 'dpi', 'dimension',
149                 'bg_color', 'html_id', 'html_class',
150                 'layout_disposition', 'layout_position_anchor'
151             ]
152         conf_list.extend( get_conf_from_list(conf_general_atts) )
153         return conf_list
156     def get_slicer_layer(self):
157         # Test if webslicer-layer layer existis
158         layer = self.document.xpath(
159                      '//*[@id="webslicer-layer" and @inkscape:groupmode="layer"]',
160                      namespaces=inkex.NSS)
161         if len(layer) is 0:
162             # Create a new layer
163             layer = inkex.etree.SubElement(self.document.getroot(), 'g')
164             layer.set('id', 'webslicer-layer')
165             layer.set(inkex.addNS('label', 'inkscape'), 'Web Slicer')
166             layer.set(inkex.addNS('groupmode', 'inkscape'), 'layer')
167         else:
168             layer = layer[0]
169         return layer
172 if __name__ == '__main__':
173     e = WebSlicer_CreateRect()
174     e.affect()