From 1d52acdf77694dc80e380d9eca927766470b4bf4 Mon Sep 17 00:00:00 2001 From: ChuckBuilds <33324927+ChuckBuilds@users.noreply.github.com> Date: Wed, 23 Apr 2025 15:03:20 -0500 Subject: [PATCH] fix: handle both FreeType and PIL fonts in get_text_width method --- src/display_manager.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/display_manager.py b/src/display_manager.py index 53d25963..18fd1955 100644 --- a/src/display_manager.py +++ b/src/display_manager.py @@ -220,8 +220,21 @@ class DisplayManager: def get_text_width(self, text, font): """Get the width of text when rendered with the given font.""" - bbox = self.draw.textbbox((0, 0), text, font=font) - return bbox[2] - bbox[0] + try: + if isinstance(font, freetype.Face): + # For FreeType faces, calculate width using freetype + width = 0 + for char in text: + font.load_char(char) + width += font.glyph.advance.x >> 6 + return width + else: + # For PIL fonts, use textbbox + bbox = self.draw.textbbox((0, 0), text, font=font) + return bbox[2] - bbox[0] + except Exception as e: + logger.error(f"Error getting text width: {e}") + return 0 # Return 0 as fallback def draw_text(self, text: str, x: int = None, y: int = None, color: tuple = (255, 255, 255), small_font: bool = False, font: ImageFont = None):