22 lines
719 B
Python
22 lines
719 B
Python
from PIL import ImageFont
|
|
|
|
class FontManager:
|
|
def __init__(self, size=10):
|
|
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_line = line + word + " "
|
|
if draw.textlength(test_line, font=self.font) <= width:
|
|
line = test_line
|
|
else:
|
|
lines.append(line)
|
|
line = word + " "
|
|
lines.append(line)
|
|
|
|
for i, l in enumerate(lines):
|
|
draw.text((x, y + i * (self.font.size + 2)), l, font=self.font, fill=255)
|