Skip to content

Updates bitmap sizing and logic checks for adding/removing bitmap #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions adafruit_display_text/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down