66Display Text module helper functions
77"""
88from displayio import Group , Palette
9-
9+ try :
10+ from typing import Tuple
11+ except :
12+ pass
1013
1114def wrap_text_to_pixels (string , max_width , font = None , indent0 = "" , indent1 = "" ):
1215 """wrap_text_to_pixels function
@@ -183,27 +186,26 @@ class LabelBase(Group):
183186 def __init__ (
184187 self ,
185188 font ,
186- x = 0 ,
187- y = 0 ,
188- text = "" ,
189- max_glyphs = None ,
190- # with label.py
191- color = 0xFFFFFF ,
192- background_color = None ,
193- line_spacing = 1.25 ,
194- background_tight = False ,
195- padding_top = 0 ,
196- padding_bottom = 0 ,
197- padding_left = 0 ,
198- padding_right = 0 ,
199- anchor_point = None ,
200- anchored_position = None ,
201- save_text = True , # can reduce memory use if save_text = False
202- scale = 1 ,
203- base_alignment = False ,
204- tab_replacement = (4 , " " ),
189+ x : int = 0 ,
190+ y : int = 0 ,
191+ text : str = "" ,
192+ max_glyphs : int = None ,
193+ color : int = 0xFFFFFF ,
194+ background_color : int = None ,
195+ line_spacing : float = 1.25 ,
196+ background_tight : bool = False ,
197+ padding_top : int = 0 ,
198+ padding_bottom : int = 0 ,
199+ padding_left : int = 0 ,
200+ padding_right : int = 0 ,
201+ anchor_point : Tuple [float , float ] = None ,
202+ anchored_position : Tuple [int , int ] = None ,
203+ save_text : bool = True , # can reduce memory use if save_text = False
204+ scale :int = 1 ,
205+ base_alignment : bool = False ,
206+ tab_replacement : Tuple [int , str ] = (4 , " " ),
205207 ** kwargs ,
206- ):
208+ ) -> None :
207209 super ().__init__ (max_size = 1 , x = x , y = y , scale = 1 )
208210
209211 self ._font = font
@@ -230,7 +232,7 @@ def __init__(
230232 else :
231233 self ._y_offset = self ._get_ascent () // 2
232234
233- def _get_ascent_descent (self ):
235+ def _get_ascent_descent (self ) -> Tuple [ int , int ] :
234236 """ Private function to calculate ascent and descent font values """
235237 if hasattr (self .font , "ascent" ):
236238 return self .font .ascent , self .font .descent
@@ -251,29 +253,29 @@ def _get_ascent_descent(self):
251253 descender_max = max (descender_max , - this_glyph .dy )
252254 return ascender_max , descender_max
253255
254- def _get_ascent (self ):
256+ def _get_ascent (self ) -> int :
255257 return self ._get_ascent_descent ()[0 ]
256258
257259 @property
258- def font (self ):
260+ def font (self ) -> None :
259261 """Font to use for text display."""
260262 return self ._font
261263
262- def _set_font (self , new_font ):
264+ def _set_font (self , new_font ) -> None :
263265 # subclasses should override this
264266 pass
265267
266268 @font .setter
267- def font (self , new_font ):
269+ def font (self , new_font ) -> None :
268270 self ._set_font (new_font )
269271
270272 @property
271- def color (self ):
273+ def color (self ) -> int :
272274 """Color of the text as an RGB hex number."""
273275 return self ._color
274276
275277 @color .setter
276- def color (self , new_color ):
278+ def color (self , new_color : int ):
277279 self ._color = new_color
278280 if new_color is not None :
279281 self .palette [1 ] = new_color
@@ -283,12 +285,12 @@ def color(self, new_color):
283285 self .palette .make_transparent (1 )
284286
285287 @property
286- def background_color (self ):
288+ def background_color (self ) -> int :
287289 """Color of the background as an RGB hex number."""
288290 return self ._background_color
289291
290292 @background_color .setter
291- def background_color (self , new_color ) :
293+ def background_color (self , new_color : int ) -> None :
292294 self ._background_color = new_color
293295 if new_color is not None :
294296 self .palette [0 ] = new_color
@@ -298,14 +300,14 @@ def background_color(self, new_color):
298300 self .palette .make_transparent (0 )
299301
300302 @property
301- def anchor_point (self ):
303+ def anchor_point (self ) -> Tuple [ float , float ] :
302304 """Point that anchored_position moves relative to.
303305 Tuple with decimal percentage of width and height.
304306 (E.g. (0,0) is top left, (1.0, 0.5): is middle right.)"""
305307 return self ._anchor_point
306308
307309 @anchor_point .setter
308- def anchor_point (self , new_anchor_point ) :
310+ def anchor_point (self , new_anchor_point : Tuple [ float , float ]) -> None :
309311 if new_anchor_point [1 ] == self .baseline :
310312 self ._anchor_point = (new_anchor_point [0 ], - 1.0 )
311313 else :
@@ -315,13 +317,13 @@ def anchor_point(self, new_anchor_point):
315317 ) # update the anchored_position using setter
316318
317319 @property
318- def anchored_position (self ):
320+ def anchored_position (self ) -> Tuple [ int , int ] :
319321 """Position relative to the anchor_point. Tuple containing x,y
320322 pixel coordinates."""
321323 return self ._anchored_position
322324
323325 @anchored_position .setter
324- def anchored_position (self , new_position ) :
326+ def anchored_position (self , new_position : Tuple [ int , int ]) -> None :
325327 self ._anchored_position = new_position
326328 # Set anchored_position
327329 if (self ._anchor_point is not None ) and (self ._anchored_position is not None ):
@@ -340,44 +342,44 @@ def anchored_position(self, new_position):
340342 )
341343
342344 @property
343- def scale (self ):
345+ def scale (self ) -> int :
344346 """Set the scaling of the label, in integer values"""
345347 return self .local_group .scale
346348
347349 @scale .setter
348- def scale (self , new_scale ) :
350+ def scale (self , new_scale : int ) -> None :
349351 self .local_group .scale = new_scale
350352 self .anchored_position = self ._anchored_position # update the anchored_position
351353
352- def _set_text (self , new_text , scale ) :
354+ def _set_text (self , new_text : str , scale : int ) -> None :
353355 # subclasses should override this
354356 pass
355357
356358 @property
357- def text (self ):
359+ def text (self ) -> str :
358360 """Text to be displayed."""
359361 return self ._text
360362
361363 @text .setter # Cannot set color or background color with text setter, use separate setter
362- def text (self , new_text ) :
364+ def text (self , new_text : str ) -> None :
363365 self ._set_text (new_text , self .scale )
364366
365367 @property
366- def bounding_box (self ):
368+ def bounding_box (self ) -> Tuple [ int , int ] :
367369 """An (x, y, w, h) tuple that completely covers all glyphs. The
368370 first two numbers are offset from the x, y origin of this group"""
369371 return tuple (self ._bounding_box )
370372
371373 @property
372- def line_spacing (self ):
374+ def line_spacing (self ) -> float :
373375 """The amount of space between lines of text, in multiples of the font's
374376 bounding-box height. (E.g. 1.0 is the bounding-box height)"""
375377 return self ._line_spacing
376378
377- def _set_line_spacing (self , new_line_spacing ) :
379+ def _set_line_spacing (self , new_line_spacing : float ) -> None :
378380 # subclass should override this.
379381 pass
380382
381383 @line_spacing .setter
382- def line_spacing (self , new_line_spacing ) :
384+ def line_spacing (self , new_line_spacing : float ) -> None :
383385 self ._set_line_spacing (new_line_spacing )
0 commit comments