Skip to content

Commit 8ca8663

Browse files
author
Kevin J Walters
committed
Reusing bitmap if dimensions have not changed of the text.
The tilegrid is also reused if bitmap has not changed. New self._prev_label_direction to allow for tilegrid properties to be set efficiently. Reusing existing bitmap needs clearing due to required use of skip_index on blit(). #166
1 parent 144cbf9 commit 8ca8663

File tree

1 file changed

+51
-32
lines changed

1 file changed

+51
-32
lines changed

adafruit_display_text/bitmap_label.py

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,22 @@ class Label(LabelBase):
8484
configurations possibles ``LTR``-Left-To-Right ``RTL``-Right-To-Left
8585
``UPD``-Upside Down ``UPR``-Upwards ``DWR``-Downwards. It defaults to ``LTR``"""
8686

87+
# This maps label_direction to TileGrid's transpose_xy, flip_x, flip_y
88+
_DIR_MAP = {
89+
"UPR": (True, True, False),
90+
"DWR": (True, False, True),
91+
"UPD": (False, True, True),
92+
"LTR": (False, False, False),
93+
"RTL": (False, False, False),
94+
}
95+
8796
def __init__(
8897
self, font: Union[BuiltinFont, BDF, PCF], save_text: bool = True, **kwargs
8998
) -> None:
9099

91100
self._bitmap = None
101+
self._tilegrid = None
102+
self._prev_label_direction = None
92103

93104
super().__init__(font, **kwargs)
94105

@@ -113,7 +124,7 @@ def _reset_text(
113124
line_spacing: Optional[float] = None,
114125
scale: Optional[int] = None,
115126
) -> None:
116-
# pylint: disable=too-many-branches, too-many-statements
127+
# pylint: disable=too-many-branches, too-many-statements, too-many-locals
117128

118129
# Store all the instance variables
119130
if font is not None:
@@ -179,8 +190,17 @@ def _reset_text(
179190
box_x = box_x + self._padding_left + self._padding_right
180191
box_y = box_y + self._padding_top + self._padding_bottom
181192

182-
# Create the bitmap and TileGrid
183-
self._bitmap = displayio.Bitmap(box_x, box_y, len(self._palette))
193+
# Create the Bitmap unless it can be reused
194+
new_bitmap = None
195+
if (
196+
self._bitmap is None
197+
or self._bitmap.width != box_x
198+
or self._bitmap.height != box_y
199+
):
200+
new_bitmap = displayio.Bitmap(box_x, box_y, len(self._palette))
201+
self._bitmap = new_bitmap
202+
else:
203+
self._bitmap.fill(0)
184204

185205
# Place the text into the Bitmap
186206
self._place_text(
@@ -196,35 +216,33 @@ def _reset_text(
196216
else:
197217
label_position_yoffset = self._ascent // 2
198218

199-
self._tilegrid = displayio.TileGrid(
200-
self._bitmap,
201-
pixel_shader=self._palette,
202-
width=1,
203-
height=1,
204-
tile_width=box_x,
205-
tile_height=box_y,
206-
default_tile=0,
207-
x=-self._padding_left + x_offset,
208-
y=label_position_yoffset - y_offset - self._padding_top,
209-
)
210-
211-
if self._label_direction == "UPR":
212-
self._tilegrid.transpose_xy = True
213-
self._tilegrid.flip_x = True
214-
if self._label_direction == "DWR":
215-
self._tilegrid.transpose_xy = True
216-
self._tilegrid.flip_y = True
217-
if self._label_direction == "UPD":
218-
self._tilegrid.flip_x = True
219-
self._tilegrid.flip_y = True
220-
221-
# Clear out any items in the local_group Group, in case this is an update to
222-
# the bitmap_label
223-
for _ in self._local_group:
224-
self._local_group.pop(0)
225-
self._local_group.append(
226-
self._tilegrid
227-
) # add the bitmap's tilegrid to the group
219+
# Create the TileGrid if not created bitmap unchanged
220+
if self._tilegrid is None or new_bitmap:
221+
self._tilegrid = displayio.TileGrid(
222+
self._bitmap,
223+
pixel_shader=self._palette,
224+
width=1,
225+
height=1,
226+
tile_width=box_x,
227+
tile_height=box_y,
228+
default_tile=0,
229+
x=-self._padding_left + x_offset,
230+
y=label_position_yoffset - y_offset - self._padding_top,
231+
)
232+
# Clear out any items in the local_group Group, in case this is an update to
233+
# the bitmap_label
234+
for _ in self._local_group:
235+
self._local_group.pop(0)
236+
self._local_group.append(
237+
self._tilegrid
238+
) # add the bitmap's tilegrid to the group
239+
240+
# Set TileGrid properties based on label_direction
241+
if self._label_direction != self._prev_label_direction:
242+
tg = self._tilegrid
243+
tg.transpose_xy, tg.flip_x, tg.flip_y = self._DIR_MAP[
244+
self._label_direction
245+
]
228246

229247
# Update bounding_box values. Note: To be consistent with label.py,
230248
# this is the bounding box for the text only, not including the background.
@@ -537,6 +555,7 @@ def _set_background_color(self, new_color: Optional[int]):
537555
self._palette.make_transparent(0)
538556

539557
def _set_label_direction(self, new_label_direction: str) -> None:
558+
self._prev_label_direction = self._label_direction
540559
self._label_direction = new_label_direction
541560
self._reset_text(text=str(self._text)) # Force a recalculation
542561

0 commit comments

Comments
 (0)