From 723b7ce190f22be56dfe4d64e74ec075b66397d0 Mon Sep 17 00:00:00 2001 From: Chuck <33324927+ChuckBuilds@users.noreply.github.com> Date: Wed, 23 Jul 2025 14:17:53 -0500 Subject: [PATCH] fix bdf font for weather --- src/display_manager.py | 12 +++++- test/test_matrix_light6_font.py | 69 +++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 test/test_matrix_light6_font.py diff --git a/src/display_manager.py b/src/display_manager.py index a0ac2e75..5725e3cf 100644 --- a/src/display_manager.py +++ b/src/display_manager.py @@ -150,6 +150,10 @@ class DisplayManager: face.load_char(char) bitmap = face.glyph.bitmap + # Get glyph metrics + glyph_left = face.glyph.bitmap_left + glyph_top = face.glyph.bitmap_top + # Draw the character for i in range(bitmap.rows): for j in range(bitmap.width): @@ -157,7 +161,13 @@ class DisplayManager: if byte_index < len(bitmap.buffer): byte = bitmap.buffer[byte_index] if byte & (1 << (7 - (j % 8))): - self.draw.point((x + j, y + i), fill=color) + # Calculate actual pixel position + pixel_x = x + glyph_left + j + pixel_y = y + glyph_top - i + # Only draw if within bounds + if (0 <= pixel_x < self.matrix.width and + 0 <= pixel_y < self.matrix.height): + self.draw.point((pixel_x, pixel_y), fill=color) # Move to next character x += face.glyph.advance.x >> 6 diff --git a/test/test_matrix_light6_font.py b/test/test_matrix_light6_font.py new file mode 100644 index 00000000..b910400d --- /dev/null +++ b/test/test_matrix_light6_font.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 + +import sys +import os +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +from src.display_manager import DisplayManager +from src.config_manager import ConfigManager +from PIL import Image, ImageDraw +import freetype + +def test_matrix_light6_font(): + """Test the MatrixLight6 font rendering.""" + print("Testing MatrixLight6 font rendering...") + + # Load config + config_manager = ConfigManager() + config = config_manager.get_config() + + # Initialize display manager + display_manager = DisplayManager(config) + + # Test if the font was loaded + if hasattr(display_manager, 'matrix_light6_font'): + print(f"MatrixLight6 font loaded: {type(display_manager.matrix_light6_font)}") + if isinstance(display_manager.matrix_light6_font, freetype.Face): + print(f"Font size: {display_manager.matrix_light6_font.size.height >> 6} pixels") + else: + print("Font is not a FreeType face") + else: + print("MatrixLight6 font not found") + return + + # Test text rendering + test_text = "45 / 67" + print(f"Testing text: '{test_text}'") + + # Create a test image + image = Image.new('RGB', (display_manager.matrix.width, display_manager.matrix.height)) + draw = ImageDraw.Draw(image) + + # Try to render the text using the BDF font + try: + # Calculate width + text_width = display_manager.get_text_width(test_text, display_manager.matrix_light6_font) + print(f"Calculated width: {text_width}") + + # Calculate position (center) + x = (display_manager.matrix.width - text_width) // 2 + y = 10 + + print(f"Drawing at position: ({x}, {y})") + + # Draw the text + display_manager._draw_bdf_text(test_text, x, y, (255, 255, 255), display_manager.matrix_light6_font) + + # Update the display + display_manager.image = image + display_manager.update_display() + + print("Text should be displayed on the matrix") + + except Exception as e: + print(f"Error rendering text: {e}") + import traceback + traceback.print_exc() + +if __name__ == "__main__": + test_matrix_light6_font() \ No newline at end of file