Skip to content

Commit

Permalink
Merge pull request #2 from FoamyGuy/bitmap_zero_patch
Browse files Browse the repository at this point in the history
fix issue with background being shifted from text. only create bitmap…

There are additional changes that will be merged in the next pull request.
  • Loading branch information
kmatch98 committed Jul 8, 2020
2 parents 2d65dc0 + c5af111 commit c2e3a79
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions adafruit_display_text/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -171,21 +166,49 @@ 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:
# 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)

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(
(
Expand Down Expand Up @@ -267,7 +290,20 @@ 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)
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):
Expand Down Expand Up @@ -369,6 +405,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

0 comments on commit c2e3a79

Please sign in to comment.