mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-11 13:23:00 +00:00
change font test size
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
import freetype
|
||||||
from PIL import Image, ImageDraw, ImageFont
|
from PIL import Image, ImageDraw, ImageFont
|
||||||
import logging
|
import logging
|
||||||
from typing import Dict, Any
|
from typing import Dict, Any
|
||||||
@@ -10,28 +11,65 @@ logging.basicConfig(level=logging.INFO)
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class FontTestManager:
|
class FontTestManager:
|
||||||
"""Manager for testing 5x7 regular TTF font."""
|
"""Manager for testing fonts with easy BDF/TTF switching."""
|
||||||
|
|
||||||
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/5by7.regular.ttf"
|
|
||||||
self.logger = logging.getLogger('FontTest')
|
self.logger = logging.getLogger('FontTest')
|
||||||
|
|
||||||
|
# FONT CONFIGURATION - EASY SWITCHING
|
||||||
|
# Set to 'bdf' or 'ttf' to switch font types
|
||||||
|
self.font_type = 'bdf' # Change this to 'ttf' to use TTF font
|
||||||
|
|
||||||
|
# Font configurations
|
||||||
|
self.font_configs = {
|
||||||
|
'bdf': {
|
||||||
|
'path': "assets/fonts/5x7.bdf",
|
||||||
|
'display_name': "5x7 BDF",
|
||||||
|
'description': "5x7 BDF font"
|
||||||
|
},
|
||||||
|
'ttf': {
|
||||||
|
'path': "assets/fonts/5by7.regular.ttf",
|
||||||
|
'display_name': "5x7 Regular TTF",
|
||||||
|
'description': "5x7 regular TTF font"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get current font configuration
|
||||||
|
self.current_config = self.font_configs[self.font_type]
|
||||||
|
self.font_path = self.current_config['path']
|
||||||
|
|
||||||
# Verify font exists
|
# Verify font exists
|
||||||
if not os.path.exists(self.font_path):
|
if not os.path.exists(self.font_path):
|
||||||
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 TTF font with PIL
|
# Load the font based on type
|
||||||
|
if self.font_type == 'bdf':
|
||||||
|
self._load_bdf_font()
|
||||||
|
else:
|
||||||
|
self._load_ttf_font()
|
||||||
|
|
||||||
|
self.logger.info(f"Initialized FontTestManager with {self.current_config['description']}")
|
||||||
|
|
||||||
|
def _load_bdf_font(self):
|
||||||
|
"""Load BDF font using freetype."""
|
||||||
try:
|
try:
|
||||||
self.font = ImageFont.truetype(self.font_path, 8) # Size 7 for 5x7 font
|
self.face = freetype.Face(self.font_path)
|
||||||
self.logger.info(f"Successfully loaded 5x7 regular TTF font from {self.font_path}")
|
self.logger.info(f"Successfully loaded BDF font from {self.font_path}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error(f"Failed to load 5x7 TTF font: {e}")
|
self.logger.error(f"Failed to load BDF font: {e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
self.logger.info("Initialized FontTestManager with 5x7 regular TTF font")
|
def _load_ttf_font(self):
|
||||||
|
"""Load TTF font using PIL."""
|
||||||
|
try:
|
||||||
|
self.font = ImageFont.truetype(self.font_path, 8) # Size 8 for 5x7 font
|
||||||
|
self.logger.info(f"Successfully loaded TTF font from {self.font_path}")
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error(f"Failed to load TTF font: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""No update needed for static display."""
|
"""No update needed for static display."""
|
||||||
@@ -48,9 +86,9 @@ 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("5x7 Regular", y=2, color=(255, 255, 255))
|
self.display_manager.draw_text(self.current_config['display_name'], y=2, color=(255, 255, 255))
|
||||||
|
|
||||||
# Draw sample text using TTF font
|
# Draw sample text
|
||||||
draw = ImageDraw.Draw(self.display_manager.image)
|
draw = ImageDraw.Draw(self.display_manager.image)
|
||||||
sample_text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
sample_text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
|
||||||
@@ -58,8 +96,11 @@ class FontTestManager:
|
|||||||
x = 10 # Start 10 pixels from the left
|
x = 10 # Start 10 pixels from the left
|
||||||
y = 10 # Start 10 pixels from the top
|
y = 10 # Start 10 pixels from the top
|
||||||
|
|
||||||
# Draw the sample text using PIL's text drawing
|
# Draw text based on font type
|
||||||
draw.text((x, y), sample_text, font=self.font, fill=(255, 255, 255))
|
if self.font_type == 'bdf':
|
||||||
|
self._draw_bdf_text(draw, sample_text, x, y)
|
||||||
|
else:
|
||||||
|
self._draw_ttf_text(draw, sample_text, x, y)
|
||||||
|
|
||||||
# Update the display once
|
# Update the display once
|
||||||
self.display_manager.update_display()
|
self.display_manager.update_display()
|
||||||
@@ -69,3 +110,32 @@ class FontTestManager:
|
|||||||
|
|
||||||
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)
|
||||||
|
|
||||||
|
def _draw_bdf_text(self, draw, text, x, y):
|
||||||
|
"""Draw text using BDF font."""
|
||||||
|
for char in text:
|
||||||
|
# Load the glyph
|
||||||
|
self.face.load_char(char)
|
||||||
|
bitmap = self.face.glyph.bitmap
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
def _draw_ttf_text(self, draw, text, x, y):
|
||||||
|
"""Draw text using TTF font."""
|
||||||
|
draw.text((x, y), text, font=self.font, fill=(255, 255, 255))
|
||||||
Reference in New Issue
Block a user