Skip to content

Commit

Permalink
Merge pull request #313 from hchargois/add-text-anchor
Browse files Browse the repository at this point in the history
  • Loading branch information
mathoudebine authored Sep 21, 2023
2 parents 02072f9 + 4057b0d commit a681cc9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
3 changes: 2 additions & 1 deletion library/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def display_static_text(self):
background_color=config.THEME_DATA['static_text'][text].get("BACKGROUND_COLOR", (255, 255, 255)),
background_image=_get_full_path(config.THEME_DATA['PATH'],
config.THEME_DATA['static_text'][text].get("BACKGROUND_IMAGE",
None))
None)),
anchor="lt"
)


Expand Down
31 changes: 20 additions & 11 deletions library/lcd/lcd_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import sys
import threading
import time
import math
from abc import ABC, abstractmethod
from enum import IntEnum
from typing import Tuple
Expand Down Expand Up @@ -206,7 +207,8 @@ def DisplayText(
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'
align: str = 'left',
anchor: str = None,
):
# Convert text to bitmap using PIL and display it
# Provide the background image path to display text with transparent background
Expand Down Expand Up @@ -238,19 +240,26 @@ def DisplayText(
# Get text bounding box
font = ImageFont.truetype("./res/fonts/" + font, font_size)
d = ImageDraw.Draw(text_image)
left, top, text_width, text_height = d.textbbox((0, 0), text, font=font)
left, top, right, bottom = d.textbbox((x, y), text, font=font, align=align, anchor=anchor)

# Draw text with specified color & font, remove left/top margins
d.text((x - left, y - top), text, font=font, fill=font_color, align=align)
# 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)

# Crop text bitmap to keep only the text (also crop if text overflows display)
text_image = text_image.crop(box=(
x, y,
min(x + text_width - left, self.get_width()),
min(y + text_height - top, self.get_height())
))
# Draw text onto the background image with specified color & font
d.text((x, y), text, font=font, fill=font_color, align=align, anchor=anchor)

self.DisplayPILImage(text_image, x, y)
# Restrict the dimensions if they overflow the display size
left = max(left, 0)
top = max(top, 0)
right = min(right, self.get_width())
bottom = min(bottom, self.get_height())

# Crop text bitmap to keep only the text
text_image = text_image.crop(box=(left, top, right, bottom))

self.DisplayPILImage(text_image, left, top)

def DisplayProgressBar(self, x: int, y: int, width: int, height: int, min_value: int = 0, max_value: int = 100,
value: int = 50,
Expand Down
3 changes: 2 additions & 1 deletion library/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def display_themed_value(theme_data, value, min_size=0, unit=''):
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))
background_image=get_theme_file_path(theme_data.get("BACKGROUND_IMAGE", None)),
anchor="lt"
)


Expand Down

0 comments on commit a681cc9

Please sign in to comment.