Skip to content

Commit

Permalink
now available to specify two fonts
Browse files Browse the repository at this point in the history
first is heading, second is general
as mentioned in #16
  • Loading branch information
peetzweg committed Mar 10, 2016
1 parent 590b6db commit ce443b7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 46 deletions.
29 changes: 17 additions & 12 deletions papr/layouts/oneyear.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def drawCalendar(env):
cr = cairo.Context(surface)

date = datetime.date(env.year, env.month, 1)
drawMonth(cr, env, date)
drawMonth(cr, env, date) # TODO using it totally worng, loop here over the function not in the function itself!
logging.info("Finished drawing Calendar!")


Expand Down Expand Up @@ -58,13 +58,16 @@ def drawMonthTitle(cr, env, date):
pc = pangocairo.CairoContext(cr)
pc.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
layout = pc.create_layout()
font = pango.FontDescription("%s %s" % (env.font, env.font_size * 2))
size = math.ceil(env.row_height * 0.9) # calculating font-size depending on the row height
font = pango.FontDescription("%s %s" % (env.fontHeading, size))
layout.set_font_description(font)

# preparing month string
style = "%B"
if(env.abbreviate_all):
style = "%b"
style = "%b"
# TODO String is always abbreviated, adjust the font-size depending on the row width for all month titles!
# style = "%B"
# if(env.abbreviate_all):
# style = "%b"
monthString = date.strftime(style)

layout.set_text(monthString)
Expand Down Expand Up @@ -107,8 +110,9 @@ def drawDay(cr, env, date):
dayLayout = pc.create_layout()
numberLayout = pc.create_layout()

daySize = math.floor(env.row_height * 0.2)
daySize = math.floor(env.row_height * 0.25)
numberSize = math.floor(env.row_height * 0.5)
yOffset = (env.row_height* 0.25) / 2

dayFont = pango.FontDescription("%s %s" % (env.font, daySize)) # day text is way smaller than number
numberFont = pango.FontDescription("%s %s" % (env.font, numberSize))
Expand All @@ -125,18 +129,19 @@ def drawDay(cr, env, date):

# draw Number
cr.save()
xOffset = math.floor((env.row_width / 8) - (numberLayout.get_pixel_size()[0] / 2))
yOffset = (env.row_height - numberSize - daySize) / 3
cr.translate(xOffset, yOffset)
xOffset = math.ceil((env.row_width / 8) - (numberLayout.get_pixel_size()[0] / 2))
# yOffset = math.floor((env.row_height - numberSize - daySize - (daySize/2)) / 2)
numberOffset = yOffset + ((numberSize - numberLayout.get_pixel_size()[1])/2)

cr.translate(xOffset, numberOffset)
pc.update_layout(numberLayout)
pc.show_layout(numberLayout)
cr.restore()

# draw Weekday
cr.save()
xOffset = math.floor((env.row_width / 8) - (dayLayout.get_pixel_size()[0] / 2))

yOffset = env.row_height - yOffset -daySize
xOffset = math.ceil((env.row_width / 8) - (dayLayout.get_pixel_size()[0] / 2))
yOffset = env.row_height - yOffset - daySize
cr.translate(xOffset, yOffset)
pc.update_layout(dayLayout)
pc.show_layout(dayLayout)
Expand Down
78 changes: 44 additions & 34 deletions papr/papr.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def main():
help="color date numbers", default=False)

font_map = pangocairo.cairo_font_map_get_default()
parser.add_argument("-f", "--font", choices=[f.get_name(
) for f in font_map.list_families()], help="choose which font to use", default="Sans", metavar="FONT")
parser.add_argument("-f", "--fonts", choices=[f.get_name(
) for f in font_map.list_families()], help="choose which font to use", default="Sans", metavar="FONT", nargs="+")

parser.add_argument("-l", "--locale",
help="choose locale to use (default en_US.UTF8, check 'locale -a' for available locales)", default="en_US")
Expand Down Expand Up @@ -63,62 +63,72 @@ def main():
layouts = ("classic", "column", 'oneyear')
parser.add_argument("layout", choices=layouts, metavar="LAYOUT",
help="choose calendar layout: " + str(layouts))
enviroment = parser.parse_args()
environment = parser.parse_args()

# defining output
if(enviroment.debug):
if(environment.debug):
logging.basicConfig(format='%(message)s', level=logging.DEBUG)
elif(enviroment.verbose):
elif(environment.verbose):
logging.basicConfig(format='%(message)s', level=logging.INFO)

# setting locale
try:
logging.debug("setting locale to '%s'", enviroment.locale)
locale.setlocale(locale.LC_ALL, enviroment.locale)
logging.debug("setting locale to '%s'", environment.locale)
locale.setlocale(locale.LC_ALL, environment.locale)
except locale.Error:
logging.error(
"locale: '%s' not found!\nList all installed locales with 'locale -a' and choose locale with -l/--locale option.", enviroment.locale)
"locale: '%s' not found!\nList all installed locales with 'locale -a' and choose locale with -l/--locale option.", environment.locale)
sys.exit(1)

logging.debug(
"Adjusting width and height values according to desired paper format: " + enviroment.paper)
if(enviroment.paper == "A5"):
enviroment.width = 14.8 * metrics.CM
enviroment.height = 21.0 * metrics.CM
elif(enviroment.paper == "A4"):
enviroment.width = 21.0 * metrics.CM
enviroment.height = 29.7 * metrics.CM
elif(enviroment.paper == "A3"):
enviroment.width = 29.7 * metrics.CM
enviroment.height = 42.0 * metrics.CM
elif(enviroment.paper == "A2"):
enviroment.width = 42.0 * metrics.CM
enviroment.height = 59.4 * metrics.CM
elif(enviroment.paper == "A1"):
enviroment.width = 59.4 * metrics.CM
enviroment.height = 84.1 * metrics.CM
elif(enviroment.paper == "A0"):
enviroment.width = 84.1 * metrics.CM
enviroment.height = 118.9 * metrics.CM
elif(enviroment.paper == "USLetter"):
enviroment.width = 8.5 * metrics.INCH
enviroment.height = 11.0 * metrics.INCH
"Adjusting width and height values according to desired paper format: " + environment.paper)
if environment.paper == "A5":
environment.width = 14.8 * metrics.CM
environment.height = 21.0 * metrics.CM
elif environment.paper == "A4":
environment.width = 21.0 * metrics.CM
environment.height = 29.7 * metrics.CM
elif environment.paper == "A3":
environment.width = 29.7 * metrics.CM
environment.height = 42.0 * metrics.CM
elif environment.paper == "A2":
environment.width = 42.0 * metrics.CM
environment.height = 59.4 * metrics.CM
elif environment.paper == "A1":
environment.width = 59.4 * metrics.CM
environment.height = 84.1 * metrics.CM
elif environment.paper == "A0":
environment.width = 84.1 * metrics.CM
environment.height = 118.9 * metrics.CM
elif environment.paper == "USLetter":
environment.width = 8.5 * metrics.INCH
environment.height = 11.0 * metrics.INCH


# Setup fonts
environment.font = environment.fonts.pop() # last provided font is used generally
try:
environment.fontHeading = environment.fonts.pop() # use additional provided font for headers
except IndexError:
environment.fontHeading = environment.font # if just one font set heading font same as general


# env.safety margin for printing (A4 printers a unable to print on the
# whole page)
enviroment.safety = enviroment.margin * metrics.MM
environment.safety = environment.margin * metrics.MM


if (enviroment.debug):
if (environment.debug):
# Printing Options for Debugging
dic = vars(enviroment)
dic = vars(environment)
for key in dic:
if(dic[key] != None):
logging.debug("%s = %s", key, dic[key])

drawCalendar = {"classic": classic.drawCalendar,
"column": column.drawCalendar,
"oneyear": oneyear.drawCalendar}
drawCalendar[enviroment.layout](enviroment)
drawCalendar[environment.layout](environment)

return 0

Expand Down

0 comments on commit ce443b7

Please sign in to comment.