feat: add tom-thumb BDF font support for calendar events

This commit is contained in:
ChuckBuilds
2025-04-22 20:32:06 -05:00
parent 9b09ddadd0
commit 0899aa6a14

View File

@@ -149,16 +149,16 @@ class DisplayManager:
self.small_font = ImageFont.truetype("assets/fonts/PressStart2P-Regular.ttf", 8) self.small_font = ImageFont.truetype("assets/fonts/PressStart2P-Regular.ttf", 8)
logger.info("Press Start 2P small font loaded successfully") logger.info("Press Start 2P small font loaded successfully")
# Load MatrixChunky6 font for calendar events # Load tom-thumb BDF font for calendar events
try: try:
script_dir = os.path.dirname(os.path.abspath(__file__)) script_dir = os.path.dirname(os.path.abspath(__file__))
relative_font_path = os.path.join(script_dir, "../assets/fonts/MatrixChunky6.ttf") relative_font_path = os.path.join(script_dir, "../assets/fonts/tom-thumb.bdf")
font_path = os.path.abspath(relative_font_path) font_path = os.path.abspath(relative_font_path)
logger.info(f"Attempting to load MatrixChunky6 font from: {font_path} at size 8") logger.info(f"Attempting to load tom-thumb font from: {font_path}")
self.calendar_font = ImageFont.truetype(font_path, 8) self.calendar_font = ImageFont.load(font_path)
logger.info(f"MatrixChunky6 calendar font loaded successfully from {font_path}") logger.info(f"tom-thumb calendar font loaded successfully from {font_path}")
except Exception as font_err: except Exception as font_err:
logger.error(f"Failed to load MatrixChunky6 font: {font_err}. Falling back to small font.") logger.error(f"Failed to load tom-thumb font: {font_err}. Falling back to small font.")
self.calendar_font = self.small_font self.calendar_font = self.small_font
# Load 4x6 font as extra_small_font # Load 4x6 font as extra_small_font
@@ -185,34 +185,32 @@ class DisplayManager:
if not hasattr(self, 'extra_small_font'): if not hasattr(self, 'extra_small_font'):
self.extra_small_font = self.regular_font self.extra_small_font = self.regular_font
def draw_text(self, text: str, x: int = None, y: int = None, color: Tuple[int, int, int] = (255, 255, 255), small_font: bool = False, font: ImageFont = None) -> None: def draw_text(self, text: str, x: int = None, y: int = None, color: tuple = (255, 255, 255),
"""Draw text on the display with improved clarity.""" small_font: bool = False, font: ImageFont = None):
# Use provided font if specified, otherwise use small_font or regular_font """Draw text on the canvas with optional font selection."""
font = font if font else (self.small_font if small_font else self.regular_font) try:
# Select font based on parameters
# Get text dimensions including ascenders and descenders if font:
bbox = self.draw.textbbox((0, 0), text, font=font) current_font = font
text_width = bbox[2] - bbox[0] else:
text_height = bbox[3] - bbox[1] current_font = self.small_font if small_font else self.regular_font
# Add padding to prevent cutoff # Calculate x position if not provided (center text)
padding = 1 # Reduced padding since Press Start 2P has built-in spacing if x is None:
if isinstance(current_font, ImageFont.FreeTypeFont):
# Center text horizontally if x not specified # For TTF fonts, use textlength
if x is None: text_width = self.draw.textlength(text, font=current_font)
x = (self.matrix.width - text_width) // 2 else:
# For BDF fonts, use textbbox
# Center text vertically if y not specified, with padding bbox = self.draw.textbbox((0, 0), text, font=current_font)
if y is None: text_width = bbox[2] - bbox[0]
y = (self.matrix.height - text_height) // 2 x = (self.matrix.width - text_width) // 2
else:
# Ensure text doesn't get cut off at bottom # Draw the text
max_y = self.matrix.height - text_height - padding self.draw.text((x, y), text, font=current_font, fill=color)
y = min(y, max_y)
y = max(y, padding) # Ensure text doesn't get cut off at top except Exception as e:
logger.error(f"Error drawing text: {e}", exc_info=True)
# Draw the text
self.draw.text((x, y), text, font=font, fill=color)
def draw_sun(self, x: int, y: int, size: int = 16): def draw_sun(self, x: int, y: int, size: int = 16):
"""Draw a sun icon using yellow circles and lines.""" """Draw a sun icon using yellow circles and lines."""