2019-02-28 15:17:43 +01:00
|
|
|
from DesignEntity import DesignEntity
|
|
|
|
from PIL import ImageFont, ImageDraw, ImageOps
|
2019-04-08 16:06:56 +02:00
|
|
|
from Assets import path, defaultfont, colors, defaultfontsize
|
2019-03-05 22:09:55 +01:00
|
|
|
from TextWraper import wrap_text_with_font
|
2019-02-28 15:17:43 +01:00
|
|
|
|
2019-03-08 21:35:52 +01:00
|
|
|
paddingcorrection = -3
|
2019-03-10 18:51:46 +01:00
|
|
|
truncateerror_fontsize = 0.5
|
2019-02-28 20:31:51 +01:00
|
|
|
|
2019-02-28 15:17:43 +01:00
|
|
|
class TextDesign (DesignEntity):
|
|
|
|
"""Object that manages all information relevant to text
|
|
|
|
and prints it to an image"""
|
2019-04-08 16:08:20 +02:00
|
|
|
def __init__ (self, size, color=colors["fg"], background_color=colors["bg"], font = None, fontsize = defaultfontsize, text = "", horizontalalignment = "left", verticalalignment = "top", mask=True, truncate=False, truncate_suffix = '...', wrap=False):
|
2019-03-03 13:52:36 +01:00
|
|
|
super(TextDesign, self).__init__(size, mask = mask)
|
2019-04-08 16:08:20 +02:00
|
|
|
if font is None:
|
|
|
|
font = defaultfont
|
2019-02-28 15:17:43 +01:00
|
|
|
self.font_family = font
|
|
|
|
self.font_size = fontsize
|
|
|
|
self.text = text
|
|
|
|
self.horizontal_alignment = horizontalalignment
|
|
|
|
self.vertical_alignment = verticalalignment
|
2019-03-03 16:45:06 +01:00
|
|
|
self.truncate = truncate
|
|
|
|
self.truncate_suffix = truncate_suffix
|
2019-03-05 22:09:55 +01:00
|
|
|
self.wrap = wrap
|
2019-03-07 21:27:13 +01:00
|
|
|
self.color = color
|
|
|
|
self.background_color = background_color
|
2019-02-28 15:17:43 +01:00
|
|
|
|
|
|
|
def __finish_image__ (self):
|
2019-03-07 21:27:13 +01:00
|
|
|
if self.color is "white":
|
|
|
|
self.invert_mask = True
|
2019-04-08 11:20:04 +02:00
|
|
|
if self.background_color not in ["white", "black"] or self.color in ["red"]:
|
2019-03-07 21:27:30 +01:00
|
|
|
self.color_key = True
|
2019-03-07 21:27:13 +01:00
|
|
|
self.__init_image__(self.background_color)
|
|
|
|
|
2019-03-03 10:12:04 +01:00
|
|
|
self.__font__ = self.__get_font__()
|
2019-03-05 22:09:55 +01:00
|
|
|
if self.wrap is False and self.truncate:
|
2019-03-03 16:45:06 +01:00
|
|
|
self.__truncate_text__()
|
2019-03-05 22:09:55 +01:00
|
|
|
if self.wrap:
|
|
|
|
self.__wrap_text__()
|
2019-02-28 15:17:43 +01:00
|
|
|
pos = self.__pos_from_alignment__()
|
2019-03-07 21:27:13 +01:00
|
|
|
ImageDraw.Draw(self.__image__).text(pos, self.text, fill=self.color, font=self.__font__)
|
2019-03-03 16:45:06 +01:00
|
|
|
|
|
|
|
def __truncate_text__ (self):
|
2019-03-17 20:32:32 +01:00
|
|
|
if self.__font__.getsize_multiline(self.text)[0] <= self.size[0]: #does not need truncating
|
2019-03-03 16:45:06 +01:00
|
|
|
return
|
2019-03-17 20:32:32 +01:00
|
|
|
suffix_length = self.__font__.getsize_multiline(self.truncate_suffix)[0]
|
2019-04-08 16:23:59 +02:00
|
|
|
while len(self.text) > 1 and self.__font__.getsize_multiline(self.text)[0] + suffix_length >= self.size[0]:
|
2019-03-03 16:45:06 +01:00
|
|
|
self.text = self.text[0:-1]
|
2019-04-08 16:23:59 +02:00
|
|
|
self.text = self.text.rstrip(' ')
|
2019-03-03 16:45:06 +01:00
|
|
|
self.text += self.truncate_suffix
|
2019-02-28 15:17:43 +01:00
|
|
|
|
|
|
|
def __pos_from_alignment__ (self):
|
2019-03-17 20:32:32 +01:00
|
|
|
width, height = self.__font__.getsize_multiline(self.text)
|
2019-02-28 15:17:43 +01:00
|
|
|
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))
|
2019-03-03 18:13:02 +01:00
|
|
|
elif self.horizontal_alignment == "right":
|
2019-02-28 15:17:43 +01:00
|
|
|
x = int(self.size[0] - width)
|
|
|
|
|
2019-03-03 10:12:04 +01:00
|
|
|
return (x, y + paddingcorrection)
|
|
|
|
|
2019-03-05 22:09:55 +01:00
|
|
|
def __wrap_text__ (self):
|
|
|
|
self.text = wrap_text_with_font(self.text, self.size[0], self.__font__)
|
|
|
|
|
2019-03-03 10:12:04 +01:00
|
|
|
def __get_font__(self):
|
2019-03-09 17:23:30 +01:00
|
|
|
return ImageFont.truetype(path + self.font_family, int(self.font_size))
|