Skip to content

Commit dc19a7c

Browse files
committed
Added terminalio font support and scaling
1 parent 8975f24 commit dc19a7c

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

adafruit_pyportal.py

100644100755
Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
CircuitPython driver for Adafruit PyPortal.
2727
2828
29-
* Author(s): Limor Fried, Kevin J. Walters
29+
* Author(s): Limor Fried, Kevin J. Walters, Melissa LeBlanc-Williams
3030
3131
Implementation Notes
3232
--------------------
@@ -58,6 +58,7 @@
5858
import storage
5959
import displayio
6060
from adafruit_display_text.label import Label
61+
import terminalio
6162
import audioio
6263
import audiocore
6364
import rtc
@@ -147,6 +148,7 @@ class PyPortal:
147148
``False``, no wrapping.
148149
:param text_maxlen: The max length of the text for text wrapping. Defaults to 0.
149150
:param text_transform: A function that will be called on the text before display
151+
:param int text_scale: The factor to scale the default size of the text by
150152
:param json_transform: A function or a list of functions to call with the parsed JSON.
151153
Changes and additions are permitted for the ``dict`` object.
152154
:param image_json_path: The JSON traversal path for a background image to display. Defaults to
@@ -184,12 +186,13 @@ def __init__(
184186
regexp_path=None,
185187
default_bg=0x000000,
186188
status_neopixel=None,
187-
text_font=None,
189+
text_font=terminalio.FONT,
188190
text_position=None,
189191
text_color=0x808080,
190192
text_wrap=False,
191193
text_maxlen=0,
192194
text_transform=None,
195+
text_scale=1,
193196
json_transform=None,
194197
image_json_path=None,
195198
image_resize=None,
@@ -373,13 +376,18 @@ def __init__(
373376
text_wrap = (text_wrap,)
374377
text_maxlen = (text_maxlen,)
375378
text_transform = (text_transform,)
379+
text_scale = (text_scale,)
376380
self._text = [None] * num
377381
self._text_color = [None] * num
378382
self._text_position = [None] * num
379383
self._text_wrap = [None] * num
380384
self._text_maxlen = [None] * num
381385
self._text_transform = [None] * num
382-
self._text_font = bitmap_font.load_font(text_font)
386+
self._text_scale = [None] * num
387+
if text_font is not terminalio.FONT:
388+
self._text_font = bitmap_font.load_font(text_font)
389+
else:
390+
self._text_font = terminalio.FONT
383391
if self._debug:
384392
print("Loading font glyphs")
385393
# self._text_font.load_glyphs(b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
@@ -395,6 +403,9 @@ def __init__(
395403
self._text_wrap[i] = text_wrap[i]
396404
self._text_maxlen[i] = text_maxlen[i]
397405
self._text_transform[i] = text_transform[i]
406+
if not isinstance(text_scale[i], (int, float)) or text_scale[i] < 1:
407+
text_scale[i] = 1
408+
self._text_scale[i] = text_scale[i]
398409
else:
399410
self._text_font = None
400411
self._text = None
@@ -547,7 +558,7 @@ def preload_font(self, glyphs=None):
547558
if not glyphs:
548559
glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-!,. \"'?!"
549560
print("Preloading font glyphs:", glyphs)
550-
if self._text_font:
561+
if self._text_font and self._text_font is not terminalio.FONT:
551562
self._text_font.load_glyphs(glyphs)
552563

553564
def set_caption(self, caption_text, caption_position, caption_color):
@@ -607,7 +618,9 @@ def set_text(self, val, index=0):
607618
text_index = i
608619
break
609620

610-
self._text[index] = Label(self._text_font, text=string)
621+
self._text[index] = Label(
622+
self._text_font, text=string, scale=self._text_scale[index]
623+
)
611624
self._text[index].color = self._text_color[index]
612625
self._text[index].x = self._text_position[index][0]
613626
self._text[index].y = self._text_position[index][1]
@@ -616,7 +629,9 @@ def set_text(self, val, index=0):
616629

617630
if self._text_position[index]: # if we want it placed somewhere...
618631
print("Making text area with string:", string)
619-
self._text[index] = Label(self._text_font, text=string)
632+
self._text[index] = Label(
633+
self._text_font, text=string, scale=self._text_scale[index]
634+
)
620635
self._text[index].color = self._text_color[index]
621636
self._text[index].x = self._text_position[index][0]
622637
self._text[index].y = self._text_position[index][1]

0 commit comments

Comments
 (0)