diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 748cba8..fdc41d7 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -112,11 +112,14 @@ 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 + if self._background_color: + self.append( + displayio.TileGrid( + displayio.Bitmap(1, 1, 1), pixel_shader=self._background_palette + ) + ) # initialize with a blank tilegrid placeholder for background + self._added_background_tilegrid = True self._padding_top = padding_top self._padding_bottom = padding_bottom @@ -160,7 +163,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 @@ -183,10 +185,28 @@ def _update_background_color(self, new_color): 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: + + self._added_background_tilegrid = True + self.insert(0, self._create_background_box(lines, y_offset)) + else: + self[0] = self._create_background_box(lines, y_offset) + + 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 +288,16 @@ 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) + if ( + self._background_color + and len(new_text) + self._padding_left + self._padding_right > 0 + ): + if not self._added_background_tilegrid: + + self._added_background_tilegrid = True + self.insert(0, self._create_background_box(lines, y_offset)) + else: + self[0] = self._create_background_box(lines, y_offset) @property def bounding_box(self): @@ -351,15 +380,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 +394,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