Skip to content

Bitmap zero - Creates 1-pixel bitmap during initialization of tilegrid for background of label #59

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 22 commits into from
Jul 9, 2020
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4acafa4
Added black line at row 46, per recommendation of foamyguy
May 23, 2020
1c19264
trial to see if pylint too-many-instance-attributes can be overridden
May 23, 2020
83f43de
added spaces and deleted one tab, thanks foamyguy
May 23, 2020
ec7b69a
Merge remote-tracking branch 'upstream/master'
May 29, 2020
646b46d
Merge remote-tracking branch 'upstream/master'
Jul 2, 2020
2caeb59
Updated initial instancing of Bitmap to non-zero size
Jul 2, 2020
c9309da
Updated anchored position getter/setter
Jul 2, 2020
11b6fcb
ran black
Jul 2, 2020
0b524cf
Updated anchored_position getter/setter
Jul 3, 2020
062b1bc
Fixed off-by-one error by int() truncation
Jul 3, 2020
631d044
Ran black, deleted printdebug statements
Jul 3, 2020
2d65dc0
Reverted a half-updated function for background bitmap management
Jul 3, 2020
51e13b2
fix issue with background being shifted from text. only create bitmap…
FoamyGuy Jul 4, 2020
fdd5bd8
black format
FoamyGuy Jul 4, 2020
2063412
remove the background tilegrid when not in use. Don't add background …
FoamyGuy Jul 7, 2020
aa4d0a0
check if the background tilegrid was added before removing it
FoamyGuy Jul 7, 2020
c5af111
remove background tilegrid if text is set to empty and no padding. Fi…
FoamyGuy Jul 7, 2020
f106d48
Fixed bounding box size calculations and logical flow for updating bi…
Jul 8, 2020
05affa4
ran black
Jul 8, 2020
c2e3a79
Merge pull request #2 from FoamyGuy/bitmap_zero_patch
kmatch98 Jul 8, 2020
b6ec018
Merge branch 'bitmap_zero' into bitmap_zeroV2
kmatch98 Jul 8, 2020
ec98a7f
Merge pull request #3 from kmatch98/bitmap_zeroV2
kmatch98 Jul 8, 2020
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
81 changes: 61 additions & 20 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(0, 0, 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 @@ -178,15 +173,65 @@ 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(
(
Expand Down Expand Up @@ -268,7 +313,8 @@ 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):
Expand Down Expand Up @@ -351,15 +397,11 @@ def anchored_position(self):
"""Position relative to the anchor_point. Tuple containing x,y
pixel coordinates."""
return (
int(
self.x
+ self._boundingbox[0]
+ self._anchor_point[0] * self._boundingbox[2]
),
int(self.x + (self._anchor_point[0] * self._boundingbox[2] * self._scale)),
int(
self.y
+ self._boundingbox[1]
+ self._anchor_point[1] * self._boundingbox[3]
+ (self._anchor_point[1] * self._boundingbox[3] * self._scale)
- round((self._boundingbox[3] * self._scale) / 2.0)
),
)

Expand All @@ -369,11 +411,10 @@ 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)
)
self._boundingbox = (new_x, new_y, self._boundingbox[2], self._boundingbox[3])
self.x = new_x
self.y = new_y