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 10 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
35 changes: 20 additions & 15 deletions adafruit_display_text/label.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,14 @@
"""
`adafruit_display_text.label`
====================================================

Displays text labels using CircuitPython's displayio.

* Author(s): Scott Shawcroft

Implementation Notes
--------------------

**Hardware:**

**Software and Dependencies:**

* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases

"""

import displayio
Expand All @@ -50,7 +43,6 @@ class Label(displayio.Group):
properties will be the left edge of the bounding box, and in the center of a M
glyph (if its one line), or the (number of lines * linespacing + M)/2. That is,
it will try to have it be center-left as close as possible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the issue might be caused by the removal of this blank line. This SO Q/A mentions the same error: https://stackoverflow.com/questions/45880348/how-to-remove-the-cause-of-an-unexpected-indentation-warning-when-generating-cod

Copy link
Contributor Author

@kmatch98 kmatch98 May 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, I added this blank line back in and now pylint is throwing this error:
adafruit_display_text/label.py:46:0: C0303: Trailing whitespace (trailing-whitespace)

Sorry if this is simple stuff, but I'm totally unfamiliar with pylint or Sphinx, so I will appreciate your guidance.

Oh, and in the process of checking out these changes I fixed a calculation bug and added several other improvements to the getters/setters in another pull request. Let me know if you want me to cancel this PR and we can just focus on that one. Thanks for your patience with me on this!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. I am pretty new to sphinx and pylint myself. They can definitely be finicky.

I think the issue now is the empty line has a tab indention on it. Pylint wants it to be an empty newline with no tabs or spaces in it.

:param Font font: A font class that has ``get_bounding_box`` and ``get_glyph``.
Must include a capital M for measuring character size.
:param str text: Text to display
Expand All @@ -77,7 +69,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 +86,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 +101,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 +158,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 +167,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 +229,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