#!/usr/bin/python
# -*- coding: UTF-8 -*-
# This Script creates a twelve page calendar for next year
# 
# Written by Henry Hartley, October 2004
# modified quite a bit in early 2008
# You are free to use this script in any way you wish
# 

# 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 = 'Letter'
#PageSize = 'Tabloid'
#PageSize = 'A3'
PageSize = 'A2'

import calendar
import time
import scribus

scribus.statusMessage("Creating "+Language+" Calendar...")
scribus.progressReset()
scribus.progressTotal(26)
scribus.progressSet(1)

# Be default, the calendar will be for "next" year.
Year = time.localtime()[0] + 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
#TitleFontName    = "Bitstream Vera Sans Roman"	# Name of the font to use
TitleFontName    = "Garamond Regular"		# 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

# Number of months to create (1 through 12)
monthCount = 12


# Put your copyright notice here or leave blank (defaults to same year as calendar
copyrightNotice  = "Copyright %4d Henry H. Hartley, all rights reserved" % Year

# Now sizes, spacing, and color:
TextHt           = 90			# 
copyrightSize    = 12			# 
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 == 'Letter':
	# Create a new letter size document (11x17 inches), mgns as set above,
	# landscape, first page = 1, measurements in points, facing pages,
	# FirstSideRight 
	pagew            = 792		# Page width
	pageh            = 612		# Page height
	Ymgn             = 0		# Vertical offset (mgn) (full bleed = 0)
	Xmgn             = 0		# Horizontal offset (mgn) (full bleed = 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
	dayw             = 110		# Width of each day cell (column)
	dayheight        = 100		# Height of each day cell (row)
	Xoffset          = 10		# The horizontal offset to the left of the main calendar block
	Yoffset          = 10		# The vertical offset to the top of the main calendar block
	TitleFontSize    = 36		# 
	DayNameFontSize  = 18		# 
	DayFontSize      = 18		# 
elif PageSize == 'Tabloid':
	# Create a new tabloid size document (11x17 inches), mgns as set above,
	# portrait, first page = 1, measurements in points, no facing pages,
	# FirstSideRight 
	pagew            = 792		# Page width
	pageh            = 1224		# Page height
	Ymgn             = 10		# Vertical offset (mgn)
	Xmgn             = 10		# Horizontal offset (mgn)
	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
	dayw             = 110		# Width of each day cell (column)
	dayheight        = 113		# Ht of each day cell (row)
	Xoffset          = 10		# The horizontal offset to the left of the main calendar block
	Yoffset          = 559		# The vertical offset to the top of the main calendar block
	TitleFontSize    = 60		# 
	DayNameFontSize  = 18		# 
	DayFontSize      = 12		# 
elif PageSize == 'A3':
	# Create a new A3 size document (297x420 mm), mgns as set above, portrait,
	# first page = 1, measurements in points, no facing pages, FirstSideRight 
	pagew            = 842		# Page width
	pageh            = 1191		# Page height
	Ymgn             = 10		# Vertical offset (mgn)
	Xmgn             = 10		# Horizontal offset (mgn)
	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
	dayw             = 117		# Width of each day cell (column)
	dayheight        = 108		# Ht of each day cell (row)
	Xoffset          = 10		# The horizontal offset to the left of the main calendar block
	Yoffset          = 560		# The vertical offset to the top of the main calendar block
	TitleFontSize    = 60		# 
	DayNameFontSize  = 18		# 
	DayFontSize      = 12		# 
elif PageSize == 'A2':
	# Create a new A2 size document (420x594 mm), mgns as set above, portrait,
	# first page = 1, measurements in points, no facing pages, FirstSideRight 
	pagew            = 1191		# Page width
	pageh            = 1684		# Page height
	Ymgn             = 20		# Vertical offset (mgn)
	Xmgn             = 20		# Horizontal offset (mgn)
	monthlabelheight = 72		# The height (in points) of the area with the name of the month
	daylabelheight   = 26		# The height of the row of day labels
	dayw             = 164		# Width of each day cell (column)
	dayheight        = 150		# Ht of each day cell (row)
	Xoffset          = 20		# The horizontal offset to the left of the main calendar block
	Yoffset          = 800		# The vertical offset to the top of the main calendar block
	TitleFontSize    = 60		# 
	DayNameFontSize  = 18		# 
	DayFontSize      = 12		# 

scribus.newDoc((pagew, pageh), (Xmgn, Xmgn, Ymgn, Ymgn), 0, 1, 0, 0, 0)

if Language == 'English':
	# English
	MnList = ['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
	MnList = ['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
	MnList = ['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
	MnList = ['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
	MnList = ['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
	MnList = ['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 ;-) ##
########################################################################################################

# Create the cover, which only has an image

scribus.statusMessage("Creating "+Language+" Calendar")
scribus.progressSet(1)

# For letter sized calendars, we include a cover page
if PageSize == 'Letter':
	scribus.newPage(1)
	scribus.setRedraw(0)
	Objects = []
	ob = scribus.createImage(Xmgn, Ymgn, pagew, pageh, "CoverImage")
	Objects.append(ob)
	scribus.setRedraw(1)


# For each month starting January of next year...
for Mn in range(monthCount):
	scribus.statusMessage("Creating "+Language+" Calendar - Page %d" % (Mn+1))
	scribus.progressSet((Mn+1)*2)

	# We're creating either one or two pages for each day depending on the style of the calendar.
	# If it's Tabloid or A3, just one page.  If it's Letter, two pages.

	# Create a new page for that month's image (and calendar if Tabloid or A3)
	if PageSize == 'Letter':
		scribus.newPage((Mn*2)+2)
	else:
		scribus.newPage(Mn+1)
	scribus.setRedraw(0)
	Objects = []
	Xcoor = Xoffset
	Ycoor = monthlabelheight + Yoffset
	DayC = 0
	Calend = calendar.monthcalendar(Year, Mn+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
	
	# Create empty Image Object for Artwork
	if PageSize == 'Letter':
		ImgWd = pagew
		ImgHt = pageh
	else:
		ImgWd = dayw*7
		ImgHt = int((dayw*7)/1.5) # The height of the image object (aspect ratio is 1:1.5)
	ob = scribus.createImage(Xmgn, Ymgn, ImgWd, ImgHt, "Image" + str(Mn))
	Objects.append(ob)
	
	# and a new page for the calendar page
	if PageSize == 'Letter':
		scribus.setRedraw(1)
		scribus.newPage((Mn*2)+3)
		scribus.setRedraw(0)
		Objects = []
		#ob = scribus.createText(Xoffset, Yoffset, dayw*7, TextHt)
		ob = scribus.createText(Xoffset, Yoffset, dayw*7, TextHt, "Text%02d" % Mn)
	else:
		#ob = scribus.createText(Xoffset, 2*Yoffset+ImgHt, dayw*7, TextHt)
		ob = scribus.createText(Xoffset, 2*Yoffset+ImgHt, dayw*7, TextHt, "Text%02d" % Mn)
	
	# Now append whichever was created
	Objects.append(ob)

	# Write Month Name
	ob = scribus.createText(Xoffset, Yoffset+8, dayw*7, monthlabelheight)
	scribus.setText(MnList[Mn] + " " + str(Year), ob)
	scribus.setFontSize(TitleFontSize, ob)
	scribus.setFont(TitleFontName, ob)
	scribus.setTextAlignment(scribus.ALIGN_CENTERED, ob)
	Objects.append(ob)
	
	# Draw vertical lines between day columns
	for lx in range(Xoffset, (dayw*7)+Xoffset+1, dayw):
		ob = scribus.createLine(lx, monthlabelheight+Yoffset, lx, height+Yoffset)
		scribus.setLineWidth(linewidth, ob)
		scribus.setLineColor(linecolor, ob)
		Objects.append(ob)

	# Draw horizontal lines between day rows
	for ly in range(monthlabelheight+daylabelheight+Yoffset, Yoffset+height+1, dayheight):
		ob = scribus.createLine(Xoffset, ly, Xoffset+(dayw*7), ly)
		scribus.setLineWidth(linewidth, ob)
		scribus.setLineColor(linecolor, ob)
		Objects.append(ob)
	# And another line above day labels
	ob = scribus.createLine(Xoffset, monthlabelheight+Yoffset, Xoffset+(dayw*7), monthlabelheight+Yoffset)
	scribus.setLineWidth(linewidth, ob)
	scribus.setLineColor(linecolor, ob)
	Objects.append(ob)

	# Enter the seven day names
	for day in range(7):
		ob = scribus.createText(Xcoor, Ycoor+8, dayw, 30)
		scribus.setText(DaysList[day], ob)
		scribus.setTextAlignment(1, ob) # 1 = Centered
		scribus.setFont(DayFontName, ob)
		scribus.setFontSize(DayNameFontSize, ob)
		if day == SundayNumber: # Let's make Sunday a different color
			scribus.setTextColor(SundayColor, ob)
		else:
			scribus.setTextColor(DayColor, ob)
		Objects.append(ob)
		Xcoor = Xcoor + dayw

	# Put date numbers in day cells
	scribus.progressSet(((Mn+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 = Xoffset + 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 = scribus.createLine(Xcoor, Ycoor-3, Xcoor+dayw-6, Ycoor-3)
					scribus.setLineWidth(linewidth, ob)
					scribus.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 = scribus.createText(Xcoor, Ycoor+(dayheight-32), dayw-6, 32)
					scribus.setTextAlignment(0, ob) # 0 = Left Aligned
					scribus.setFont(NumFontName, ob)
					scribus.setFontSize(18, ob)
				ob = scribus.createText(Xcoor, Ycoor, 20, 22)
				scribus.setText(str(rows), ob)
				scribus.setFont(TextFontName, ob)
				scribus.setTextAlignment(2, ob) # 2 = Right Aligned
				scribus.setFontSize(DayFontSize, ob)
				# Again, we'll make Sundays a different color
				if DayC == SundayNumber:
					scribus.setTextColor(SundayColor, ob)
				else:
					scribus.setTextColor(DayColor, ob)
				Objects.append(ob)
			Xcoor = Xcoor + dayw
			DayC = DayC + 1
		Ycoor = Ycoor + dayheight
	
	# Put the copyright notice at the bottom of the page
	if Week > 5:
		Ycoor = Ycoor - ( dayheight / 2 )
	ob = scribus.createText(Xoffset, Ycoor, dayw*7, 16)
	scribus.setText(copyrightNotice, ob)
	scribus.setFontSize(copyrightSize, ob)
	scribus.setFont(CRFontName, ob)
	scribus.setTextAlignment(2, ob) # 2 = Right align
	Objects.append(ob)
	
# scribus.deletePage((2*monthCount)+2) # That last blank page can go away
scribus.setRedraw(1)
scribus.redrawAll
scribus.progressSet(26)
scribus.statusMessage("")
scribus.progressReset()

