From 1b92692b51d84dfb55e941175b768d90a6a786cc Mon Sep 17 00:00:00 2001 From: Max G Date: Tue, 5 Mar 2019 22:08:18 +0100 Subject: [PATCH] Centralized helper module to wrap texts --- Calendar/TextWraper.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Calendar/TextWraper.py diff --git a/Calendar/TextWraper.py b/Calendar/TextWraper.py new file mode 100644 index 0000000..ef529a4 --- /dev/null +++ b/Calendar/TextWraper.py @@ -0,0 +1,18 @@ +from Assets import path, defaultfont +from PIL import ImageFont + +def wrap_text_with_font (text, width, font): + words = text.split(' ') + result = "" + for index, word in enumerate(words): + until_current = (result + " " + word).strip() + txt_width, _ = font.getsize_multiline(until_current) + if txt_width > width: + result += '\n' + else: + result += ' ' + result += word + return result.strip() + +def wrap_text (text, width, font_size, font_family = defaultfont): + return wrap_text_with_font(text, width, ImageFont.truetype(path + font_family, font_size)) \ No newline at end of file