Skip to content

Commit a681cc9

Browse files
authored
Merge pull request #313 from hchargois/add-text-anchor
2 parents 02072f9 + 4057b0d commit a681cc9

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

library/display.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ def display_static_text(self):
136136
background_color=config.THEME_DATA['static_text'][text].get("BACKGROUND_COLOR", (255, 255, 255)),
137137
background_image=_get_full_path(config.THEME_DATA['PATH'],
138138
config.THEME_DATA['static_text'][text].get("BACKGROUND_IMAGE",
139-
None))
139+
None)),
140+
anchor="lt"
140141
)
141142

142143

library/lcd/lcd_comm.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import sys
2222
import threading
2323
import time
24+
import math
2425
from abc import ABC, abstractmethod
2526
from enum import IntEnum
2627
from typing import Tuple
@@ -206,7 +207,8 @@ def DisplayText(
206207
font_color: Tuple[int, int, int] = (0, 0, 0),
207208
background_color: Tuple[int, int, int] = (255, 255, 255),
208209
background_image: str = None,
209-
align: str = 'left'
210+
align: str = 'left',
211+
anchor: str = None,
210212
):
211213
# Convert text to bitmap using PIL and display it
212214
# Provide the background image path to display text with transparent background
@@ -238,19 +240,26 @@ def DisplayText(
238240
# Get text bounding box
239241
font = ImageFont.truetype("./res/fonts/" + font, font_size)
240242
d = ImageDraw.Draw(text_image)
241-
left, top, text_width, text_height = d.textbbox((0, 0), text, font=font)
243+
left, top, right, bottom = d.textbbox((x, y), text, font=font, align=align, anchor=anchor)
242244

243-
# Draw text with specified color & font, remove left/top margins
244-
d.text((x - left, y - top), text, font=font, fill=font_color, align=align)
245+
# textbbox may return float values, which is not good for the bitmap operations below.
246+
# Let's extend the bounding box to the next whole pixel in all directions
247+
left, top = math.floor(left), math.floor(top)
248+
right, bottom = math.ceil(right), math.ceil(bottom)
245249

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

253-
self.DisplayPILImage(text_image, x, y)
253+
# Restrict the dimensions if they overflow the display size
254+
left = max(left, 0)
255+
top = max(top, 0)
256+
right = min(right, self.get_width())
257+
bottom = min(bottom, self.get_height())
258+
259+
# Crop text bitmap to keep only the text
260+
text_image = text_image.crop(box=(left, top, right, bottom))
261+
262+
self.DisplayPILImage(text_image, left, top)
254263

255264
def DisplayProgressBar(self, x: int, y: int, width: int, height: int, min_value: int = 0, max_value: int = 100,
256265
value: int = 50,

library/stats.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ def display_themed_value(theme_data, value, min_size=0, unit=''):
9494
font_size=theme_data.get("FONT_SIZE", 10),
9595
font_color=theme_data.get("FONT_COLOR", (0, 0, 0)),
9696
background_color=theme_data.get("BACKGROUND_COLOR", (255, 255, 255)),
97-
background_image=get_theme_file_path(theme_data.get("BACKGROUND_IMAGE", None))
97+
background_image=get_theme_file_path(theme_data.get("BACKGROUND_IMAGE", None)),
98+
anchor="lt"
9899
)
99100

100101

0 commit comments

Comments
 (0)