@@ -98,8 +98,8 @@ def __init__(
98
98
x = 0 ,
99
99
y = 0 ,
100
100
text = "" ,
101
- max_glyphs = None , # This input parameter is ignored, only present for compatibility
102
- # with label.py
101
+ max_glyphs = None , # This input parameter is ignored, only present for compatibility
102
+ # with label.py
103
103
color = 0xFFFFFF ,
104
104
background_color = None ,
105
105
line_spacing = 1.25 ,
@@ -114,22 +114,31 @@ def __init__(
114
114
** kwargs
115
115
):
116
116
117
-
118
117
# Scale will be passed to Group using kwargs.
119
118
if "scale" in kwargs .keys ():
120
119
scale = kwargs ["scale" ]
121
- kwargs .pop ("scale" ) # Do not change scale of self Group, use this value to set scale of local_group
120
+ kwargs .pop (
121
+ "scale"
122
+ ) # Do not change scale of self Group, use this value to set scale of
123
+ # local_group
122
124
123
125
# instance the Group
124
- # self Group will contain a single local_group which contains one TileGrid which contains the text bitmap
126
+ # self Group will contain a single local_group which contains one TileGrid which contains
127
+ # the text bitmap
125
128
super ().__init__ (
126
129
max_size = 1 , x = x , y = y , ** kwargs
127
130
) # this will include any arguments, including scale
128
131
129
- self .local_group = displayio .Group (max_size = 1 , ** kwargs ) # local_group holds the tileGrid and sets the scaling
130
- self .append (self .local_group ) # the local_group will always stay in the self Group
132
+ self .local_group = displayio .Group (
133
+ max_size = 1 , ** kwargs
134
+ ) # local_group holds the tileGrid and
135
+ # sets the scaling
136
+ self .append (
137
+ self .local_group
138
+ ) # the local_group will always stay in the self Group
131
139
132
140
self ._font = font
141
+ self ._text = text
133
142
134
143
# Create the two-color palette
135
144
self .palette = displayio .Palette (2 )
@@ -140,23 +149,23 @@ def __init__(
140
149
self ._anchored_position = anchored_position
141
150
142
151
# call the text updater with all the arguments.
143
- self ._reset_text (font = font ,
144
- x = x ,
145
- y = y ,
146
- text = text ,
147
- line_spacing = line_spacing ,
148
- background_tight = background_tight ,
149
- padding_top = padding_top ,
150
- padding_bottom = padding_bottom ,
151
- padding_left = padding_left ,
152
- padding_right = padding_right ,
153
- anchor_point = anchor_point ,
154
- anchored_position = anchored_position ,
155
- save_text = save_text ,
156
- scale = scale ,
157
- ** kwargs ,
158
- )
159
-
152
+ self ._reset_text (
153
+ font = font ,
154
+ x = x ,
155
+ y = y ,
156
+ text = text ,
157
+ line_spacing = line_spacing ,
158
+ background_tight = background_tight ,
159
+ padding_top = padding_top ,
160
+ padding_bottom = padding_bottom ,
161
+ padding_left = padding_left ,
162
+ padding_right = padding_right ,
163
+ anchor_point = anchor_point ,
164
+ anchored_position = anchored_position ,
165
+ save_text = save_text ,
166
+ scale = scale ,
167
+ ** kwargs ,
168
+ )
160
169
161
170
def _reset_text (
162
171
self ,
@@ -175,7 +184,7 @@ def _reset_text(
175
184
save_text = None ,
176
185
scale = None ,
177
186
** kwargs
178
- ):
187
+ ):
179
188
180
189
# Store all the instance variables
181
190
if font is not None :
@@ -202,8 +211,10 @@ def _reset_text(
202
211
self ._anchored_position = anchored_position
203
212
if save_text is not None :
204
213
self ._save_text = save_text
205
- if scale is not None : # Scale will be defined in local_group (Note: self should have scale=1)
206
- self .scale = scale # call the setter
214
+ if (
215
+ scale is not None
216
+ ): # Scale will be defined in local_group (Note: self should have scale=1)
217
+ self .scale = scale # call the setter
207
218
208
219
# if text is not provided as a parameter (text is None), use the previous value.
209
220
if (text is None ) and self ._save_text :
@@ -214,22 +225,23 @@ def _reset_text(
214
225
else :
215
226
self ._text = None # save a None value since text string is not saved
216
227
217
-
218
228
# Check for empty string
219
- if (text == "" ) or (text is None ): # If empty string, just create a zero-sized bounding box and that's it.
229
+ if (text == "" ) or (
230
+ text is None
231
+ ): # If empty string, just create a zero-sized bounding box and that's it.
220
232
221
233
self ._bounding_box = (
222
- 0 ,
223
- 0 ,
224
- 0 , # zero width with text == ""
225
- 0 , # zero height with text == ""
226
- )
234
+ 0 ,
235
+ 0 ,
236
+ 0 , # zero width with text == ""
237
+ 0 , # zero height with text == ""
238
+ )
227
239
# Clear out any items in the self Group, in case this is an update to the bitmap_label
228
- for item in self :
240
+ for _ in self :
229
241
self .pop (0 )
230
242
231
- else : # The text string is not empty, so create the Bitmap and TileGrid and append to the self Group
232
-
243
+ else : # The text string is not empty, so create the Bitmap and TileGrid and
244
+ # append to the self Group
233
245
234
246
# Calculate the text bounding box
235
247
@@ -241,19 +253,19 @@ def _reset_text(
241
253
x_offset ,
242
254
tight_y_offset ,
243
255
loose_box_y ,
244
- loose_y_offset
256
+ loose_y_offset ,
245
257
) = self ._text_bounding_box (
246
258
text , self ._font , self ._line_spacing ,
247
- ) # calculate the box size for a tight and loose backgrounds
259
+ ) # calculate the box size for a tight and loose backgrounds
248
260
249
- if self ._background_tight :
261
+ if self ._background_tight :
250
262
box_y = tight_box_y
251
263
y_offset = tight_y_offset
252
264
253
- else : # calculate the box size for a loose background
265
+ else : # calculate the box size for a loose background
254
266
box_y = loose_box_y
255
267
y_offset = loose_y_offset
256
-
268
+
257
269
# Calculate the background size including padding
258
270
box_x = box_x + self ._padding_left + self ._padding_right
259
271
box_y = box_y + self ._padding_top + self ._padding_bottom
@@ -287,10 +299,13 @@ def _reset_text(
287
299
y = label_position_yoffset - y_offset - self ._padding_top ,
288
300
)
289
301
290
- # Clear out any items in the local_group Group, in case this is an update to the bitmap_label
291
- for item in self .local_group :
302
+ # Clear out any items in the local_group Group, in case this is an update to
303
+ # the bitmap_label
304
+ for _ in self .local_group :
292
305
self .local_group .pop (0 )
293
- self .local_group .append (self .tilegrid ) # add the bitmap's tilegrid to the group
306
+ self .local_group .append (
307
+ self .tilegrid
308
+ ) # add the bitmap's tilegrid to the group
294
309
295
310
# Update bounding_box values. Note: To be consistent with label.py,
296
311
# this is the bounding box for the text only, not including the background.
@@ -303,7 +318,8 @@ def _reset_text(
303
318
304
319
self .anchored_position = (
305
320
self ._anchored_position
306
- ) # set the anchored_position with setter after bitmap is created, sets the x,y positions of the label
321
+ ) # set the anchored_position with setter after bitmap is created, sets the
322
+ # x,y positions of the label
307
323
308
324
@staticmethod
309
325
def _line_spacing_ypixels (font , line_spacing ):
@@ -383,18 +399,24 @@ def _text_bounding_box(self, text, font, line_spacing):
383
399
384
400
final_box_width = right - left
385
401
386
-
387
402
final_box_height_tight = bottom - top
388
403
final_y_offset_tight = - top + y_offset_tight
389
404
390
405
final_box_height_loose = (lines - 1 ) * self ._line_spacing_ypixels (
391
- font , line_spacing
392
- ) + (ascender_max + descender_max )
406
+ font , line_spacing
407
+ ) + (ascender_max + descender_max )
393
408
final_y_offset_loose = ascender_max
394
409
395
410
# return (final_box_width, final_box_height, left, final_y_offset)
396
411
397
- return (final_box_width , final_box_height_tight , left , final_y_offset_tight , final_box_height_loose , final_y_offset_loose )
412
+ return (
413
+ final_box_width ,
414
+ final_box_height_tight ,
415
+ left ,
416
+ final_y_offset_tight ,
417
+ final_box_height_loose ,
418
+ final_y_offset_loose ,
419
+ )
398
420
399
421
# pylint: disable=too-many-nested-blocks
400
422
def _place_text (
@@ -459,7 +481,7 @@ def _place_text(
459
481
glyph_offset_x = (
460
482
my_glyph .tile_index * my_glyph .width
461
483
) # for type BuiltinFont, this creates the x-offset in the glyph bitmap.
462
- # for BDF loaded fonts, this should equal 0
484
+ # for BDF loaded fonts, this should equal 0
463
485
464
486
try :
465
487
bitmap .blit (
@@ -470,14 +492,16 @@ def _place_text(
470
492
y1 = 0 ,
471
493
x2 = glyph_offset_x + my_glyph .width ,
472
494
y2 = 0 + my_glyph .height ,
473
- skip_index = 0 , # do not copy over any 0 background pixels
495
+ skip_index = 0 , # do not copy over any 0 background pixels
474
496
)
475
497
476
498
except :
477
499
for y in range (my_glyph .height ):
478
500
for x in range (my_glyph .width ):
479
501
x_placement = x + xposition + my_glyph .dx
480
- y_placement = y + yposition - my_glyph .height - my_glyph .dy
502
+ y_placement = (
503
+ y + yposition - my_glyph .height - my_glyph .dy
504
+ )
481
505
482
506
if (bitmap_width > x_placement >= 0 ) and (
483
507
bitmap_height > y_placement >= 0
@@ -492,7 +516,9 @@ def _place_text(
492
516
493
517
this_pixel_color = palette_indexes [
494
518
my_glyph .bitmap [
495
- y * my_glyph .bitmap .width + x + glyph_offset_x
519
+ y * my_glyph .bitmap .width
520
+ + x
521
+ + glyph_offset_x
496
522
]
497
523
]
498
524
@@ -519,12 +545,12 @@ def bounding_box(self):
519
545
def scale (self ):
520
546
"""Set the scaling of the label"""
521
547
return self ._scale
522
-
548
+
523
549
@scale .setter
524
550
def scale (self , new_scale ):
525
- self .local_group .scale = new_scale
526
- self ._scale = new_scale
527
- self .anchored_position = self ._anchored_position # update the anchored_position
551
+ self .local_group .scale = new_scale
552
+ self ._scale = new_scale
553
+ self .anchored_position = self ._anchored_position # update the anchored_position
528
554
529
555
@property
530
556
def line_spacing (self ):
@@ -539,7 +565,6 @@ def line_spacing(self, new_line_spacing):
539
565
else :
540
566
raise RuntimeError ("line_spacing is immutable when save_text is False" )
541
567
542
-
543
568
@property
544
569
def color (self ):
545
570
"""Color of the text as an RGB hex number."""
@@ -575,17 +600,18 @@ def text(self):
575
600
"""Text to displayed."""
576
601
return self ._text
577
602
578
- @text .setter # Cannot set color or background color with text setter, use separate setter
603
+ @text .setter # Cannot set color or background color with text setter, use separate setter
579
604
def text (self , new_text ):
580
605
self ._reset_text (text = new_text )
581
606
582
607
@property
583
608
def font (self ):
584
609
"""Font to use for text display."""
585
- return self .font
610
+ return self ._font
586
611
587
612
@font .setter
588
613
def font (self , new_font ):
614
+ self ._font = new_font
589
615
if self ._save_text :
590
616
self ._reset_text (font = new_font )
591
617
else :
0 commit comments