@@ -62,22 +62,22 @@ class Label(displayio.Group):
62
62
# This has a lot of getters/setters, maybe it needs cleanup.
63
63
64
64
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
81
81
):
82
82
if "scale" in kwargs .keys ():
83
83
self ._scale = kwargs ["scale" ]
@@ -89,6 +89,7 @@ def __init__(
89
89
max_glyphs = len (text )
90
90
# add one to max_size for the background bitmap tileGrid
91
91
super ().__init__ (max_size = max_glyphs + 1 , ** kwargs )
92
+ print ("max_size={}" .format (max_glyphs + 1 ))
92
93
93
94
self .width = max_glyphs
94
95
self ._font = font
@@ -112,11 +113,15 @@ def __init__(
112
113
113
114
self ._background_color = background_color
114
115
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
116
+ self ._added_background_tilegrid = False
117
+ if self ._background_color :
118
+ print ("adding background tilegrid from init" )
119
+ self .append (
120
+ displayio .TileGrid (
121
+ displayio .Bitmap (1 , 1 , 1 ), pixel_shader = self ._background_palette
122
+ )
123
+ ) # initialize with a blank tilegrid placeholder for background
124
+ self ._added_background_tilegrid = True
120
125
121
126
self ._padding_top = padding_top
122
127
self ._padding_bottom = padding_bottom
@@ -153,14 +158,14 @@ def _create_background_box(self, lines, y_offset):
153
158
box_width = self ._boundingbox [2 ] + self ._padding_left + self ._padding_right
154
159
x_box_offset = - self ._padding_left
155
160
box_height = (
156
- (ascender_max + descender_max )
157
- + int ((lines - 1 ) * self .height * self ._line_spacing )
158
- + self ._padding_top
159
- + self ._padding_bottom
161
+ (ascender_max + descender_max )
162
+ + int ((lines - 1 ) * self .height * self ._line_spacing )
163
+ + self ._padding_top
164
+ + self ._padding_bottom
160
165
)
161
166
y_box_offset = - ascender_max + y_offset - self ._padding_top
162
167
163
- self ._update_background_color (self ._background_color )
168
+ # self._update_background_color(self._background_color)
164
169
box_width = max (0 , box_width ) # remove any negative values
165
170
box_height = max (0 , box_height ) # remove any negative values
166
171
@@ -171,27 +176,47 @@ def _create_background_box(self, lines, y_offset):
171
176
x = left + x_box_offset ,
172
177
y = y_box_offset ,
173
178
)
174
-
175
179
return tile_grid
176
180
177
181
def _update_background_color (self , new_color ):
178
182
179
183
if new_color is None :
184
+ print ("setting {} transparent" .format (self ._background_palette [0 ]))
180
185
self ._background_palette .make_transparent (0 )
181
186
else :
182
187
self ._background_palette .make_opaque (0 )
183
188
self ._background_palette [0 ] = new_color
184
189
self ._background_color = new_color
185
190
191
+ y_offset = int (
192
+ (
193
+ self ._font .get_glyph (ord ("M" )).height
194
+ - self .text .count ("\n " ) * self .height * self .line_spacing
195
+ )
196
+ / 2
197
+ )
198
+ lines = self .text .count ("\n " ) + 1
199
+ if not self ._added_background_tilegrid :
200
+
201
+ self ._added_background_tilegrid = True
202
+ print ("adding background tilegrid in update background color" )
203
+ print ("len self = {}" .format (len (self )))
204
+ self .insert (0 , self ._create_background_box (lines , y_offset ))
205
+ else :
206
+ self [0 ] = self ._create_background_box (lines , y_offset )
207
+
186
208
def _update_text (self , new_text ): # pylint: disable=too-many-locals
187
209
x = 0
188
210
y = 0
189
- i = 1
211
+ if self ._added_background_tilegrid :
212
+ i = 1
213
+ else :
214
+ i = 0
190
215
old_c = 0
191
216
y_offset = int (
192
217
(
193
- self ._font .get_glyph (ord ("M" )).height
194
- - new_text .count ("\n " ) * self .height * self .line_spacing
218
+ self ._font .get_glyph (ord ("M" )).height
219
+ - new_text .count ("\n " ) * self .height * self .line_spacing
195
220
)
196
221
/ 2
197
222
)
@@ -213,9 +238,9 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
213
238
position_y = y - glyph .height - glyph .dy + y_offset
214
239
position_x = x + glyph .dx
215
240
if (
216
- not self ._text
217
- or old_c >= len (self ._text )
218
- or character != self ._text [old_c ]
241
+ not self ._text
242
+ or old_c >= len (self ._text )
243
+ or character != self ._text [old_c ]
219
244
):
220
245
try :
221
246
# pylint: disable=unexpected-keyword-arg
@@ -240,6 +265,7 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
240
265
if i < len (self ):
241
266
self [i ] = face
242
267
else :
268
+ print ("appending face" )
243
269
self .append (face )
244
270
elif self ._text and character == self ._text [old_c ]:
245
271
@@ -255,20 +281,29 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
255
281
old_c += 1
256
282
# skip all non-printables in the old string
257
283
while (
258
- self ._text
259
- and old_c < len (self ._text )
260
- and (
261
- self ._text [old_c ] == "\n "
262
- or not self ._font .get_glyph (ord (self ._text [old_c ]))
263
- )
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
+ )
264
290
):
265
291
old_c += 1
266
292
# Remove the rest
267
293
while len (self ) > i :
268
294
self .pop ()
269
295
self ._text = new_text
270
296
self ._boundingbox = (left , top , left + right , bottom - top )
271
- self [0 ] = self ._create_background_box (lines , y_offset )
297
+ print ("bg color: {}" .format (self ._background_color ))
298
+ if self ._background_color and len (new_text ) + self ._padding_left + self ._padding_right > 0 :
299
+ if not self ._added_background_tilegrid :
300
+
301
+ self ._added_background_tilegrid = True
302
+ print ("adding background tilegrid" )
303
+ print ("len self = {}" .format (len (self )))
304
+ self .insert (0 , self ._create_background_box (lines , y_offset ))
305
+ else :
306
+ self [0 ] = self ._create_background_box (lines , y_offset )
272
307
273
308
@property
274
309
def bounding_box (self ):
@@ -351,15 +386,11 @@ def anchored_position(self):
351
386
"""Position relative to the anchor_point. Tuple containing x,y
352
387
pixel coordinates."""
353
388
return (
354
- int (
355
- self .x
356
- + self ._boundingbox [0 ]
357
- + self ._anchor_point [0 ] * self ._boundingbox [2 ]
358
- ),
389
+ int (self .x + (self ._anchor_point [0 ] * self ._boundingbox [2 ] * self ._scale )),
359
390
int (
360
391
self .y
361
- + self ._boundingbox [1 ]
362
- + self ._anchor_point [ 1 ] * self ._boundingbox [ 3 ]
392
+ + ( self ._anchor_point [1 ] * self . _boundingbox [ 3 ] * self . _scale )
393
+ - round (( self ._boundingbox [ 3 ] * self ._scale ) / 2.0 )
363
394
),
364
395
)
365
396
@@ -369,10 +400,10 @@ def anchored_position(self, new_position):
369
400
new_position [0 ]
370
401
- self ._anchor_point [0 ] * (self ._boundingbox [2 ] * self ._scale )
371
402
)
372
- new_y = self . y = int (
403
+ new_y = int (
373
404
new_position [1 ]
374
- - self ._anchor_point [1 ] * ( self ._boundingbox [3 ] * self ._scale )
375
- + ( self ._boundingbox [3 ] * self ._scale ) / 2
405
+ - ( self ._anchor_point [1 ] * self ._boundingbox [3 ] * self ._scale )
406
+ + round (( self ._boundingbox [3 ] * self ._scale ) / 2.0 )
376
407
)
377
408
self ._boundingbox = (new_x , new_y , self ._boundingbox [2 ], self ._boundingbox [3 ])
378
409
self .x = new_x
0 commit comments