diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py old mode 100644 new mode 100755 index 24ef7b4..1e057e5 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -58,6 +58,9 @@ class Label(displayio.Group): :param int color: Color of all text in RGB hex :param double line_spacing: Line spacing of text to display""" + # pylint: disable=too-many-instance-attributes + # This has a lot of getters/setters, maybe it needs cleanup. + def __init__( self, font, @@ -77,7 +80,7 @@ def __init__( max_glyphs = len(text) super().__init__(max_size=max_glyphs, **kwargs) self.width = max_glyphs - self.font = font + self._font = font self._text = None self._anchor_point = (0, 0) self.x = x @@ -94,7 +97,7 @@ def __init__( self._transparent_background = True self.palette[1] = color - bounds = self.font.get_bounding_box() + bounds = self._font.get_bounding_box() self.height = bounds[1] self._line_spacing = line_spacing self._boundingbox = None @@ -109,19 +112,18 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals old_c = 0 y_offset = int( ( - self.font.get_glyph(ord("M")).height + self._font.get_glyph(ord("M")).height - new_text.count("\n") * self.height * self.line_spacing ) / 2 ) - # print("y offset from baseline", y_offset) left = right = top = bottom = 0 for character in new_text: if character == "\n": y += int(self.height * self._line_spacing) x = 0 continue - glyph = self.font.get_glyph(ord(character)) + glyph = self._font.get_glyph(ord(character)) if not glyph: continue right = max(right, x + glyph.width) @@ -167,7 +169,7 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals x += glyph.shift_x - # TODO skip this for control sequences or non-printables. + # TODO skip this for control sequences or non-qables. i += 1 old_c += 1 # skip all non-prinables in the old string @@ -176,7 +178,7 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals and old_c < len(self._text) and ( self._text[old_c] == "\n" - or not self.font.get_glyph(ord(self._text[old_c])) + or not self._font.get_glyph(ord(self._text[old_c])) ) ): old_c += 1 @@ -238,6 +240,20 @@ def text(self): def text(self, new_text): self._update_text(str(new_text)) + @property + def font(self): + """Font to use for text display.""" + return self._font + + @font.setter + def font(self, new_font): + old_text = self._text + self._text = "" + self._font = new_font + bounds = self._font.get_bounding_box() + self.height = bounds[1] + self._update_text(str(old_text)) + @property def anchor_point(self): """Point that anchored_position moves relative to.