Display Manager improvements: 1) Update BDF font handling to use passed font 2) Fix font selection in _draw_bdf_text method

This commit is contained in:
ChuckBuilds
2025-04-25 10:45:18 -05:00
parent f3dff7431c
commit 5ef851678e

View File

@@ -140,11 +140,11 @@ class DisplayManager:
except Exception as e: except Exception as e:
logger.error(f"Error clearing display: {e}") logger.error(f"Error clearing display: {e}")
def _draw_bdf_text(self, text, x, y, color=(255, 255, 255)): def _draw_bdf_text(self, text, x, y, color=(255, 255, 255), font=None):
"""Draw text using BDF font with proper bitmap handling.""" """Draw text using BDF font with proper bitmap handling."""
try: try:
# Use the existing calendar_font instead of creating a new Face # Use the passed font or fall back to calendar_font
face = self.calendar_font face = font if font else self.calendar_font
for char in text: for char in text:
face.load_char(char) face.load_char(char)
@@ -250,17 +250,7 @@ class DisplayManager:
# Calculate x position if not provided (center text) # Calculate x position if not provided (center text)
if x is None: if x is None:
if isinstance(current_font, ImageFont.FreeTypeFont): text_width = self.get_text_width(text, current_font)
# For TTF fonts, use textlength
text_width = self.draw.textlength(text, font=current_font)
else:
# For BDF fonts, use freetype to calculate width
face = freetype.Face(self.calendar_font_path)
width = 0
for char in text:
face.load_char(char)
width += face.glyph.advance.x >> 6
text_width = width
x = (self.matrix.width - text_width) // 2 x = (self.matrix.width - text_width) // 2
# Set default y position if not provided # Set default y position if not provided
@@ -269,10 +259,15 @@ class DisplayManager:
# Draw the text # Draw the text
if isinstance(current_font, freetype.Face): if isinstance(current_font, freetype.Face):
self._draw_bdf_text(text, x, y, color) # For BDF fonts, use _draw_bdf_text
self._draw_bdf_text(text, x, y, color, current_font)
else: else:
# For TTF fonts, use PIL's text drawing
self.draw.text((x, y), text, font=current_font, fill=color) self.draw.text((x, y), text, font=current_font, fill=color)
# Update the display to show the text
self.update_display()
except Exception as e: except Exception as e:
logger.error(f"Error drawing text: {e}", exc_info=True) logger.error(f"Error drawing text: {e}", exc_info=True)