Skip to content

Commit a6b3378

Browse files
committed
Change type annotations to use fontio.FontProtocol
1 parent a567299 commit a6b3378

File tree

3 files changed

+23
-33
lines changed

3 files changed

+23
-33
lines changed

adafruit_display_text/__init__.py

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

1010
try:
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
11+
from typing import Optional, List, Tuple
12+
from fontio import FontProtocol
1513
except ImportError:
1614
pass
1715
from displayio import Group, Palette
@@ -20,7 +18,7 @@
2018
def wrap_text_to_pixels(
2119
string: str,
2220
max_width: int,
23-
font: Optional[Union[BuiltinFont, BDF, PCF]] = None,
21+
font: Optional[FontProtocol] = None,
2422
indent0: str = "",
2523
indent1: str = "",
2624
) -> List[str]:
@@ -35,7 +33,7 @@ def wrap_text_to_pixels(
3533
:param str string: The text to be wrapped.
3634
:param int max_width: The maximum number of pixels on a line before wrapping.
3735
:param font: The font to use for measuring the text.
38-
:type font: ~BuiltinFont, ~BDF, or ~PCF
36+
:type font: ~FontProtocol
3937
:param str indent0: Additional character(s) to add to the first line.
4038
:param str indent1: Additional character(s) to add to all other lines.
4139
@@ -191,7 +189,7 @@ class LabelBase(Group):
191189
192190
:param font: A font class that has ``get_bounding_box`` and ``get_glyph``.
193191
Must include a capital M for measuring character size.
194-
:type font: ~BuiltinFont, ~BDF, or ~PCF
192+
:type font: ~FontProtocol
195193
:param str text: Text to display
196194
:param int color: Color of all text in RGB hex
197195
:param int background_color: Color of the background, use `None` for transparent
@@ -218,7 +216,7 @@ class LabelBase(Group):
218216

219217
def __init__(
220218
self,
221-
font: Union[BuiltinFont, BDF, PCF],
219+
font: FontProtocol,
222220
x: int = 0,
223221
y: int = 0,
224222
text: str = "",
@@ -304,15 +302,15 @@ def _get_ascent_descent(self) -> Tuple[int, int]:
304302
return ascender_max, descender_max
305303

306304
@property
307-
def font(self) -> Union[BuiltinFont, BDF, PCF]:
305+
def font(self) -> FontProtocol:
308306
"""Font to use for text display."""
309307
return self._font
310308

311-
def _set_font(self, new_font: Union[BuiltinFont, BDF, PCF]) -> None:
309+
def _set_font(self, new_font: FontProtocol) -> None:
312310
raise NotImplementedError("{} MUST override '_set_font'".format(type(self)))
313311

314312
@font.setter
315-
def font(self, new_font: Union[BuiltinFont, BDF, PCF]) -> None:
313+
def font(self, new_font: FontProtocol) -> None:
316314
self._set_font(new_font)
317315

318316
@property

adafruit_display_text/bitmap_label.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@
2828

2929

3030
try:
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
31+
from typing import Optional, Tuple
32+
from fontio import FontProtocol
3533
except ImportError:
3634
pass
3735

@@ -56,7 +54,7 @@ class Label(LabelBase):
5654
5755
:param font: A font class that has ``get_bounding_box`` and ``get_glyph``.
5856
Must include a capital M for measuring character size.
59-
:type font: ~BuiltinFont, ~BDF, or ~PCF
57+
:type font: ~FontProtocol
6058
:param str text: Text to display
6159
:param int color: Color of all text in RGB hex
6260
:param int background_color: Color of the background, use `None` for transparent
@@ -93,9 +91,7 @@ class Label(LabelBase):
9391
"RTL": (False, False, False),
9492
}
9593

96-
def __init__(
97-
self, font: Union[BuiltinFont, BDF, PCF], save_text: bool = True, **kwargs
98-
) -> None:
94+
def __init__(self, font: FontProtocol, save_text: bool = True, **kwargs) -> None:
9995

10096
self._bitmap = None
10197
self._tilegrid = None
@@ -116,7 +112,7 @@ def __init__(
116112

117113
def _reset_text(
118114
self,
119-
font: Optional[Union[BuiltinFont, BDF, PCF]] = None,
115+
font: Optional[FontProtocol] = None,
120116
text: Optional[str] = None,
121117
line_spacing: Optional[float] = None,
122118
scale: Optional[int] = None,
@@ -270,15 +266,13 @@ def _reset_text(
270266
self.anchored_position = self._anchored_position
271267

272268
@staticmethod
273-
def _line_spacing_ypixels(
274-
font: Union[BuiltinFont, BDF, PCF], line_spacing: float
275-
) -> int:
269+
def _line_spacing_ypixels(font: FontProtocol, line_spacing: float) -> int:
276270
# Note: Scaling is provided at the Group level
277271
return_value = int(line_spacing * font.get_bounding_box()[1])
278272
return return_value
279273

280274
def _text_bounding_box(
281-
self, text: str, font: Union[BuiltinFont, BDF, PCF]
275+
self, text: str, font: FontProtocol
282276
) -> Tuple[int, int, int, int, int, int]:
283277
# pylint: disable=too-many-locals
284278

@@ -360,7 +354,7 @@ def _place_text(
360354
self,
361355
bitmap: displayio.Bitmap,
362356
text: str,
363-
font: Union[BuiltinFont, BDF, PCF],
357+
font: FontProtocol,
364358
xposition: int,
365359
yposition: int,
366360
skip_index: int = 0, # set to None to write all pixels, other wise skip this palette index
@@ -534,7 +528,7 @@ def _set_line_spacing(self, new_line_spacing: float) -> None:
534528
else:
535529
raise RuntimeError("line_spacing is immutable when save_text is False")
536530

537-
def _set_font(self, new_font: Union[BuiltinFont, BDF, PCF]) -> None:
531+
def _set_font(self, new_font: FontProtocol) -> None:
538532
self._font = new_font
539533
if self._save_text:
540534
self._reset_text(font=new_font, scale=self.scale)

adafruit_display_text/label.py

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

2828

2929
try:
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
30+
from typing import Optional, Tuple
31+
from fontio import FontProtocol
3432
except ImportError:
3533
pass
3634

@@ -49,7 +47,7 @@ class Label(LabelBase):
4947
5048
:param font: A font class that has ``get_bounding_box`` and ``get_glyph``.
5149
Must include a capital M for measuring character size.
52-
:type font: ~BuiltinFont, ~BDF, or ~PCF
50+
:type font: ~FontProtocol
5351
:param str text: Text to display
5452
:param int color: Color of all text in RGB hex
5553
:param int background_color: Color of the background, use `None` for transparent
@@ -83,7 +81,7 @@ class Label(LabelBase):
8381
configurations possibles ``LTR``-Left-To-Right ``RTL``-Right-To-Left
8482
``TTB``-Top-To-Bottom ``UPR``-Upwards ``DWR``-Downwards. It defaults to ``LTR``"""
8583

86-
def __init__(self, font: Union[BuiltinFont, BDF, PCF], **kwargs) -> None:
84+
def __init__(self, font: FontProtocol, **kwargs) -> None:
8785
self._background_palette = Palette(1)
8886
self._added_background_tilegrid = False
8987

@@ -403,7 +401,7 @@ def _reset_text(self, new_text: str) -> None:
403401
self._update_text(str(self._replace_tabs(new_text)))
404402
self.anchored_position = current_anchored_position
405403

406-
def _set_font(self, new_font: Union[BuiltinFont, BDF, PCF]) -> None:
404+
def _set_font(self, new_font: FontProtocol) -> None:
407405
old_text = self._text
408406
current_anchored_position = self.anchored_position
409407
self._text = ""

0 commit comments

Comments
 (0)