From f106d483a118f30a0ecbf09e1239ccb0f3311bc0 Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Tue, 7 Jul 2020 19:54:51 -0500 Subject: [PATCH 1/2] Fixed bounding box size calculations and logical flow for updating bitmap in _update_background_color --- adafruit_display_text/label.py | 52 +++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index e915ecb..2d23cec 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(1, 1, 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 @@ -171,21 +166,59 @@ def _create_background_box(self, lines, y_offset): x=left + x_box_offset, y=y_box_offset, ) + return tile_grid 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( ( @@ -267,7 +300,7 @@ 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): @@ -369,6 +402,5 @@ def anchored_position(self, new_position): - (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 From 05affa4097b24af2ede9284d53c8ae2403bf2e81 Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Tue, 7 Jul 2020 19:56:14 -0500 Subject: [PATCH 2/2] ran black --- adafruit_display_text/label.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 2d23cec..b7f6c40 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -190,23 +190,35 @@ def _update_background_color(self, new_color): ) lines = self.text.count("\n") + 1 - if not self._added_background_tilegrid: # no bitmap is in the self Group + 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._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 + 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)): + 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 + else: # delete the existing bitmap self.pop(0) self._added_background_tilegrid = False