Skip to content

Commit ec98a7f

Browse files
authored
Merge pull request #3 from kmatch98/bitmap_zeroV2
Updates bitmap sizing and logic checks for adding/removing bitmap
2 parents c2e3a79 + b6ec018 commit ec98a7f

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

adafruit_display_text/label.py

+30-19
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,39 @@ def _update_background_color(self, new_color):
189189
/ 2
190190
)
191191
lines = self.text.count("\n") + 1
192-
if not self._added_background_tilegrid:
193-
# only if we have text or padding
194-
if len(self.text) + self._padding_left + self._padding_right > 0:
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 (
196+
(len(self._text) > 0)
197+
and (
198+
self._boundingbox[2] + self._padding_left + self._padding_right > 0
199+
)
200+
and (
201+
self._boundingbox[3] + self._padding_top + self._padding_bottom > 0
202+
)
203+
):
195204
if len(self) > 0:
196205
self.insert(0, self._create_background_box(lines, y_offset))
197206
else:
198207
self.append(self._create_background_box(lines, y_offset))
199208
self._added_background_tilegrid = True
200-
else:
201-
self[0] = self._create_background_box(lines, y_offset)
209+
210+
else: # a bitmap is present in the self Group
211+
# update bitmap if text is present and bitmap sizes > 0 pixels
212+
if (
213+
(len(self._text) > 0)
214+
and (
215+
self._boundingbox[2] + self._padding_left + self._padding_right > 0
216+
)
217+
and (
218+
self._boundingbox[3] + self._padding_top + self._padding_bottom > 0
219+
)
220+
):
221+
self[0] = self._create_background_box(lines, y_offset)
222+
else: # delete the existing bitmap
223+
self.pop(0)
224+
self._added_background_tilegrid = False
202225

203226
def _update_text(
204227
self, new_text
@@ -290,20 +313,8 @@ def _update_text(
290313
self.pop()
291314
self._text = new_text
292315
self._boundingbox = (left, top, left + right, bottom - top)
293-
if (
294-
self._background_color
295-
and len(new_text) + self._padding_left + self._padding_right > 0
296-
):
297-
if not self._added_background_tilegrid:
298-
self._added_background_tilegrid = True
299-
self.insert(0, self._create_background_box(lines, y_offset))
300-
else:
301-
self[0] = self._create_background_box(lines, y_offset)
302-
else:
303-
self._background_palette.make_transparent(0)
304-
if self._added_background_tilegrid:
305-
self.pop(0)
306-
self._added_background_tilegrid = False
316+
317+
self._update_background_color(self._background_color)
307318

308319
@property
309320
def bounding_box(self):

0 commit comments

Comments
 (0)