#!/usr/bin/python
# -*- coding: UTF-8 -*-
# This Script creates a twelve page calendar for next year
# 
# Written by Henry Hartley, October 2004
# changed by E. Jobs, October 2004
# changed by H. Hartley, November 2004
# You are free to use this script in any way
# 

# Uncomment one of these statements to select the language for the calendar
Language = 'English'
#Language = 'French'
#Language = 'German'
#Language = 'Italian'
#Language = 'Portuguese'
#Language = 'Spanish'

# Uncomment one of these statements to select the paper size for the calendar
PageSize = 'Tabloid'
#PageSize = 'A3'

import calendar
import time
from scribus import *
StatusMessage("Creating "+Language+" Calendar...")
ProgressReset()
ProgressTotal(26)
ProgressSet(1)

# Change any of these settings to customize your calendar:

# The default is to use the same font for everything.
# But you don't have to:
TitleFontName    = "URW Palladio L Roman"	# Name of the font to use
DayFontName      = TitleFontName	# Font for names of days
NumFontName      = TitleFontName	# Font for day numbers
TextFontName     = TitleFontName	# Font for holiday text
CRFontName       = TitleFontName	# Font for Copyright notice
# Now sizes, spacing, and color:
TitleFontSize    = 24			# 
DayNameFontSize  = 18			# 
DayFontSize      = 12			# 
TextHeight       = 90			# 
copyrightNotice  = ""			# Put your copyright notice here or leave blank
Ymargin          = 10			# Vertical offset (margin)
Xmargin          = 10			# Horizontal offset (margin)
linewidth        = 0.5			# Thickness of the lines (0.0 to 12.0)
linecolor        = "Black"		# Line color
lineshade        = 100			# Line shade (0 to 100)
DayColor         = "Black"		# Color for the names of days (except Sunday)
SundayColor      = "Black"		# Color for Sunday (name and numbers)

if PageSize == 'Tabloid':
	# Create a new tabloid size document (11x17 inches), margins as set above,
	# portrait, first page = 1, measurements in points, no facing pages,
	# FirstSideRight 
	NewDoc((792, 1224), (Xmargin, Xmargin, Ymargin, Ymargin), 0, 1, 0, 0, 0)
	monthlabelheight = 44		# The height (in points) of the area with the name of the month
	daylabelheight   = 30		# The height of the row of day labels
	daywidth         = 110		# Width of each day cell (column)
	dayheight        = 113		# Height of each day cell (row)
	Yoffset          = 559		# The vertical offset to the top of the main calendar block
elif PageSize == 'A3':
	# Create a new A3 size document (297x420 mm), margins as set above, portrait,
	# first page = 1, measurements in points, no facing pages, FirstSideRight 
	NewDoc((842, 1191), (Xmargin, Xmargin, Ymargin, Ymargin), 0, 1, 0, 0, 0)
	monthlabelheight = 38		# The height (in points) of the area with the name of the month
	daylabelheight   = 26		# The height of the row of day labels
	daywidth         = 117		# Width of each day cell (column)
	dayheight        = 108		# Height of each day cell (row)
	Yoffset          = 560		# The vertical offset to the top of the main calendar block


if Language == 'English':
	# English
	MonthList = ['January','February','March','April','May','June','July','August','September','October','November','December']
	DaysList	 = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
	# Comment out this line to put Monday in the first column
	calendar.setfirstweekday(calendar.SUNDAY)
	SundayNumber = 0 ;
elif Language == 'French':
	# French
	MonthList = ['Janvier',u'F\xe9vrier','Mars','Avril','Mai','Juin','Juillet',u'Ao\xfbt','Septembre','Octobre','Novembre',u'D\xe9cembre']
	DaysList	 = ['Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi','Dimanche']
	# Commented out by default for this language
	#calendar.setfirstweekday(calendar.SUNDAY)
	SundayNumber = 6 ;
elif Language == 'German':
	# German
	MonthList = ['Januar','Februar',u'M\xe4rz','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember']
	DaysList	 = ['Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag','Sonntag']
	# Commented out by default for this language
	#calendar.setfirstweekday(calendar.SUNDAY)
	SundayNumber = 6 ;
elif Language == 'Italian':
	# Italian
	MonthList = ['Gennaio','Febbraio','Marzo','Aprile',u'Pu\xf2','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre']
	DaysList	 = ['Lunedì','Martedì','Mercoledì','Giovedì',u'Venerd\xec','Sabato','Domenica']
	# Commented out by default for this language
	#calendar.setfirstweekday(calendar.SUNDAY)
	SundayNumber = 6 ;
elif Language == 'Portuguese':
	# Portuguese
	MonthList = ['Janeiro','Fevereiro',u'Mar\xe7o','Abril','Pode','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro']
	DaysList	 = ['Segunda-feira',u'Ter\xe7a-feira','Quarta-feira','Quinta-feira','Sexta-feira',u'S\xe1bado','Domingo']
	DayNameFontSize  = 17 # We need these a little smaller or they won't fit
	# Commented out by default for this language
	#calendar.setfirstweekday(calendar.SUNDAY)
	SundayNumber = 6 ;
elif Language == 'Spanish':
	# Spanish
	MonthList = ['Enero','Febrero','Marcha','Abril','Puede','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre']
	DaysList	 = ['Lunes','Martes',u'Mi\xe9rcoles','Jueves','Viernes',u'S\xe1bado','Domingo']
	# Commented out by default for this language
	#calendar.setfirstweekday(calendar.SUNDAY)
	SundayNumber = 6 ;

## End of custimization settings - you shouldn't have to change anything below here - but you can ;-) ##
########################################################################################################

# For each month starting January of next year...
Year = time.localtime()[0] + 1
for Month in range(12):
	StatusMessage("Creating "+Language+" Calendar - Page %d" % (Month+1))
	ProgressSet((Month+1)*2)
	# Create a new page for that month
	NewPage(Month+1)
	SetRedraw(0)
	Objects = []
	Xcoor = Xmargin
	Ycoor = monthlabelheight + Yoffset
	DayC = 0
	Calend = calendar.monthcalendar(Year, Month+1) # Get the month's information using the Calendar module
	height = (dayheight*5) + monthlabelheight + daylabelheight # The height of the calendar block, five rows of days plus the label areas
	ImageHeight = int((daywidth*7)/1.5) # The height of the image object (aspect ratio is 1:1.5)

	# Create empty Image Object for Artwork
	ob = CreateImage(Xmargin, Ymargin, daywidth*7, ImageHeight, "Image" + str(Month))
	Objects.append(ob)

	ob = CreateText(Xmargin, 2*Ymargin+ImageHeight, daywidth*7, TextHeight, "Text" + str(Month))
	Objects.append(ob)

	# Write Month Name
	ob = CreateText(Xmargin, Yoffset, daywidth*7, monthlabelheight)
	SetFontSize(TitleFontSize, ob)
	SetFont(TitleFontName, ob)
	SetTextAlignment(1, ob) # 1 = Centered
	SetText(MonthList[Month] + " " + str(Year), ob)
	Objects.append(ob)

	# Draw vertical lines between day columns
	for lx in range(Xmargin, (daywidth*7)+Xmargin+1, daywidth):
		ob = CreateLine(lx, monthlabelheight+Yoffset, lx, height+Yoffset)
		SetLineWidth(linewidth, ob)
		SetLineColor(linecolor, ob)
		Objects.append(ob)

	# Draw horizontal lines between day rows
	for ly in range(monthlabelheight+daylabelheight+Yoffset, Yoffset+height+1, dayheight):
		ob = CreateLine(Xmargin, ly, Xmargin+(daywidth*7), ly)
		SetLineWidth(linewidth, ob)
		SetLineColor(linecolor, ob)
		Objects.append(ob)
	# And another line above day labels
	ob = CreateLine(Xmargin, monthlabelheight+Yoffset, Xmargin+(daywidth*7), monthlabelheight+Yoffset)
	SetLineWidth(linewidth, ob)
	SetLineColor(linecolor, ob)
	Objects.append(ob)

	# Enter the seven day names
	for day in range(7):
		ob = CreateText(Xcoor, Ycoor, daywidth, 30)
		SetTextAlignment(1, ob) # 1 = Centered
		SetFont(DayFontName, ob)
		SetFontSize(DayNameFontSize, ob)
		SetText(DaysList[day], ob)
		if day == SundayNumber: # Let's make Sunday a different color
			SetTextColor(SundayColor, ob)
		else:
			SetTextColor(DayColor, ob)
		Objects.append(ob)
		Xcoor = Xcoor + daywidth

	# Put date numbers in day cells
	ProgressSet(((Month+1)*2)+1)
	Ycoor = Ycoor + daylabelheight + 3
	Week = 0
	for lines in Calend:
		Week = Week + 1
		# If the month starts on a Friday or Saturday and has 30 or 31 days
		# so that the last day or two is on a Sunday or Monday, we need to put
		# those days in the same cell as the previous week.  This happens two
		# or three months of most years.
		if Week > 5:
			Ycoor = Ycoor - ( dayheight / 2 )
		Xcoor = Xmargin + 3
		DayC = 0
		for rows in lines:
			if rows != 0:
				# if this is one of those last days then we need to divide the
				# day's cell in half
				if Week > 5:
					# so draw another line
					ob = CreateLine(Xcoor, Ycoor-3, Xcoor+daywidth-6, Ycoor-3)
					SetLineWidth(linewidth, ob)
					SetLineColor(linecolor, ob)
					Objects.append(ob)
				else:
					# Otherwise, 
					# Create an empty text object for each day, also.
					# This can be used for holiday information, etc.  Or 
					# just leave it blank and it won't show up.
					ob = CreateText(Xcoor, Ycoor+(dayheight-32), daywidth-6, 32)
					SetTextAlignment(0, ob) # 0 = Left Aligned
					SetFont(NumFontName, ob)
					SetFontSize(10, ob)
				ob = CreateText(Xcoor, Ycoor, 20, 22)
				SetFont(TextFontName, ob)
				SetTextAlignment(2, ob) # 2 = Right Aligned
				SetFontSize(DayFontSize, ob)
				SetText(str(rows), ob)
				# Again, we'll make Sundays a different color
				if DayC == SundayNumber:
					SetTextColor(SundayColor, ob)
				else:
					SetTextColor(DayColor, ob)
				Objects.append(ob)
			Xcoor = Xcoor + daywidth
			DayC = DayC + 1
		Ycoor = Ycoor + dayheight
	
	# Put the copyright notice at the bottom of the page
	if Week > 5:
		Ycoor = Ycoor - ( dayheight / 2 )
	ob = CreateText(Xmargin, Ycoor, daywidth*7, 12)
	SetFontSize(6, ob)
	SetFont(CRFontName, ob)
	SetTextAlignment(2, ob) # 2 = Right align
	SetText(copyrightNotice, ob)
	Objects.append(ob)
	
DeletePage(13) # That last blank page can go away
SetRedraw(1)
RedrawAll
ProgressSet(26)
StatusMessage("")
ProgressReset()

