Skip to content

Fix anchor_position getter/setter, anchor_point setter and display_text_anchored_position example #43

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 22 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
9a73454
Added getter/setter for font to ensure proper screen update when font…
May 22, 2020
756088b
deleted print statements
May 22, 2020
bb79810
Updated font.height and ran black formatter
May 22, 2020
37febae
updated newFont to new_font for plint
May 22, 2020
b0ca00e
re-ran black for formatting
May 22, 2020
b5593bc
added comment for font setter to satisfy pylint
May 22, 2020
a2473b5
deleted one blank line
May 22, 2020
52a3bec
deleted blank space, blank line, added some spaces after =, updated c…
May 22, 2020
96f2f11
modified comment description to try to overcome Sphinx Unexpected Ind…
May 22, 2020
a1c4859
reverted back to previous revision without extra lines. Still no luc…
May 22, 2020
e60fb65
Corrected calculation errors in anchored_position getter and setter
May 23, 2020
995f3de
ran black
May 23, 2020
12d8b25
updated setter for anchor_point so that x,y are updated upon change t…
May 23, 2020
02f26d9
updated anchor_positions example so that anchor positions correspond …
May 23, 2020
69cc1bf
updated font and text setters, so that the anchored_position is prope…
May 23, 2020
0defe7d
trial to overcome too many instance variables issue from pylint
May 23, 2020
ed64879
added a line in the comments to try to resolve unexpected indent resp…
May 23, 2020
173d0c5
updated spacing in header and deleted one tab, thanks foamyguy
May 23, 2020
ac63a18
Added one more missing line, thanks foamyguy
May 23, 2020
68c9522
added pylint disable=too-many-instance-attributes to resolve getter-s…
May 23, 2020
a102ba2
ran black
May 23, 2020
6695e3c
fixed an error I made to a comment
May 23, 2020
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
60 changes: 49 additions & 11 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 @@ -170,13 +172,13 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
# TODO skip this for control sequences or non-printables.
i += 1
old_c += 1
# skip all non-prinables in the old string
# skip all non-printables in the old string
while (
self._text
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 @@ -236,7 +238,25 @@ def text(self):

@text.setter
def text(self, new_text):
current_anchored_position = self.anchored_position
self._update_text(str(new_text))
self.anchored_position = current_anchored_position

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

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

@property
def anchor_point(self):
Expand All @@ -247,18 +267,36 @@ def anchor_point(self):

@anchor_point.setter
def anchor_point(self, new_anchor_point):
current_anchored_position = self.anchored_position
self._anchor_point = new_anchor_point
self.anchored_position = current_anchored_position

@property
def anchored_position(self):
"""Position relative to the anchor_point. Tuple containing x,y
pixel coordinates."""
return (
self.x - self._boundingbox[2] * self._anchor_point[0],
self.y - self._boundingbox[3] * self._anchor_point[1],
int(
self.x
+ self._boundingbox[0]
+ self._anchor_point[0] * self._boundingbox[2]
),
int(
self.y
+ self._boundingbox[1]
+ self._anchor_point[1] * self._boundingbox[3]
),
)

@anchored_position.setter
def anchored_position(self, new_position):
self.x = int(new_position[0] - (self._boundingbox[2] * self._anchor_point[0]))
self.y = int(new_position[1] - (self._boundingbox[3] * self._anchor_point[1]))
self.x = int(
new_position[0]
- self._boundingbox[0]
- self._anchor_point[0] * self._boundingbox[2]
)
self.y = int(
new_position[1]
- self._boundingbox[1]
- self._anchor_point[1] * self._boundingbox[3]
)
14 changes: 7 additions & 7 deletions examples/display_text_anchored_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,39 @@

text_area_top_left = label.Label(terminalio.FONT, text=TEXT)
text_area_top_left.anchor_point = (0.0, 0.0)
text_area_top_left.anchored_position = (10, 10)
text_area_top_left.anchored_position = (0, 0)

text_area_top_middle = label.Label(terminalio.FONT, text=TEXT)
text_area_top_middle.anchor_point = (0.5, 0.0)
text_area_top_middle.anchored_position = (DISPLAY_WIDTH / 2, 10)
text_area_top_middle.anchored_position = (DISPLAY_WIDTH / 2, 0)

text_area_top_right = label.Label(terminalio.FONT, text=TEXT)
text_area_top_right.anchor_point = (1.0, 0.0)
text_area_top_right.anchored_position = (DISPLAY_WIDTH - 10, 10)
text_area_top_right.anchored_position = (DISPLAY_WIDTH, 0)

text_area_middle_left = label.Label(terminalio.FONT, text=TEXT)
text_area_middle_left.anchor_point = (0.0, 0.5)
text_area_middle_left.anchored_position = (10, DISPLAY_HEIGHT / 2)
text_area_middle_left.anchored_position = (0, DISPLAY_HEIGHT / 2)

text_area_middle_middle = label.Label(terminalio.FONT, text=TEXT)
text_area_middle_middle.anchor_point = (0.5, 0.5)
text_area_middle_middle.anchored_position = (DISPLAY_WIDTH / 2, DISPLAY_HEIGHT / 2)

text_area_middle_right = label.Label(terminalio.FONT, text=TEXT)
text_area_middle_right.anchor_point = (1.0, 0.5)
text_area_middle_right.anchored_position = (DISPLAY_WIDTH - 10, DISPLAY_HEIGHT / 2)
text_area_middle_right.anchored_position = (DISPLAY_WIDTH, DISPLAY_HEIGHT / 2)

text_area_bottom_left = label.Label(terminalio.FONT, text=TEXT)
text_area_bottom_left.anchor_point = (0.0, 1.0)
text_area_bottom_left.anchored_position = (10, DISPLAY_HEIGHT)
text_area_bottom_left.anchored_position = (0, DISPLAY_HEIGHT)

text_area_bottom_middle = label.Label(terminalio.FONT, text=TEXT)
text_area_bottom_middle.anchor_point = (0.5, 1.0)
text_area_bottom_middle.anchored_position = (DISPLAY_WIDTH / 2, DISPLAY_HEIGHT)

text_area_bottom_right = label.Label(terminalio.FONT, text=TEXT)
text_area_bottom_right.anchor_point = (1.0, 1.0)
text_area_bottom_right.anchored_position = (DISPLAY_WIDTH - 10, DISPLAY_HEIGHT)
text_area_bottom_right.anchored_position = (DISPLAY_WIDTH, DISPLAY_HEIGHT)

text_group = displayio.Group(max_size=9)
text_group.append(text_area_top_middle)
Expand Down