Skip to content

Added terminalio font support and scaling #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions adafruit_pyportal.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
CircuitPython driver for Adafruit PyPortal.


* Author(s): Limor Fried, Kevin J. Walters
* Author(s): Limor Fried, Kevin J. Walters, Melissa LeBlanc-Williams

Implementation Notes
--------------------
Expand Down Expand Up @@ -59,6 +59,7 @@
import storage
import displayio
from adafruit_display_text.label import Label
import terminalio
import audioio
import audiocore
import rtc
Expand Down Expand Up @@ -159,6 +160,7 @@ class PyPortal:
``False``, no wrapping.
:param text_maxlen: The max length of the text for text wrapping. Defaults to 0.
:param text_transform: A function that will be called on the text before display
:param int text_scale: The factor to scale the default size of the text by
:param json_transform: A function or a list of functions to call with the parsed JSON.
Changes and additions are permitted for the ``dict`` object.
:param image_json_path: The JSON traversal path for a background image to display. Defaults to
Expand Down Expand Up @@ -197,12 +199,13 @@ def __init__(
convert_image=True,
default_bg=0x000000,
status_neopixel=None,
text_font=None,
text_font=terminalio.FONT,
text_position=None,
text_color=0x808080,
text_wrap=False,
text_maxlen=0,
text_transform=None,
text_scale=1,
json_transform=None,
image_json_path=None,
image_resize=None,
Expand Down Expand Up @@ -380,20 +383,27 @@ def __init__(
text_maxlen = [0] * num
if not text_transform:
text_transform = [None] * num
if not isinstance(text_scale, (list, tuple)):
text_scale = [text_scale] * num
else:
num = 1
text_position = (text_position,)
text_color = (text_color,)
text_wrap = (text_wrap,)
text_maxlen = (text_maxlen,)
text_transform = (text_transform,)
text_scale = (text_scale,)
self._text = [None] * num
self._text_color = [None] * num
self._text_position = [None] * num
self._text_wrap = [None] * num
self._text_maxlen = [None] * num
self._text_transform = [None] * num
self._text_font = bitmap_font.load_font(text_font)
self._text_scale = [None] * num
if text_font is not terminalio.FONT:
self._text_font = bitmap_font.load_font(text_font)
else:
self._text_font = terminalio.FONT
if self._debug:
print("Loading font glyphs")
# self._text_font.load_glyphs(b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
Expand All @@ -409,6 +419,9 @@ def __init__(
self._text_wrap[i] = text_wrap[i]
self._text_maxlen[i] = text_maxlen[i]
self._text_transform[i] = text_transform[i]
if not isinstance(text_scale[i], (int, float)) or text_scale[i] < 1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested these changes and I am getting and I'm getting an exception from this line:

Traceback (most recent call last):
  File "code.py", line 80, in <module>
  File "/lib/adafruit_pyportal.py", line 410, in __init__
TypeError: 'int' object is not subscriptable

I'm using the code from the quotes example here: https://learn.adafruit.com/adafruit-pyportal/parsing-json. I tried with the code as-is, and with the new text_scale parameter added, I'm getting same results both ways.

Copy link
Contributor

@FoamyGuy FoamyGuy Oct 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to get past this error by adding a check for text_scale type inside the body of the if statement at line 368 of the modified code. Now mine looks like:

if isinstance(text_position[0], (list, tuple)):
    num = len(text_position)
    if not text_wrap:
        text_wrap = [0] * num
    if not text_maxlen:
        text_maxlen = [0] * num
    if not text_transform:
        text_transform = [None] * num
    if isinstance(text_scale, int):  # new check for text scale
        text_scale = [text_scale] * num

text_scale[i] = 1
self._text_scale[i] = text_scale[i]
else:
self._text_font = None
self._text = None
Expand Down Expand Up @@ -561,7 +574,7 @@ def preload_font(self, glyphs=None):
if not glyphs:
glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-!,. \"'?!"
print("Preloading font glyphs:", glyphs)
if self._text_font:
if self._text_font and self._text_font is not terminalio.FONT:
self._text_font.load_glyphs(glyphs)

def set_caption(self, caption_text, caption_position, caption_color):
Expand Down Expand Up @@ -621,7 +634,9 @@ def set_text(self, val, index=0):
text_index = i
break

self._text[index] = Label(self._text_font, text=string)
self._text[index] = Label(
self._text_font, text=string, scale=self._text_scale[index]
)
self._text[index].color = self._text_color[index]
self._text[index].x = self._text_position[index][0]
self._text[index].y = self._text_position[index][1]
Expand All @@ -630,7 +645,9 @@ def set_text(self, val, index=0):

if self._text_position[index]: # if we want it placed somewhere...
print("Making text area with string:", string)
self._text[index] = Label(self._text_font, text=string)
self._text[index] = Label(
self._text_font, text=string, scale=self._text_scale[index]
)
self._text[index].color = self._text_color[index]
self._text[index].x = self._text_position[index][0]
self._text[index].y = self._text_position[index][1]
Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"adafruit_io",
"adafruit_cursorcontrol",
"adafruit_requests",
"terminalio",
]


Expand Down