@@ -112,11 +112,7 @@ 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 (0 , 0 , 1 ), pixel_shader = self ._background_palette
118- )
119- ) # initialize with a blank tilegrid placeholder for background
115+ self ._added_background_tilegrid = False
120116
121117 self ._padding_top = padding_top
122118 self ._padding_bottom = padding_bottom
@@ -160,7 +156,6 @@ def _create_background_box(self, lines, y_offset):
160156 )
161157 y_box_offset = - ascender_max + y_offset - self ._padding_top
162158
163- self ._update_background_color (self ._background_color )
164159 box_width = max (0 , box_width ) # remove any negative values
165160 box_height = max (0 , box_height ) # remove any negative values
166161
@@ -178,15 +173,65 @@ def _update_background_color(self, new_color):
178173
179174 if new_color is None :
180175 self ._background_palette .make_transparent (0 )
176+ if self ._added_background_tilegrid :
177+ self .pop (0 )
178+ self ._added_background_tilegrid = False
181179 else :
182180 self ._background_palette .make_opaque (0 )
183181 self ._background_palette [0 ] = new_color
184182 self ._background_color = new_color
185183
186- 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 (
196+ (len (self ._text ) > 0 )
197+ and (
198+ self ._boundingbox [2 ] + self ._padding_left + self ._padding_right > 0
199+ )
200+ and (
201+ self ._boundingbox [3 ] + self ._padding_top + self ._padding_bottom > 0
202+ )
203+ ):
204+ if len (self ) > 0 :
205+ self .insert (0 , self ._create_background_box (lines , y_offset ))
206+ else :
207+ self .append (self ._create_background_box (lines , y_offset ))
208+ self ._added_background_tilegrid = True
209+
210+ else : # a bitmap is present in the self Group
211+ # update bitmap if text is present and bitmap sizes > 0 pixels
212+ if (
213+ (len (self ._text ) > 0 )
214+ and (
215+ self ._boundingbox [2 ] + self ._padding_left + self ._padding_right > 0
216+ )
217+ and (
218+ self ._boundingbox [3 ] + self ._padding_top + self ._padding_bottom > 0
219+ )
220+ ):
221+ self [0 ] = self ._create_background_box (lines , y_offset )
222+ else : # delete the existing bitmap
223+ self .pop (0 )
224+ self ._added_background_tilegrid = False
225+
226+ def _update_text (
227+ self , new_text
228+ ): # pylint: disable=too-many-locals ,too-many-branches, too-many-statements
187229 x = 0
188230 y = 0
189- i = 1
231+ if self ._added_background_tilegrid :
232+ i = 1
233+ else :
234+ i = 0
190235 old_c = 0
191236 y_offset = int (
192237 (
@@ -268,7 +313,8 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
268313 self .pop ()
269314 self ._text = new_text
270315 self ._boundingbox = (left , top , left + right , bottom - top )
271- self [0 ] = self ._create_background_box (lines , y_offset )
316+
317+ self ._update_background_color (self ._background_color )
272318
273319 @property
274320 def bounding_box (self ):
@@ -351,15 +397,11 @@ def anchored_position(self):
351397 """Position relative to the anchor_point. Tuple containing x,y
352398 pixel coordinates."""
353399 return (
354- int (
355- self .x
356- + self ._boundingbox [0 ]
357- + self ._anchor_point [0 ] * self ._boundingbox [2 ]
358- ),
400+ int (self .x + (self ._anchor_point [0 ] * self ._boundingbox [2 ] * self ._scale )),
359401 int (
360402 self .y
361- + self ._boundingbox [1 ]
362- + self ._anchor_point [ 1 ] * self ._boundingbox [ 3 ]
403+ + ( self ._anchor_point [1 ] * self . _boundingbox [ 3 ] * self . _scale )
404+ - round (( self ._boundingbox [ 3 ] * self ._scale ) / 2.0 )
363405 ),
364406 )
365407
@@ -369,11 +411,10 @@ def anchored_position(self, new_position):
369411 new_position [0 ]
370412 - self ._anchor_point [0 ] * (self ._boundingbox [2 ] * self ._scale )
371413 )
372- new_y = self . y = int (
414+ new_y = int (
373415 new_position [1 ]
374- - self ._anchor_point [1 ] * ( self ._boundingbox [3 ] * self ._scale )
375- + ( self ._boundingbox [3 ] * self ._scale ) / 2
416+ - ( self ._anchor_point [1 ] * self ._boundingbox [3 ] * self ._scale )
417+ + round (( self ._boundingbox [3 ] * self ._scale ) / 2.0 )
376418 )
377- self ._boundingbox = (new_x , new_y , self ._boundingbox [2 ], self ._boundingbox [3 ])
378419 self .x = new_x
379420 self .y = new_y
0 commit comments