diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 748cba8..c7d3809 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -112,11 +112,7 @@ def __init__( self._background_color = background_color self._background_palette = displayio.Palette(1) - self.append( - displayio.TileGrid( - displayio.Bitmap(0, 0, 1), pixel_shader=self._background_palette - ) - ) # initialize with a blank tilegrid placeholder for background + self._added_background_tilegrid = False self._padding_top = padding_top self._padding_bottom = padding_bottom @@ -160,7 +156,6 @@ def _create_background_box(self, lines, y_offset): ) y_box_offset = -ascender_max + y_offset - self._padding_top - self._update_background_color(self._background_color) box_width = max(0, box_width) # remove any negative values box_height = max(0, box_height) # remove any negative values @@ -178,15 +173,65 @@ def _update_background_color(self, new_color): if new_color is None: self._background_palette.make_transparent(0) + if self._added_background_tilegrid: + self.pop(0) + self._added_background_tilegrid = False else: self._background_palette.make_opaque(0) self._background_palette[0] = new_color self._background_color = new_color - def _update_text(self, new_text): # pylint: disable=too-many-locals + y_offset = int( + ( + self._font.get_glyph(ord("M")).height + - self.text.count("\n") * self.height * self.line_spacing + ) + / 2 + ) + lines = self.text.count("\n") + 1 + + if not self._added_background_tilegrid: # no bitmap is in the self Group + # add bitmap if text is present and bitmap sizes > 0 pixels + if ( + (len(self._text) > 0) + and ( + self._boundingbox[2] + self._padding_left + self._padding_right > 0 + ) + and ( + self._boundingbox[3] + self._padding_top + self._padding_bottom > 0 + ) + ): + if len(self) > 0: + self.insert(0, self._create_background_box(lines, y_offset)) + else: + self.append(self._create_background_box(lines, y_offset)) + self._added_background_tilegrid = True + + else: # a bitmap is present in the self Group + # update bitmap if text is present and bitmap sizes > 0 pixels + if ( + (len(self._text) > 0) + and ( + self._boundingbox[2] + self._padding_left + self._padding_right > 0 + ) + and ( + self._boundingbox[3] + self._padding_top + self._padding_bottom > 0 + ) + ): + self[0] = self._create_background_box(lines, y_offset) + else: # delete the existing bitmap + self.pop(0) + self._added_background_tilegrid = False + + def _update_text( + self, new_text + ): # pylint: disable=too-many-locals ,too-many-branches, too-many-statements x = 0 y = 0 - i = 1 + if self._added_background_tilegrid: + i = 1 + else: + i = 0 old_c = 0 y_offset = int( ( @@ -268,7 +313,8 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals self.pop() self._text = new_text self._boundingbox = (left, top, left + right, bottom - top) - self[0] = self._create_background_box(lines, y_offset) + + self._update_background_color(self._background_color) @property def bounding_box(self): @@ -351,15 +397,11 @@ def anchored_position(self): """Position relative to the anchor_point. Tuple containing x,y pixel coordinates.""" return ( - int( - self.x - + self._boundingbox[0] - + self._anchor_point[0] * self._boundingbox[2] - ), + int(self.x + (self._anchor_point[0] * self._boundingbox[2] * self._scale)), int( self.y - + self._boundingbox[1] - + self._anchor_point[1] * self._boundingbox[3] + + (self._anchor_point[1] * self._boundingbox[3] * self._scale) + - round((self._boundingbox[3] * self._scale) / 2.0) ), ) @@ -369,11 +411,10 @@ def anchored_position(self, new_position): new_position[0] - self._anchor_point[0] * (self._boundingbox[2] * self._scale) ) - new_y = self.y = int( + new_y = int( new_position[1] - - self._anchor_point[1] * (self._boundingbox[3] * self._scale) - + (self._boundingbox[3] * self._scale) / 2 + - (self._anchor_point[1] * self._boundingbox[3] * self._scale) + + round((self._boundingbox[3] * self._scale) / 2.0) ) - self._boundingbox = (new_x, new_y, self._boundingbox[2], self._boundingbox[3]) self.x = new_x self.y = new_y