Code

Updated the text of the JessyInk effect extension to make translation easier (bug...
[inkscape.git] / share / extensions / interp_att_g.py
1 #!/usr/bin/env python
2 '''
3 Copyright (C) 2009 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 '''
19 import inkex, simplestyle, math, re, string
21 class InterpAttG(inkex.Effect):
23     def __init__(self):
24         inkex.Effect.__init__(self)
25         self.OptionParser.add_option("-a", "--att",
26                         action="store", type="string",
27                         dest="att", default="fill",
28                         help="Attribute to be interpolated.")
29         self.OptionParser.add_option("-o", "--att-other",
30                         action="store", type="string",
31                         dest="att_other",
32                         help="Other attribute (for a limited UI).")
33         self.OptionParser.add_option("-t", "--att-other-type",
34                         action="store", type="string",
35                         dest="att_other_type",
36                         help="The other attribute type.")
37         self.OptionParser.add_option("-w", "--att-other-where",
38                         action="store", type="string",
39                         dest="att_other_where",
40                         help="That is a tag attribute or a style attribute?")
41         self.OptionParser.add_option("-s", "--start-val",
42                         action="store", type="string",
43                         dest="start_val", default="#F00",
44                         help="Initial interpolation value.")
45         self.OptionParser.add_option("-e", "--end-val",
46                         action="store", type="string",
47                         dest="end_val", default="#00F",
48                         help="End interpolation value.")
49         self.OptionParser.add_option("-u", "--unit",
50                         action="store", type="string",
51                         dest="unit", default="color",
52                         help="Values unit.")
53         self.OptionParser.add_option("--tab",
54                         action="store", type="string",
55                         dest="tab",
56                         help="The selected UI-tab when OK was pressed")
58     def getColorValues(self):
59       sv = string.replace( self.options.start_val, '#', '' )
60       ev = string.replace( self.options.end_val, '#', '' )
61       if re.search('\s|,', sv):
62         # There are separators. That must be a integer RGB color definition.
63         sv = re.split( '[\s,]+', sv )
64         ev = re.split( '[\s,]+', ev )
65         self.R_ini = int( sv[0] )
66         self.G_ini = int( sv[1] )
67         self.B_ini = int( sv[2] )
68         self.R_end = int( ev[0] )
69         self.G_end = int( ev[1] )
70         self.B_end = int( ev[2] )
71       else:
72         # There is no separator. That must be a Hex RGB color definition.
73         if len(sv) == 3:
74           self.R_ini = int( sv[0] + sv[0], 16 )
75           self.G_ini = int( sv[1] + sv[1], 16 )
76           self.B_ini = int( sv[2] + sv[2], 16 )
77           self.R_end = int( ev[0] + ev[0], 16 )
78           self.G_end = int( ev[1] + ev[1], 16 )
79           self.B_end = int( ev[2] + ev[2], 16 )
80         else: #the len must be 6
81           self.R_ini = int( sv[0] + sv[1], 16 )
82           self.G_ini = int( sv[2] + sv[3], 16 )
83           self.B_ini = int( sv[4] + sv[5], 16 )
84           self.R_end = int( ev[0] + ev[1], 16 )
85           self.G_end = int( ev[2] + ev[3], 16 )
86           self.B_end = int( ev[4] + ev[5], 16 )
87       self.R_inc = ( self.R_end - self.R_ini ) / float( self.tot_el - 1 )
88       self.G_inc = ( self.G_end - self.G_ini ) / float( self.tot_el - 1 )
89       self.B_inc = ( self.B_end - self.B_ini ) / float( self.tot_el - 1 )
90       self.R_cur = self.R_ini
91       self.G_cur = self.G_ini
92       self.B_cur = self.B_ini
94     def getNumberValues(self):
95       sv = self.options.start_val
96       ev = self.options.end_val
97       if self.inte_att_type and self.inte_att_type != 'none':
98         sv = inkex.unittouu( sv + self.inte_att_type )
99         ev = inkex.unittouu( ev + self.inte_att_type )
100       self.val_cur = self.val_ini = sv
101       self.val_end = ev
102       self.val_inc = ( ev - sv ) / float( self.tot_el - 1 )
104     def getTotElements(self):
105       self.tot_el = 0
106       self.collection = None
107       if len( self.selected ) == 0:
108         return False
109       if len( self.selected ) > 1:
110         # multiple selection
111         self.collection = self.options.ids
112         for i in self.options.ids:
113           path = '//*[@id="%s"]' % i
114           self.collection[self.tot_el] = self.document.xpath(path, namespaces=inkex.NSS)[0]
115           self.tot_el += 1
116       else:
117         # must be a group
118         self.collection = self.selected[ self.options.ids[0] ]
119         for i in self.collection:
120           self.tot_el += 1
122     def effect(self):
123       if self.options.att == 'other':
124         self.inte_att = self.options.att_other
125         self.inte_att_type = self.options.att_other_type
126         self.where = self.options.att_other_where
127       else:
128         self.inte_att = self.options.att
129         if   self.inte_att == 'width':
130           self.inte_att_type = 'float'
131           self.where = 'tag'
132         elif self.inte_att == 'height':
133           self.inte_att_type = 'float'
134           self.where = 'tag'
135         elif self.inte_att == 'scale':
136           self.inte_att_type = 'float'
137           self.where = 'transform'
138         elif self.inte_att == 'trans-x':
139           self.inte_att_type = 'float'
140           self.where = 'transform'
141         elif self.inte_att == 'trans-y':
142           self.inte_att_type = 'float'
143           self.where = 'transform'
144         elif self.inte_att == 'fill':
145           self.inte_att_type = 'color'
146           self.where = 'style'
147         elif self.inte_att == 'opacity':
148           self.inte_att_type = 'float'
149           self.where = 'style'
151       self.getTotElements()
153       if self.inte_att_type == 'color':
154         self.getColorValues()
155       else:
156         self.getNumberValues()
158       if self.collection is None:
159         inkex.errormsg( 'There is no selection to interpolate' )
160         return False
162       for node in self.collection:
163         if self.inte_att_type == 'color':
164           val = 'rgb('+ \
165                   str(int(round(self.R_cur))) +','+ \
166                   str(int(round(self.G_cur))) +','+ \
167                   str(int(round(self.B_cur))) +')'
168         else:
169           if self.inte_att_type == 'float':
170             val = self.val_cur
171           else: # inte_att_type == 'int'
172             val = round(self.val_cur)
174         if self.where == 'style':
175           s = node.get('style')
176           re_find = '(^|;)'+ self.inte_att +':[^;]*(;|$)'
177           if re.search( re_find, s ):
178             s = re.sub( re_find, '\\1'+ self.inte_att +':'+ str(val) +'\\2', s )
179           else:
180             s += ';'+ self.inte_att +':'+ str(val)
181           node.set( 'style', s )
182         elif self.where == 'transform':
183           t = node.get('transform')
184           if t == None: t = ""
185           if self.inte_att == 'trans-x':
186             val = "translate("+ str(val) +",0)"
187           elif self.inte_att == 'trans-y':
188             val = "translate(0,"+ str(val) +")"
189           else:
190             val = self.inte_att + "("+ str(val) +")"
191           node.set( 'transform', t +" "+ val )
192         else: # self.where == 'tag':
193           node.set( self.inte_att, str(val) )
195         if self.inte_att_type == 'color':
196           self.R_cur += self.R_inc
197           self.G_cur += self.G_inc
198           self.B_cur += self.B_inc
199         else:
200           self.val_cur += self.val_inc
202       return True
204 if __name__ == '__main__':   #pragma: no cover
205     e = InterpAttG()
206     if e.affect():
207       exit(0)
208     else:
209       exit(1)