Skip to content

Commit 088663b

Browse files
committed
trying to remove usage of 0 sized bitmaps
1 parent 225b0b1 commit 088663b

File tree

1 file changed

+81
-50
lines changed

1 file changed

+81
-50
lines changed

adafruit_display_text/label.py

Lines changed: 81 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,22 @@ class Label(displayio.Group):
6262
# This has a lot of getters/setters, maybe it needs cleanup.
6363

6464
def __init__(
65-
self,
66-
font,
67-
*,
68-
x=0,
69-
y=0,
70-
text="",
71-
max_glyphs=None,
72-
color=0xFFFFFF,
73-
background_color=None,
74-
line_spacing=1.25,
75-
background_tight=False,
76-
padding_top=0,
77-
padding_bottom=0,
78-
padding_left=0,
79-
padding_right=0,
80-
**kwargs
65+
self,
66+
font,
67+
*,
68+
x=0,
69+
y=0,
70+
text="",
71+
max_glyphs=None,
72+
color=0xFFFFFF,
73+
background_color=None,
74+
line_spacing=1.25,
75+
background_tight=False,
76+
padding_top=0,
77+
padding_bottom=0,
78+
padding_left=0,
79+
padding_right=0,
80+
**kwargs
8181
):
8282
if "scale" in kwargs.keys():
8383
self._scale = kwargs["scale"]
@@ -89,6 +89,7 @@ def __init__(
8989
max_glyphs = len(text)
9090
# add one to max_size for the background bitmap tileGrid
9191
super().__init__(max_size=max_glyphs + 1, **kwargs)
92+
print("max_size={}".format(max_glyphs + 1))
9293

9394
self.width = max_glyphs
9495
self._font = font
@@ -112,11 +113,15 @@ def __init__(
112113

113114
self._background_color = background_color
114115
self._background_palette = displayio.Palette(1)
115-
self.append(
116-
displayio.TileGrid(
117-
displayio.Bitmap(0, 0, 1), pixel_shader=self._background_palette
118-
)
119-
) # initialize with a blank tilegrid placeholder for background
116+
self._added_background_tilegrid = False
117+
if self._background_color:
118+
print("adding background tilegrid from init")
119+
self.append(
120+
displayio.TileGrid(
121+
displayio.Bitmap(1, 1, 1), pixel_shader=self._background_palette
122+
)
123+
) # initialize with a blank tilegrid placeholder for background
124+
self._added_background_tilegrid = True
120125

121126
self._padding_top = padding_top
122127
self._padding_bottom = padding_bottom
@@ -153,14 +158,14 @@ def _create_background_box(self, lines, y_offset):
153158
box_width = self._boundingbox[2] + self._padding_left + self._padding_right
154159
x_box_offset = -self._padding_left
155160
box_height = (
156-
(ascender_max + descender_max)
157-
+ int((lines - 1) * self.height * self._line_spacing)
158-
+ self._padding_top
159-
+ self._padding_bottom
161+
(ascender_max + descender_max)
162+
+ int((lines - 1) * self.height * self._line_spacing)
163+
+ self._padding_top
164+
+ self._padding_bottom
160165
)
161166
y_box_offset = -ascender_max + y_offset - self._padding_top
162167

163-
self._update_background_color(self._background_color)
168+
# self._update_background_color(self._background_color)
164169
box_width = max(0, box_width) # remove any negative values
165170
box_height = max(0, box_height) # remove any negative values
166171

@@ -171,27 +176,47 @@ def _create_background_box(self, lines, y_offset):
171176
x=left + x_box_offset,
172177
y=y_box_offset,
173178
)
174-
175179
return tile_grid
176180

177181
def _update_background_color(self, new_color):
178182

179183
if new_color is None:
184+
print("setting {} transparent".format(self._background_palette[0]))
180185
self._background_palette.make_transparent(0)
181186
else:
182187
self._background_palette.make_opaque(0)
183188
self._background_palette[0] = new_color
184189
self._background_color = new_color
185190

191+
y_offset = int(
192+
(
193+
self._font.get_glyph(ord("M")).height
194+
- self.text.count("\n") * self.height * self.line_spacing
195+
)
196+
/ 2
197+
)
198+
lines = self.text.count("\n") + 1
199+
if not self._added_background_tilegrid:
200+
201+
self._added_background_tilegrid = True
202+
print("adding background tilegrid in update background color")
203+
print("len self = {}".format(len(self)))
204+
self.insert(0, self._create_background_box(lines, y_offset))
205+
else:
206+
self[0] = self._create_background_box(lines, y_offset)
207+
186208
def _update_text(self, new_text): # pylint: disable=too-many-locals
187209
x = 0
188210
y = 0
189-
i = 1
211+
if self._added_background_tilegrid:
212+
i = 1
213+
else:
214+
i = 0
190215
old_c = 0
191216
y_offset = int(
192217
(
193-
self._font.get_glyph(ord("M")).height
194-
- new_text.count("\n") * self.height * self.line_spacing
218+
self._font.get_glyph(ord("M")).height
219+
- new_text.count("\n") * self.height * self.line_spacing
195220
)
196221
/ 2
197222
)
@@ -213,9 +238,9 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
213238
position_y = y - glyph.height - glyph.dy + y_offset
214239
position_x = x + glyph.dx
215240
if (
216-
not self._text
217-
or old_c >= len(self._text)
218-
or character != self._text[old_c]
241+
not self._text
242+
or old_c >= len(self._text)
243+
or character != self._text[old_c]
219244
):
220245
try:
221246
# pylint: disable=unexpected-keyword-arg
@@ -240,6 +265,7 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
240265
if i < len(self):
241266
self[i] = face
242267
else:
268+
print("appending face")
243269
self.append(face)
244270
elif self._text and character == self._text[old_c]:
245271

@@ -255,20 +281,29 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
255281
old_c += 1
256282
# skip all non-printables in the old string
257283
while (
258-
self._text
259-
and old_c < len(self._text)
260-
and (
261-
self._text[old_c] == "\n"
262-
or not self._font.get_glyph(ord(self._text[old_c]))
263-
)
284+
self._text
285+
and old_c < len(self._text)
286+
and (
287+
self._text[old_c] == "\n"
288+
or not self._font.get_glyph(ord(self._text[old_c]))
289+
)
264290
):
265291
old_c += 1
266292
# Remove the rest
267293
while len(self) > i:
268294
self.pop()
269295
self._text = new_text
270296
self._boundingbox = (left, top, left + right, bottom - top)
271-
self[0] = self._create_background_box(lines, y_offset)
297+
print("bg color: {}".format(self._background_color))
298+
if self._background_color and len(new_text) + self._padding_left + self._padding_right > 0:
299+
if not self._added_background_tilegrid:
300+
301+
self._added_background_tilegrid = True
302+
print("adding background tilegrid")
303+
print("len self = {}".format(len(self)))
304+
self.insert(0, self._create_background_box(lines, y_offset))
305+
else:
306+
self[0] = self._create_background_box(lines, y_offset)
272307

273308
@property
274309
def bounding_box(self):
@@ -351,15 +386,11 @@ def anchored_position(self):
351386
"""Position relative to the anchor_point. Tuple containing x,y
352387
pixel coordinates."""
353388
return (
354-
int(
355-
self.x
356-
+ self._boundingbox[0]
357-
+ self._anchor_point[0] * self._boundingbox[2]
358-
),
389+
int(self.x + (self._anchor_point[0] * self._boundingbox[2] * self._scale)),
359390
int(
360391
self.y
361-
+ self._boundingbox[1]
362-
+ self._anchor_point[1] * self._boundingbox[3]
392+
+ (self._anchor_point[1] * self._boundingbox[3] * self._scale)
393+
- round((self._boundingbox[3] * self._scale) / 2.0)
363394
),
364395
)
365396

@@ -369,10 +400,10 @@ def anchored_position(self, new_position):
369400
new_position[0]
370401
- self._anchor_point[0] * (self._boundingbox[2] * self._scale)
371402
)
372-
new_y = self.y = int(
403+
new_y = int(
373404
new_position[1]
374-
- self._anchor_point[1] * (self._boundingbox[3] * self._scale)
375-
+ (self._boundingbox[3] * self._scale) / 2
405+
- (self._anchor_point[1] * self._boundingbox[3] * self._scale)
406+
+ round((self._boundingbox[3] * self._scale) / 2.0)
376407
)
377408
self._boundingbox = (new_x, new_y, self._boundingbox[2], self._boundingbox[3])
378409
self.x = new_x

0 commit comments

Comments
 (0)