Handle also line return in alerts

This commit is contained in:
nemunaire 2022-08-11 17:00:25 +02:00
parent f8c6d00257
commit 560dd5f7a8

View File

@ -39,31 +39,39 @@ def draw_format_infos(infos, width, height, fnt_R, fnt_B, label_margin, align_he
return image
def display_longtext(draw, pos, text, font, maxwidth=9999, **kwargs):
def display_longtext(draw, pos, text, font, anchor="lt", maxwidth=9999, **kwargs):
x,y = pos
words = text.split(" ")
lines = []
cur = ""
while len(words) > 0:
added = (" " if len(cur) > 0 else "") + words[0]
width = font.getsize(cur + added)[0]
if width < maxwidth:
words.pop(0)
cur += added
else:
lines.append(cur)
cur = words.pop(0)
if len(cur) > 0:
lines.append(cur)
mainlines = text.split("\n")
for line in mainlines:
words = line.split(" ")
cur = ""
while len(words) > 0:
added = (" " if len(cur) > 0 else "") + words[0]
width = font.getsize(cur + added)[0]
if width < maxwidth:
words.pop(0)
cur += added
else:
lines.append(cur)
cur = words.pop(0)
if len(cur) > 0:
lines.append(cur)
line_height = font.getsize("test")[1]
y -= line_height * len(lines) / 2
if anchor[1] == "m":
y -= line_height * len(lines) / 2
elif anchor[1] == "b":
y -= line_height * len(lines)
for line in lines:
draw.text((x,y), line, font=font, **kwargs)
draw.text((x,y), line, font=font, anchor=anchor, **kwargs)
y += line_height
return line_height * len(lines)
class WeatherToolbarModule:
def __init__(self):
@ -303,7 +311,7 @@ class WeatherAlertsModule:
fnt_R = ImageFont.truetype(config.fnt_R_path, 16)
fnt_B = ImageFont.truetype(config.fnt_RB_path, 16)
align = 10
align = 9
for alert in alerts:
if alert["severity"] == "watch" or alert["title"].startswith("Moderate"):
icon = "wi-small-craft-advisory.png"
@ -340,15 +348,14 @@ class WeatherAlertsModule:
align += fnt_B.getsize(alert["title"])[1]
draw.multiline_text(
align += display_longtext(
draw,
(self.icon_size - 5, align),
alert["description"],
fill="white", font=fnt_R
fill="white", font=fnt_R,
maxwidth=width-self.icon_size-5
)
align += draw.multiline_textsize(
alert["description"],
font=fnt_R
)[1]
align += 7
image = image.crop((0,0,width, align))