From a3c5f9a74f1f36279d5daeffaa3dd9d28ff75578 Mon Sep 17 00:00:00 2001 From: Chuck <33324927+ChuckBuilds@users.noreply.github.com> Date: Thu, 24 Jul 2025 15:33:58 -0500 Subject: [PATCH] programmatic changes to artist and album font --- src/display_manager.py | 18 ++++++++++++++++++ src/music_manager.py | 24 +++++++++++++++++++----- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/display_manager.py b/src/display_manager.py index 220e2a68..d3f7b34b 100644 --- a/src/display_manager.py +++ b/src/display_manager.py @@ -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.""" diff --git a/src/music_manager.py b/src/music_manager.py index e353e7c7..e26c8ff5 100644 --- a/src/music_manager.py +++ b/src/music_manager.py @@ -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: