Skip to content

Commit

Permalink
Allow Display.text_pixels() to receive a loaded font or a font string. (
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
fractal13 authored and WasabiFan committed Mar 20, 2019
1 parent e9a1598 commit daad42a
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions ev3dev2/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ def text_pixels(self, text, clear_screen=True, x=0, y=0, text_color='black', fon
Display `text` starting at pixel (x, y).
The EV3 display is 178x128 pixels
- (0, 0) would be the top left corner of the display
- (89, 64) would be right in the middle of the display
Expand All @@ -374,15 +375,22 @@ def text_pixels(self, text, clear_screen=True, x=0, y=0, text_color='black', fon
https://www.w3schools.com/colors/colors_names.asp
'font' : can be any font displayed here
http://ev3dev-lang.readthedocs.io/projects/python-ev3dev/en/ev3dev-stretch/other.html#bitmap-fonts
http://ev3dev-lang.readthedocs.io/projects/python-ev3dev/en/ev3dev-stretch/display.html#bitmap-fonts
- If font is a string, it is the name of a font to be loaded.
- If font is a Font object, returned from :meth:`ev3dev2.fonts.load`, then it is
used directly. This is desirable for faster display times.
"""

if clear_screen:
self.clear()

if font is not None:
assert font in fonts.available(), "%s is an invalid font" % font
return self.draw.text((x, y), text, fill=text_color, font=fonts.load(font))
if isinstance(font, str):
assert font in fonts.available(), "%s is an invalid font" % font
font = fonts.load(font)
return self.draw.text((x, y), text, fill=text_color, font=font)
else:
return self.draw.text((x, y), text, fill=text_color)

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

assert 0 <= x < Display.GRID_COLUMNS,\
Expand Down

0 comments on commit daad42a

Please sign in to comment.