Skip to content

Commit f36abbd

Browse files
authored
Merge pull request #487 from Xzonn/text-box
2 parents 05cb882 + 2387c6f commit f36abbd

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

library/display.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,17 @@ def display_static_text(self):
126126
text=config.THEME_DATA['static_text'][text].get("TEXT"),
127127
x=config.THEME_DATA['static_text'][text].get("X", 0),
128128
y=config.THEME_DATA['static_text'][text].get("Y", 0),
129+
width=config.THEME_DATA['static_text'][text].get("WIDTH", 0),
130+
height=config.THEME_DATA['static_text'][text].get("HEIGHT", 0),
129131
font=config.THEME_DATA['static_text'][text].get("FONT", "roboto-mono/RobotoMono-Regular.ttf"),
130132
font_size=config.THEME_DATA['static_text'][text].get("FONT_SIZE", 10),
131133
font_color=config.THEME_DATA['static_text'][text].get("FONT_COLOR", (0, 0, 0)),
132134
background_color=config.THEME_DATA['static_text'][text].get("BACKGROUND_COLOR", (255, 255, 255)),
133135
background_image=_get_full_path(config.THEME_DATA['PATH'],
134136
config.THEME_DATA['static_text'][text].get("BACKGROUND_IMAGE",
135137
None)),
136-
anchor="lt"
138+
align=config.THEME_DATA['static_text'][text].get("ALIGN", "left"),
139+
anchor=config.THEME_DATA['static_text'][text].get("ANCHOR", "lt"),
137140
)
138141

139142

library/lcd/lcd_comm.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,15 @@ def DisplayText(
209209
text: str,
210210
x: int = 0,
211211
y: int = 0,
212+
width: int = 0,
213+
height: int = 0,
212214
font: str = "roboto-mono/RobotoMono-Regular.ttf",
213215
font_size: int = 20,
214216
font_color: Tuple[int, int, int] = (0, 0, 0),
215217
background_color: Tuple[int, int, int] = (255, 255, 255),
216218
background_image: str = None,
217219
align: str = 'left',
218-
anchor: str = None,
220+
anchor: str = 'lt',
219221
):
220222
# Convert text to bitmap using PIL and display it
221223
# Provide the background image path to display text with transparent background
@@ -249,12 +251,30 @@ def DisplayText(
249251
self.font_cache[(font, font_size)] = ImageFont.truetype("./res/fonts/" + font, font_size)
250252
font = self.font_cache[(font, font_size)]
251253
d = ImageDraw.Draw(text_image)
252-
left, top, right, bottom = d.textbbox((x, y), text, font=font, align=align, anchor=anchor)
253254

254-
# textbbox may return float values, which is not good for the bitmap operations below.
255-
# Let's extend the bounding box to the next whole pixel in all directions
256-
left, top = math.floor(left), math.floor(top)
257-
right, bottom = math.ceil(right), math.ceil(bottom)
255+
if width == 0 or height == 0:
256+
left, top, right, bottom = d.textbbox((x, y), text, font=font, align=align, anchor=anchor)
257+
258+
# textbbox may return float values, which is not good for the bitmap operations below.
259+
# Let's extend the bounding box to the next whole pixel in all directions
260+
left, top = math.floor(left), math.floor(top)
261+
right, bottom = math.ceil(right), math.ceil(bottom)
262+
else:
263+
left, top, right, bottom = x, y, x + width, y + height
264+
265+
if anchor.startswith("m"):
266+
x = (right + left) / 2
267+
elif anchor.startswith("r"):
268+
x = right
269+
else:
270+
x = left
271+
272+
if anchor.endswith("m"):
273+
y = (bottom + top) / 2
274+
elif anchor.endswith("b"):
275+
y = bottom
276+
else:
277+
y = top
258278

259279
# Draw text onto the background image with specified color & font
260280
d.text((x, y), text, font=font, fill=font_color, align=align, anchor=anchor)

library/stats.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,15 @@ def display_themed_value(theme_data, value, min_size=0, unit=''):
9999
text=text,
100100
x=theme_data.get("X", 0),
101101
y=theme_data.get("Y", 0),
102+
width=theme_data.get("WIDTH", 0),
103+
height=theme_data.get("HEIGHT", 0),
102104
font=theme_data.get("FONT", "roboto-mono/RobotoMono-Regular.ttf"),
103105
font_size=theme_data.get("FONT_SIZE", 10),
104106
font_color=theme_data.get("FONT_COLOR", (0, 0, 0)),
105107
background_color=theme_data.get("BACKGROUND_COLOR", (255, 255, 255)),
106108
background_image=get_theme_file_path(theme_data.get("BACKGROUND_IMAGE", None)),
107-
anchor="lt"
109+
align=theme_data.get("ALIGN", "left"),
110+
anchor=theme_data.get("ANCHOR", "lt"),
108111
)
109112

110113

0 commit comments

Comments
 (0)