Skip to content

Commit 59508b7

Browse files
committed
Check if generated text overflows, crop it if necessary
1 parent 3d37304 commit 59508b7

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

main.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def DisplayText(ser, text, x=0, y=0,
106106

107107
# Crop text bitmap to keep only the text
108108
text_width, text_height = d.textsize(text, font=font)
109-
text_image = text_image.crop((x, y, x + text_width, y + text_height))
109+
text_image = text_image.crop(box=(x, y, min(x + text_width, DISPLAY_WIDTH), min(y + text_height, DISPLAY_HEIGHT)))
110110

111111
DisplayPILImage(ser, text_image, x, y)
112112

@@ -127,16 +127,25 @@ def DisplayText(ser, text, x=0, y=0,
127127
# Display sample text
128128
DisplayText(lcd_comm, "Basic text", 50, 100)
129129

130+
# Display custom text with solid background
130131
DisplayText(lcd_comm, "Custom italic text", 5, 150,
131132
font="roboto/Roboto-Italic.ttf",
132133
font_size=30,
133134
font_color=(0, 0, 255),
134135
background_color=(255, 255, 0))
135136

137+
# Display custom text with transparent background
136138
DisplayText(lcd_comm, "Transparent bold text", 5, 300,
137139
font="roboto/Roboto-Bold.ttf",
138140
font_size=30,
139141
font_color=(255, 255, 255),
140142
background_image="res/example.png")
141143

144+
# Display text that overflows
145+
DisplayText(lcd_comm, "Text overflow!", 5, 450,
146+
font="roboto/Roboto-Bold.ttf",
147+
font_size=60,
148+
font_color=(255, 255, 255),
149+
background_image="res/example.png")
150+
142151
lcd_comm.close()

0 commit comments

Comments
 (0)