Code

modify calculation of control arm length
[inkscape.git] / share / extensions / svgcalendar.py
1 #!/usr/bin/env python
3 '''
4 calendar.py
5 A calendar generator plugin for Inkscape, but also can be used as a standalone
6 command line application.
8 Copyright (C) 2008 Aurelio A. Heckert <aurium(a)gmail.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 '''
25 __version__ = "0.2"
27 import inkex, simplestyle, re, calendar
28 from datetime import *
30 class SVGCalendar (inkex.Effect):
32     def __init__(self):
33         inkex.Effect.__init__(self)
34         self.OptionParser.add_option("--tab",
35           action="store", type="string",
36           dest="tab")
37         self.OptionParser.add_option("--month",
38           action="store", type="int",
39           dest="month", default=0,
40           help="Month to be generated. If 0, then the entry year will be generated.")
41         self.OptionParser.add_option("--year",
42           action="store", type="int",
43           dest="year", default=0,
44           help="Year to be generated. If 0, then the current year will be generated.")
45         self.OptionParser.add_option("--fill-empty-day-boxes",
46           action="store", type="inkbool",
47           dest="fill_edb", default=True,
48           help="Fill empty day boxes with next month days.")
49         self.OptionParser.add_option("--start-day",
50           action="store", type="string",
51           dest="start_day", default="sun",
52           help='Week start day. ("sun" or "mon")')
53         self.OptionParser.add_option("--weekend",
54           action="store", type="string",
55           dest="weekend", default="sat+sun",
56           help='Define the weekend days. ("sat+sun" or "sat" or "sun")')
57         self.OptionParser.add_option("--color-year",
58           action="store", type="string",
59           dest="color_year", default="#888",
60           help='Color for the year header.')
61         self.OptionParser.add_option("--color-month",
62           action="store", type="string",
63           dest="color_month", default="#666",
64           help='Color for the month name header.')
65         self.OptionParser.add_option("--color-day-name",
66           action="store", type="string",
67           dest="color_day_name", default="#999",
68           help='Color for the week day names header.')
69         self.OptionParser.add_option("--color-day",
70           action="store", type="string",
71           dest="color_day", default="#000",
72           help='Color for the common day box.')
73         self.OptionParser.add_option("--color-weekend",
74           action="store", type="string",
75           dest="color_weekend", default="#777",
76           help='Color for the weekend days.')
77         self.OptionParser.add_option("--color-nmd",
78           action="store", type="string",
79           dest="color_nmd", default="#BBB",
80           help='Color for the next month day, in enpty day boxes.')
81         self.OptionParser.add_option("--month-names",
82           action="store", type="string",
83           dest="month_names", default='January February March April May June '+\
84                               'July August September October November December',
85           help='The month names for localization.')
86         self.OptionParser.add_option("--day-names",
87           action="store", type="string",
88           dest="day_names", default='Sun Mon Tue Wed Thu Fri Sat',
89           help='The week day names for localization.')
90         self.OptionParser.add_option("--encoding",
91           action="store", type="string",
92           dest="input_encode", default='utf-8',
93           help='The input encoding of the names.')
95     def validate_options(self):
96         #inkex.errormsg( self.options.input_encode )
97         # Convert string names lists in real lists:
98         m = re.match( '\s*(.*[^\s])\s*', self.options.month_names )
99         self.options.month_names = re.split( '\s+', m.group(1) )
100         m = re.match( '\s*(.*[^\s])\s*', self.options.day_names )
101         self.options.day_names = re.split( '\s+', m.group(1) )
102         # Validate names lists:
103         if len(self.options.month_names) != 12:
104           inkex.errormsg('The month name list "'+
105                          str(self.options.month_names)+
106                          '" is invalid. Using default.')
107           self.options.month_names = ['January','February','March',
108                                       'April',  'May',     'June',
109                                       'July',   'August',  'September',
110                                       'October','November','December']
111         if len(self.options.day_names) != 7:
112           inkex.errormsg('The day name list "'+
113                          str(self.options.day_names)+
114                          '" is invalid. Using default.')
115           self.options.day_names = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']
116         # Convert year 0 to current year:
117         if self.options.year == 0: self.options.year = datetime.today().year
118         # Set the calendar start day:
119         if self.options.start_day=='sun':
120           calendar.setfirstweekday(6)
121         else:
122           calendar.setfirstweekday(0)
124     # initial values:
125     month_x_pos = 0
126     month_y_pos = 0
128     def calculate_size_and_positions(self):
129         self.doc_w = inkex.unittouu(self.document.getroot().get('width'))
130         self.doc_h = inkex.unittouu(self.document.getroot().get('height'))
131         if self.doc_h > self.doc_w:
132           self.months_per_line = 3
133         else:
134           self.months_per_line = 4
135         #self.month_w = self.doc_w / self.months_per_line
136         self.month_w = (self.doc_w * 0.8) / self.months_per_line
137         self.month_margin = self.month_w / 10
138         self.day_w = self.month_w / 7
139         self.day_h = self.month_w / 9
140         self.month_h = self.day_w * 7
141         if self.options.month == 0:
142           self.year_margin = (( self.doc_w + self.day_w -
143                                (self.month_w*self.months_per_line) -
144                                (self.month_margin*(self.months_per_line-1))
145                              ) / 2 ) #- self.month_margin
146         else:
147           self.year_margin = ( self.doc_w - self.month_w ) / 2
148         self.style_day = {
149           'font-size': str( self.day_w / 2 ),
150           'font-family': 'arial',
151           'text-anchor': 'middle',
152           'text-align': 'center',
153           'fill': self.options.color_day
154           }
155         self.style_weekend = self.style_day.copy()
156         self.style_weekend['fill'] = self.options.color_weekend
157         self.style_nmd = self.style_day.copy()
158         self.style_nmd['fill'] = self.options.color_nmd
159         self.style_month = self.style_day.copy()
160         self.style_month['fill'] = self.options.color_month
161         self.style_month['font-size'] = str( self.day_w / 1.5 )
162         self.style_month['font-weight'] = 'bold'
163         self.style_day_name = self.style_day.copy()
164         self.style_day_name['fill'] = self.options.color_day_name
165         self.style_day_name['font-size'] = str( self.day_w / 3 )
166         self.style_year = self.style_day.copy()
167         self.style_year['fill'] = self.options.color_year
168         self.style_year['font-size'] = str( self.day_w * 2 )
169         self.style_year['font-weight'] = 'bold'
171     def is_weekend(self, pos):
172         # weekend values: "sat+sun" or "sat" or "sun"
173         if self.options.start_day=='sun':
174           if self.options.weekend=='sat+sun' and pos==0: return True
175           if self.options.weekend=='sat+sun' and pos==6: return True
176           if self.options.weekend=='sat' and pos==6: return True
177           if self.options.weekend=='sun' and pos==0: return True
178         else:
179           if self.options.weekend=='sat+sun' and pos==5: return True
180           if self.options.weekend=='sat+sun' and pos==6: return True
181           if self.options.weekend=='sat' and pos==5: return True
182           if self.options.weekend=='sun' and pos==6: return True
183         return False
185     def in_line_month(self, cal):
186         cal2 = []
187         for week in cal:
188           for day in week:
189             if day != 0:
190               cal2.append(day)
191         return cal2
193     def write_month_header(self, g, m):
194         txt_atts = {'style': simplestyle.formatStyle(self.style_month),
195                     'x': str( (self.month_w - self.day_w) / 2 ),
196                     'y': str( self.day_h / 5 ) }
197         try:
198           inkex.etree.SubElement(g, 'text', txt_atts).text = unicode(self.options.month_names[m-1], self.options.input_encode)
199         except:
200           inkex.errormsg('You must select your correct system encode.')
201           exit(1)
202         gw = inkex.etree.SubElement(g, 'g')
203         week_x = 0
204         if self.options.start_day=='sun':
205           for wday in self.options.day_names:
206             txt_atts = {'style': simplestyle.formatStyle(self.style_day_name),
207                         'x': str( self.day_w * week_x ),
208                         'y': str( self.day_h ) }
209             try:
210               inkex.etree.SubElement(gw, 'text', txt_atts).text = unicode(wday, self.options.input_encode)
211             except:
212               inkex.errormsg('You must select your correct system encode.')
213               exit(1)
214             week_x += 1
215         else:
216           w2 = self.options.day_names[1:]
217           w2.append(self.options.day_names[0])
218           for wday in w2:
219             txt_atts = {'style': simplestyle.formatStyle(self.style_day_name),
220                         'x': str( self.day_w * week_x ),
221                         'y': str( self.day_h ) }
222             try:
223               inkex.etree.SubElement(gw, 'text', txt_atts).text = unicode(wday, self.options.input_encode)
224             except:
225               inkex.errormsg('You must select your correct system encode.')
226               exit(1)
227             week_x += 1
229     def create_month(self, m):
230         txt_atts = {
231           'transform': 'translate('+str(self.year_margin +
232                                        (self.month_w + self.month_margin) *
233                                         self.month_x_pos) +
234                                 ','+str((self.day_h * 4) +
235                                        (self.month_h * self.month_y_pos))+')',
236           'id': 'month_'+str(m)+'_'+str(self.options.year) }
237         g = inkex.etree.SubElement(self.year_g, 'g', txt_atts)
238         self.write_month_header(g, m)
239         gdays = inkex.etree.SubElement(g, 'g')
240         cal = calendar.monthcalendar(self.options.year,m)
241         if m == 1:
242           before_month = \
243             self.in_line_month( calendar.monthcalendar(self.options.year-1, 12) )
244         else:
245           before_month = \
246             self.in_line_month( calendar.monthcalendar(self.options.year, m-1) )
247         if m == 12:
248           next_month = \
249             self.in_line_month( calendar.monthcalendar(self.options.year+1, 1) )
250         else:
251           next_month = \
252             self.in_line_month( calendar.monthcalendar(self.options.year, m+1) )
253         if len(cal) < 6: # add a line after the last week
254           cal.append([0,0,0,0,0,0,0])
255         if len(cal) < 6: # add a line before the first week (Feb 2009)
256           cal.reverse()
257           cal.append([0,0,0,0,0,0,0])
258           cal.reverse()
259         # How mutch before month days will be showed:
260         bmd = cal[0].count(0) + cal[1].count(0)
261         before = True
262         week_y = 0
263         for week in cal:
264           week_x = 0
265           for day in week:
266             style = self.style_day
267             if self.is_weekend(week_x): style = self.style_weekend
268             if day == 0: style = self.style_nmd
269             txt_atts = {'style': simplestyle.formatStyle(style),
270                         'x': str( self.day_w * week_x ),
271                         'y': str( self.day_h * (week_y+2) ) }
272             if day==0 and not self.options.fill_edb:
273               pass # draw nothing
274             elif day==0:
275               if before:
276                 inkex.etree.SubElement(gdays, 'text', txt_atts).text = str( before_month[-bmd] )
277                 bmd -= 1
278               else:
279                 inkex.etree.SubElement(gdays, 'text', txt_atts).text = str( next_month[bmd] )
280                 bmd += 1
281             else:
282               inkex.etree.SubElement(gdays, 'text', txt_atts).text = str(day)
283               before = False
284             week_x += 1
285           week_y += 1
286         self.month_x_pos += 1
287         if self.month_x_pos >= self.months_per_line:
288           self.month_x_pos = 0
289           self.month_y_pos += 1
291     def effect(self):
292         self.validate_options()
293         self.calculate_size_and_positions()
294         parent = self.document.getroot()
295         txt_atts = {
296           'id': 'year_'+str(self.options.year) }
297         self.year_g = inkex.etree.SubElement(parent, 'g', txt_atts)
298         txt_atts = {'style': simplestyle.formatStyle(self.style_year),
299                     'x': str( self.doc_w / 2 ),
300                     'y': str( self.day_w * 1.5 ) }
301         inkex.etree.SubElement(self.year_g, 'text', txt_atts).text = str(self.options.year)
302         if self.options.month == 0:
303           for m in range(1,13):
304             self.create_month(m)
305         else:
306           self.create_month(self.options.month)
309 if __name__ == '__main__':
310     e = SVGCalendar()
311     e.affect()