mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-11 21:33:00 +00:00
programmatic changes to artist and album font
This commit is contained in:
@@ -261,6 +261,24 @@ class DisplayManager:
|
|||||||
logger.error(f"Error getting text width: {e}")
|
logger.error(f"Error getting text width: {e}")
|
||||||
return 0 # Return 0 as fallback
|
return 0 # Return 0 as fallback
|
||||||
|
|
||||||
|
def get_font_height(self, font):
|
||||||
|
"""Get the height of the given font for line spacing purposes."""
|
||||||
|
try:
|
||||||
|
if isinstance(font, freetype.Face):
|
||||||
|
# For FreeType faces (BDF), the 'height' metric gives the recommended line spacing.
|
||||||
|
return font.size.height >> 6
|
||||||
|
else:
|
||||||
|
# For PIL TTF fonts, getmetrics() provides ascent and descent.
|
||||||
|
# The line height is the sum of ascent and descent.
|
||||||
|
ascent, descent = font.getmetrics()
|
||||||
|
return ascent + descent
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error getting font height for font type {type(font).__name__}: {e}")
|
||||||
|
# Fallback for TTF font if getmetrics() fails, or for other font types.
|
||||||
|
if hasattr(font, 'size'):
|
||||||
|
return font.size
|
||||||
|
return 8 # A reasonable default for an 8px font.
|
||||||
|
|
||||||
def draw_text(self, text: str, x: int = None, y: int = None, color: tuple = (255, 255, 255),
|
def draw_text(self, text: str, x: int = None, y: int = None, color: tuple = (255, 255, 255),
|
||||||
small_font: bool = False, font: ImageFont = None):
|
small_font: bool = False, font: ImageFont = None):
|
||||||
"""Draw text on the canvas with optional font selection."""
|
"""Draw text on the canvas with optional font selection."""
|
||||||
|
|||||||
@@ -748,14 +748,28 @@ class MusicManager:
|
|||||||
|
|
||||||
font_title = self.display_manager.small_font
|
font_title = self.display_manager.small_font
|
||||||
font_artist_album = self.display_manager.bdf_5x7_font
|
font_artist_album = self.display_manager.bdf_5x7_font
|
||||||
line_height_title = 8
|
|
||||||
line_height_artist_album = 7
|
# Dynamically calculate line heights and positions
|
||||||
padding_between_lines = 1
|
line_height_title = self.display_manager.get_font_height(font_title)
|
||||||
|
line_height_artist_album = self.display_manager.get_font_height(font_artist_album)
|
||||||
|
padding_between_lines = 1
|
||||||
|
|
||||||
TEXT_SCROLL_DIVISOR = 5
|
TEXT_SCROLL_DIVISOR = 5
|
||||||
|
|
||||||
# --- Title ---
|
# --- Title ---
|
||||||
y_pos_title = 2
|
# Start title's baseline y_pos slightly lower to give it padding from the top.
|
||||||
|
# The ascender gives the distance from the baseline to the top of the glyphs.
|
||||||
|
try:
|
||||||
|
# For TTF fonts, getbbox gives a tight bounding box.
|
||||||
|
# We use it to find the height of the actual rendered pixels for the letter 'A'
|
||||||
|
# and position the text based on that.
|
||||||
|
title_ascent, _ = font_title.getmetrics()
|
||||||
|
y_pos_title = title_ascent + 1 # Start 1 pixel below the top
|
||||||
|
except AttributeError:
|
||||||
|
# Fallback for BDF/other fonts that don't have getmetrics()
|
||||||
|
y_pos_title = line_height_title
|
||||||
|
|
||||||
|
|
||||||
title_width = self.display_manager.get_text_width(title, font_title)
|
title_width = self.display_manager.get_text_width(title, font_title)
|
||||||
current_title_display_text = title
|
current_title_display_text = title
|
||||||
if title_width > text_area_width:
|
if title_width > text_area_width:
|
||||||
@@ -764,7 +778,7 @@ class MusicManager:
|
|||||||
current_title_display_text = title[self.scroll_position_title:] + " " + title[:self.scroll_position_title]
|
current_title_display_text = title[self.scroll_position_title:] + " " + title[:self.scroll_position_title]
|
||||||
|
|
||||||
self.display_manager.draw_text(current_title_display_text,
|
self.display_manager.draw_text(current_title_display_text,
|
||||||
x=text_area_x_start, y=y_pos_title, color=(255, 255, 255), font=font_title)
|
x=text_area_x_start, y=y_pos_title - title_ascent, color=(255, 255, 255), font=font_title) # Adjust y for baseline
|
||||||
if title_width > text_area_width:
|
if title_width > text_area_width:
|
||||||
self.title_scroll_tick += 1
|
self.title_scroll_tick += 1
|
||||||
if self.title_scroll_tick % TEXT_SCROLL_DIVISOR == 0:
|
if self.title_scroll_tick % TEXT_SCROLL_DIVISOR == 0:
|
||||||
|
|||||||
Reference in New Issue
Block a user