E-Paper-Calendar/Calendar/TextDesign.py

38 lines
1.5 KiB
Python
Raw Normal View History

from DesignEntity import DesignEntity
from PIL import ImageFont, ImageDraw, ImageOps
from Assets import path
class TextDesign (DesignEntity):
"""Object that manages all information relevant to text
and prints it to an image"""
def __init__ (self, size, font = "Assistant-Regular.ttf", fontsize = 12, text = "", horizontalalignment = "left", verticalalignment = "top"):
super(TextDesign, self).__init__(size)
self.font_family = font
self.font_size = fontsize
self.text = text
self.horizontal_alignment = horizontalalignment
self.vertical_alignment = verticalalignment
self.is_bitmap = True
def __finish_image__ (self):
self.__init_image__()
self.__font__ = ImageFont.truetype(path + self.font_family, self.font_size)
pos = self.__pos_from_alignment__()
ImageDraw.Draw(self.__image__).text(pos, self.text, fill=0, font=self.__font__)
self.__image__ = ImageOps.invert(self.__image__)
def __pos_from_alignment__ (self):
width, height = self.__font__.getsize(self.text)
x, y = 0, 0
if self.vertical_alignment == "center":
y = int((self.size[1] / 2) - (height / 2))
elif self.vertical_alignment == "bottom":
y = int(self.size[1] - height)
if self.horizontal_alignment == "center":
x = int((self.size[0] / 2) - (width / 2))
elif self.vertical_alignment == "right":
x = int(self.size[0] - width)
return (x, y)