Skip to content

Commit f0d0447

Browse files
committed
base_alignment anchor point
1 parent 2b4f1ad commit f0d0447

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

adafruit_display_text/__init__.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ def __init__(
221221
self.local_group = None
222222

223223
self._text = text
224+
self._baseline = -1.0
225+
226+
self.base_alignment = base_alignment
227+
228+
if self.base_alignment:
229+
self._y_offset = 0
230+
else:
231+
self._y_offset = self._get_ascent() // 2
224232

225233
def _get_ascent_descent(self):
226234
""" Private function to calculate ascent and descent font values """
@@ -298,7 +306,10 @@ def anchor_point(self):
298306

299307
@anchor_point.setter
300308
def anchor_point(self, new_anchor_point):
301-
self._anchor_point = new_anchor_point
309+
if new_anchor_point[1] == self._baseline:
310+
self._anchor_point = (new_anchor_point[0], -1.0)
311+
else:
312+
self._anchor_point = new_anchor_point
302313
self.anchored_position = (
303314
self._anchored_position
304315
) # update the anchored_position using setter
@@ -319,11 +330,17 @@ def anchored_position(self, new_position):
319330
- (self._bounding_box[0] * self.scale)
320331
- round(self._anchor_point[0] * (self._bounding_box[2] * self.scale))
321332
)
322-
self.y = int(
323-
new_position[1]
324-
- (self._bounding_box[1] * self.scale)
325-
- round(self._anchor_point[1] * self._bounding_box[3] * self.scale)
326-
)
333+
if self._anchor_point[1] == self._baseline:
334+
self.y = int(
335+
new_position[1]
336+
- (self._y_offset * self.scale)
337+
)
338+
else:
339+
self.y = int(
340+
new_position[1]
341+
- (self._bounding_box[1] * self.scale)
342+
- round(self._anchor_point[1] * self._bounding_box[3] * self.scale)
343+
)
327344

328345
@property
329346
def scale(self):

adafruit_display_text/label.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ def _update_text(
238238
i = 0
239239
tilegrid_count = i
240240
if self.base_alignment:
241-
y_offset = 0
241+
self._y_offset = 0
242242
else:
243-
y_offset = self._get_ascent() // 2
243+
self._y_offset = self._get_ascent() // 2
244244

245245
right = top = bottom = 0
246246
left = None
@@ -260,9 +260,9 @@ def _update_text(
260260
else:
261261
left = min(left, glyph.dx)
262262
if y == 0: # first line, find the Ascender height
263-
top = min(top, -glyph.height - glyph.dy + y_offset)
264-
bottom = max(bottom, y - glyph.dy + y_offset)
265-
position_y = y - glyph.height - glyph.dy + y_offset
263+
top = min(top, -glyph.height - glyph.dy + self._y_offset)
264+
bottom = max(bottom, y - glyph.dy + self._y_offset)
265+
position_y = y - glyph.height - glyph.dy + self._y_offset
266266
position_x = x + glyph.dx
267267
if glyph.width > 0 and glyph.height > 0:
268268
try:

0 commit comments

Comments
 (0)