From 3ac5417b1af72962b84074649344a26ecf4a9ac8 Mon Sep 17 00:00:00 2001 From: Xargana Date: Fri, 16 May 2025 15:40:03 +0200 Subject: [PATCH] Add display/font_manager.py --- display/font_manager.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 display/font_manager.py diff --git a/display/font_manager.py b/display/font_manager.py new file mode 100644 index 0000000..1d1810d --- /dev/null +++ b/display/font_manager.py @@ -0,0 +1,25 @@ +from PIL import ImageFont + +class FontManager: + def __init__(self, size=12): + self.font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", size) + + def set_font_size(self, size): + self.font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", size) + + def draw_multiline_text(self, draw, text, x, y, width, height): + lines = [] + words = text.split(" ") + line = "" + for word in words: + test = f"{line} {word}".strip() + if draw.textlength(test, font=self.font) <= width: + line = test + else: + lines.append(line) + line = word + lines.append(line) + + draw.rectangle((0, 0, width, height), outline=0, fill=0) + for idx, line in enumerate(lines): + draw.text((x, y + idx * self.font.size), line, font=self.font, fill=255)