Skip to content

Commit 8acf265

Browse files
committed
fix issue with background being shifted from text. Clean up debugging prints
1 parent 088663b commit 8acf265

File tree

1 file changed

+39
-46
lines changed

1 file changed

+39
-46
lines changed

adafruit_display_text/label.py

Lines changed: 39 additions & 46 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,7 +89,6 @@ 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))
9392

9493
self.width = max_glyphs
9594
self._font = font
@@ -115,7 +114,6 @@ def __init__(
115114
self._background_palette = displayio.Palette(1)
116115
self._added_background_tilegrid = False
117116
if self._background_color:
118-
print("adding background tilegrid from init")
119117
self.append(
120118
displayio.TileGrid(
121119
displayio.Bitmap(1, 1, 1), pixel_shader=self._background_palette
@@ -158,14 +156,13 @@ def _create_background_box(self, lines, y_offset):
158156
box_width = self._boundingbox[2] + self._padding_left + self._padding_right
159157
x_box_offset = -self._padding_left
160158
box_height = (
161-
(ascender_max + descender_max)
162-
+ int((lines - 1) * self.height * self._line_spacing)
163-
+ self._padding_top
164-
+ self._padding_bottom
159+
(ascender_max + descender_max)
160+
+ int((lines - 1) * self.height * self._line_spacing)
161+
+ self._padding_top
162+
+ self._padding_bottom
165163
)
166164
y_box_offset = -ascender_max + y_offset - self._padding_top
167165

168-
# self._update_background_color(self._background_color)
169166
box_width = max(0, box_width) # remove any negative values
170167
box_height = max(0, box_height) # remove any negative values
171168

@@ -176,12 +173,12 @@ def _create_background_box(self, lines, y_offset):
176173
x=left + x_box_offset,
177174
y=y_box_offset,
178175
)
176+
179177
return tile_grid
180178

181179
def _update_background_color(self, new_color):
182180

183181
if new_color is None:
184-
print("setting {} transparent".format(self._background_palette[0]))
185182
self._background_palette.make_transparent(0)
186183
else:
187184
self._background_palette.make_opaque(0)
@@ -190,22 +187,20 @@ def _update_background_color(self, new_color):
190187

191188
y_offset = int(
192189
(
193-
self._font.get_glyph(ord("M")).height
194-
- self.text.count("\n") * self.height * self.line_spacing
190+
self._font.get_glyph(ord("M")).height
191+
- self.text.count("\n") * self.height * self.line_spacing
195192
)
196193
/ 2
197194
)
198195
lines = self.text.count("\n") + 1
199196
if not self._added_background_tilegrid:
200197

201198
self._added_background_tilegrid = True
202-
print("adding background tilegrid in update background color")
203-
print("len self = {}".format(len(self)))
204199
self.insert(0, self._create_background_box(lines, y_offset))
205200
else:
206201
self[0] = self._create_background_box(lines, y_offset)
207202

208-
def _update_text(self, new_text): # pylint: disable=too-many-locals
203+
def _update_text(self, new_text): # pylint: disable=too-many-locals ,too-many-branches, too-many-statements
209204
x = 0
210205
y = 0
211206
if self._added_background_tilegrid:
@@ -215,8 +210,8 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
215210
old_c = 0
216211
y_offset = int(
217212
(
218-
self._font.get_glyph(ord("M")).height
219-
- new_text.count("\n") * self.height * self.line_spacing
213+
self._font.get_glyph(ord("M")).height
214+
- new_text.count("\n") * self.height * self.line_spacing
220215
)
221216
/ 2
222217
)
@@ -238,9 +233,9 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
238233
position_y = y - glyph.height - glyph.dy + y_offset
239234
position_x = x + glyph.dx
240235
if (
241-
not self._text
242-
or old_c >= len(self._text)
243-
or character != self._text[old_c]
236+
not self._text
237+
or old_c >= len(self._text)
238+
or character != self._text[old_c]
244239
):
245240
try:
246241
# pylint: disable=unexpected-keyword-arg
@@ -265,7 +260,6 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
265260
if i < len(self):
266261
self[i] = face
267262
else:
268-
print("appending face")
269263
self.append(face)
270264
elif self._text and character == self._text[old_c]:
271265

@@ -281,26 +275,26 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
281275
old_c += 1
282276
# skip all non-printables in the old string
283277
while (
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-
)
278+
self._text
279+
and old_c < len(self._text)
280+
and (
281+
self._text[old_c] == "\n"
282+
or not self._font.get_glyph(ord(self._text[old_c]))
283+
)
290284
):
291285
old_c += 1
292286
# Remove the rest
293287
while len(self) > i:
294288
self.pop()
295289
self._text = new_text
296290
self._boundingbox = (left, top, left + right, bottom - top)
297-
print("bg color: {}".format(self._background_color))
298-
if self._background_color and len(new_text) + self._padding_left + self._padding_right > 0:
291+
if (
292+
self._background_color
293+
and len(new_text) + self._padding_left + self._padding_right > 0
294+
):
299295
if not self._added_background_tilegrid:
300296

301297
self._added_background_tilegrid = True
302-
print("adding background tilegrid")
303-
print("len self = {}".format(len(self)))
304298
self.insert(0, self._create_background_box(lines, y_offset))
305299
else:
306300
self[0] = self._create_background_box(lines, y_offset)
@@ -405,6 +399,5 @@ def anchored_position(self, new_position):
405399
- (self._anchor_point[1] * self._boundingbox[3] * self._scale)
406400
+ round((self._boundingbox[3] * self._scale) / 2.0)
407401
)
408-
self._boundingbox = (new_x, new_y, self._boundingbox[2], self._boundingbox[3])
409402
self.x = new_x
410403
self.y = new_y

0 commit comments

Comments
 (0)