Skip to content

Commit

Permalink
Merge pull request #487 from Xzonn/text-box
Browse files Browse the repository at this point in the history
  • Loading branch information
mathoudebine authored Jun 18, 2024
2 parents 05cb882 + 2387c6f commit f36abbd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
5 changes: 4 additions & 1 deletion library/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,17 @@ 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)),
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)),
anchor="lt"
align=config.THEME_DATA['static_text'][text].get("ALIGN", "left"),
anchor=config.THEME_DATA['static_text'][text].get("ANCHOR", "lt"),
)


Expand Down
32 changes: 26 additions & 6 deletions library/lcd/lcd_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion library/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)


Expand Down

0 comments on commit f36abbd

Please sign in to comment.