31
31
except ImportError :
32
32
pass
33
33
34
- import displayio
34
+ from displayio import Bitmap , Palette , TileGrid
35
35
36
36
from adafruit_display_text import LabelBase
37
37
@@ -81,17 +81,13 @@ class Label(LabelBase):
81
81
# This has a lot of getters/setters, maybe it needs cleanup.
82
82
83
83
def __init__ (self , font , ** kwargs ) -> None :
84
- self ._background_palette = displayio . Palette (1 )
84
+ self ._background_palette = Palette (1 )
85
85
self ._added_background_tilegrid = False
86
86
87
87
super ().__init__ (font , ** kwargs )
88
88
89
89
text = self ._replace_tabs (self ._text )
90
90
91
- # local_group will set the scale
92
- self .local_group = displayio .Group (scale = kwargs .get ("scale" , 1 ))
93
- self .append (self .local_group )
94
-
95
91
self .width = len (text )
96
92
self ._font = font
97
93
self ._text = None
@@ -106,7 +102,7 @@ def __init__(self, font, **kwargs) -> None:
106
102
if text is not None :
107
103
self ._reset_text (str (text ))
108
104
109
- def _create_background_box (self , lines : int , y_offset : int ) -> None :
105
+ def _create_background_box (self , lines : int , y_offset : int ) -> TileGrid :
110
106
"""Private Class function to create a background_box
111
107
:param lines: int number of lines
112
108
:param y_offset: int y pixel bottom coordinate for the background_box"""
@@ -165,8 +161,8 @@ def _create_background_box(self, lines: int, y_offset: int) -> None:
165
161
movx = left + x_box_offset
166
162
movy = y_box_offset
167
163
168
- background_bitmap = displayio . Bitmap (box_width , box_height , 1 )
169
- tile_grid = displayio . TileGrid (
164
+ background_bitmap = Bitmap (box_width , box_height , 1 )
165
+ tile_grid = TileGrid (
170
166
background_bitmap ,
171
167
pixel_shader = self ._background_palette ,
172
168
x = movx ,
@@ -182,7 +178,7 @@ def _set_background_color(self, new_color: int) -> None:
182
178
if new_color is None :
183
179
self ._background_palette .make_transparent (0 )
184
180
if self ._added_background_tilegrid :
185
- self .local_group .pop (0 )
181
+ self ._local_group .pop (0 )
186
182
self ._added_background_tilegrid = False
187
183
else :
188
184
self ._background_palette .make_opaque (0 )
@@ -207,15 +203,9 @@ def _set_background_color(self, new_color: int) -> None:
207
203
self ._bounding_box [3 ] + self ._padding_top + self ._padding_bottom > 0
208
204
)
209
205
):
210
- # This can be simplified in CP v6.0, when group.append(0) bug is corrected
211
- if len (self .local_group ) > 0 :
212
- self .local_group .insert (
213
- 0 , self ._create_background_box (lines , y_offset )
214
- )
215
- else :
216
- self .local_group .append (
217
- self ._create_background_box (lines , y_offset )
218
- )
206
+ self ._local_group .insert (
207
+ 0 , self ._create_background_box (lines , y_offset )
208
+ )
219
209
self ._added_background_tilegrid = True
220
210
221
211
else : # a bitmap is present in the self Group
@@ -229,9 +219,11 @@ def _set_background_color(self, new_color: int) -> None:
229
219
self ._bounding_box [3 ] + self ._padding_top + self ._padding_bottom > 0
230
220
)
231
221
):
232
- self .local_group [0 ] = self ._create_background_box (lines , self ._y_offset )
222
+ self ._local_group [0 ] = self ._create_background_box (
223
+ lines , self ._y_offset
224
+ )
233
225
else : # delete the existing bitmap
234
- self .local_group .pop (0 )
226
+ self ._local_group .pop (0 )
235
227
self ._added_background_tilegrid = False
236
228
237
229
# pylint: disable = too-many-branches, too-many-statements
@@ -269,6 +261,8 @@ def _update_text(
269
261
if not glyph :
270
262
continue
271
263
264
+ position_x , position_y = 0 , 0
265
+
272
266
if self ._label_direction in ("LTR" , "RTL" ):
273
267
bottom = max (bottom , y - glyph .dy + self ._y_offset )
274
268
if y == 0 : # first line, find the Ascender height
@@ -337,26 +331,15 @@ def _update_text(
337
331
position_x = x + glyph .dy - self ._y_offset
338
332
339
333
if glyph .width > 0 and glyph .height > 0 :
340
- try :
341
- # pylint: disable=unexpected-keyword-arg
342
- face = displayio .TileGrid (
343
- glyph .bitmap ,
344
- pixel_shader = self ._palette ,
345
- default_tile = glyph .tile_index ,
346
- tile_width = glyph .width ,
347
- tile_height = glyph .height ,
348
- position = (position_x , position_y ),
349
- )
350
- except TypeError :
351
- face = displayio .TileGrid (
352
- glyph .bitmap ,
353
- pixel_shader = self ._palette ,
354
- default_tile = glyph .tile_index ,
355
- tile_width = glyph .width ,
356
- tile_height = glyph .height ,
357
- x = position_x ,
358
- y = position_y ,
359
- )
334
+ face = TileGrid (
335
+ glyph .bitmap ,
336
+ pixel_shader = self ._palette ,
337
+ default_tile = glyph .tile_index ,
338
+ tile_width = glyph .width ,
339
+ tile_height = glyph .height ,
340
+ x = position_x ,
341
+ y = position_y ,
342
+ )
360
343
361
344
if self ._label_direction == "UPR" :
362
345
face .transpose_xy = True
@@ -365,10 +348,10 @@ def _update_text(
365
348
face .transpose_xy = True
366
349
face .flip_y = True
367
350
368
- if tilegrid_count < len (self .local_group ):
369
- self .local_group [tilegrid_count ] = face
351
+ if tilegrid_count < len (self ._local_group ):
352
+ self ._local_group [tilegrid_count ] = face
370
353
else :
371
- self .local_group .append (face )
354
+ self ._local_group .append (face )
372
355
tilegrid_count += 1
373
356
374
357
if self ._label_direction == "RTL" :
@@ -394,8 +377,8 @@ def _update_text(
394
377
if self ._label_direction == "TTB" and top is None :
395
378
top = 0
396
379
397
- while len (self .local_group ) > tilegrid_count : # i:
398
- self .local_group .pop ()
380
+ while len (self ._local_group ) > tilegrid_count : # i:
381
+ self ._local_group .pop ()
399
382
# pylint: disable=invalid-unary-operand-type
400
383
if self ._label_direction == "RTL" :
401
384
self ._bounding_box = (- left , top , left - right , bottom - top )
0 commit comments