27
27
__version__ = "0.0.0-auto.0"
28
28
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
29
29
30
+ from adafruit_display_text import LabelBase
30
31
31
- class Label (displayio .Group ):
32
+
33
+ class Label (LabelBase ):
32
34
"""A label displaying a string of text. The origin point set by ``x`` and ``y``
33
35
properties will be the left edge of the bounding box, and in the center of a M
34
36
glyph (if its one line), or the (number of lines * linespacing + M)/2. That is,
@@ -66,28 +68,12 @@ class Label(displayio.Group):
66
68
# pylint: disable=too-many-instance-attributes, too-many-locals
67
69
# This has a lot of getters/setters, maybe it needs cleanup.
68
70
69
- def __init__ (
70
- self ,
71
- font ,
72
- * ,
73
- x = 0 ,
74
- y = 0 ,
75
- text = "" ,
76
- max_glyphs = None ,
77
- color = 0xFFFFFF ,
78
- background_color = None ,
79
- line_spacing = 1.25 ,
80
- background_tight = False ,
81
- padding_top = 0 ,
82
- padding_bottom = 0 ,
83
- padding_left = 0 ,
84
- padding_right = 0 ,
85
- anchor_point = None ,
86
- anchored_position = None ,
87
- scale = 1 ,
88
- base_alignment = False ,
89
- ** kwargs
90
- ):
71
+ def __init__ (self , font , ** kwargs ):
72
+ super ().__init__ (font , ** kwargs )
73
+
74
+ max_glyphs = kwargs .get ("max_glyphs" , None )
75
+ text = kwargs .get ("text" , None )
76
+
91
77
if not max_glyphs and not text :
92
78
raise RuntimeError ("Please provide a max size, or initial text" )
93
79
if not max_glyphs :
@@ -98,64 +84,69 @@ def __init__(
98
84
# self Group will contain a single local_group which contains a Group (self.local_group)
99
85
# which contains a TileGrid
100
86
# The self scale should always be 1
101
- super (). __init__ ( max_size = 1 , scale = 1 , ** kwargs )
87
+
102
88
# local_group will set the scale
103
- self .local_group = displayio .Group (max_size = max_glyphs + 1 , scale = scale )
89
+ self .local_group = displayio .Group (
90
+ max_size = max_glyphs + 1 , scale = kwargs .get ("scale" , 1 )
91
+ )
104
92
self .append (self .local_group )
105
93
106
94
self .width = max_glyphs
107
95
self ._font = font
108
96
self ._text = None
109
- self ._anchor_point = anchor_point
110
- self .x = x
111
- self .y = y
97
+ self ._anchor_point = kwargs . get ( " anchor_point" , None )
98
+ self .x = kwargs . get ( "x" , 0 )
99
+ self .y = kwargs . get ( "y" , 0 )
112
100
113
101
self .height = self ._font .get_bounding_box ()[1 ]
114
- self ._line_spacing = line_spacing
115
- self ._boundingbox = None
102
+ self ._line_spacing = kwargs . get ( " line_spacing" , 1.25 )
103
+ self ._bounding_box = None
116
104
117
- self ._background_tight = (
118
- background_tight # sets padding status for text background box
119
- )
105
+ self ._background_tight = kwargs . get (
106
+ " background_tight" , False
107
+ ) # sets padding status for text background box
120
108
121
109
# Create the two-color text palette
122
110
self .palette = displayio .Palette (2 )
123
111
self .palette [0 ] = 0
124
112
self .palette .make_transparent (0 )
125
- self .color = color
113
+ self .color = kwargs . get ( " color" , 0xFFFFFF )
126
114
127
- self ._background_color = background_color
115
+ self ._background_color = kwargs . get ( " background_color" , None )
128
116
self ._background_palette = displayio .Palette (1 )
129
117
self ._added_background_tilegrid = False
130
118
131
- self ._padding_top = padding_top
132
- self ._padding_bottom = padding_bottom
133
- self ._padding_left = padding_left
134
- self ._padding_right = padding_right
135
- self .base_alignment = base_alignment
119
+ self ._padding_top = kwargs . get ( " padding_top" , 0 )
120
+ self ._padding_bottom = kwargs . get ( " padding_bottom" , 0 )
121
+ self ._padding_left = kwargs . get ( " padding_left" , 0 )
122
+ self ._padding_right = kwargs . get ( " padding_right" , 0 )
123
+ self .base_alignment = kwargs . get ( " base_alignment" , False )
136
124
137
125
if text is not None :
126
+ print ("calling _update_text" )
138
127
self ._update_text (str (text ))
139
- if (anchored_position is not None ) and (anchor_point is not None ):
140
- self .anchored_position = anchored_position
128
+ if (kwargs .get ("anchored_position" , None ) is not None ) and (
129
+ kwargs .get ("anchor_point" , None ) is not None
130
+ ):
131
+ self .anchored_position = kwargs .get ("anchored_position" , None )
141
132
142
133
def _create_background_box (self , lines , y_offset ):
143
134
"""Private Class function to create a background_box
144
135
:param lines: int number of lines
145
136
:param y_offset: int y pixel bottom coordinate for the background_box"""
146
137
147
- left = self ._boundingbox [0 ]
138
+ left = self ._bounding_box [0 ]
148
139
149
140
if self ._background_tight : # draw a tight bounding box
150
- box_width = self ._boundingbox [2 ]
151
- box_height = self ._boundingbox [3 ]
141
+ box_width = self ._bounding_box [2 ]
142
+ box_height = self ._bounding_box [3 ]
152
143
x_box_offset = 0
153
- y_box_offset = self ._boundingbox [1 ]
144
+ y_box_offset = self ._bounding_box [1 ]
154
145
155
146
else : # draw a "loose" bounding box to include any ascenders/descenders.
156
147
ascent , descent = self ._get_ascent_descent ()
157
148
158
- box_width = self ._boundingbox [2 ] + self ._padding_left + self ._padding_right
149
+ box_width = self ._bounding_box [2 ] + self ._padding_left + self ._padding_right
159
150
x_box_offset = - self ._padding_left
160
151
box_height = (
161
152
(ascent + descent )
@@ -181,30 +172,6 @@ def _create_background_box(self, lines, y_offset):
181
172
182
173
return tile_grid
183
174
184
- def _get_ascent_descent (self ):
185
- """ Private function to calculate ascent and descent font values """
186
- if hasattr (self .font , "ascent" ):
187
- return self .font .ascent , self .font .descent
188
-
189
- # check a few glyphs for maximum ascender and descender height
190
- glyphs = "M j'" # choose glyphs with highest ascender and lowest
191
- try :
192
- self ._font .load_glyphs (glyphs )
193
- except AttributeError :
194
- # Builtin font doesn't have or need load_glyphs
195
- pass
196
- # descender, will depend upon font used
197
- ascender_max = descender_max = 0
198
- for char in glyphs :
199
- this_glyph = self ._font .get_glyph (ord (char ))
200
- if this_glyph :
201
- ascender_max = max (ascender_max , this_glyph .height + this_glyph .dy )
202
- descender_max = max (descender_max , - this_glyph .dy )
203
- return ascender_max , descender_max
204
-
205
- def _get_ascent (self ):
206
- return self ._get_ascent_descent ()[0 ]
207
-
208
175
def _update_background_color (self , new_color ):
209
176
"""Private class function that allows updating the font box background color
210
177
:param new_color: int color as an RGB hex number."""
@@ -227,10 +194,10 @@ def _update_background_color(self, new_color):
227
194
if (
228
195
(len (self ._text ) > 0 )
229
196
and (
230
- self ._boundingbox [2 ] + self ._padding_left + self ._padding_right > 0
197
+ self ._bounding_box [2 ] + self ._padding_left + self ._padding_right > 0
231
198
)
232
199
and (
233
- self ._boundingbox [3 ] + self ._padding_top + self ._padding_bottom > 0
200
+ self ._bounding_box [3 ] + self ._padding_top + self ._padding_bottom > 0
234
201
)
235
202
):
236
203
# This can be simplified in CP v6.0, when group.append(0) bug is corrected
@@ -249,10 +216,10 @@ def _update_background_color(self, new_color):
249
216
if (
250
217
(len (self ._text ) > 0 )
251
218
and (
252
- self ._boundingbox [2 ] + self ._padding_left + self ._padding_right > 0
219
+ self ._bounding_box [2 ] + self ._padding_left + self ._padding_right > 0
253
220
)
254
221
and (
255
- self ._boundingbox [3 ] + self ._padding_top + self ._padding_bottom > 0
222
+ self ._bounding_box [3 ] + self ._padding_top + self ._padding_bottom > 0
256
223
)
257
224
):
258
225
self .local_group [0 ] = self ._create_background_box (lines , y_offset )
@@ -333,88 +300,32 @@ def _update_text(
333
300
while len (self .local_group ) > tilegrid_count : # i:
334
301
self .local_group .pop ()
335
302
self ._text = new_text
336
- self ._boundingbox = (left , top , right - left , bottom - top )
303
+ print ("setting bounding box" )
304
+ self ._bounding_box = (left , top , right - left , bottom - top )
337
305
338
306
if self .background_color is not None :
339
307
self ._update_background_color (self ._background_color )
340
308
341
- @property
342
- def bounding_box (self ):
343
- """An (x, y, w, h) tuple that completely covers all glyphs. The
344
- first two numbers are offset from the x, y origin of this group"""
345
- return tuple (self ._boundingbox )
346
-
347
- @property
348
- def line_spacing (self ):
349
- """The amount of space between lines of text, in multiples of the font's
350
- bounding-box height. (E.g. 1.0 is the bounding-box height)"""
351
- return self ._line_spacing
352
-
353
- @line_spacing .setter
354
- def line_spacing (self , spacing ):
355
- self ._line_spacing = spacing
356
- self .text = self ._text # redraw the box
357
-
358
- @property
359
- def color (self ):
360
- """Color of the text as an RGB hex number."""
361
- return self .palette [1 ]
362
-
363
- @color .setter
364
- def color (self , new_color ):
365
- self ._color = new_color
366
- if new_color is not None :
367
- self .palette [1 ] = new_color
368
- self .palette .make_opaque (1 )
369
- else :
370
- self .palette [1 ] = 0
371
- self .palette .make_transparent (1 )
372
-
373
- @property
374
- def background_color (self ):
375
- """Color of the background as an RGB hex number."""
376
- return self ._background_color
377
-
378
- @background_color .setter
379
- def background_color (self , new_color ):
380
- self ._update_background_color (new_color )
381
-
382
- @property
383
- def text (self ):
384
- """Text to display."""
385
- return self ._text
386
-
387
- @text .setter
388
- def text (self , new_text ):
309
+ def _reset_text (self , new_text , scale ):
389
310
try :
390
311
current_anchored_position = self .anchored_position
391
312
self ._update_text (str (new_text ))
392
313
self .anchored_position = current_anchored_position
393
314
except RuntimeError as run_error :
394
315
raise RuntimeError ("Text length exceeds max_glyphs" ) from run_error
395
316
396
- @property
397
- def scale (self ):
398
- """Set the scaling of the label, in integer values"""
399
- return self .local_group .scale
400
-
401
- @scale .setter
402
- def scale (self , new_scale ):
403
- current_anchored_position = self .anchored_position
404
- self .local_group .scale = new_scale
405
- self .anchored_position = current_anchored_position
406
-
407
- @property
408
- def font (self ):
409
- """Font to use for text display."""
410
- return self ._font
411
-
412
- @font .setter
413
- def font (self , new_font ):
317
+ def _set_font (self , new_font ):
414
318
old_text = self ._text
415
319
current_anchored_position = self .anchored_position
416
320
self ._text = ""
417
321
self ._font = new_font
418
322
self .height = self ._font .get_bounding_box ()[1 ]
419
323
self ._update_text (str (old_text ))
420
324
self .anchored_position = current_anchored_position
325
+
326
+ def _set_line_spacing (self , new_line_spacing ):
327
+ self ._line_spacing = new_line_spacing
328
+ self .text = self ._text # redraw the box
329
+
330
+ def _set_text (self , text , scale ):
331
+ self ._reset_text (text , scale )
0 commit comments