Skip to content

Commit 13fc5f9

Browse files
committed
Allow different fonts for multiple labels
1 parent 1bf7658 commit 13fc5f9

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

adafruit_matrixportal/matrixportal.py

+20-14
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def __init__(
126126
self._text_scrolling = []
127127
self._text_scale = []
128128
self._scrolling_index = None
129-
self._text_font = terminalio.FONT
129+
self._text_font = []
130130
self._text_line_spacing = []
131131

132132
gc.collect()
@@ -135,7 +135,7 @@ def __init__(
135135
def add_text(
136136
self,
137137
text_position=None,
138-
text_font=None,
138+
text_font=terminalio.FONT,
139139
text_color=0x808080,
140140
text_wrap=False,
141141
text_maxlen=0,
@@ -165,9 +165,9 @@ def add_text(
165165
"""
166166
if text_font:
167167
if text_font is terminalio.FONT:
168-
self._text_font = text_font
168+
self._text_font.append(text_font)
169169
else:
170-
self._text_font = bitmap_font.load_font(text_font)
170+
self._text_font.append(bitmap_font.load_font(text_font))
171171
if not text_wrap:
172172
text_wrap = 0
173173
if not text_maxlen:
@@ -228,7 +228,7 @@ def set_background(self, file_or_color, position=None):
228228
"""
229229
self.graphics.set_background(file_or_color, position)
230230

231-
def preload_font(self, glyphs=None):
231+
def preload_font(self, glyphs=None, index=0):
232232
# pylint: disable=line-too-long
233233
"""Preload font.
234234
@@ -239,8 +239,8 @@ def preload_font(self, glyphs=None):
239239
if not glyphs:
240240
glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-!,. \"'?!"
241241
print("Preloading font glyphs:", glyphs)
242-
if self._text_font is not terminalio.FONT:
243-
self._text_font.load_glyphs(glyphs)
242+
if self._text_font[index] is not terminalio.FONT:
243+
self._text_font[index].load_glyphs(glyphs)
244244

245245
def set_text_color(self, color, index=0):
246246
"""Update the text color, with indexing into our list of text boxes.
@@ -279,7 +279,7 @@ def set_text(self, val, index=0):
279279

280280
if len(string) > 0:
281281
self._text[index] = Label(
282-
self._text_font, text=string, scale=self._text_scale[index]
282+
self._text_font[index], text=string, scale=self._text_scale[index]
283283
)
284284
self._text[index].color = self._text_color[index]
285285
self._text[index].x = self._text_position[index][0]
@@ -373,12 +373,18 @@ def scroll_text(self, frame_delay=0.02):
373373
"""
374374
if self._scrolling_index is None: # Not initialized yet
375375
return
376-
377-
self._text[self._scrolling_index].x = self.graphics.display.width
378-
line_width = self._text[self._scrolling_index].bounding_box[2]
379-
for _ in range(self.graphics.display.width + line_width + 1):
380-
self.scroll()
381-
sleep(frame_delay)
376+
if self._text[self._scrolling_index] is not None:
377+
self._text[self._scrolling_index].x = self.graphics.display.width
378+
line_width = self._text[self._scrolling_index].bounding_box[2]
379+
for _ in range(self.graphics.display.width + line_width + 1):
380+
self.scroll()
381+
sleep(frame_delay)
382+
else:
383+
raise RuntimeError(
384+
"Please assign text to the label with index {} before scrolling".format(
385+
self._scrolling_index
386+
)
387+
)
382388

383389
def fetch(self, refresh_url=None, timeout=10):
384390
"""Fetch data from the url we initialized with, perfom any parsing,

0 commit comments

Comments
 (0)