@@ -58,6 +58,9 @@ class Label(displayio.Group):
5858 :param int color: Color of all text in RGB hex
5959 :param double line_spacing: Line spacing of text to display"""
6060
61+ # pylint: disable=too-many-instance-attributes
62+ # This has a lot of getters/setters, maybe it needs cleanup.
63+
6164 def __init__ (
6265 self ,
6366 font ,
@@ -77,7 +80,7 @@ def __init__(
7780 max_glyphs = len (text )
7881 super ().__init__ (max_size = max_glyphs , ** kwargs )
7982 self .width = max_glyphs
80- self .font = font
83+ self ._font = font
8184 self ._text = None
8285 self ._anchor_point = (0 , 0 )
8386 self .x = x
@@ -94,7 +97,7 @@ def __init__(
9497 self ._transparent_background = True
9598 self .palette [1 ] = color
9699
97- bounds = self .font .get_bounding_box ()
100+ bounds = self ._font .get_bounding_box ()
98101 self .height = bounds [1 ]
99102 self ._line_spacing = line_spacing
100103 self ._boundingbox = None
@@ -109,19 +112,18 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
109112 old_c = 0
110113 y_offset = int (
111114 (
112- self .font .get_glyph (ord ("M" )).height
115+ self ._font .get_glyph (ord ("M" )).height
113116 - new_text .count ("\n " ) * self .height * self .line_spacing
114117 )
115118 / 2
116119 )
117- # print("y offset from baseline", y_offset)
118120 left = right = top = bottom = 0
119121 for character in new_text :
120122 if character == "\n " :
121123 y += int (self .height * self ._line_spacing )
122124 x = 0
123125 continue
124- glyph = self .font .get_glyph (ord (character ))
126+ glyph = self ._font .get_glyph (ord (character ))
125127 if not glyph :
126128 continue
127129 right = max (right , x + glyph .width )
@@ -170,13 +172,13 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
170172 # TODO skip this for control sequences or non-printables.
171173 i += 1
172174 old_c += 1
173- # skip all non-prinables in the old string
175+ # skip all non-printables in the old string
174176 while (
175177 self ._text
176178 and old_c < len (self ._text )
177179 and (
178180 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 ]))
180182 )
181183 ):
182184 old_c += 1
@@ -236,7 +238,25 @@ def text(self):
236238
237239 @text .setter
238240 def text (self , new_text ):
241+ current_anchored_position = self .anchored_position
239242 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
240260
241261 @property
242262 def anchor_point (self ):
@@ -247,18 +267,36 @@ def anchor_point(self):
247267
248268 @anchor_point .setter
249269 def anchor_point (self , new_anchor_point ):
270+ current_anchored_position = self .anchored_position
250271 self ._anchor_point = new_anchor_point
272+ self .anchored_position = current_anchored_position
251273
252274 @property
253275 def anchored_position (self ):
254276 """Position relative to the anchor_point. Tuple containing x,y
255277 pixel coordinates."""
256278 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+ ),
259289 )
260290
261291 @anchored_position .setter
262292 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