found a nice font for weather daily tempts

This commit is contained in:
Chuck
2025-07-23 13:50:46 -05:00
parent d371b5ad09
commit 58bfdc04a0
2 changed files with 43 additions and 24 deletions

View File

@@ -216,6 +216,29 @@ class DisplayManager:
logger.error(f"Failed to load 4x6 TTF font: {font_err}. Falling back.") logger.error(f"Failed to load 4x6 TTF font: {font_err}. Falling back.")
self.extra_small_font = self.small_font self.extra_small_font = self.small_font
# Load MatrixLight6 BDF font for weather forecasts
try:
script_dir = os.path.dirname(os.path.abspath(__file__))
relative_font_path = os.path.join(script_dir, "../assets/fonts/MatrixLight6.bdf")
self.matrix_light6_font_path = os.path.abspath(relative_font_path)
logger.info(f"Attempting to load MatrixLight6 font from: {self.matrix_light6_font_path}")
if not os.path.exists(self.matrix_light6_font_path):
raise FileNotFoundError(f"Font file not found at {self.matrix_light6_font_path}")
# Load with freetype for proper BDF handling
face = freetype.Face(self.matrix_light6_font_path)
logger.info(f"MatrixLight6 font loaded successfully from {self.matrix_light6_font_path}")
logger.info(f"MatrixLight6 font size: {face.size.height >> 6} pixels")
# Store the face for later use
self.matrix_light6_font = face
except Exception as font_err:
logger.error(f"Failed to load MatrixLight6 font: {str(font_err)}", exc_info=True)
logger.error("Falling back to small font")
self.matrix_light6_font = self.small_font
except Exception as e: except Exception as e:
logger.error(f"Error in font loading: {e}", exc_info=True) logger.error(f"Error in font loading: {e}", exc_info=True)
# Fallback to default font # Fallback to default font
@@ -226,6 +249,8 @@ class DisplayManager:
self.extra_small_font = self.regular_font self.extra_small_font = self.regular_font
if not hasattr(self, 'bdf_5x7_font'): # Ensure bdf_5x7_font also gets a fallback if not hasattr(self, 'bdf_5x7_font'): # Ensure bdf_5x7_font also gets a fallback
self.bdf_5x7_font = self.regular_font self.bdf_5x7_font = self.regular_font
if not hasattr(self, 'matrix_light6_font'): # Ensure matrix_light6_font also gets a fallback
self.matrix_light6_font = self.regular_font
def get_text_width(self, text, font): def get_text_width(self, text, font):
"""Get the width of text when rendered with the given font.""" """Get the width of text when rendered with the given font."""

View File

@@ -3,28 +3,11 @@ import time
from datetime import datetime from datetime import datetime
from typing import Dict, Any, List from typing import Dict, Any, List
from PIL import Image, ImageDraw from PIL import Image, ImageDraw
import freetype
from .weather_icons import WeatherIcons from .weather_icons import WeatherIcons
from .cache_manager import CacheManager from .cache_manager import CacheManager
class WeatherManager: class WeatherManager:
# Weather condition to larger colored icons (we'll use these as placeholders until you provide custom ones)
WEATHER_ICONS = {
'Clear': '🌞', # Larger sun with rays
'Clouds': '☁️', # Cloud
'Rain': '🌧️', # Rain cloud
'Snow': '❄️', # Snowflake
'Thunderstorm': '⛈️', # Storm cloud
'Drizzle': '🌦️', # Sun behind rain cloud
'Mist': '🌫️', # Fog
'Fog': '🌫️', # Fog
'Haze': '🌫️', # Fog
'Smoke': '🌫️', # Fog
'Dust': '🌫️', # Fog
'Sand': '🌫️', # Fog
'Ash': '🌫️', # Fog
'Squall': '💨', # Dash symbol
'Tornado': '🌪️' # Tornado
}
def __init__(self, config: Dict[str, Any], display_manager): def __init__(self, config: Dict[str, Any], display_manager):
self.config = config self.config = config
@@ -520,12 +503,23 @@ class WeatherManager:
# Draw high/low temperatures at bottom (without degree symbol) # Draw high/low temperatures at bottom (without degree symbol)
temp_text = f"{forecast['temp_low']} / {forecast['temp_high']}" # Removed degree symbols temp_text = f"{forecast['temp_low']} / {forecast['temp_high']}" # Removed degree symbols
temp_width = draw.textlength(temp_text, font=self.display_manager.extra_small_font) # Use MatrixLight6 font for temperature text
temp_y = self.display_manager.matrix.height - 8 # Position at bottom with small margin if hasattr(self.display_manager, 'matrix_light6_font'):
draw.text((center_x - temp_width // 2, temp_y), # For BDF fonts, we need to calculate width manually and use _draw_bdf_text
temp_text, temp_width = self.display_manager.get_text_width(temp_text, self.display_manager.matrix_light6_font)
font=self.display_manager.extra_small_font, # Calculate y position for BDF font at bottom
fill=self.COLORS['text']) self.display_manager.matrix_light6_font.load_char('A')
font_height = self.display_manager.matrix_light6_font.glyph.bitmap.rows
temp_y = self.display_manager.matrix.height - font_height - 1 # Position at bottom with small margin
self.display_manager._draw_bdf_text(temp_text, center_x - temp_width // 2, temp_y, self.COLORS['text'], self.display_manager.matrix_light6_font)
else:
# Fallback to extra_small font
temp_width = draw.textlength(temp_text, font=self.display_manager.extra_small_font)
temp_y = self.display_manager.matrix.height - 8 # Position at bottom with small margin
draw.text((center_x - temp_width // 2, temp_y),
temp_text,
font=self.display_manager.extra_small_font,
fill=self.COLORS['text'])
# Update the display # Update the display
self.display_manager.image = image self.display_manager.image = image