refactor: simplify tom-thumb font test to show sample text

This commit is contained in:
ChuckBuilds
2025-04-22 20:01:51 -05:00
parent fc2f62e545
commit b560e1ad93

View File

@@ -10,13 +10,12 @@ logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class FontTestManager: class FontTestManager:
"""Manager for testing different font sizes of tom-thumb font.""" """Manager for testing tom-thumb 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/tom-thumb.bdf"
self.font_sizes = [4, 5, 6, 7, 8, 9] # Adjusted sizes for BDF font
self.logger = logging.getLogger('FontTest') self.logger = logging.getLogger('FontTest')
# Verify font exists # Verify font exists
@@ -24,14 +23,14 @@ 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}")
self.logger.info(f"Initialized FontTestManager with {len(self.font_sizes)} font sizes to test using tom-thumb font") self.logger.info("Initialized FontTestManager with tom-thumb font")
def update(self): def update(self):
"""No update needed for static display.""" """No update needed for static display."""
pass pass
def display(self, force_clear: bool = False): def display(self, force_clear: bool = False):
"""Display all font sizes at once across the screen.""" """Display the font with sample text."""
try: try:
# Clear the display # Clear the display
self.display_manager.clear() self.display_manager.clear()
@@ -40,41 +39,33 @@ class FontTestManager:
width = self.display_manager.matrix.width width = self.display_manager.matrix.width
height = self.display_manager.matrix.height height = self.display_manager.matrix.height
# Calculate spacing between font sizes # Load the BDF font
total_sizes = len(self.font_sizes) font = ImageFont.load(self.font_path)
spacing = width // (total_sizes + 1) # Add 1 to account for edges
# 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("tom-thumb", y=2, color=(255, 255, 255))
# Draw each font size # Draw sample text
draw = ImageDraw.Draw(self.display_manager.image) draw = ImageDraw.Draw(self.display_manager.image)
sample_text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i, size in enumerate(self.font_sizes): # Get text dimensions
# Load the BDF font bbox = draw.textbbox((0, 0), sample_text, font=font)
font = ImageFont.load(self.font_path) text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1]
# Create text to display (the size number)
text = str(size) # Calculate position to center the text
x = (width - text_width) // 2
# Get text dimensions y = (height - text_height) // 2
bbox = draw.textbbox((0, 0), text, font=font)
text_width = bbox[2] - bbox[0] # Draw the text
text_height = bbox[3] - bbox[1] draw.text((x, y), sample_text, font=font, fill=(255, 255, 255))
# Calculate position to center the text horizontally
x = spacing * (i + 1) - (text_width // 2)
# Position vertically in the middle of the screen
y = (height - text_height) // 2
# Draw the text
draw.text((x, y), text, font=font, fill=(255, 255, 255))
# Update the display once # Update the display once
self.display_manager.update_display() self.display_manager.update_display()
# Log that display is complete # Log that display is complete
self.logger.info("Font size test display complete. All sizes shown at once.") self.logger.info("Font test display complete.")
except Exception as e: except Exception as e:
self.logger.error(f"Error displaying font test: {e}", exc_info=True) self.logger.error(f"Error displaying font test: {e}", exc_info=True)