39
39
40
40
from adafruit_display_text import LabelBase
41
41
42
-
42
+ # pylint: disable=too-many-instance-attributes
43
43
class Label (LabelBase ):
44
44
"""A label displaying a string of text that is stored in a bitmap.
45
45
Note: This ``bitmap_label.py`` library utilizes a :py:class:`~displayio.Bitmap`
@@ -84,20 +84,28 @@ class Label(LabelBase):
84
84
configurations possibles ``LTR``-Left-To-Right ``RTL``-Right-To-Left
85
85
``UPD``-Upside Down ``UPR``-Upwards ``DWR``-Downwards. It defaults to ``LTR``"""
86
86
87
+ # This maps label_direction to TileGrid's transpose_xy, flip_x, flip_y
88
+ _DIR_MAP = {
89
+ "UPR" : (True , True , False ),
90
+ "DWR" : (True , False , True ),
91
+ "UPD" : (False , True , True ),
92
+ "LTR" : (False , False , False ),
93
+ "RTL" : (False , False , False ),
94
+ }
95
+
87
96
def __init__ (
88
97
self , font : Union [BuiltinFont , BDF , PCF ], save_text : bool = True , ** kwargs
89
98
) -> None :
90
99
91
100
self ._bitmap = None
101
+ self ._tilegrid = None
102
+ self ._prev_label_direction = None
92
103
93
104
super ().__init__ (font , ** kwargs )
94
105
95
106
self ._save_text = save_text
96
107
self ._text = self ._replace_tabs (self ._text )
97
108
98
- if self ._label_direction == "RTL" :
99
- self ._text = "" .join (reversed (self ._text ))
100
-
101
109
# call the text updater with all the arguments.
102
110
self ._reset_text (
103
111
font = font ,
@@ -113,7 +121,7 @@ def _reset_text(
113
121
line_spacing : Optional [float ] = None ,
114
122
scale : Optional [int ] = None ,
115
123
) -> None :
116
- # pylint: disable=too-many-branches, too-many-statements
124
+ # pylint: disable=too-many-branches, too-many-statements, too-many-locals
117
125
118
126
# Store all the instance variables
119
127
if font is not None :
@@ -127,8 +135,6 @@ def _reset_text(
127
135
128
136
if self ._save_text : # text string will be saved
129
137
self ._text = self ._replace_tabs (text )
130
- if self ._label_direction == "RTL" :
131
- self ._text = "" .join (reversed (self ._text ))
132
138
else :
133
139
self ._text = None # save a None value since text string is not saved
134
140
@@ -179,13 +185,24 @@ def _reset_text(
179
185
box_x = box_x + self ._padding_left + self ._padding_right
180
186
box_y = box_y + self ._padding_top + self ._padding_bottom
181
187
182
- # Create the bitmap and TileGrid
183
- self ._bitmap = displayio .Bitmap (box_x , box_y , len (self ._palette ))
188
+ # Create the Bitmap unless it can be reused
189
+ new_bitmap = None
190
+ if (
191
+ self ._bitmap is None
192
+ or self ._bitmap .width != box_x
193
+ or self ._bitmap .height != box_y
194
+ ):
195
+ new_bitmap = displayio .Bitmap (box_x , box_y , len (self ._palette ))
196
+ self ._bitmap = new_bitmap
197
+ else :
198
+ self ._bitmap .fill (0 )
184
199
185
200
# Place the text into the Bitmap
186
201
self ._place_text (
187
202
self ._bitmap ,
188
- text ,
203
+ text
204
+ if self ._label_direction != "RTL"
205
+ else "" .join (reversed (self ._text )),
189
206
self ._font ,
190
207
self ._padding_left - x_offset ,
191
208
self ._padding_top + y_offset ,
@@ -196,35 +213,33 @@ def _reset_text(
196
213
else :
197
214
label_position_yoffset = self ._ascent // 2
198
215
199
- self ._tilegrid = displayio .TileGrid (
200
- self ._bitmap ,
201
- pixel_shader = self ._palette ,
202
- width = 1 ,
203
- height = 1 ,
204
- tile_width = box_x ,
205
- tile_height = box_y ,
206
- default_tile = 0 ,
207
- x = - self ._padding_left + x_offset ,
208
- y = label_position_yoffset - y_offset - self ._padding_top ,
209
- )
210
-
211
- if self ._label_direction == "UPR" :
212
- self ._tilegrid .transpose_xy = True
213
- self ._tilegrid .flip_x = True
214
- if self ._label_direction == "DWR" :
215
- self ._tilegrid .transpose_xy = True
216
- self ._tilegrid .flip_y = True
217
- if self ._label_direction == "UPD" :
218
- self ._tilegrid .flip_x = True
219
- self ._tilegrid .flip_y = True
220
-
221
- # Clear out any items in the local_group Group, in case this is an update to
222
- # the bitmap_label
223
- for _ in self ._local_group :
224
- self ._local_group .pop (0 )
225
- self ._local_group .append (
226
- self ._tilegrid
227
- ) # add the bitmap's tilegrid to the group
216
+ # Create the TileGrid if not created bitmap unchanged
217
+ if self ._tilegrid is None or new_bitmap :
218
+ self ._tilegrid = displayio .TileGrid (
219
+ self ._bitmap ,
220
+ pixel_shader = self ._palette ,
221
+ width = 1 ,
222
+ height = 1 ,
223
+ tile_width = box_x ,
224
+ tile_height = box_y ,
225
+ default_tile = 0 ,
226
+ x = - self ._padding_left + x_offset ,
227
+ y = label_position_yoffset - y_offset - self ._padding_top ,
228
+ )
229
+ # Clear out any items in the local_group Group, in case this is an update to
230
+ # the bitmap_label
231
+ for _ in self ._local_group :
232
+ self ._local_group .pop (0 )
233
+ self ._local_group .append (
234
+ self ._tilegrid
235
+ ) # add the bitmap's tilegrid to the group
236
+
237
+ # Set TileGrid properties based on label_direction
238
+ if self ._label_direction != self ._prev_label_direction :
239
+ tg1 = self ._tilegrid
240
+ tg1 .transpose_xy , tg1 .flip_x , tg1 .flip_y = self ._DIR_MAP [
241
+ self ._label_direction
242
+ ]
228
243
229
244
# Update bounding_box values. Note: To be consistent with label.py,
230
245
# this is the bounding box for the text only, not including the background.
@@ -537,6 +552,7 @@ def _set_background_color(self, new_color: Optional[int]):
537
552
self ._palette .make_transparent (0 )
538
553
539
554
def _set_label_direction (self , new_label_direction : str ) -> None :
555
+ self ._prev_label_direction = self ._label_direction
540
556
self ._label_direction = new_label_direction
541
557
self ._reset_text (text = str (self ._text )) # Force a recalculation
542
558
0 commit comments