@@ -58,6 +58,9 @@ class Label(displayio.Group):
58
58
:param int color: Color of all text in RGB hex
59
59
:param double line_spacing: Line spacing of text to display"""
60
60
61
+ # pylint: disable=too-many-instance-attributes
62
+ # This has a lot of getters/setters, maybe it needs cleanup.
63
+
61
64
def __init__ (
62
65
self ,
63
66
font ,
@@ -77,7 +80,7 @@ def __init__(
77
80
max_glyphs = len (text )
78
81
super ().__init__ (max_size = max_glyphs , ** kwargs )
79
82
self .width = max_glyphs
80
- self .font = font
83
+ self ._font = font
81
84
self ._text = None
82
85
self ._anchor_point = (0 , 0 )
83
86
self .x = x
@@ -94,7 +97,7 @@ def __init__(
94
97
self ._transparent_background = True
95
98
self .palette [1 ] = color
96
99
97
- bounds = self .font .get_bounding_box ()
100
+ bounds = self ._font .get_bounding_box ()
98
101
self .height = bounds [1 ]
99
102
self ._line_spacing = line_spacing
100
103
self ._boundingbox = None
@@ -109,19 +112,18 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
109
112
old_c = 0
110
113
y_offset = int (
111
114
(
112
- self .font .get_glyph (ord ("M" )).height
115
+ self ._font .get_glyph (ord ("M" )).height
113
116
- new_text .count ("\n " ) * self .height * self .line_spacing
114
117
)
115
118
/ 2
116
119
)
117
- # print("y offset from baseline", y_offset)
118
120
left = right = top = bottom = 0
119
121
for character in new_text :
120
122
if character == "\n " :
121
123
y += int (self .height * self ._line_spacing )
122
124
x = 0
123
125
continue
124
- glyph = self .font .get_glyph (ord (character ))
126
+ glyph = self ._font .get_glyph (ord (character ))
125
127
if not glyph :
126
128
continue
127
129
right = max (right , x + glyph .width )
@@ -170,13 +172,13 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
170
172
# TODO skip this for control sequences or non-printables.
171
173
i += 1
172
174
old_c += 1
173
- # skip all non-prinables in the old string
175
+ # skip all non-printables in the old string
174
176
while (
175
177
self ._text
176
178
and old_c < len (self ._text )
177
179
and (
178
180
self ._text [old_c ] == "\n "
179
- or not self .font .get_glyph (ord (self ._text [old_c ]))
181
+ or not self ._font .get_glyph (ord (self ._text [old_c ]))
180
182
)
181
183
):
182
184
old_c += 1
@@ -236,7 +238,25 @@ def text(self):
236
238
237
239
@text .setter
238
240
def text (self , new_text ):
241
+ current_anchored_position = self .anchored_position
239
242
self ._update_text (str (new_text ))
243
+ self .anchored_position = current_anchored_position
244
+
245
+ @property
246
+ def font (self ):
247
+ """Font to use for text display."""
248
+ return self ._font
249
+
250
+ @font .setter
251
+ def font (self , new_font ):
252
+ old_text = self ._text
253
+ current_anchored_position = self .anchored_position
254
+ self ._text = ""
255
+ self ._font = new_font
256
+ bounds = self ._font .get_bounding_box ()
257
+ self .height = bounds [1 ]
258
+ self ._update_text (str (old_text ))
259
+ self .anchored_position = current_anchored_position
240
260
241
261
@property
242
262
def anchor_point (self ):
@@ -247,18 +267,36 @@ def anchor_point(self):
247
267
248
268
@anchor_point .setter
249
269
def anchor_point (self , new_anchor_point ):
270
+ current_anchored_position = self .anchored_position
250
271
self ._anchor_point = new_anchor_point
272
+ self .anchored_position = current_anchored_position
251
273
252
274
@property
253
275
def anchored_position (self ):
254
276
"""Position relative to the anchor_point. Tuple containing x,y
255
277
pixel coordinates."""
256
278
return (
257
- self .x - self ._boundingbox [2 ] * self ._anchor_point [0 ],
258
- self .y - self ._boundingbox [3 ] * self ._anchor_point [1 ],
279
+ int (
280
+ self .x
281
+ + self ._boundingbox [0 ]
282
+ + self ._anchor_point [0 ] * self ._boundingbox [2 ]
283
+ ),
284
+ int (
285
+ self .y
286
+ + self ._boundingbox [1 ]
287
+ + self ._anchor_point [1 ] * self ._boundingbox [3 ]
288
+ ),
259
289
)
260
290
261
291
@anchored_position .setter
262
292
def anchored_position (self , new_position ):
263
- self .x = int (new_position [0 ] - (self ._boundingbox [2 ] * self ._anchor_point [0 ]))
264
- self .y = int (new_position [1 ] - (self ._boundingbox [3 ] * self ._anchor_point [1 ]))
293
+ self .x = int (
294
+ new_position [0 ]
295
+ - self ._boundingbox [0 ]
296
+ - self ._anchor_point [0 ] * self ._boundingbox [2 ]
297
+ )
298
+ self .y = int (
299
+ new_position [1 ]
300
+ - self ._boundingbox [1 ]
301
+ - self ._anchor_point [1 ] * self ._boundingbox [3 ]
302
+ )
0 commit comments