Skip to content

Commit bb9cb75

Browse files
authored
Merge pull request #173 from tekktrik/doc/use-font-protocol
Use `fontio.FontProtocol` for type annotations
2 parents a567299 + 367af1d commit bb9cb75

File tree

4 files changed

+36
-44
lines changed

4 files changed

+36
-44
lines changed

adafruit_display_text/__init__.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77
=======================
88
"""
99

10+
__version__ = "0.0.0-auto.0"
11+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
12+
13+
from displayio import Group, Palette
14+
1015
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
16+
from typing import Optional, List, Tuple
17+
from fontio import FontProtocol
1518
except ImportError:
1619
pass
17-
from displayio import Group, Palette
1820

1921

2022
def wrap_text_to_pixels(
2123
string: str,
2224
max_width: int,
23-
font: Optional[Union[BuiltinFont, BDF, PCF]] = None,
25+
font: Optional[FontProtocol] = None,
2426
indent0: str = "",
2527
indent1: str = "",
2628
) -> List[str]:
@@ -35,7 +37,7 @@ def wrap_text_to_pixels(
3537
:param str string: The text to be wrapped.
3638
:param int max_width: The maximum number of pixels on a line before wrapping.
3739
:param font: The font to use for measuring the text.
38-
:type font: ~BuiltinFont, ~BDF, or ~PCF
40+
:type font: ~FontProtocol
3941
:param str indent0: Additional character(s) to add to the first line.
4042
:param str indent1: Additional character(s) to add to all other lines.
4143
@@ -191,7 +193,7 @@ class LabelBase(Group):
191193
192194
:param font: A font class that has ``get_bounding_box`` and ``get_glyph``.
193195
Must include a capital M for measuring character size.
194-
:type font: ~BuiltinFont, ~BDF, or ~PCF
196+
:type font: ~FontProtocol
195197
:param str text: Text to display
196198
:param int color: Color of all text in RGB hex
197199
:param int background_color: Color of the background, use `None` for transparent
@@ -218,7 +220,7 @@ class LabelBase(Group):
218220

219221
def __init__(
220222
self,
221-
font: Union[BuiltinFont, BDF, PCF],
223+
font: FontProtocol,
222224
x: int = 0,
223225
y: int = 0,
224226
text: str = "",
@@ -304,15 +306,15 @@ def _get_ascent_descent(self) -> Tuple[int, int]:
304306
return ascender_max, descender_max
305307

306308
@property
307-
def font(self) -> Union[BuiltinFont, BDF, PCF]:
309+
def font(self) -> FontProtocol:
308310
"""Font to use for text display."""
309311
return self._font
310312

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

314316
@font.setter
315-
def font(self, new_font: Union[BuiltinFont, BDF, PCF]) -> None:
317+
def font(self, new_font: FontProtocol) -> None:
316318
self._set_font(new_font)
317319

318320
@property

adafruit_display_text/bitmap_label.py

+11-18
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,15 @@
2626
__version__ = "0.0.0-auto.0"
2727
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
2828

29+
import displayio
30+
from adafruit_display_text import LabelBase
2931

3032
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
33+
from typing import Optional, Tuple
34+
from fontio import FontProtocol
3535
except ImportError:
3636
pass
3737

38-
import displayio
39-
40-
from adafruit_display_text import LabelBase
4138

4239
# pylint: disable=too-many-instance-attributes
4340
class Label(LabelBase):
@@ -56,7 +53,7 @@ class Label(LabelBase):
5653
5754
:param font: A font class that has ``get_bounding_box`` and ``get_glyph``.
5855
Must include a capital M for measuring character size.
59-
:type font: ~BuiltinFont, ~BDF, or ~PCF
56+
:type font: ~FontProtocol
6057
:param str text: Text to display
6158
:param int color: Color of all text in RGB hex
6259
:param int background_color: Color of the background, use `None` for transparent
@@ -93,9 +90,7 @@ class Label(LabelBase):
9390
"RTL": (False, False, False),
9491
}
9592

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

10095
self._bitmap = None
10196
self._tilegrid = None
@@ -116,7 +111,7 @@ def __init__(
116111

117112
def _reset_text(
118113
self,
119-
font: Optional[Union[BuiltinFont, BDF, PCF]] = None,
114+
font: Optional[FontProtocol] = None,
120115
text: Optional[str] = None,
121116
line_spacing: Optional[float] = None,
122117
scale: Optional[int] = None,
@@ -270,15 +265,13 @@ def _reset_text(
270265
self.anchored_position = self._anchored_position
271266

272267
@staticmethod
273-
def _line_spacing_ypixels(
274-
font: Union[BuiltinFont, BDF, PCF], line_spacing: float
275-
) -> int:
268+
def _line_spacing_ypixels(font: FontProtocol, line_spacing: float) -> int:
276269
# Note: Scaling is provided at the Group level
277270
return_value = int(line_spacing * font.get_bounding_box()[1])
278271
return return_value
279272

280273
def _text_bounding_box(
281-
self, text: str, font: Union[BuiltinFont, BDF, PCF]
274+
self, text: str, font: FontProtocol
282275
) -> Tuple[int, int, int, int, int, int]:
283276
# pylint: disable=too-many-locals
284277

@@ -360,7 +353,7 @@ def _place_text(
360353
self,
361354
bitmap: displayio.Bitmap,
362355
text: str,
363-
font: Union[BuiltinFont, BDF, PCF],
356+
font: FontProtocol,
364357
xposition: int,
365358
yposition: int,
366359
skip_index: int = 0, # set to None to write all pixels, other wise skip this palette index
@@ -534,7 +527,7 @@ def _set_line_spacing(self, new_line_spacing: float) -> None:
534527
else:
535528
raise RuntimeError("line_spacing is immutable when save_text is False")
536529

537-
def _set_font(self, new_font: Union[BuiltinFont, BDF, PCF]) -> None:
530+
def _set_font(self, new_font: FontProtocol) -> None:
538531
self._font = new_font
539532
if self._save_text:
540533
self._reset_text(font=new_font, scale=self.scale)

adafruit_display_text/label.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,15 @@
2626
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
2727

2828

29+
from displayio import Bitmap, Palette, TileGrid
30+
from adafruit_display_text import LabelBase
31+
2932
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
33+
from typing import Optional, Tuple
34+
from fontio import FontProtocol
3435
except ImportError:
3536
pass
3637

37-
from displayio import Bitmap, Palette, TileGrid
38-
39-
from adafruit_display_text import LabelBase
40-
4138

4239
class Label(LabelBase):
4340
# pylint: disable=too-many-instance-attributes
@@ -49,7 +46,7 @@ class Label(LabelBase):
4946
5047
:param font: A font class that has ``get_bounding_box`` and ``get_glyph``.
5148
Must include a capital M for measuring character size.
52-
:type font: ~BuiltinFont, ~BDF, or ~PCF
49+
:type font: ~FontProtocol
5350
:param str text: Text to display
5451
:param int color: Color of all text in RGB hex
5552
:param int background_color: Color of the background, use `None` for transparent
@@ -83,7 +80,7 @@ class Label(LabelBase):
8380
configurations possibles ``LTR``-Left-To-Right ``RTL``-Right-To-Left
8481
``TTB``-Top-To-Bottom ``UPR``-Upwards ``DWR``-Downwards. It defaults to ``LTR``"""
8582

86-
def __init__(self, font: Union[BuiltinFont, BDF, PCF], **kwargs) -> None:
83+
def __init__(self, font: FontProtocol, **kwargs) -> None:
8784
self._background_palette = Palette(1)
8885
self._added_background_tilegrid = False
8986

@@ -403,7 +400,7 @@ def _reset_text(self, new_text: str) -> None:
403400
self._update_text(str(self._replace_tabs(new_text)))
404401
self.anchored_position = current_anchored_position
405402

406-
def _set_font(self, new_font: Union[BuiltinFont, BDF, PCF]) -> None:
403+
def _set_font(self, new_font: FontProtocol) -> None:
407404
old_text = self._text
408405
current_anchored_position = self.anchored_position
409406
self._text = ""

adafruit_display_text/scrolling_label.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626
__version__ = "0.0.0-auto.0"
2727
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
2828

29+
import time
30+
from adafruit_display_text import bitmap_label
31+
2932
try:
3033
from typing import Optional
3134
from fontio import FontProtocol
3235
except ImportError:
3336
pass
3437

35-
import time
36-
from adafruit_display_text import bitmap_label
37-
3838

3939
class ScrollingLabel(bitmap_label.Label):
4040
"""ScrollingLabel - A fixed-width label that will scroll to the left

0 commit comments

Comments
 (0)