Skip to content

Commit b592b4c

Browse files
committed
Update type hints
Added types for fonts, as well as a few minor fixes to type hints and docstrings
1 parent d2b44c5 commit b592b4c

File tree

3 files changed

+46
-31
lines changed

3 files changed

+46
-31
lines changed

adafruit_display_text/__init__.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
"""
99

1010
try:
11-
from typing import List, Tuple
11+
from typing import Optional, Union, List, Tuple
12+
from fontio import BuiltinFont
13+
from adafruit_bitmap_font.bdf import BDF
14+
from adafruit_bitmap_font.pcf import PCF
1215
except ImportError:
1316
pass
1417
from displayio import Group, Palette
1518

1619

1720
def wrap_text_to_pixels(
18-
string: str, max_width: int, font=None, indent0: str = "", indent1: str = ""
21+
string: str, max_width: int, font: Optional[Union[BuiltinFont, BDF, PCF]] = None, indent0: str = "", indent1: str = ""
1922
) -> List[str]:
2023
# pylint: disable=too-many-branches, too-many-locals
2124

@@ -27,7 +30,8 @@ def wrap_text_to_pixels(
2730
2831
:param str string: The text to be wrapped.
2932
:param int max_width: The maximum number of pixels on a line before wrapping.
30-
:param Font font: The font to use for measuring the text.
33+
:param font: The font to use for measuring the text.
34+
:type font: ~BuiltinFont, ~BDF, or ~PCF
3135
:param str indent0: Additional character(s) to add to the first line.
3236
:param str indent1: Additional character(s) to add to all other lines.
3337
@@ -164,8 +168,9 @@ class LabelBase(Group):
164168
Subclasses should implement ``_set_text``, ``_set_font``, and ``_set_line_spacing`` to
165169
have the correct behavior for that type of label.
166170
167-
:param Font font: A font class that has ``get_bounding_box`` and ``get_glyph``.
171+
:param font: A font class that has ``get_bounding_box`` and ``get_glyph``.
168172
Must include a capital M for measuring character size.
173+
:type font: ~BuiltinFont, ~BDF, or ~PCF
169174
:param str text: Text to display
170175
:param int color: Color of all text in RGB hex
171176
:param int background_color: Color of the background, use `None` for transparent
@@ -192,7 +197,7 @@ class LabelBase(Group):
192197

193198
def __init__(
194199
self,
195-
font,
200+
font: Union[BuiltinFont, BDF, PCF],
196201
x: int = 0,
197202
y: int = 0,
198203
text: str = "",
@@ -278,15 +283,15 @@ def _get_ascent_descent(self) -> Tuple[int, int]:
278283
return ascender_max, descender_max
279284

280285
@property
281-
def font(self) -> None:
286+
def font(self) -> Union[BuiltinFont, BDF, PCF]:
282287
"""Font to use for text display."""
283288
return self._font
284289

285-
def _set_font(self, new_font) -> None:
290+
def _set_font(self, new_font: Union[BuiltinFont, BDF, PCF]) -> None:
286291
raise NotImplementedError("{} MUST override '_set_font'".format(type(self)))
287292

288293
@font.setter
289-
def font(self, new_font) -> None:
294+
def font(self, new_font: Union[BuiltinFont, BDF, PCF]) -> None:
290295
self._set_font(new_font)
291296

292297
@property
@@ -435,5 +440,5 @@ def label_direction(self, new_label_direction: str) -> None:
435440
raise RuntimeError("Please provide a valid text direction")
436441
self._set_label_direction(new_label_direction)
437442

438-
def _replace_tabs(self, text):
443+
def _replace_tabs(self, text: str) -> str:
439444
return self._tab_text.join(text.split("\t"))

adafruit_display_text/bitmap_label.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828

2929

3030
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
3235
except ImportError:
3336
pass
3437

@@ -51,8 +54,9 @@ class Label(LabelBase):
5154
glyph (if its one line), or the (number of lines * linespacing + M)/2. That is,
5255
it will try to have it be center-left as close as possible.
5356
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``.
5558
Must include a capital M for measuring character size.
59+
:type font: ~BuiltinFont, ~BDF, or ~PCF
5660
:param str text: Text to display
5761
:param int color: Color of all text in RGB hex
5862
:param int background_color: Color of the background, use `None` for transparent
@@ -80,7 +84,7 @@ class Label(LabelBase):
8084
configurations possibles ``LTR``-Left-To-Right ``RTL``-Right-To-Left
8185
``UPD``-Upside Down ``UPR``-Upwards ``DWR``-Downwards. It defaults to ``LTR``"""
8286

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:
8488

8589
self._bitmap = None
8690

@@ -102,10 +106,10 @@ def __init__(self, font, save_text=True, **kwargs) -> None:
102106

103107
def _reset_text(
104108
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,
109113
) -> None:
110114
# pylint: disable=too-many-branches, too-many-statements
111115

@@ -247,13 +251,13 @@ def _reset_text(
247251
self.anchored_position = self._anchored_position
248252

249253
@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:
251255
# Note: Scaling is provided at the Group level
252256
return_value = int(line_spacing * font.get_bounding_box()[1])
253257
return return_value
254258

255259
def _text_bounding_box(
256-
self, text: str, font
260+
self, text: str, font: Union[BuiltinFont, BDF, PCF]
257261
) -> Tuple[int, int, int, int, int, int]:
258262
# pylint: disable=too-many-locals
259263

@@ -333,9 +337,9 @@ def _text_bounding_box(
333337

334338
def _place_text(
335339
self,
336-
bitmap,
340+
bitmap: displayio.Bitmap,
337341
text: str,
338-
font,
342+
font: Union[BuiltinFont, BDF, PCF],
339343
xposition: int,
340344
yposition: int,
341345
skip_index: int = 0, # set to None to write all pixels, other wise skip this palette index
@@ -432,10 +436,10 @@ def _place_text(
432436

433437
def _blit(
434438
self,
435-
bitmap, # target bitmap
439+
bitmap: displayio.Bitmap, # target bitmap
436440
x: int, # target x upper left corner
437441
y: int, # target y upper left corner
438-
source_bitmap, # source bitmap
442+
source_bitmap: displayio.Bitmap, # source bitmap
439443
x_1: int = 0, # source x start
440444
y_1: int = 0, # source y start
441445
x_2: int = None, # source x end
@@ -509,7 +513,7 @@ def _set_line_spacing(self, new_line_spacing: float) -> None:
509513
else:
510514
raise RuntimeError("line_spacing is immutable when save_text is False")
511515

512-
def _set_font(self, new_font) -> None:
516+
def _set_font(self, new_font: Union[BuiltinFont, BDF, PCF]) -> None:
513517
self._font = new_font
514518
if self._save_text:
515519
self._reset_text(font=new_font, scale=self.scale)
@@ -519,7 +523,7 @@ def _set_font(self, new_font) -> None:
519523
def _set_text(self, new_text: str, scale: int) -> None:
520524
self._reset_text(text=self._replace_tabs(new_text), scale=self.scale)
521525

522-
def _set_background_color(self, new_color):
526+
def _set_background_color(self, new_color: Optional[int]):
523527
self._background_color = new_color
524528
if new_color is not None:
525529
self._palette[0] = new_color
@@ -536,7 +540,7 @@ def _get_valid_label_directions(self) -> Tuple[str, ...]:
536540
return "LTR", "RTL", "UPD", "UPR", "DWR"
537541

538542
@property
539-
def bitmap(self):
543+
def bitmap(self) -> displayio.Bitmap:
540544
"""
541545
The Bitmap object that the text and background are drawn into.
542546

adafruit_display_text/label.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727

2828

2929
try:
30-
from typing import Tuple
30+
from typing import Union, Optional, Tuple
31+
from fontio import BuiltinFont
32+
from adafruit_bitmap_font.bdf import BDF
33+
from adafruit_bitmap_font.pcf import PCF
3134
except ImportError:
3235
pass
3336

@@ -44,8 +47,9 @@ class Label(LabelBase):
4447
glyph (if its one line), or the (number of lines * linespacing + M)/2. That is,
4548
it will try to have it be center-left as close as possible.
4649
47-
:param Font font: A font class that has ``get_bounding_box`` and ``get_glyph``.
50+
:param font: A font class that has ``get_bounding_box`` and ``get_glyph``.
4851
Must include a capital M for measuring character size.
52+
:type font: ~BuiltinFont, ~BDF, or ~PCF
4953
:param str text: Text to display
5054
:param int color: Color of all text in RGB hex
5155
:param int background_color: Color of the background, use `None` for transparent
@@ -79,7 +83,7 @@ class Label(LabelBase):
7983
configurations possibles ``LTR``-Left-To-Right ``RTL``-Right-To-Left
8084
``TTB``-Top-To-Bottom ``UPR``-Upwards ``DWR``-Downwards. It defaults to ``LTR``"""
8185

82-
def __init__(self, font, **kwargs) -> None:
86+
def __init__(self, font: Union[BuiltinFont, BDF, PCF], **kwargs) -> None:
8387
self._background_palette = Palette(1)
8488
self._added_background_tilegrid = False
8589

@@ -166,9 +170,11 @@ def _create_background_box(self, lines: int, y_offset: int) -> TileGrid:
166170

167171
return tile_grid
168172

169-
def _set_background_color(self, new_color: int) -> None:
173+
def _set_background_color(self, new_color: Optional[int]) -> None:
170174
"""Private class function that allows updating the font box background color
171-
:param int new_color: color as an RGB hex number."""
175+
176+
:param int new_color: Color as an RGB hex number, setting to None makes it transparent
177+
"""
172178

173179
if new_color is None:
174180
self._background_palette.make_transparent(0)
@@ -397,7 +403,7 @@ def _reset_text(self, new_text: str) -> None:
397403
self._update_text(str(self._replace_tabs(new_text)))
398404
self.anchored_position = current_anchored_position
399405

400-
def _set_font(self, new_font) -> None:
406+
def _set_font(self, new_font: Union[BuiltinFont, BDF, PCF]) -> None:
401407
old_text = self._text
402408
current_anchored_position = self.anchored_position
403409
self._text = ""

0 commit comments

Comments
 (0)