diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py old mode 100644 new mode 100755 index 24ef7b4..64730b6 --- 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) @@ -170,13 +172,13 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals # TODO skip this for control sequences or non-printables. i += 1 old_c += 1 - # skip all non-prinables in the old string + # skip all non-printables in the old string while ( self._text 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 @@ -236,7 +238,25 @@ def text(self): @text.setter def text(self, new_text): + current_anchored_position = self.anchored_position self._update_text(str(new_text)) + self.anchored_position = current_anchored_position + + @property + def font(self): + """Font to use for text display.""" + return self._font + + @font.setter + def font(self, new_font): + old_text = self._text + current_anchored_position = self.anchored_position + self._text = "" + self._font = new_font + bounds = self._font.get_bounding_box() + self.height = bounds[1] + self._update_text(str(old_text)) + self.anchored_position = current_anchored_position @property def anchor_point(self): @@ -247,18 +267,36 @@ def anchor_point(self): @anchor_point.setter def anchor_point(self, new_anchor_point): + current_anchored_position = self.anchored_position self._anchor_point = new_anchor_point + self.anchored_position = current_anchored_position @property def anchored_position(self): """Position relative to the anchor_point. Tuple containing x,y pixel coordinates.""" return ( - self.x - self._boundingbox[2] * self._anchor_point[0], - self.y - self._boundingbox[3] * self._anchor_point[1], + int( + self.x + + self._boundingbox[0] + + self._anchor_point[0] * self._boundingbox[2] + ), + int( + self.y + + self._boundingbox[1] + + self._anchor_point[1] * self._boundingbox[3] + ), ) @anchored_position.setter def anchored_position(self, new_position): - self.x = int(new_position[0] - (self._boundingbox[2] * self._anchor_point[0])) - self.y = int(new_position[1] - (self._boundingbox[3] * self._anchor_point[1])) + self.x = int( + new_position[0] + - self._boundingbox[0] + - self._anchor_point[0] * self._boundingbox[2] + ) + self.y = int( + new_position[1] + - self._boundingbox[1] + - self._anchor_point[1] * self._boundingbox[3] + ) diff --git a/examples/display_text_anchored_position.py b/examples/display_text_anchored_position.py index b0728fe..5592c0d 100644 --- a/examples/display_text_anchored_position.py +++ b/examples/display_text_anchored_position.py @@ -12,19 +12,19 @@ text_area_top_left = label.Label(terminalio.FONT, text=TEXT) text_area_top_left.anchor_point = (0.0, 0.0) -text_area_top_left.anchored_position = (10, 10) +text_area_top_left.anchored_position = (0, 0) text_area_top_middle = label.Label(terminalio.FONT, text=TEXT) text_area_top_middle.anchor_point = (0.5, 0.0) -text_area_top_middle.anchored_position = (DISPLAY_WIDTH / 2, 10) +text_area_top_middle.anchored_position = (DISPLAY_WIDTH / 2, 0) text_area_top_right = label.Label(terminalio.FONT, text=TEXT) text_area_top_right.anchor_point = (1.0, 0.0) -text_area_top_right.anchored_position = (DISPLAY_WIDTH - 10, 10) +text_area_top_right.anchored_position = (DISPLAY_WIDTH, 0) text_area_middle_left = label.Label(terminalio.FONT, text=TEXT) text_area_middle_left.anchor_point = (0.0, 0.5) -text_area_middle_left.anchored_position = (10, DISPLAY_HEIGHT / 2) +text_area_middle_left.anchored_position = (0, DISPLAY_HEIGHT / 2) text_area_middle_middle = label.Label(terminalio.FONT, text=TEXT) text_area_middle_middle.anchor_point = (0.5, 0.5) @@ -32,11 +32,11 @@ text_area_middle_right = label.Label(terminalio.FONT, text=TEXT) text_area_middle_right.anchor_point = (1.0, 0.5) -text_area_middle_right.anchored_position = (DISPLAY_WIDTH - 10, DISPLAY_HEIGHT / 2) +text_area_middle_right.anchored_position = (DISPLAY_WIDTH, DISPLAY_HEIGHT / 2) text_area_bottom_left = label.Label(terminalio.FONT, text=TEXT) text_area_bottom_left.anchor_point = (0.0, 1.0) -text_area_bottom_left.anchored_position = (10, DISPLAY_HEIGHT) +text_area_bottom_left.anchored_position = (0, DISPLAY_HEIGHT) text_area_bottom_middle = label.Label(terminalio.FONT, text=TEXT) text_area_bottom_middle.anchor_point = (0.5, 1.0) @@ -44,7 +44,7 @@ text_area_bottom_right = label.Label(terminalio.FONT, text=TEXT) text_area_bottom_right.anchor_point = (1.0, 1.0) -text_area_bottom_right.anchored_position = (DISPLAY_WIDTH - 10, DISPLAY_HEIGHT) +text_area_bottom_right.anchored_position = (DISPLAY_WIDTH, DISPLAY_HEIGHT) text_group = displayio.Group(max_size=9) text_group.append(text_area_top_middle)