Skip to content

Commit 51e13b2

Browse files
committed
fix issue with background being shifted from text. only create bitmap if it will get used.
1 parent 2d65dc0 commit 51e13b2

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

adafruit_display_text/label.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,14 @@ def __init__(
112112

113113
self._background_color = background_color
114114
self._background_palette = displayio.Palette(1)
115-
self.append(
116-
displayio.TileGrid(
117-
displayio.Bitmap(1, 1, 1), pixel_shader=self._background_palette
118-
)
119-
) # initialize with a blank tilegrid placeholder for background
115+
self._added_background_tilegrid = False
116+
if self._background_color:
117+
self.append(
118+
displayio.TileGrid(
119+
displayio.Bitmap(1, 1, 1), pixel_shader=self._background_palette
120+
)
121+
) # initialize with a blank tilegrid placeholder for background
122+
self._added_background_tilegrid = True
120123

121124
self._padding_top = padding_top
122125
self._padding_bottom = padding_bottom
@@ -160,7 +163,6 @@ def _create_background_box(self, lines, y_offset):
160163
)
161164
y_box_offset = -ascender_max + y_offset - self._padding_top
162165

163-
self._update_background_color(self._background_color)
164166
box_width = max(0, box_width) # remove any negative values
165167
box_height = max(0, box_height) # remove any negative values
166168

@@ -171,6 +173,7 @@ def _create_background_box(self, lines, y_offset):
171173
x=left + x_box_offset,
172174
y=y_box_offset,
173175
)
176+
174177
return tile_grid
175178

176179
def _update_background_color(self, new_color):
@@ -182,10 +185,28 @@ def _update_background_color(self, new_color):
182185
self._background_palette[0] = new_color
183186
self._background_color = new_color
184187

185-
def _update_text(self, new_text): # pylint: disable=too-many-locals
188+
y_offset = int(
189+
(
190+
self._font.get_glyph(ord("M")).height
191+
- self.text.count("\n") * self.height * self.line_spacing
192+
)
193+
/ 2
194+
)
195+
lines = self.text.count("\n") + 1
196+
if not self._added_background_tilegrid:
197+
198+
self._added_background_tilegrid = True
199+
self.insert(0, self._create_background_box(lines, y_offset))
200+
else:
201+
self[0] = self._create_background_box(lines, y_offset)
202+
203+
def _update_text(self, new_text): # pylint: disable=too-many-locals ,too-many-branches, too-many-statements
186204
x = 0
187205
y = 0
188-
i = 1
206+
if self._added_background_tilegrid:
207+
i = 1
208+
else:
209+
i = 0
189210
old_c = 0
190211
y_offset = int(
191212
(
@@ -267,7 +288,16 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
267288
self.pop()
268289
self._text = new_text
269290
self._boundingbox = (left, top, left + right, bottom - top)
270-
self[0] = self._create_background_box(lines, y_offset)
291+
if (
292+
self._background_color
293+
and len(new_text) + self._padding_left + self._padding_right > 0
294+
):
295+
if not self._added_background_tilegrid:
296+
297+
self._added_background_tilegrid = True
298+
self.insert(0, self._create_background_box(lines, y_offset))
299+
else:
300+
self[0] = self._create_background_box(lines, y_offset)
271301

272302
@property
273303
def bounding_box(self):
@@ -369,6 +399,5 @@ def anchored_position(self, new_position):
369399
- (self._anchor_point[1] * self._boundingbox[3] * self._scale)
370400
+ round((self._boundingbox[3] * self._scale) / 2.0)
371401
)
372-
self._boundingbox = (new_x, new_y, self._boundingbox[2], self._boundingbox[3])
373402
self.x = new_x
374403
self.y = new_y

0 commit comments

Comments
 (0)