diff --git a/library/display.py b/library/display.py index 441989fb..4a5f8e7a 100644 --- a/library/display.py +++ b/library/display.py @@ -126,6 +126,8 @@ def display_static_text(self): text=config.THEME_DATA['static_text'][text].get("TEXT"), x=config.THEME_DATA['static_text'][text].get("X", 0), y=config.THEME_DATA['static_text'][text].get("Y", 0), + width=config.THEME_DATA['static_text'][text].get("WIDTH", 0), + height=config.THEME_DATA['static_text'][text].get("HEIGHT", 0), font=config.THEME_DATA['static_text'][text].get("FONT", "roboto-mono/RobotoMono-Regular.ttf"), font_size=config.THEME_DATA['static_text'][text].get("FONT_SIZE", 10), font_color=config.THEME_DATA['static_text'][text].get("FONT_COLOR", (0, 0, 0)), @@ -133,7 +135,8 @@ def display_static_text(self): background_image=_get_full_path(config.THEME_DATA['PATH'], config.THEME_DATA['static_text'][text].get("BACKGROUND_IMAGE", None)), - anchor="lt" + align=config.THEME_DATA['static_text'][text].get("ALIGN", "left"), + anchor=config.THEME_DATA['static_text'][text].get("ANCHOR", "lt"), ) diff --git a/library/lcd/lcd_comm.py b/library/lcd/lcd_comm.py index faffcfaa..a1372b45 100644 --- a/library/lcd/lcd_comm.py +++ b/library/lcd/lcd_comm.py @@ -209,13 +209,15 @@ def DisplayText( text: str, x: int = 0, y: int = 0, + width: int = 0, + height: int = 0, font: str = "roboto-mono/RobotoMono-Regular.ttf", font_size: int = 20, font_color: Tuple[int, int, int] = (0, 0, 0), background_color: Tuple[int, int, int] = (255, 255, 255), background_image: str = None, align: str = 'left', - anchor: str = None, + anchor: str = 'lt', ): # Convert text to bitmap using PIL and display it # Provide the background image path to display text with transparent background @@ -249,12 +251,30 @@ def DisplayText( self.font_cache[(font, font_size)] = ImageFont.truetype("./res/fonts/" + font, font_size) font = self.font_cache[(font, font_size)] d = ImageDraw.Draw(text_image) - left, top, right, bottom = d.textbbox((x, y), text, font=font, align=align, anchor=anchor) - # textbbox may return float values, which is not good for the bitmap operations below. - # Let's extend the bounding box to the next whole pixel in all directions - left, top = math.floor(left), math.floor(top) - right, bottom = math.ceil(right), math.ceil(bottom) + if width == 0 or height == 0: + left, top, right, bottom = d.textbbox((x, y), text, font=font, align=align, anchor=anchor) + + # textbbox may return float values, which is not good for the bitmap operations below. + # Let's extend the bounding box to the next whole pixel in all directions + left, top = math.floor(left), math.floor(top) + right, bottom = math.ceil(right), math.ceil(bottom) + else: + left, top, right, bottom = x, y, x + width, y + height + + if anchor.startswith("m"): + x = (right + left) / 2 + elif anchor.startswith("r"): + x = right + else: + x = left + + if anchor.endswith("m"): + y = (bottom + top) / 2 + elif anchor.endswith("b"): + y = bottom + else: + y = top # Draw text onto the background image with specified color & font d.text((x, y), text, font=font, fill=font_color, align=align, anchor=anchor) diff --git a/library/stats.py b/library/stats.py index e01157e9..09352f1c 100644 --- a/library/stats.py +++ b/library/stats.py @@ -99,12 +99,15 @@ def display_themed_value(theme_data, value, min_size=0, unit=''): text=text, x=theme_data.get("X", 0), y=theme_data.get("Y", 0), + width=theme_data.get("WIDTH", 0), + height=theme_data.get("HEIGHT", 0), font=theme_data.get("FONT", "roboto-mono/RobotoMono-Regular.ttf"), font_size=theme_data.get("FONT_SIZE", 10), font_color=theme_data.get("FONT_COLOR", (0, 0, 0)), background_color=theme_data.get("BACKGROUND_COLOR", (255, 255, 255)), background_image=get_theme_file_path(theme_data.get("BACKGROUND_IMAGE", None)), - anchor="lt" + align=theme_data.get("ALIGN", "left"), + anchor=theme_data.get("ANCHOR", "lt"), )