Skip to content

Commit 3a2873b

Browse files
committed
Text optimizations
1 parent 28168a5 commit 3a2873b

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

adafruit_framebuf.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,18 @@ def text(self, string, x, y, color, *, font_name="font5x8.bin", size=1):
404404
if not self._font or self._font.font_name != font_name:
405405
# load the font!
406406
self._font = BitmapFont(font_name)
407-
w = self._font.font_width
407+
width = self._font.font_width
408+
height = self._font.font_height
408409
for i, char in enumerate(chunk):
409-
self._font.draw_char(
410-
char, x + (i * (w + 1)) * size, y, self, color, size=size
411-
)
412-
y += self._font.font_height * size
410+
char_x = x + (i * (width + 1)) * size
411+
if (
412+
char_x + (width * size) > 0
413+
and char_x < self.width
414+
and y + (height * size) > 0
415+
and y < self.height
416+
):
417+
self._font.draw_char(char, char_x, y, self, color, size=size)
418+
y += height * size
413419

414420
# pylint: enable=too-many-arguments
415421

0 commit comments

Comments
 (0)