Skip to content

Commit e191c8a

Browse files
authored
Merge pull request #43 from kmatch98/fix_anchoring
Fix font getter/setter and fix anchor_position getter/setter, anchor_point setter and display_text_anchored_position example
2 parents f26a881 + 6695e3c commit e191c8a

File tree

2 files changed

+56
-18
lines changed

2 files changed

+56
-18
lines changed

adafruit_display_text/label.py

100644100755
Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class Label(displayio.Group):
5858
:param int color: Color of all text in RGB hex
5959
:param double line_spacing: Line spacing of text to display"""
6060

61+
# pylint: disable=too-many-instance-attributes
62+
# This has a lot of getters/setters, maybe it needs cleanup.
63+
6164
def __init__(
6265
self,
6366
font,
@@ -77,7 +80,7 @@ def __init__(
7780
max_glyphs = len(text)
7881
super().__init__(max_size=max_glyphs, **kwargs)
7982
self.width = max_glyphs
80-
self.font = font
83+
self._font = font
8184
self._text = None
8285
self._anchor_point = (0, 0)
8386
self.x = x
@@ -94,7 +97,7 @@ def __init__(
9497
self._transparent_background = True
9598
self.palette[1] = color
9699

97-
bounds = self.font.get_bounding_box()
100+
bounds = self._font.get_bounding_box()
98101
self.height = bounds[1]
99102
self._line_spacing = line_spacing
100103
self._boundingbox = None
@@ -109,19 +112,18 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
109112
old_c = 0
110113
y_offset = int(
111114
(
112-
self.font.get_glyph(ord("M")).height
115+
self._font.get_glyph(ord("M")).height
113116
- new_text.count("\n") * self.height * self.line_spacing
114117
)
115118
/ 2
116119
)
117-
# print("y offset from baseline", y_offset)
118120
left = right = top = bottom = 0
119121
for character in new_text:
120122
if character == "\n":
121123
y += int(self.height * self._line_spacing)
122124
x = 0
123125
continue
124-
glyph = self.font.get_glyph(ord(character))
126+
glyph = self._font.get_glyph(ord(character))
125127
if not glyph:
126128
continue
127129
right = max(right, x + glyph.width)
@@ -170,13 +172,13 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
170172
# TODO skip this for control sequences or non-printables.
171173
i += 1
172174
old_c += 1
173-
# skip all non-prinables in the old string
175+
# skip all non-printables in the old string
174176
while (
175177
self._text
176178
and old_c < len(self._text)
177179
and (
178180
self._text[old_c] == "\n"
179-
or not self.font.get_glyph(ord(self._text[old_c]))
181+
or not self._font.get_glyph(ord(self._text[old_c]))
180182
)
181183
):
182184
old_c += 1
@@ -236,7 +238,25 @@ def text(self):
236238

237239
@text.setter
238240
def text(self, new_text):
241+
current_anchored_position = self.anchored_position
239242
self._update_text(str(new_text))
243+
self.anchored_position = current_anchored_position
244+
245+
@property
246+
def font(self):
247+
"""Font to use for text display."""
248+
return self._font
249+
250+
@font.setter
251+
def font(self, new_font):
252+
old_text = self._text
253+
current_anchored_position = self.anchored_position
254+
self._text = ""
255+
self._font = new_font
256+
bounds = self._font.get_bounding_box()
257+
self.height = bounds[1]
258+
self._update_text(str(old_text))
259+
self.anchored_position = current_anchored_position
240260

241261
@property
242262
def anchor_point(self):
@@ -247,18 +267,36 @@ def anchor_point(self):
247267

248268
@anchor_point.setter
249269
def anchor_point(self, new_anchor_point):
270+
current_anchored_position = self.anchored_position
250271
self._anchor_point = new_anchor_point
272+
self.anchored_position = current_anchored_position
251273

252274
@property
253275
def anchored_position(self):
254276
"""Position relative to the anchor_point. Tuple containing x,y
255277
pixel coordinates."""
256278
return (
257-
self.x - self._boundingbox[2] * self._anchor_point[0],
258-
self.y - self._boundingbox[3] * self._anchor_point[1],
279+
int(
280+
self.x
281+
+ self._boundingbox[0]
282+
+ self._anchor_point[0] * self._boundingbox[2]
283+
),
284+
int(
285+
self.y
286+
+ self._boundingbox[1]
287+
+ self._anchor_point[1] * self._boundingbox[3]
288+
),
259289
)
260290

261291
@anchored_position.setter
262292
def anchored_position(self, new_position):
263-
self.x = int(new_position[0] - (self._boundingbox[2] * self._anchor_point[0]))
264-
self.y = int(new_position[1] - (self._boundingbox[3] * self._anchor_point[1]))
293+
self.x = int(
294+
new_position[0]
295+
- self._boundingbox[0]
296+
- self._anchor_point[0] * self._boundingbox[2]
297+
)
298+
self.y = int(
299+
new_position[1]
300+
- self._boundingbox[1]
301+
- self._anchor_point[1] * self._boundingbox[3]
302+
)

examples/display_text_anchored_position.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,39 @@
1212

1313
text_area_top_left = label.Label(terminalio.FONT, text=TEXT)
1414
text_area_top_left.anchor_point = (0.0, 0.0)
15-
text_area_top_left.anchored_position = (10, 10)
15+
text_area_top_left.anchored_position = (0, 0)
1616

1717
text_area_top_middle = label.Label(terminalio.FONT, text=TEXT)
1818
text_area_top_middle.anchor_point = (0.5, 0.0)
19-
text_area_top_middle.anchored_position = (DISPLAY_WIDTH / 2, 10)
19+
text_area_top_middle.anchored_position = (DISPLAY_WIDTH / 2, 0)
2020

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

2525
text_area_middle_left = label.Label(terminalio.FONT, text=TEXT)
2626
text_area_middle_left.anchor_point = (0.0, 0.5)
27-
text_area_middle_left.anchored_position = (10, DISPLAY_HEIGHT / 2)
27+
text_area_middle_left.anchored_position = (0, DISPLAY_HEIGHT / 2)
2828

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

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

3737
text_area_bottom_left = label.Label(terminalio.FONT, text=TEXT)
3838
text_area_bottom_left.anchor_point = (0.0, 1.0)
39-
text_area_bottom_left.anchored_position = (10, DISPLAY_HEIGHT)
39+
text_area_bottom_left.anchored_position = (0, DISPLAY_HEIGHT)
4040

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

4545
text_area_bottom_right = label.Label(terminalio.FONT, text=TEXT)
4646
text_area_bottom_right.anchor_point = (1.0, 1.0)
47-
text_area_bottom_right.anchored_position = (DISPLAY_WIDTH - 10, DISPLAY_HEIGHT)
47+
text_area_bottom_right.anchored_position = (DISPLAY_WIDTH, DISPLAY_HEIGHT)
4848

4949
text_group = displayio.Group(max_size=9)
5050
text_group.append(text_area_top_middle)

0 commit comments

Comments
 (0)