mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-11 21:33:00 +00:00
feat: use freetype for proper BDF font support
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
from PIL import Image, ImageDraw, ImageFont
|
import freetype
|
||||||
|
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
|
||||||
@@ -24,6 +25,10 @@ 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
|
||||||
|
self.face = freetype.Face(self.font_path)
|
||||||
|
self.face.set_char_size(self.font_size * 64) # Size is in 1/64th of points
|
||||||
|
|
||||||
self.logger.info("Initialized FontTestManager with tom-thumb font")
|
self.logger.info("Initialized FontTestManager with tom-thumb font")
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
@@ -40,9 +45,6 @@ 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
|
||||||
|
|
||||||
# Load the BDF font
|
|
||||||
font = ImageFont.truetype(self.font_path, self.font_size)
|
|
||||||
|
|
||||||
# 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))
|
||||||
|
|
||||||
@@ -50,17 +52,24 @@ class FontTestManager:
|
|||||||
draw = ImageDraw.Draw(self.display_manager.image)
|
draw = ImageDraw.Draw(self.display_manager.image)
|
||||||
sample_text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
sample_text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
|
||||||
# Get text dimensions
|
# Calculate starting position
|
||||||
bbox = draw.textbbox((0, 0), sample_text, font=font)
|
x = 10 # Start 10 pixels from the left
|
||||||
text_width = bbox[2] - bbox[0]
|
y = (height - self.font_size) // 2 # Center vertically
|
||||||
text_height = bbox[3] - bbox[1]
|
|
||||||
|
|
||||||
# Calculate position to center the text
|
# Draw each character
|
||||||
x = (width - text_width) // 2
|
for char in sample_text:
|
||||||
y = (height - text_height) // 2
|
# Load the glyph
|
||||||
|
self.face.load_char(char)
|
||||||
|
bitmap = self.face.glyph.bitmap
|
||||||
|
|
||||||
# Draw the text
|
# Draw the glyph
|
||||||
draw.text((x, y), sample_text, font=font, fill=(255, 255, 255))
|
for i in range(bitmap.rows):
|
||||||
|
for j in range(bitmap.width):
|
||||||
|
if bitmap.buffer[i * bitmap.width + j]:
|
||||||
|
draw.point((x + j, y + i), fill=(255, 255, 255))
|
||||||
|
|
||||||
|
# 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()
|
||||||
|
|||||||
Reference in New Issue
Block a user