-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_calendar.py
148 lines (119 loc) · 5.05 KB
/
generate_calendar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python
# encoding: utf-8
"""
generate_calendar.py
Created by Maximillian Dornseif on 2010-07-13.
Copyright (c) 2010 HUDORA. All rights reserved.
"""
import sys
import os
import math
import cairo
import calendar
import datetime
LENGTHFACTOR = 1
WIDTH, HEIGHT = 4800, 3250 * LENGTHFACTOR
MONTHFONTSIZE = 36
DAYFONTSIZE = 6
DAYTEXTOFFSET = 12
DAYNUMFONTSIZE = 42
DAYTOPOFFSET = 200
DAYHEIGHT = 80 * LENGTHFACTOR
MONTHWIDTH = 350
def print_centered(ctx, text, x, y):
x_bearing, y_bearing, width, height = ctx.text_extents(text)[:4]
ctx.move_to(x - width / 2 - x_bearing, y)
ctx.show_text(text)
monthnames = u"dummy Januar Februar März April Mai Juni Juli August September Oktober November Dezember".split()
daynames = u"Montag Dienstag Mittwoch Donnerstag Freitag Samstag Sonntag".split()
def main(year):
surface = cairo.PSSurface("%s.eps" % year, WIDTH, HEIGHT)
surface.set_eps(True)
ctx = cairo.Context(surface)
ctx.set_source_rgb(1, 1, 1)
ctx.rectangle(0, 0, WIDTH, HEIGHT)
ctx.fill()
# ctx.scale (WIDTH/1.0, HEIGHT/1.0) # Normalizing the canvas
#ctx.scale (72/25.4, 72/25.4);
# pat = cairo.LinearGradient (0.0, 0.0, 0.0, 1.0)
# pat.add_color_stop_rgba (1, 0.7, 0, 0, 0.5) # First stop, 50% opacity
# pat.add_color_stop_rgba (0, 0.9, 0.7, 0.2, 1) # Last stop, 100% opacity
# #
# ctx.rectangle (0, 0, 1, 1) # Rectangle(x0, y0, x1, y1)
# ctx.set_source (pat)
# ctx.fill ()
ctx.translate (0.1, 0.1) # Changing the current transformation matrix
ctx.move_to (0, 0)
ctx.arc (0.2, 0.1, 0.1, -math.pi/2, 0) # Arc(cx, cy, radius, start_angle, stop_angle)
ctx.line_to (0.5, 0.1) # Line to (x,y)
ctx.curve_to (0.5, 0.2, 0.5, 0.4, 0.2, 0.8) # Curve(x1, y1, x2, y2, x3, y3)
ctx.close_path ()
ctx.set_source_rgb (0.3, 0.2, 0.5) # Solid color
ctx.set_line_width(0.01)
ctx.stroke()
ctx.set_source_rgb(0.0, 0.0, 0.0)
ctx.select_font_face("Helvetica", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
ctx.set_font_size(DAYFONTSIZE)
# Drucke eine Spalte mit Wochentagsnamen
# for i in range(0, 40):
# ctx.move_to(10, (DAYHEIGHT * (i+1))+DAYTOPOFFSET + DAYTEXTOFFSET)
# ctx.show_text(daynames[i%7])
for month in range(1, 13):
# Monatsnamen
ctx.set_font_size(MONTHFONTSIZE)
print_centered(ctx, monthnames[month], MONTHWIDTH * (month + 0.5), DAYTOPOFFSET-10)
day1, ndays = calendar.monthrange(year, month)
# draw day rectangle background
for i in range(day1, ndays+day1):
daynum = i - day1 + 1
monthposx, monthposy = MONTHWIDTH*month, (DAYHEIGHT * (i + 1) + DAYTOPOFFSET)
week = int(datetime.date(year, month, daynum).strftime('%V')) # works on sane C libraries
if week % 2 == 0 or (week==53 and month==1):
ctx.set_source_rgb(0.9, 0.95, 1)
ctx.rectangle(monthposx, monthposy, MONTHWIDTH, DAYHEIGHT)
ctx.fill()
# draw week numbers
ctx.set_font_size(140)
lastweek = 0
for i in range(day1, ndays+day1):
daynum = i - day1 + 1
monthposx, monthposy = MONTHWIDTH*month, (DAYHEIGHT * (i + 1) + DAYTOPOFFSET)
week = int(datetime.date(year, month, daynum).strftime('%V')) # works on sane C libraries
if week != lastweek:
lastweek = week
if week % 2 == 0 or (week==53 and month==1):
ctx.set_source_rgb(1, 1, 1)
else:
ctx.set_source_rgb(0.9, 0.95, 1)
if i > 5:
print_centered(ctx, str(week), MONTHWIDTH*month+(MONTHWIDTH*0.6), (DAYHEIGHT * (i + 5) + DAYTOPOFFSET))
else:
print_centered(ctx, str(week), MONTHWIDTH*month+(MONTHWIDTH*0.6), (DAYHEIGHT * (i+(7-i)) + DAYTOPOFFSET))
# draw day rectangle
for i in range(day1, ndays+day1):
daynum = i - day1 + 1
monthposx, monthposy = MONTHWIDTH*month, (DAYHEIGHT * (i + 1) + DAYTOPOFFSET)
week = int(datetime.date(year, month, daynum).strftime('%V')) # works on sane C libraries
ctx.set_line_width(0.002)
ctx.set_source_rgb(0, 0, 0)
ctx.rectangle(monthposx, monthposy, MONTHWIDTH, DAYHEIGHT)
ctx.stroke()
# draw days
for i in range(day1, ndays+day1):
ctx.set_source_rgb(0, 0, 0)
daynum = i - day1 + 1
monthposx, monthposy = MONTHWIDTH*month, (DAYHEIGHT * (i + 1) + DAYTOPOFFSET)
ctx.set_font_size(DAYNUMFONTSIZE)
ctx.move_to((MONTHWIDTH*month)+5, (DAYHEIGHT * (i + 1)) + DAYTOPOFFSET + (3*DAYTEXTOFFSET))
ctx.show_text(str(daynum))
ctx.set_font_size(DAYFONTSIZE)
ctx.move_to((MONTHWIDTH*month)+60, (DAYHEIGHT * (i + 1)) + DAYTOPOFFSET + DAYTEXTOFFSET)
ctx.show_text(daynames[i%7])
surface.write_to_png("%s.png" % year) # Output to PNG
if __name__ == '__main__':
main(2013)
main(2014)
main(2015)
main(2016)
main(2017)
main(2018)