a
This commit is contained in:
parent
9811a0d0ad
commit
54277a7208
|
@ -7,18 +7,25 @@ class FontManager:
|
||||||
def set_font_size(self, size):
|
def set_font_size(self, size):
|
||||||
self.font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", size)
|
self.font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", size)
|
||||||
|
|
||||||
def draw_multiline_text(self, draw, text, x, y, width, height):
|
def draw_multiline_text(self, draw, text, x, y, width, height):
|
||||||
lines = []
|
lines = []
|
||||||
words = text.split(" ")
|
words = text.split(" ")
|
||||||
line = ""
|
line = ""
|
||||||
for word in words:
|
for word in words:
|
||||||
test = f"{line} {word}".strip()
|
test = f"{line} {word}".strip()
|
||||||
if draw.textlength(test, font=self.font) <= width:
|
bbox = self.font.getbbox(test)
|
||||||
line = test
|
test_width = bbox[2] - bbox[0]
|
||||||
else:
|
if test_width <= width:
|
||||||
lines.append(line)
|
line = test
|
||||||
line = word
|
else:
|
||||||
lines.append(line)
|
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)
|
||||||
|
|
||||||
|
|
||||||
draw.rectangle((0, 0, width, height), outline=0, fill=0)
|
draw.rectangle((0, 0, width, height), outline=0, fill=0)
|
||||||
for idx, line in enumerate(lines):
|
for idx, line in enumerate(lines):
|
||||||
|
|
Loading…
Reference in a new issue