diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index c27e124..c7d3809 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -189,16 +189,39 @@ def _update_background_color(self, new_color): / 2 ) lines = self.text.count("\n") + 1 - if not self._added_background_tilegrid: - # only if we have text or padding - if len(self.text) + self._padding_left + self._padding_right > 0: + + 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: - self[0] = self._create_background_box(lines, y_offset) + + 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 @@ -290,20 +313,8 @@ def _update_text( self.pop() self._text = new_text self._boundingbox = (left, top, left + right, bottom - top) - 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) - else: - self._background_palette.make_transparent(0) - if self._added_background_tilegrid: - self.pop(0) - self._added_background_tilegrid = False + + self._update_background_color(self._background_color) @property def bounding_box(self):