left justify OTD display and update font display to test 5x7regular

This commit is contained in:
Chuck
2025-07-22 21:38:43 -05:00
parent 584976fc49
commit eb96285a5c
3 changed files with 25 additions and 51 deletions

Binary file not shown.

View File

@@ -1,7 +1,6 @@
import os import os
import time import time
import freetype from PIL import Image, ImageDraw, ImageFont
from PIL import Image, ImageDraw
import logging import logging
from typing import Dict, Any from typing import Dict, Any
from src.display_manager import DisplayManager from src.display_manager import DisplayManager
@@ -11,12 +10,12 @@ logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class FontTestManager: class FontTestManager:
"""Manager for testing tom-thumb font.""" """Manager for testing 5x7 regular TTF font."""
def __init__(self, config: Dict[str, Any], display_manager: DisplayManager): def __init__(self, config: Dict[str, Any], display_manager: DisplayManager):
self.display_manager = display_manager self.display_manager = display_manager
self.config = config self.config = config
self.font_path = "assets/fonts/tom-thumb.bdf" self.font_path = "assets/fonts/5by7.regular.ttf"
self.logger = logging.getLogger('FontTest') self.logger = logging.getLogger('FontTest')
# Verify font exists # Verify font exists
@@ -24,10 +23,15 @@ class FontTestManager:
self.logger.error(f"Font file not found: {self.font_path}") self.logger.error(f"Font file not found: {self.font_path}")
raise FileNotFoundError(f"Font file not found: {self.font_path}") raise FileNotFoundError(f"Font file not found: {self.font_path}")
# Load the font # Load the TTF font with PIL
self.face = freetype.Face(self.font_path) try:
self.font = ImageFont.truetype(self.font_path, 7) # Size 7 for 5x7 font
self.logger.info(f"Successfully loaded 5x7 regular TTF font from {self.font_path}")
except Exception as e:
self.logger.error(f"Failed to load 5x7 TTF font: {e}")
raise
self.logger.info("Initialized FontTestManager with tom-thumb font") self.logger.info("Initialized FontTestManager with 5x7 regular TTF font")
def update(self): def update(self):
"""No update needed for static display.""" """No update needed for static display."""
@@ -44,42 +48,18 @@ class FontTestManager:
height = self.display_manager.matrix.height height = self.display_manager.matrix.height
# Draw font name at the top # Draw font name at the top
self.display_manager.draw_text("tom-thumb", y=2, color=(255, 255, 255)) self.display_manager.draw_text("5x7 Regular", y=2, color=(255, 255, 255))
# Draw sample text # Draw sample text using TTF font
draw = ImageDraw.Draw(self.display_manager.image) draw = ImageDraw.Draw(self.display_manager.image)
sample_text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" sample_text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# Calculate starting position # Calculate starting position
x = 10 # Start 10 pixels from the left x = 10 # Start 10 pixels from the left
y = (height - self.face.size.height) // 2 # Center vertically using font's natural height y = 10 # Start 10 pixels from the top
# Draw each character # Draw the sample text using PIL's text drawing
for char in sample_text: draw.text((x, y), sample_text, font=self.font, fill=(255, 255, 255))
# Load the glyph
self.face.load_char(char)
bitmap = self.face.glyph.bitmap
# Log bitmap details for debugging
self.logger.debug(f"Bitmap for '{char}': width={bitmap.width}, rows={bitmap.rows}, pitch={bitmap.pitch}")
# Draw the glyph
for i in range(bitmap.rows):
for j in range(bitmap.width):
try:
# Get the byte containing the pixel
byte_index = i * bitmap.pitch + (j // 8)
if byte_index < len(bitmap.buffer):
byte = bitmap.buffer[byte_index]
# Check if the specific bit is set
if byte & (1 << (7 - (j % 8))):
draw.point((x + j, y + i), fill=(255, 255, 255))
except IndexError:
self.logger.warning(f"Index out of range for char '{char}' at position ({i}, {j})")
continue
# Move to next character position
x += self.face.glyph.advance.x >> 6
# Update the display once # Update the display once
self.display_manager.update_display() self.display_manager.update_display()

View File

@@ -162,19 +162,17 @@ class OfTheDayManager:
logger.debug(f"Drawing item: title='{title}', subtitle='{subtitle}', description='{description}'") logger.debug(f"Drawing item: title='{title}', subtitle='{subtitle}', description='{description}'")
self._last_draw_debug_log = current_time self._last_draw_debug_log = current_time
# Draw title (Word) at the very top - condensed layout # Draw title (Word) at the very top - left justified
title_width = self.display_manager.get_text_width(title, self.display_manager.extra_small_font)
title_x = (self.display_manager.matrix.width - title_width) // 2
# Throttle debug logging to once every 5 seconds # Throttle debug logging to once every 5 seconds
if not hasattr(self, '_last_title_debug_log') or current_time - self._last_title_debug_log > 5: if not hasattr(self, '_last_title_debug_log') or current_time - self._last_title_debug_log > 5:
logger.debug(f"Drawing title '{title}' at position ({title_x}, 0) with width {title_width}") logger.debug(f"Drawing title '{title}' at position (1, 0) - left justified")
self._last_title_debug_log = current_time self._last_title_debug_log = current_time
self.display_manager.draw_text(title, title_x, 0, self.display_manager.draw_text(title, 1, 0,
color=self.title_color, color=self.title_color,
font=self.display_manager.extra_small_font) font=self.display_manager.extra_small_font)
# Draw subtitle right below title - condensed layout # Draw subtitle right below title - left justified
if subtitle: if subtitle:
# Use full width minus 2 pixels for maximum text # Use full width minus 2 pixels for maximum text
available_width = self.display_manager.matrix.width - 2 available_width = self.display_manager.matrix.width - 2
@@ -182,17 +180,15 @@ class OfTheDayManager:
for i, line in enumerate(wrapped_lines): for i, line in enumerate(wrapped_lines):
if line.strip(): if line.strip():
line_width = self.display_manager.get_text_width(line, self.display_manager.extra_small_font)
line_x = (self.display_manager.matrix.width - line_width) // 2
# Throttle debug logging to once every 5 seconds # Throttle debug logging to once every 5 seconds
if not hasattr(self, '_last_subtitle_debug_log') or current_time - self._last_subtitle_debug_log > 5: if not hasattr(self, '_last_subtitle_debug_log') or current_time - self._last_subtitle_debug_log > 5:
logger.debug(f"Drawing subtitle line '{line}' at position ({line_x}, 6) with width {line_width}") logger.debug(f"Drawing subtitle line '{line}' at position (1, 6) - left justified")
self._last_subtitle_debug_log = current_time self._last_subtitle_debug_log = current_time
self.display_manager.draw_text(line, line_x, 6, self.display_manager.draw_text(line, 1, 6,
color=self.subtitle_color, color=self.subtitle_color,
font=self.display_manager.extra_small_font) font=self.display_manager.extra_small_font)
# Draw description at the bottom - condensed layout # Draw description at the bottom - left justified
if description: if description:
# Use full width minus 2 pixels for maximum text # Use full width minus 2 pixels for maximum text
available_width = self.display_manager.matrix.width - 2 available_width = self.display_manager.matrix.width - 2
@@ -200,13 +196,11 @@ class OfTheDayManager:
for i, line in enumerate(wrapped_lines): for i, line in enumerate(wrapped_lines):
if line.strip(): if line.strip():
line_width = self.display_manager.get_text_width(line, self.display_manager.extra_small_font)
line_x = (self.display_manager.matrix.width - line_width) // 2
# Throttle debug logging to once every 5 seconds # Throttle debug logging to once every 5 seconds
if not hasattr(self, '_last_description_debug_log') or current_time - self._last_description_debug_log > 5: if not hasattr(self, '_last_description_debug_log') or current_time - self._last_description_debug_log > 5:
logger.debug(f"Drawing description line '{line}' at position ({line_x}, {12 + (i * 6)}) with width {line_width}") logger.debug(f"Drawing description line '{line}' at position (1, {12 + (i * 6)}) - left justified")
self._last_description_debug_log = current_time self._last_description_debug_log = current_time
self.display_manager.draw_text(line, line_x, 12 + (i * 6), self.display_manager.draw_text(line, 1, 12 + (i * 6),
color=self.subtitle_color, color=self.subtitle_color,
font=self.display_manager.extra_small_font) font=self.display_manager.extra_small_font)