Skip to content

Commit 259435c

Browse files
committed
Add anchor parameter to DisplayText
The anchor parameter is required to easily (and correctly) placing text on the drawn image. See https://pillow.readthedocs.io/en/stable/handbook/text-anchors.html
1 parent 7ab4a45 commit 259435c

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

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,

0 commit comments

Comments
 (0)