Skip to content

Commit daad42a

Browse files
fractal13WasabiFan
authored andcommitted
Allow Display.text_pixels() to receive a loaded font or a font string. (ev3dev#601)
* Allow Display.text_pixels() to receive a loaded font or a font string. This will allow the users to preload a font and eliminate the constant reloading of fonts, which can produce lag when displaying in a loop. * doc-string for Display.text_pixels and Display.text_grid updated for sphinx.
1 parent e9a1598 commit daad42a

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

‎ev3dev2/display.py‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ def text_pixels(self, text, clear_screen=True, x=0, y=0, text_color='black', fon
365365
Display `text` starting at pixel (x, y).
366366
367367
The EV3 display is 178x128 pixels
368+
368369
- (0, 0) would be the top left corner of the display
369370
- (89, 64) would be right in the middle of the display
370371
@@ -374,15 +375,22 @@ def text_pixels(self, text, clear_screen=True, x=0, y=0, text_color='black', fon
374375
https://www.w3schools.com/colors/colors_names.asp
375376
376377
'font' : can be any font displayed here
377-
http://ev3dev-lang.readthedocs.io/projects/python-ev3dev/en/ev3dev-stretch/other.html#bitmap-fonts
378+
http://ev3dev-lang.readthedocs.io/projects/python-ev3dev/en/ev3dev-stretch/display.html#bitmap-fonts
379+
380+
- If font is a string, it is the name of a font to be loaded.
381+
- If font is a Font object, returned from :meth:`ev3dev2.fonts.load`, then it is
382+
used directly. This is desirable for faster display times.
383+
378384
"""
379385

380386
if clear_screen:
381387
self.clear()
382388

383389
if font is not None:
384-
assert font in fonts.available(), "%s is an invalid font" % font
385-
return self.draw.text((x, y), text, fill=text_color, font=fonts.load(font))
390+
if isinstance(font, str):
391+
assert font in fonts.available(), "%s is an invalid font" % font
392+
font = fonts.load(font)
393+
return self.draw.text((x, y), text, fill=text_color, font=font)
386394
else:
387395
return self.draw.text((x, y), text, fill=text_color)
388396

@@ -400,7 +408,12 @@ def text_grid(self, text, clear_screen=True, x=0, y=0, text_color='black', font=
400408
https://www.w3schools.com/colors/colors_names.asp
401409
402410
'font' : can be any font displayed here
403-
http://ev3dev-lang.readthedocs.io/projects/python-ev3dev/en/ev3dev-stretch/other.html#bitmap-fonts
411+
http://ev3dev-lang.readthedocs.io/projects/python-ev3dev/en/ev3dev-stretch/display.html#bitmap-fonts
412+
413+
- If font is a string, it is the name of a font to be loaded.
414+
- If font is a Font object, returned from :meth:`ev3dev2.fonts.load`, then it is
415+
used directly. This is desirable for faster display times.
416+
404417
"""
405418

406419
assert 0 <= x < Display.GRID_COLUMNS,\

0 commit comments

Comments
 (0)