28
28
29
29
30
30
try :
31
- from typing import Tuple
31
+ from typing import Union , Optional , Tuple
32
+ from fontio import BuiltinFont
33
+ from adafruit_bitmap_font .bdf import BDF
34
+ from adafruit_bitmap_font .pcf import PCF
32
35
except ImportError :
33
36
pass
34
37
@@ -51,8 +54,9 @@ class Label(LabelBase):
51
54
glyph (if its one line), or the (number of lines * linespacing + M)/2. That is,
52
55
it will try to have it be center-left as close as possible.
53
56
54
- :param Font font: A font class that has ``get_bounding_box`` and ``get_glyph``.
57
+ :param font: A font class that has ``get_bounding_box`` and ``get_glyph``.
55
58
Must include a capital M for measuring character size.
59
+ :type font: ~BuiltinFont, ~BDF, or ~PCF
56
60
:param str text: Text to display
57
61
:param int color: Color of all text in RGB hex
58
62
:param int background_color: Color of the background, use `None` for transparent
@@ -80,7 +84,7 @@ class Label(LabelBase):
80
84
configurations possibles ``LTR``-Left-To-Right ``RTL``-Right-To-Left
81
85
``UPD``-Upside Down ``UPR``-Upwards ``DWR``-Downwards. It defaults to ``LTR``"""
82
86
83
- def __init__ (self , font , save_text = True , ** kwargs ) -> None :
87
+ def __init__ (self , font : Union [ BuiltinFont , BDF , PCF ], save_text : bool = True , ** kwargs ) -> None :
84
88
85
89
self ._bitmap = None
86
90
@@ -102,10 +106,10 @@ def __init__(self, font, save_text=True, **kwargs) -> None:
102
106
103
107
def _reset_text (
104
108
self ,
105
- font = None ,
106
- text : str = None ,
107
- line_spacing : float = None ,
108
- scale : int = None ,
109
+ font : Optional [ Union [ BuiltinFont , BDF , PCF ]] = sNone ,
110
+ text : Optional [ str ] = None ,
111
+ line_spacing : Optional [ float ] = None ,
112
+ scale : Optional [ int ] = None ,
109
113
) -> None :
110
114
# pylint: disable=too-many-branches, too-many-statements
111
115
@@ -247,13 +251,13 @@ def _reset_text(
247
251
self .anchored_position = self ._anchored_position
248
252
249
253
@staticmethod
250
- def _line_spacing_ypixels (font , line_spacing : float ) -> int :
254
+ def _line_spacing_ypixels (font : Union [ BuiltinFont , BDF , PCF ] , line_spacing : float ) -> int :
251
255
# Note: Scaling is provided at the Group level
252
256
return_value = int (line_spacing * font .get_bounding_box ()[1 ])
253
257
return return_value
254
258
255
259
def _text_bounding_box (
256
- self , text : str , font
260
+ self , text : str , font : Union [ BuiltinFont , BDF , PCF ]
257
261
) -> Tuple [int , int , int , int , int , int ]:
258
262
# pylint: disable=too-many-locals
259
263
@@ -333,9 +337,9 @@ def _text_bounding_box(
333
337
334
338
def _place_text (
335
339
self ,
336
- bitmap ,
340
+ bitmap : displayio . Bitmap ,
337
341
text : str ,
338
- font ,
342
+ font : Union [ BuiltinFont , BDF , PCF ] ,
339
343
xposition : int ,
340
344
yposition : int ,
341
345
skip_index : int = 0 , # set to None to write all pixels, other wise skip this palette index
@@ -432,10 +436,10 @@ def _place_text(
432
436
433
437
def _blit (
434
438
self ,
435
- bitmap , # target bitmap
439
+ bitmap : displayio . Bitmap , # target bitmap
436
440
x : int , # target x upper left corner
437
441
y : int , # target y upper left corner
438
- source_bitmap , # source bitmap
442
+ source_bitmap : displayio . Bitmap , # source bitmap
439
443
x_1 : int = 0 , # source x start
440
444
y_1 : int = 0 , # source y start
441
445
x_2 : int = None , # source x end
@@ -509,7 +513,7 @@ def _set_line_spacing(self, new_line_spacing: float) -> None:
509
513
else :
510
514
raise RuntimeError ("line_spacing is immutable when save_text is False" )
511
515
512
- def _set_font (self , new_font ) -> None :
516
+ def _set_font (self , new_font : Union [ BuiltinFont , BDF , PCF ] ) -> None :
513
517
self ._font = new_font
514
518
if self ._save_text :
515
519
self ._reset_text (font = new_font , scale = self .scale )
@@ -519,7 +523,7 @@ def _set_font(self, new_font) -> None:
519
523
def _set_text (self , new_text : str , scale : int ) -> None :
520
524
self ._reset_text (text = self ._replace_tabs (new_text ), scale = self .scale )
521
525
522
- def _set_background_color (self , new_color ):
526
+ def _set_background_color (self , new_color : Optional [ int ] ):
523
527
self ._background_color = new_color
524
528
if new_color is not None :
525
529
self ._palette [0 ] = new_color
@@ -536,7 +540,7 @@ def _get_valid_label_directions(self) -> Tuple[str, ...]:
536
540
return "LTR" , "RTL" , "UPD" , "UPR" , "DWR"
537
541
538
542
@property
539
- def bitmap (self ):
543
+ def bitmap (self ) -> displayio . Bitmap :
540
544
"""
541
545
The Bitmap object that the text and background are drawn into.
542
546
0 commit comments