Skip to content

Commit 0ec8789

Browse files
authored
Merge pull request #161 from tekktrik/doc/update-typing
Update type hints
2 parents d507c98 + af6e810 commit 0ec8789

File tree

4 files changed

+55
-32
lines changed

4 files changed

+55
-32
lines changed

adafruit_display_text/__init__.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@
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,
22+
max_width: int,
23+
font: Optional[Union[BuiltinFont, BDF, PCF]] = None,
24+
indent0: str = "",
25+
indent1: str = "",
1926
) -> List[str]:
2027
# pylint: disable=too-many-branches, too-many-locals
2128

@@ -27,7 +34,8 @@ def wrap_text_to_pixels(
2734
2835
:param str string: The text to be wrapped.
2936
: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.
37+
:param font: The font to use for measuring the text.
38+
:type font: ~BuiltinFont, ~BDF, or ~PCF
3139
:param str indent0: Additional character(s) to add to the first line.
3240
:param str indent1: Additional character(s) to add to all other lines.
3341
@@ -164,8 +172,9 @@ class LabelBase(Group):
164172
Subclasses should implement ``_set_text``, ``_set_font``, and ``_set_line_spacing`` to
165173
have the correct behavior for that type of label.
166174
167-
:param Font font: A font class that has ``get_bounding_box`` and ``get_glyph``.
175+
:param font: A font class that has ``get_bounding_box`` and ``get_glyph``.
168176
Must include a capital M for measuring character size.
177+
:type font: ~BuiltinFont, ~BDF, or ~PCF
169178
:param str text: Text to display
170179
:param int color: Color of all text in RGB hex
171180
:param int background_color: Color of the background, use `None` for transparent
@@ -192,7 +201,7 @@ class LabelBase(Group):
192201

193202
def __init__(
194203
self,
195-
font,
204+
font: Union[BuiltinFont, BDF, PCF],
196205
x: int = 0,
197206
y: int = 0,
198207
text: str = "",
@@ -278,15 +287,15 @@ def _get_ascent_descent(self) -> Tuple[int, int]:
278287
return ascender_max, descender_max
279288

280289
@property
281-
def font(self) -> None:
290+
def font(self) -> Union[BuiltinFont, BDF, PCF]:
282291
"""Font to use for text display."""
283292
return self._font
284293

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

288297
@font.setter
289-
def font(self, new_font) -> None:
298+
def font(self, new_font: Union[BuiltinFont, BDF, PCF]) -> None:
290299
self._set_font(new_font)
291300

292301
@property
@@ -435,5 +444,5 @@ def label_direction(self, new_label_direction: str) -> None:
435444
raise RuntimeError("Please provide a valid text direction")
436445
self._set_label_direction(new_label_direction)
437446

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

adafruit_display_text/bitmap_label.py

Lines changed: 24 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,9 @@ 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__(
88+
self, font: Union[BuiltinFont, BDF, PCF], save_text: bool = True, **kwargs
89+
) -> None:
8490

8591
self._bitmap = None
8692

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

103109
def _reset_text(
104110
self,
105-
font=None,
106-
text: str = None,
107-
line_spacing: float = None,
108-
scale: int = None,
111+
font: Optional[Union[BuiltinFont, BDF, PCF]] = None,
112+
text: Optional[str] = None,
113+
line_spacing: Optional[float] = None,
114+
scale: Optional[int] = None,
109115
) -> None:
110116
# pylint: disable=too-many-branches, too-many-statements
111117

@@ -247,13 +253,15 @@ def _reset_text(
247253
self.anchored_position = self._anchored_position
248254

249255
@staticmethod
250-
def _line_spacing_ypixels(font, line_spacing: float) -> int:
256+
def _line_spacing_ypixels(
257+
font: Union[BuiltinFont, BDF, PCF], line_spacing: float
258+
) -> int:
251259
# Note: Scaling is provided at the Group level
252260
return_value = int(line_spacing * font.get_bounding_box()[1])
253261
return return_value
254262

255263
def _text_bounding_box(
256-
self, text: str, font
264+
self, text: str, font: Union[BuiltinFont, BDF, PCF]
257265
) -> Tuple[int, int, int, int, int, int]:
258266
# pylint: disable=too-many-locals
259267

@@ -333,9 +341,9 @@ def _text_bounding_box(
333341

334342
def _place_text(
335343
self,
336-
bitmap,
344+
bitmap: displayio.Bitmap,
337345
text: str,
338-
font,
346+
font: Union[BuiltinFont, BDF, PCF],
339347
xposition: int,
340348
yposition: int,
341349
skip_index: int = 0, # set to None to write all pixels, other wise skip this palette index
@@ -432,10 +440,10 @@ def _place_text(
432440

433441
def _blit(
434442
self,
435-
bitmap, # target bitmap
443+
bitmap: displayio.Bitmap, # target bitmap
436444
x: int, # target x upper left corner
437445
y: int, # target y upper left corner
438-
source_bitmap, # source bitmap
446+
source_bitmap: displayio.Bitmap, # source bitmap
439447
x_1: int = 0, # source x start
440448
y_1: int = 0, # source y start
441449
x_2: int = None, # source x end
@@ -509,7 +517,7 @@ def _set_line_spacing(self, new_line_spacing: float) -> None:
509517
else:
510518
raise RuntimeError("line_spacing is immutable when save_text is False")
511519

512-
def _set_font(self, new_font) -> None:
520+
def _set_font(self, new_font: Union[BuiltinFont, BDF, PCF]) -> None:
513521
self._font = new_font
514522
if self._save_text:
515523
self._reset_text(font=new_font, scale=self.scale)
@@ -519,7 +527,7 @@ def _set_font(self, new_font) -> None:
519527
def _set_text(self, new_text: str, scale: int) -> None:
520528
self._reset_text(text=self._replace_tabs(new_text), scale=self.scale)
521529

522-
def _set_background_color(self, new_color):
530+
def _set_background_color(self, new_color: Optional[int]):
523531
self._background_color = new_color
524532
if new_color is not None:
525533
self._palette[0] = new_color
@@ -536,7 +544,7 @@ def _get_valid_label_directions(self) -> Tuple[str, ...]:
536544
return "LTR", "RTL", "UPD", "UPR", "DWR"
537545

538546
@property
539-
def bitmap(self):
547+
def bitmap(self) -> displayio.Bitmap:
540548
"""
541549
The Bitmap object that the text and background are drawn into.
542550

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 = ""

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# Uncomment the below if you use native CircuitPython modules such as
2626
# digitalio, micropython and busio. List the modules you use. Without it, the
2727
# autodoc module docs will fail to generate with a warning.
28-
autodoc_mock_imports = ["displayio"]
28+
autodoc_mock_imports = ["displayio", "adafruit_bitmap_font"]
2929

3030

3131
intersphinx_mapping = {

0 commit comments

Comments
 (0)