Skip to content

Commit f106d48

Browse files
author
Margaret Matocha
committed
Fixed bounding box size calculations and logical flow for updating bitmap in _update_background_color
1 parent 2d65dc0 commit f106d48

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

adafruit_display_text/label.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,7 @@ def __init__(
112112

113113
self._background_color = background_color
114114
self._background_palette = displayio.Palette(1)
115-
self.append(
116-
displayio.TileGrid(
117-
displayio.Bitmap(1, 1, 1), pixel_shader=self._background_palette
118-
)
119-
) # initialize with a blank tilegrid placeholder for background
115+
self._added_background_tilegrid = False
120116

121117
self._padding_top = padding_top
122118
self._padding_bottom = padding_bottom
@@ -160,7 +156,6 @@ def _create_background_box(self, lines, y_offset):
160156
)
161157
y_box_offset = -ascender_max + y_offset - self._padding_top
162158

163-
self._update_background_color(self._background_color)
164159
box_width = max(0, box_width) # remove any negative values
165160
box_height = max(0, box_height) # remove any negative values
166161

@@ -171,21 +166,59 @@ def _create_background_box(self, lines, y_offset):
171166
x=left + x_box_offset,
172167
y=y_box_offset,
173168
)
169+
174170
return tile_grid
175171

176172
def _update_background_color(self, new_color):
177173

178174
if new_color is None:
179175
self._background_palette.make_transparent(0)
176+
if self._added_background_tilegrid:
177+
self.pop(0)
178+
self._added_background_tilegrid = False
180179
else:
181180
self._background_palette.make_opaque(0)
182181
self._background_palette[0] = new_color
183182
self._background_color = new_color
184183

185-
def _update_text(self, new_text): # pylint: disable=too-many-locals
184+
y_offset = int(
185+
(
186+
self._font.get_glyph(ord("M")).height
187+
- self.text.count("\n") * self.height * self.line_spacing
188+
)
189+
/ 2
190+
)
191+
lines = self.text.count("\n") + 1
192+
193+
if not self._added_background_tilegrid: # no bitmap is in the self Group
194+
# add bitmap if text is present and bitmap sizes > 0 pixels
195+
if ((len(self._text) > 0)
196+
and (self._boundingbox[2] + self._padding_left + self._padding_right > 0)
197+
and (self._boundingbox[3] + self._padding_top + self._padding_bottom > 0)):
198+
if len(self) > 0:
199+
self.insert(0, self._create_background_box(lines, y_offset))
200+
else:
201+
self.append(self._create_background_box(lines, y_offset))
202+
self._added_background_tilegrid = True
203+
else: # a bitmap is present in the self Group
204+
# update bitmap if text is present and bitmap sizes > 0 pixels
205+
if ((len(self._text) > 0)
206+
and (self._boundingbox[2] + self._padding_left + self._padding_right > 0)
207+
and (self._boundingbox[3] + self._padding_top + self._padding_bottom > 0)):
208+
self[0] = self._create_background_box(lines, y_offset)
209+
else: # delete the existing bitmap
210+
self.pop(0)
211+
self._added_background_tilegrid = False
212+
213+
def _update_text(
214+
self, new_text
215+
): # pylint: disable=too-many-locals ,too-many-branches, too-many-statements
186216
x = 0
187217
y = 0
188-
i = 1
218+
if self._added_background_tilegrid:
219+
i = 1
220+
else:
221+
i = 0
189222
old_c = 0
190223
y_offset = int(
191224
(
@@ -267,7 +300,7 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
267300
self.pop()
268301
self._text = new_text
269302
self._boundingbox = (left, top, left + right, bottom - top)
270-
self[0] = self._create_background_box(lines, y_offset)
303+
self._update_background_color(self._background_color)
271304

272305
@property
273306
def bounding_box(self):
@@ -369,6 +402,5 @@ def anchored_position(self, new_position):
369402
- (self._anchor_point[1] * self._boundingbox[3] * self._scale)
370403
+ round((self._boundingbox[3] * self._scale) / 2.0)
371404
)
372-
self._boundingbox = (new_x, new_y, self._boundingbox[2], self._boundingbox[3])
373405
self.x = new_x
374406
self.y = new_y

0 commit comments

Comments
 (0)