@@ -112,11 +112,14 @@ 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
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
120
123
121
124
self ._padding_top = padding_top
122
125
self ._padding_bottom = padding_bottom
@@ -160,7 +163,6 @@ def _create_background_box(self, lines, y_offset):
160
163
)
161
164
y_box_offset = - ascender_max + y_offset - self ._padding_top
162
165
163
- self ._update_background_color (self ._background_color )
164
166
box_width = max (0 , box_width ) # remove any negative values
165
167
box_height = max (0 , box_height ) # remove any negative values
166
168
@@ -171,6 +173,7 @@ def _create_background_box(self, lines, y_offset):
171
173
x = left + x_box_offset ,
172
174
y = y_box_offset ,
173
175
)
176
+
174
177
return tile_grid
175
178
176
179
def _update_background_color (self , new_color ):
@@ -182,10 +185,28 @@ def _update_background_color(self, new_color):
182
185
self ._background_palette [0 ] = new_color
183
186
self ._background_color = new_color
184
187
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
186
204
x = 0
187
205
y = 0
188
- i = 1
206
+ if self ._added_background_tilegrid :
207
+ i = 1
208
+ else :
209
+ i = 0
189
210
old_c = 0
190
211
y_offset = int (
191
212
(
@@ -267,7 +288,16 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
267
288
self .pop ()
268
289
self ._text = new_text
269
290
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 )
271
301
272
302
@property
273
303
def bounding_box (self ):
@@ -369,6 +399,5 @@ def anchored_position(self, new_position):
369
399
- (self ._anchor_point [1 ] * self ._boundingbox [3 ] * self ._scale )
370
400
+ round ((self ._boundingbox [3 ] * self ._scale ) / 2.0 )
371
401
)
372
- self ._boundingbox = (new_x , new_y , self ._boundingbox [2 ], self ._boundingbox [3 ])
373
402
self .x = new_x
374
403
self .y = new_y
0 commit comments