From a615df4ba13702c1364f8e4f7c73eb23c394290d Mon Sep 17 00:00:00 2001 From: Leander Furumo Date: Thu, 29 Oct 2026 20:49:41 +0100 Subject: [PATCH] use multiline_textbox to draw text --- src/cogs/utils/misc_utils.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/cogs/utils/misc_utils.py b/src/cogs/utils/misc_utils.py index c905139..15fb4eb 100644 --- a/src/cogs/utils/misc_utils.py +++ b/src/cogs/utils/misc_utils.py @@ -206,16 +206,9 @@ def put_text_in_box( while True: next_font = ImageFont.truetype(font_path, font_size + 1) - longest_line = max(text.split("\n"), key=lambda x: next_font.getlength(x)) - font_width = next_font.getlength(longest_line) + left, top, right, bottom = draw.multiline_textbbox((0, 0), text, font=next_font, align="center") - # bbox doesn't work with multiline text, so we have to calculate the height manually - # take the height of the bounding box, multiply by 1.5 (our assumed line dividor height) - # after that, we multiply by the number of lines - font_box = next_font.getbbox(text) - font_height = ((font_box[3] - font_box[1]) * 1.5) * text.count("\n") - - if font_width >= w or font_height >= h: + if right - left >= w or bottom - top >= h: break font = next_font