Skip to content

Commit fe65bf4

Browse files
committed
Add --subtitle-text and --sidebar-text options
1 parent 2725b5a commit fe65bf4

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

generate_life_calendar.py

+32-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import argparse
44
import sys
55
import os
6+
import math
67

78
import cairo
89

@@ -214,8 +215,10 @@ def draw_grid(ctx, date, birthdate, age, darken_until_date):
214215
pos_y += box_size + BOX_MARGIN
215216
date += datetime.timedelta(weeks=52)
216217

218+
return x_margin
217219

218-
def gen_calendar(birthdate, title, age, filename, darken_until_date):
220+
def gen_calendar(birthdate, title, age, filename, darken_until_date, sidebar_text=None,
221+
subtitle_text=None):
219222
if len(title) > MAX_TITLE_SIZE:
220223
raise ValueError("Title can't be longer than %d characters"
221224
% MAX_TITLE_SIZE)
@@ -240,10 +243,27 @@ def gen_calendar(birthdate, title, age, filename, darken_until_date):
240243
ctx.move_to((DOC_WIDTH / 2) - (w / 2), (Y_MARGIN / 2) - (h / 2))
241244
ctx.show_text(title)
242245

246+
if subtitle_text is not None:
247+
ctx.set_source_rgb(0.7, 0.7, 0.7)
248+
ctx.set_font_size(SMALLFONT_SIZE)
249+
w, h = text_size(ctx, subtitle_text)
250+
ctx.move_to((DOC_WIDTH / 2) - (w / 2), (Y_MARGIN / 2) - (h / 2) + 15)
251+
ctx.show_text(subtitle_text)
252+
243253
date = back_up_to_monday(birthdate)
244254

245255
# Draw 52x90 grid of squares
246-
draw_grid(ctx, date, birthdate, age, darken_until_date)
256+
x_margin = draw_grid(ctx, date, birthdate, age, darken_until_date)
257+
258+
if sidebar_text is not None:
259+
# Draw text on sidebar
260+
w, h = text_size(ctx, sidebar_text)
261+
ctx.move_to((DOC_WIDTH - x_margin) + 20, Y_MARGIN + w + 100)
262+
ctx.set_font_size(SMALLFONT_SIZE)
263+
ctx.set_source_rgb(0.7, 0.7, 0.7)
264+
ctx.rotate(-90 * math.pi / 180)
265+
ctx.show_text(sidebar_text)
266+
247267
ctx.show_page()
248268

249269

@@ -263,6 +283,14 @@ def main():
263283
help='Calendar title text (default is "%s")' % DEFAULT_TITLE,
264284
default=DEFAULT_TITLE)
265285

286+
parser.add_argument('-s', '--sidebar-text', type=str, dest='sidebar_text',
287+
help='Text to show along the right side of grid (default is no sidebar text)',
288+
default=None)
289+
290+
parser.add_argument('-b', '--subtitle-text', type=str, dest='subtitle_text',
291+
help='Text to show under the calendar title (default is no subtitle text)',
292+
default=None)
293+
266294
parser.add_argument('-a', '--age', type=int, dest='age', choices=range(MIN_AGE, MAX_AGE + 1),
267295
metavar='[%s-%s]' % (MIN_AGE, MAX_AGE),
268296
help=('Number of rows to generate, representing years of life'),
@@ -276,7 +304,8 @@ def main():
276304
doc_name = '%s.pdf' % (os.path.splitext(args.filename)[0])
277305

278306
try:
279-
gen_calendar(args.date, args.title, args.age, doc_name, args.darken_until_date)
307+
gen_calendar(args.date, args.title, args.age, doc_name, args.darken_until_date,
308+
sidebar_text=args.sidebar_text, subtitle_text=args.subtitle_text)
280309
except Exception as e:
281310
print("Error: %s" % e)
282311
return

0 commit comments

Comments
 (0)