programmatic changes to artist and album font

This commit is contained in:
Chuck
2025-07-24 15:33:58 -05:00
parent e85bebee12
commit a3c5f9a74f
2 changed files with 37 additions and 5 deletions

View File

@@ -261,6 +261,24 @@ class DisplayManager:
logger.error(f"Error getting text width: {e}")
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),
small_font: bool = False, font: ImageFont = None):
"""Draw text on the canvas with optional font selection."""

View File

@@ -748,14 +748,28 @@ class MusicManager:
font_title = self.display_manager.small_font
font_artist_album = self.display_manager.bdf_5x7_font
line_height_title = 8
line_height_artist_album = 7
padding_between_lines = 1
# Dynamically calculate line heights and positions
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
# --- 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)
current_title_display_text = title
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]
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:
self.title_scroll_tick += 1
if self.title_scroll_tick % TEXT_SCROLL_DIVISOR == 0: