Skip to content

Commit ec7b69a

Browse files
author
Margaret Matocha
committed
Merge remote-tracking branch 'upstream/master'
2 parents 83f43de + e191c8a commit ec7b69a

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

adafruit_display_text/label.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
169169

170170
x += glyph.shift_x
171171

172-
# TODO skip this for control sequences or non-qables.
172+
# TODO skip this for control sequences or non-printables.
173173
i += 1
174174
old_c += 1
175-
# skip all non-prinables in the old string
175+
# skip all non-printables in the old string
176176
while (
177177
self._text
178178
and old_c < len(self._text)
@@ -238,7 +238,9 @@ def text(self):
238238

239239
@text.setter
240240
def text(self, new_text):
241+
current_anchored_position = self.anchored_position
241242
self._update_text(str(new_text))
243+
self.anchored_position = current_anchored_position
242244

243245
@property
244246
def font(self):
@@ -248,11 +250,13 @@ def font(self):
248250
@font.setter
249251
def font(self, new_font):
250252
old_text = self._text
253+
current_anchored_position = self.anchored_position
251254
self._text = ""
252255
self._font = new_font
253256
bounds = self._font.get_bounding_box()
254257
self.height = bounds[1]
255258
self._update_text(str(old_text))
259+
self.anchored_position = current_anchored_position
256260

257261
@property
258262
def anchor_point(self):
@@ -263,18 +267,36 @@ def anchor_point(self):
263267

264268
@anchor_point.setter
265269
def anchor_point(self, new_anchor_point):
270+
current_anchored_position = self.anchored_position
266271
self._anchor_point = new_anchor_point
272+
self.anchored_position = current_anchored_position
267273

268274
@property
269275
def anchored_position(self):
270276
"""Position relative to the anchor_point. Tuple containing x,y
271277
pixel coordinates."""
272278
return (
273-
self.x - self._boundingbox[2] * self._anchor_point[0],
274-
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+
),
275289
)
276290

277291
@anchored_position.setter
278292
def anchored_position(self, new_position):
279-
self.x = int(new_position[0] - (self._boundingbox[2] * self._anchor_point[0]))
280-
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)