callisto/display/font_manager.py

22 lines
719 B
Python
Raw Normal View History

2025-05-16 15:40:03 +02:00
from PIL import ImageFont
class FontManager:
2025-05-16 18:28:37 +02:00
def __init__(self, size=10):
2025-05-16 15:40:03 +02:00
self.font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", size)
2025-05-16 18:25:02 +02:00
def draw_multiline_text(self, draw, text, x, y, width, height):
lines = []
2025-05-16 18:28:37 +02:00
words = text.split()
2025-05-16 18:25:02 +02:00
line = ""
for word in words:
2025-05-16 18:28:37 +02:00
test_line = line + word + " "
if draw.textlength(test_line, font=self.font) <= width:
line = test_line
2025-05-16 18:25:02 +02:00
else:
lines.append(line)
2025-05-16 18:28:37 +02:00
line = word + " "
2025-05-16 18:25:02 +02:00
lines.append(line)
2025-05-16 15:40:03 +02:00
2025-05-16 18:28:37 +02:00
for i, l in enumerate(lines):
draw.text((x, y + i * (self.font.size + 2)), l, font=self.font, fill=255)