Skip to content

Use fontio.FontProtocol for type annotations #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions adafruit_display_text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@
=======================
"""

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"

from displayio import Group, Palette

try:
from typing import Optional, Union, List, Tuple
from fontio import BuiltinFont
from adafruit_bitmap_font.bdf import BDF
from adafruit_bitmap_font.pcf import PCF
from typing import Optional, List, Tuple
from fontio import FontProtocol
except ImportError:
pass
from displayio import Group, Palette


def wrap_text_to_pixels(
string: str,
max_width: int,
font: Optional[Union[BuiltinFont, BDF, PCF]] = None,
font: Optional[FontProtocol] = None,
indent0: str = "",
indent1: str = "",
) -> List[str]:
Expand All @@ -35,7 +37,7 @@ def wrap_text_to_pixels(
:param str string: The text to be wrapped.
:param int max_width: The maximum number of pixels on a line before wrapping.
:param font: The font to use for measuring the text.
:type font: ~BuiltinFont, ~BDF, or ~PCF
:type font: ~FontProtocol
:param str indent0: Additional character(s) to add to the first line.
:param str indent1: Additional character(s) to add to all other lines.

Expand Down Expand Up @@ -191,7 +193,7 @@ class LabelBase(Group):

:param font: A font class that has ``get_bounding_box`` and ``get_glyph``.
Must include a capital M for measuring character size.
:type font: ~BuiltinFont, ~BDF, or ~PCF
:type font: ~FontProtocol
:param str text: Text to display
:param int color: Color of all text in RGB hex
:param int background_color: Color of the background, use `None` for transparent
Expand All @@ -218,7 +220,7 @@ class LabelBase(Group):

def __init__(
self,
font: Union[BuiltinFont, BDF, PCF],
font: FontProtocol,
x: int = 0,
y: int = 0,
text: str = "",
Expand Down Expand Up @@ -304,15 +306,15 @@ def _get_ascent_descent(self) -> Tuple[int, int]:
return ascender_max, descender_max

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

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

@font.setter
def font(self, new_font: Union[BuiltinFont, BDF, PCF]) -> None:
def font(self, new_font: FontProtocol) -> None:
self._set_font(new_font)

@property
Expand Down
29 changes: 11 additions & 18 deletions adafruit_display_text/bitmap_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,15 @@
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"

import displayio
from adafruit_display_text import LabelBase

try:
from typing import Union, Optional, Tuple
from fontio import BuiltinFont
from adafruit_bitmap_font.bdf import BDF
from adafruit_bitmap_font.pcf import PCF
from typing import Optional, Tuple
from fontio import FontProtocol
except ImportError:
pass

import displayio

from adafruit_display_text import LabelBase

# pylint: disable=too-many-instance-attributes
class Label(LabelBase):
Expand All @@ -56,7 +53,7 @@ class Label(LabelBase):

:param font: A font class that has ``get_bounding_box`` and ``get_glyph``.
Must include a capital M for measuring character size.
:type font: ~BuiltinFont, ~BDF, or ~PCF
:type font: ~FontProtocol
:param str text: Text to display
:param int color: Color of all text in RGB hex
:param int background_color: Color of the background, use `None` for transparent
Expand Down Expand Up @@ -93,9 +90,7 @@ class Label(LabelBase):
"RTL": (False, False, False),
}

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

self._bitmap = None
self._tilegrid = None
Expand All @@ -116,7 +111,7 @@ def __init__(

def _reset_text(
self,
font: Optional[Union[BuiltinFont, BDF, PCF]] = None,
font: Optional[FontProtocol] = None,
text: Optional[str] = None,
line_spacing: Optional[float] = None,
scale: Optional[int] = None,
Expand Down Expand Up @@ -270,15 +265,13 @@ def _reset_text(
self.anchored_position = self._anchored_position

@staticmethod
def _line_spacing_ypixels(
font: Union[BuiltinFont, BDF, PCF], line_spacing: float
) -> int:
def _line_spacing_ypixels(font: FontProtocol, line_spacing: float) -> int:
# Note: Scaling is provided at the Group level
return_value = int(line_spacing * font.get_bounding_box()[1])
return return_value

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

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

def _set_font(self, new_font: Union[BuiltinFont, BDF, PCF]) -> None:
def _set_font(self, new_font: FontProtocol) -> None:
self._font = new_font
if self._save_text:
self._reset_text(font=new_font, scale=self.scale)
Expand Down
19 changes: 8 additions & 11 deletions adafruit_display_text/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,15 @@
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"


from displayio import Bitmap, Palette, TileGrid
from adafruit_display_text import LabelBase

try:
from typing import Union, Optional, Tuple
from fontio import BuiltinFont
from adafruit_bitmap_font.bdf import BDF
from adafruit_bitmap_font.pcf import PCF
from typing import Optional, Tuple
from fontio import FontProtocol
except ImportError:
pass

from displayio import Bitmap, Palette, TileGrid

from adafruit_display_text import LabelBase


class Label(LabelBase):
# pylint: disable=too-many-instance-attributes
Expand All @@ -49,7 +46,7 @@ class Label(LabelBase):

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

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

Expand Down Expand Up @@ -403,7 +400,7 @@ def _reset_text(self, new_text: str) -> None:
self._update_text(str(self._replace_tabs(new_text)))
self.anchored_position = current_anchored_position

def _set_font(self, new_font: Union[BuiltinFont, BDF, PCF]) -> None:
def _set_font(self, new_font: FontProtocol) -> None:
old_text = self._text
current_anchored_position = self.anchored_position
self._text = ""
Expand Down
6 changes: 3 additions & 3 deletions adafruit_display_text/scrolling_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"

import time
from adafruit_display_text import bitmap_label

try:
from typing import Optional
from fontio import FontProtocol
except ImportError:
pass

import time
from adafruit_display_text import bitmap_label


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