-
Notifications
You must be signed in to change notification settings - Fork 39
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
Changes from 10 commits
9a73454
756088b
bb79810
37febae
b0ca00e
b5593bc
a2473b5
52a3bec
96f2f11
a1c4859
4acafa4
1c19264
83f43de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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. | ||
|
Uh oh!
There was an error while loading. Please reload this page.