Skip to content

Create getter/setter for font #42

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

Closed
wants to merge 13 commits into from
Closed
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
30 changes: 23 additions & 7 deletions adafruit_display_text/label.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class Label(displayio.Group):
:param int color: Color of all text in RGB hex
:param double line_spacing: Line spacing of text to display"""

# pylint: disable=too-many-instance-attributes
# This has a lot of getters/setters, maybe it needs cleanup.

def __init__(
self,
font,
Expand All @@ -77,7 +80,7 @@ def __init__(
max_glyphs = len(text)
super().__init__(max_size=max_glyphs, **kwargs)
self.width = max_glyphs
self.font = font
self._font = font
self._text = None
self._anchor_point = (0, 0)
self.x = x
Expand All @@ -94,7 +97,7 @@ def __init__(
self._transparent_background = True
self.palette[1] = color

bounds = self.font.get_bounding_box()
bounds = self._font.get_bounding_box()
self.height = bounds[1]
self._line_spacing = line_spacing
self._boundingbox = None
Expand All @@ -109,19 +112,18 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
old_c = 0
y_offset = int(
(
self.font.get_glyph(ord("M")).height
self._font.get_glyph(ord("M")).height
- new_text.count("\n") * self.height * self.line_spacing
)
/ 2
)
# print("y offset from baseline", y_offset)
left = right = top = bottom = 0
for character in new_text:
if character == "\n":
y += int(self.height * self._line_spacing)
x = 0
continue
glyph = self.font.get_glyph(ord(character))
glyph = self._font.get_glyph(ord(character))
if not glyph:
continue
right = max(right, x + glyph.width)
Expand Down Expand Up @@ -167,7 +169,7 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals

x += glyph.shift_x

# TODO skip this for control sequences or non-printables.
# TODO skip this for control sequences or non-qables.
i += 1
old_c += 1
# skip all non-prinables in the old string
Expand All @@ -176,7 +178,7 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
and old_c < len(self._text)
and (
self._text[old_c] == "\n"
or not self.font.get_glyph(ord(self._text[old_c]))
or not self._font.get_glyph(ord(self._text[old_c]))
)
):
old_c += 1
Expand Down Expand Up @@ -238,6 +240,20 @@ def text(self):
def text(self, new_text):
self._update_text(str(new_text))

@property
def font(self):
"""Font to use for text display."""
return self._font

@font.setter
def font(self, new_font):
old_text = self._text
self._text = ""
self._font = new_font
bounds = self._font.get_bounding_box()
self.height = bounds[1]
self._update_text(str(old_text))

@property
def anchor_point(self):
"""Point that anchored_position moves relative to.
Expand Down