From 4acafa4b4f13a79af4c69d6d0735260fac7fd6b8 Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Fri, 22 May 2020 23:47:05 -0500 Subject: [PATCH 01/17] Added black line at row 46, per recommendation of foamyguy --- adafruit_display_text/label.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 1c82b48..007d583 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -43,6 +43,7 @@ class Label(displayio.Group): properties will be the left edge of the bounding box, and in the center of a M glyph (if its one line), or the (number of lines * linespacing + M)/2. That is, it will try to have it be center-left as close as possible. + :param Font font: A font class that has ``get_bounding_box`` and ``get_glyph``. Must include a capital M for measuring character size. :param str text: Text to display From 1c1926452304c211b515239b0940dcc82d4ad79b Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Sat, 23 May 2020 09:44:41 -0500 Subject: [PATCH 02/17] trial to see if pylint too-many-instance-attributes can be overridden --- adafruit_display_text/label.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 007d583..c6098fa 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -43,7 +43,7 @@ class Label(displayio.Group): properties will be the left edge of the bounding box, and in the center of a M glyph (if its one line), or the (number of lines * linespacing + M)/2. That is, it will try to have it be center-left as close as possible. - + :param Font font: A font class that has ``get_bounding_box`` and ``get_glyph``. Must include a capital M for measuring character size. :param str text: Text to display @@ -51,6 +51,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, From 83f43decaec49b8b114176007f193eb22c151a85 Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Sat, 23 May 2020 11:36:52 -0500 Subject: [PATCH 03/17] added spaces and deleted one tab, thanks foamyguy --- adafruit_display_text/label.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index c6098fa..1e057e5 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -22,14 +22,21 @@ """ `adafruit_display_text.label` ==================================================== + Displays text labels using CircuitPython's displayio. + * Author(s): Scott Shawcroft + Implementation Notes -------------------- + **Hardware:** + **Software and Dependencies:** + * Adafruit CircuitPython firmware for the supported boards: https://github.com/adafruit/circuitpython/releases + """ import displayio From 2caeb592cc2a15693c0f260246d7c4151167ccf3 Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Thu, 2 Jul 2020 14:00:04 -0500 Subject: [PATCH 04/17] Updated initial instancing of Bitmap to non-zero size --- adafruit_display_text/label.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 748cba8..f140f39 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -114,7 +114,7 @@ def __init__( self._background_palette = displayio.Palette(1) self.append( displayio.TileGrid( - displayio.Bitmap(0, 0, 1), pixel_shader=self._background_palette + displayio.Bitmap(1, 1, 1), pixel_shader=self._background_palette ) ) # initialize with a blank tilegrid placeholder for background From c9309dae6a15d3e9ceccfee2bf3fae90a161fffe Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Thu, 2 Jul 2020 15:11:18 -0500 Subject: [PATCH 05/17] Updated anchored position getter/setter --- adafruit_display_text/label.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index f140f39..a9a484b 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -353,12 +353,10 @@ def anchored_position(self): return ( 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] ), ) @@ -372,7 +370,6 @@ def anchored_position(self, new_position): new_y = self.y = int( new_position[1] - self._anchor_point[1] * (self._boundingbox[3] * self._scale) - + (self._boundingbox[3] * self._scale) / 2 ) self._boundingbox = (new_x, new_y, self._boundingbox[2], self._boundingbox[3]) self.x = new_x From 11b6fcbbe2d703a5399631f1e9a40dc365abe6e8 Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Thu, 2 Jul 2020 15:12:46 -0500 Subject: [PATCH 06/17] ran black --- adafruit_display_text/label.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index a9a484b..4d17b24 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -351,14 +351,8 @@ def anchored_position(self): """Position relative to the anchor_point. Tuple containing x,y pixel coordinates.""" return ( - int( - self.x - + self._anchor_point[0] * self._boundingbox[2] - ), - int( - self.y - + self._anchor_point[1] * self._boundingbox[3] - ), + int(self.x + self._anchor_point[0] * self._boundingbox[2]), + int(self.y + self._anchor_point[1] * self._boundingbox[3]), ) @anchored_position.setter From 0b524cfe2bdd38a77d47c3eee7ab4665d1d17872 Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Thu, 2 Jul 2020 19:35:50 -0500 Subject: [PATCH 07/17] Updated anchored_position getter/setter --- adafruit_display_text/label.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 4d17b24..5e3d72b 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -351,9 +351,10 @@ def anchored_position(self): """Position relative to the anchor_point. Tuple containing x,y pixel coordinates.""" return ( - int(self.x + self._anchor_point[0] * self._boundingbox[2]), - int(self.y + self._anchor_point[1] * self._boundingbox[3]), - ) + int(self.x + self._anchor_point[0] * self._boundingbox[2] * self._scale ), + int(self.y + self._anchor_point[1] * self._boundingbox[3] * self._scale + - (self._boundingbox[3] * self._scale)/2 ) + ) @anchored_position.setter def anchored_position(self, new_position): @@ -364,6 +365,7 @@ def anchored_position(self, new_position): new_y = self.y = int( new_position[1] - self._anchor_point[1] * (self._boundingbox[3] * self._scale) + + (self._boundingbox[3] * self._scale)/2 ) self._boundingbox = (new_x, new_y, self._boundingbox[2], self._boundingbox[3]) self.x = new_x From 062b1bc7473d92e0809e8edfa27fdf8897f17aa0 Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Thu, 2 Jul 2020 21:49:42 -0500 Subject: [PATCH 08/17] Fixed off-by-one error by int() truncation --- adafruit_display_text/label.py | 41 +++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 5e3d72b..ec83fa5 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -164,15 +164,17 @@ def _create_background_box(self, lines, y_offset): box_width = max(0, box_width) # remove any negative values box_height = max(0, box_height) # remove any negative values - background_bitmap = displayio.Bitmap(box_width, box_height, 1) - tile_grid = displayio.TileGrid( - background_bitmap, - pixel_shader=self._background_palette, - x=left + x_box_offset, - y=y_box_offset, - ) - - return tile_grid + if box_width > 0 and box_height > 0: + background_bitmap = displayio.Bitmap(box_width, box_height, 1) + tile_grid = displayio.TileGrid( + background_bitmap, + pixel_shader=self._background_palette, + x=left + x_box_offset, + y=y_box_offset, + ) + return tile_grid + else: + return None def _update_background_color(self, new_color): @@ -270,6 +272,7 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals self._boundingbox = (left, top, left + right, bottom - top) self[0] = self._create_background_box(lines, y_offset) + @property def bounding_box(self): """An (x, y, w, h) tuple that completely covers all glyphs. The @@ -313,8 +316,11 @@ def text(self): def text(self, new_text): try: current_anchored_position = self.anchored_position + print('start anchored_position: {}'.format(self.anchored_position)) + print('self.y: {}, self._scale: {}'.format(self.y, self._scale)) self._update_text(str(new_text)) self.anchored_position = current_anchored_position + print('end anchored_position: {}'.format(self.anchored_position)) except RuntimeError: raise RuntimeError("Text length exceeds max_glyphs") @@ -351,10 +357,10 @@ def anchored_position(self): """Position relative to the anchor_point. Tuple containing x,y pixel coordinates.""" return ( - int(self.x + self._anchor_point[0] * self._boundingbox[2] * self._scale ), - int(self.y + self._anchor_point[1] * self._boundingbox[3] * self._scale - - (self._boundingbox[3] * self._scale)/2 ) - ) + int(self.x + (self._anchor_point[0] * self._boundingbox[2] * self._scale) ), + int(self.y + (self._anchor_point[1] * self._boundingbox[3] * self._scale) + - round( (self._boundingbox[3] * self._scale)/2.0 )) + ) @anchored_position.setter def anchored_position(self, new_position): @@ -362,11 +368,14 @@ 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 ) ) + + print('new_y: {}, new_position[1]: {}, self._anchor_point[1]: {}, self._boundingbox[3]: {}'.format(new_y, new_position[1], self._anchor_point[1], self._boundingbox[3])) + self._boundingbox = (new_x, new_y, self._boundingbox[2], self._boundingbox[3]) self.x = new_x self.y = new_y From 631d0447b33304c90ba75c77c96662f4f0dd0aec Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Thu, 2 Jul 2020 21:51:10 -0500 Subject: [PATCH 09/17] Ran black, deleted printdebug statements --- adafruit_display_text/label.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index ec83fa5..29aab61 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -272,7 +272,6 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals self._boundingbox = (left, top, left + right, bottom - top) self[0] = self._create_background_box(lines, y_offset) - @property def bounding_box(self): """An (x, y, w, h) tuple that completely covers all glyphs. The @@ -316,11 +315,8 @@ def text(self): def text(self, new_text): try: current_anchored_position = self.anchored_position - print('start anchored_position: {}'.format(self.anchored_position)) - print('self.y: {}, self._scale: {}'.format(self.y, self._scale)) self._update_text(str(new_text)) self.anchored_position = current_anchored_position - print('end anchored_position: {}'.format(self.anchored_position)) except RuntimeError: raise RuntimeError("Text length exceeds max_glyphs") @@ -357,10 +353,13 @@ def anchored_position(self): """Position relative to the anchor_point. Tuple containing x,y pixel coordinates.""" return ( - int(self.x + (self._anchor_point[0] * self._boundingbox[2] * self._scale) ), - int(self.y + (self._anchor_point[1] * self._boundingbox[3] * self._scale) - - round( (self._boundingbox[3] * self._scale)/2.0 )) - ) + int(self.x + (self._anchor_point[0] * self._boundingbox[2] * self._scale)), + int( + self.y + + (self._anchor_point[1] * self._boundingbox[3] * self._scale) + - round((self._boundingbox[3] * self._scale) / 2.0) + ), + ) @anchored_position.setter def anchored_position(self, new_position): @@ -370,12 +369,9 @@ def anchored_position(self, new_position): ) new_y = int( new_position[1] - - ( self._anchor_point[1] * self._boundingbox[3] * self._scale) - + round( (self._boundingbox[3] * self._scale)/2.0 ) + - (self._anchor_point[1] * self._boundingbox[3] * self._scale) + + round((self._boundingbox[3] * self._scale) / 2.0) ) - - print('new_y: {}, new_position[1]: {}, self._anchor_point[1]: {}, self._boundingbox[3]: {}'.format(new_y, new_position[1], self._anchor_point[1], self._boundingbox[3])) - self._boundingbox = (new_x, new_y, self._boundingbox[2], self._boundingbox[3]) self.x = new_x self.y = new_y From 2d65dc0f5ca8b21c2d6b66119948599ffd3e67c2 Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Thu, 2 Jul 2020 22:03:08 -0500 Subject: [PATCH 10/17] Reverted a half-updated function for background bitmap management --- adafruit_display_text/label.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 29aab61..e915ecb 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -164,17 +164,14 @@ def _create_background_box(self, lines, y_offset): box_width = max(0, box_width) # remove any negative values box_height = max(0, box_height) # remove any negative values - if box_width > 0 and box_height > 0: - background_bitmap = displayio.Bitmap(box_width, box_height, 1) - tile_grid = displayio.TileGrid( - background_bitmap, - pixel_shader=self._background_palette, - x=left + x_box_offset, - y=y_box_offset, - ) - return tile_grid - else: - return None + background_bitmap = displayio.Bitmap(box_width, box_height, 1) + tile_grid = displayio.TileGrid( + background_bitmap, + pixel_shader=self._background_palette, + x=left + x_box_offset, + y=y_box_offset, + ) + return tile_grid def _update_background_color(self, new_color): From 51e13b2e619c9069f74c60eeb4c2f9f64b052247 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Sat, 4 Jul 2020 10:30:57 -0500 Subject: [PATCH 11/17] fix issue with background being shifted from text. only create bitmap if it will get used. --- adafruit_display_text/label.py | 49 +++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index e915ecb..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(1, 1, 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 @@ -171,6 +173,7 @@ 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): @@ -182,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( ( @@ -267,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): @@ -369,6 +399,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 fdd5bd8894505eab036ca1d7436c1b5ae999928e Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Sat, 4 Jul 2020 10:32:13 -0500 Subject: [PATCH 12/17] black format --- adafruit_display_text/label.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index fdc41d7..35599ca 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -200,7 +200,9 @@ def _update_background_color(self, new_color): 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 + def _update_text( + self, new_text + ): # pylint: disable=too-many-locals ,too-many-branches, too-many-statements x = 0 y = 0 if self._added_background_tilegrid: From 2063412f7840e88376c07a562f83354e467d077f Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Mon, 6 Jul 2020 20:50:27 -0500 Subject: [PATCH 13/17] remove the background tilegrid when not in use. Don't add background tilegrid inside init --- adafruit_display_text/label.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 35599ca..ce6282a 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -113,13 +113,6 @@ def __init__( self._background_color = background_color self._background_palette = displayio.Palette(1) 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 @@ -180,6 +173,8 @@ def _update_background_color(self, new_color): if new_color is None: self._background_palette.make_transparent(0) + self.pop(0) + self._added_background_tilegrid = False else: self._background_palette.make_opaque(0) self._background_palette[0] = new_color @@ -194,7 +189,6 @@ def _update_background_color(self, new_color): ) 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: @@ -295,7 +289,6 @@ def _update_text( 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: From aa4d0a003687602f0e7d7f683bbb029b1c85f34f Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Tue, 7 Jul 2020 17:01:16 -0500 Subject: [PATCH 14/17] check if the background tilegrid was added before removing it --- adafruit_display_text/label.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index ce6282a..20e88a2 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -173,8 +173,9 @@ def _update_background_color(self, new_color): if new_color is None: self._background_palette.make_transparent(0) - self.pop(0) - self._added_background_tilegrid = False + 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 From c5af1119d9b6e5b3f8fe697ec0e3449d90049e12 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Tue, 7 Jul 2020 17:20:07 -0500 Subject: [PATCH 15/17] remove background tilegrid if text is set to empty and no padding. Fix for Group insert/append issue --- adafruit_display_text/label.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 20e88a2..c27e124 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -190,8 +190,13 @@ def _update_background_color(self, new_color): ) 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)) + # only if we have text or padding + if len(self.text) + self._padding_left + self._padding_right > 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) @@ -294,6 +299,11 @@ def _update_text( 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 @property def bounding_box(self): From f106d483a118f30a0ecbf09e1239ccb0f3311bc0 Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Tue, 7 Jul 2020 19:54:51 -0500 Subject: [PATCH 16/17] 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 17/17] 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