Skip to content

Commit 2b4cce1

Browse files
authored
Merge pull request #30 from makermelissa/master
Allow different fonts for multiple labels
2 parents c085bb1 + 6338bfa commit 2b4cce1

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

adafruit_matrixportal/matrixportal.py

+22-17
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,
@@ -163,11 +163,10 @@ def add_text(
163163
the scrolling set to True will be cycled through.
164164
165165
"""
166-
if text_font:
167-
if text_font is terminalio.FONT:
168-
self._text_font = text_font
169-
else:
170-
self._text_font = bitmap_font.load_font(text_font)
166+
if text_font is terminalio.FONT:
167+
self._text_font.append(text_font)
168+
else:
169+
self._text_font.append(bitmap_font.load_font(text_font))
171170
if not text_wrap:
172171
text_wrap = 0
173172
if not text_maxlen:
@@ -228,7 +227,7 @@ def set_background(self, file_or_color, position=None):
228227
"""
229228
self.graphics.set_background(file_or_color, position)
230229

231-
def preload_font(self, glyphs=None):
230+
def preload_font(self, glyphs=None, index=0):
232231
# pylint: disable=line-too-long
233232
"""Preload font.
234233
@@ -239,8 +238,8 @@ def preload_font(self, glyphs=None):
239238
if not glyphs:
240239
glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-!,. \"'?!"
241240
print("Preloading font glyphs:", glyphs)
242-
if self._text_font is not terminalio.FONT:
243-
self._text_font.load_glyphs(glyphs)
241+
if self._text_font[index] is not terminalio.FONT:
242+
self._text_font[index].load_glyphs(glyphs)
244243

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

280279
if len(string) > 0:
281280
self._text[index] = Label(
282-
self._text_font, text=string, scale=self._text_scale[index]
281+
self._text_font[index], text=string, scale=self._text_scale[index]
283282
)
284283
self._text[index].color = self._text_color[index]
285284
self._text[index].x = self._text_position[index][0]
@@ -373,12 +372,18 @@ def scroll_text(self, frame_delay=0.02):
373372
"""
374373
if self._scrolling_index is None: # Not initialized yet
375374
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)
375+
if self._text[self._scrolling_index] is not None:
376+
self._text[self._scrolling_index].x = self.graphics.display.width
377+
line_width = self._text[self._scrolling_index].bounding_box[2]
378+
for _ in range(self.graphics.display.width + line_width + 1):
379+
self.scroll()
380+
sleep(frame_delay)
381+
else:
382+
raise RuntimeError(
383+
"Please assign text to the label with index {} before scrolling".format(
384+
self._scrolling_index
385+
)
386+
)
382387

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

0 commit comments

Comments
 (0)