Skip to content

Commit 6511953

Browse files
authored
Merge pull request #86 from makermelissa/terminalio
Added terminalio font support and scaling
2 parents 2271503 + 51a7868 commit 6511953

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

adafruit_pyportal.py

+23-6
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
--------------------
@@ -59,6 +59,7 @@
5959
import storage
6060
import displayio
6161
from adafruit_display_text.label import Label
62+
import terminalio
6263
import audioio
6364
import audiocore
6465
import rtc
@@ -159,6 +160,7 @@ class PyPortal:
159160
``False``, no wrapping.
160161
:param text_maxlen: The max length of the text for text wrapping. Defaults to 0.
161162
:param text_transform: A function that will be called on the text before display
163+
:param int text_scale: The factor to scale the default size of the text by
162164
:param json_transform: A function or a list of functions to call with the parsed JSON.
163165
Changes and additions are permitted for the ``dict`` object.
164166
:param image_json_path: The JSON traversal path for a background image to display. Defaults to
@@ -197,12 +199,13 @@ def __init__(
197199
convert_image=True,
198200
default_bg=0x000000,
199201
status_neopixel=None,
200-
text_font=None,
202+
text_font=terminalio.FONT,
201203
text_position=None,
202204
text_color=0x808080,
203205
text_wrap=False,
204206
text_maxlen=0,
205207
text_transform=None,
208+
text_scale=1,
206209
json_transform=None,
207210
image_json_path=None,
208211
image_resize=None,
@@ -380,20 +383,27 @@ def __init__(
380383
text_maxlen = [0] * num
381384
if not text_transform:
382385
text_transform = [None] * num
386+
if not isinstance(text_scale, (list, tuple)):
387+
text_scale = [text_scale] * num
383388
else:
384389
num = 1
385390
text_position = (text_position,)
386391
text_color = (text_color,)
387392
text_wrap = (text_wrap,)
388393
text_maxlen = (text_maxlen,)
389394
text_transform = (text_transform,)
395+
text_scale = (text_scale,)
390396
self._text = [None] * num
391397
self._text_color = [None] * num
392398
self._text_position = [None] * num
393399
self._text_wrap = [None] * num
394400
self._text_maxlen = [None] * num
395401
self._text_transform = [None] * num
396-
self._text_font = bitmap_font.load_font(text_font)
402+
self._text_scale = [None] * num
403+
if text_font is not terminalio.FONT:
404+
self._text_font = bitmap_font.load_font(text_font)
405+
else:
406+
self._text_font = terminalio.FONT
397407
if self._debug:
398408
print("Loading font glyphs")
399409
# self._text_font.load_glyphs(b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
@@ -409,6 +419,9 @@ def __init__(
409419
self._text_wrap[i] = text_wrap[i]
410420
self._text_maxlen[i] = text_maxlen[i]
411421
self._text_transform[i] = text_transform[i]
422+
if not isinstance(text_scale[i], (int, float)) or text_scale[i] < 1:
423+
text_scale[i] = 1
424+
self._text_scale[i] = text_scale[i]
412425
else:
413426
self._text_font = None
414427
self._text = None
@@ -561,7 +574,7 @@ def preload_font(self, glyphs=None):
561574
if not glyphs:
562575
glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-!,. \"'?!"
563576
print("Preloading font glyphs:", glyphs)
564-
if self._text_font:
577+
if self._text_font and self._text_font is not terminalio.FONT:
565578
self._text_font.load_glyphs(glyphs)
566579

567580
def set_caption(self, caption_text, caption_position, caption_color):
@@ -621,7 +634,9 @@ def set_text(self, val, index=0):
621634
text_index = i
622635
break
623636

624-
self._text[index] = Label(self._text_font, text=string)
637+
self._text[index] = Label(
638+
self._text_font, text=string, scale=self._text_scale[index]
639+
)
625640
self._text[index].color = self._text_color[index]
626641
self._text[index].x = self._text_position[index][0]
627642
self._text[index].y = self._text_position[index][1]
@@ -630,7 +645,9 @@ def set_text(self, val, index=0):
630645

631646
if self._text_position[index]: # if we want it placed somewhere...
632647
print("Making text area with string:", string)
633-
self._text[index] = Label(self._text_font, text=string)
648+
self._text[index] = Label(
649+
self._text_font, text=string, scale=self._text_scale[index]
650+
)
634651
self._text[index].color = self._text_color[index]
635652
self._text[index].x = self._text_position[index][0]
636653
self._text[index].y = self._text_position[index][1]

docs/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"adafruit_io",
4242
"adafruit_cursorcontrol",
4343
"adafruit_requests",
44+
"terminalio",
4445
]
4546

4647

0 commit comments

Comments
 (0)