From 13fc5f965aa926c9b21300f2f4614935080bf24c Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Fri, 23 Oct 2020 15:05:02 -0600 Subject: [PATCH 1/2] Allow different fonts for multiple labels --- adafruit_matrixportal/matrixportal.py | 34 ++++++++++++++++----------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/adafruit_matrixportal/matrixportal.py b/adafruit_matrixportal/matrixportal.py index f037067..72208af 100755 --- a/adafruit_matrixportal/matrixportal.py +++ b/adafruit_matrixportal/matrixportal.py @@ -126,7 +126,7 @@ def __init__( self._text_scrolling = [] self._text_scale = [] self._scrolling_index = None - self._text_font = terminalio.FONT + self._text_font = [] self._text_line_spacing = [] gc.collect() @@ -135,7 +135,7 @@ def __init__( def add_text( self, text_position=None, - text_font=None, + text_font=terminalio.FONT, text_color=0x808080, text_wrap=False, text_maxlen=0, @@ -165,9 +165,9 @@ def add_text( """ if text_font: if text_font is terminalio.FONT: - self._text_font = text_font + self._text_font.append(text_font) else: - self._text_font = bitmap_font.load_font(text_font) + self._text_font.append(bitmap_font.load_font(text_font)) if not text_wrap: text_wrap = 0 if not text_maxlen: @@ -228,7 +228,7 @@ def set_background(self, file_or_color, position=None): """ self.graphics.set_background(file_or_color, position) - def preload_font(self, glyphs=None): + def preload_font(self, glyphs=None, index=0): # pylint: disable=line-too-long """Preload font. @@ -239,8 +239,8 @@ def preload_font(self, glyphs=None): if not glyphs: glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-!,. \"'?!" print("Preloading font glyphs:", glyphs) - if self._text_font is not terminalio.FONT: - self._text_font.load_glyphs(glyphs) + if self._text_font[index] is not terminalio.FONT: + self._text_font[index].load_glyphs(glyphs) def set_text_color(self, color, index=0): """Update the text color, with indexing into our list of text boxes. @@ -279,7 +279,7 @@ def set_text(self, val, index=0): if len(string) > 0: self._text[index] = Label( - self._text_font, text=string, scale=self._text_scale[index] + self._text_font[index], text=string, scale=self._text_scale[index] ) self._text[index].color = self._text_color[index] self._text[index].x = self._text_position[index][0] @@ -373,12 +373,18 @@ def scroll_text(self, frame_delay=0.02): """ if self._scrolling_index is None: # Not initialized yet return - - self._text[self._scrolling_index].x = self.graphics.display.width - line_width = self._text[self._scrolling_index].bounding_box[2] - for _ in range(self.graphics.display.width + line_width + 1): - self.scroll() - sleep(frame_delay) + if self._text[self._scrolling_index] is not None: + self._text[self._scrolling_index].x = self.graphics.display.width + line_width = self._text[self._scrolling_index].bounding_box[2] + for _ in range(self.graphics.display.width + line_width + 1): + self.scroll() + sleep(frame_delay) + else: + raise RuntimeError( + "Please assign text to the label with index {} before scrolling".format( + self._scrolling_index + ) + ) def fetch(self, refresh_url=None, timeout=10): """Fetch data from the url we initialized with, perfom any parsing, From 6338bfac856d256a05efaea7a963406f585de0b8 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Fri, 23 Oct 2020 15:09:20 -0600 Subject: [PATCH 2/2] Force use of the font --- adafruit_matrixportal/matrixportal.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/adafruit_matrixportal/matrixportal.py b/adafruit_matrixportal/matrixportal.py index 72208af..41c8715 100755 --- a/adafruit_matrixportal/matrixportal.py +++ b/adafruit_matrixportal/matrixportal.py @@ -163,11 +163,10 @@ def add_text( the scrolling set to True will be cycled through. """ - if text_font: - if text_font is terminalio.FONT: - self._text_font.append(text_font) - else: - self._text_font.append(bitmap_font.load_font(text_font)) + if text_font is terminalio.FONT: + self._text_font.append(text_font) + else: + self._text_font.append(bitmap_font.load_font(text_font)) if not text_wrap: text_wrap = 0 if not text_maxlen: