@@ -112,11 +112,7 @@ def __init__(
112
112
113
113
self ._background_color = background_color
114
114
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
120
116
121
117
self ._padding_top = padding_top
122
118
self ._padding_bottom = padding_bottom
@@ -160,7 +156,6 @@ def _create_background_box(self, lines, y_offset):
160
156
)
161
157
y_box_offset = - ascender_max + y_offset - self ._padding_top
162
158
163
- self ._update_background_color (self ._background_color )
164
159
box_width = max (0 , box_width ) # remove any negative values
165
160
box_height = max (0 , box_height ) # remove any negative values
166
161
@@ -171,21 +166,59 @@ def _create_background_box(self, lines, y_offset):
171
166
x = left + x_box_offset ,
172
167
y = y_box_offset ,
173
168
)
169
+
174
170
return tile_grid
175
171
176
172
def _update_background_color (self , new_color ):
177
173
178
174
if new_color is None :
179
175
self ._background_palette .make_transparent (0 )
176
+ if self ._added_background_tilegrid :
177
+ self .pop (0 )
178
+ self ._added_background_tilegrid = False
180
179
else :
181
180
self ._background_palette .make_opaque (0 )
182
181
self ._background_palette [0 ] = new_color
183
182
self ._background_color = new_color
184
183
185
- def _update_text (self , new_text ): # pylint: disable=too-many-locals
184
+ y_offset = int (
185
+ (
186
+ self ._font .get_glyph (ord ("M" )).height
187
+ - self .text .count ("\n " ) * self .height * self .line_spacing
188
+ )
189
+ / 2
190
+ )
191
+ lines = self .text .count ("\n " ) + 1
192
+
193
+ if not self ._added_background_tilegrid : # no bitmap is in the self Group
194
+ # add bitmap if text is present and bitmap sizes > 0 pixels
195
+ if ((len (self ._text ) > 0 )
196
+ and (self ._boundingbox [2 ] + self ._padding_left + self ._padding_right > 0 )
197
+ and (self ._boundingbox [3 ] + self ._padding_top + self ._padding_bottom > 0 )):
198
+ if len (self ) > 0 :
199
+ self .insert (0 , self ._create_background_box (lines , y_offset ))
200
+ else :
201
+ self .append (self ._create_background_box (lines , y_offset ))
202
+ self ._added_background_tilegrid = True
203
+ else : # a bitmap is present in the self Group
204
+ # update bitmap if text is present and bitmap sizes > 0 pixels
205
+ if ((len (self ._text ) > 0 )
206
+ and (self ._boundingbox [2 ] + self ._padding_left + self ._padding_right > 0 )
207
+ and (self ._boundingbox [3 ] + self ._padding_top + self ._padding_bottom > 0 )):
208
+ self [0 ] = self ._create_background_box (lines , y_offset )
209
+ else : # delete the existing bitmap
210
+ self .pop (0 )
211
+ self ._added_background_tilegrid = False
212
+
213
+ def _update_text (
214
+ self , new_text
215
+ ): # pylint: disable=too-many-locals ,too-many-branches, too-many-statements
186
216
x = 0
187
217
y = 0
188
- i = 1
218
+ if self ._added_background_tilegrid :
219
+ i = 1
220
+ else :
221
+ i = 0
189
222
old_c = 0
190
223
y_offset = int (
191
224
(
@@ -267,7 +300,7 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
267
300
self .pop ()
268
301
self ._text = new_text
269
302
self ._boundingbox = (left , top , left + right , bottom - top )
270
- self [ 0 ] = self . _create_background_box ( lines , y_offset )
303
+ self . _update_background_color ( self . _background_color )
271
304
272
305
@property
273
306
def bounding_box (self ):
@@ -369,6 +402,5 @@ def anchored_position(self, new_position):
369
402
- (self ._anchor_point [1 ] * self ._boundingbox [3 ] * self ._scale )
370
403
+ round ((self ._boundingbox [3 ] * self ._scale ) / 2.0 )
371
404
)
372
- self ._boundingbox = (new_x , new_y , self ._boundingbox [2 ], self ._boundingbox [3 ])
373
405
self .x = new_x
374
406
self .y = new_y
0 commit comments