Skip to content

Commit 895c06d

Browse files
authored
Merge pull request #115 from jposada202020/base_alignement_proposal
New parameter and example creation for testing and future reference
2 parents 913c82e + 448ae32 commit 895c06d

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

adafruit_display_text/label.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ class Label(displayio.Group):
5959
(E.g. (0,0) is top left, (1.0, 0.5): is middle right.)
6060
:param (int,int) anchored_position: Position relative to the anchor_point. Tuple
6161
containing x,y pixel coordinates.
62-
:param int scale: Integer value of the pixel scaling"""
62+
:param int scale: Integer value of the pixel scaling
63+
:param bool base_alignment: when True allows to align text label to the baseline.
64+
This is helpful when two or more labels need to be aligned to the same baseline"""
6365

6466
# pylint: disable=too-many-instance-attributes, too-many-locals
6567
# This has a lot of getters/setters, maybe it needs cleanup.
@@ -83,6 +85,7 @@ def __init__(
8385
anchor_point=None,
8486
anchored_position=None,
8587
scale=1,
88+
base_alignment=False,
8689
**kwargs
8790
):
8891
if not max_glyphs and not text:
@@ -129,6 +132,7 @@ def __init__(
129132
self._padding_bottom = padding_bottom
130133
self._padding_left = padding_left
131134
self._padding_right = padding_right
135+
self.base_alignment = base_alignment
132136

133137
if text is not None:
134138
self._update_text(str(text))
@@ -159,7 +163,10 @@ def _create_background_box(self, lines, y_offset):
159163
+ self._padding_top
160164
+ self._padding_bottom
161165
)
162-
y_box_offset = -ascent + y_offset - self._padding_top
166+
if self.base_alignment:
167+
y_box_offset = -ascent - self._padding_top
168+
else:
169+
y_box_offset = -ascent + y_offset - self._padding_top
163170

164171
box_width = max(0, box_width) # remove any negative values
165172
box_height = max(0, box_height) # remove any negative values
@@ -263,8 +270,10 @@ def _update_text(
263270
else:
264271
i = 0
265272
tilegrid_count = i
266-
267-
y_offset = self._get_ascent() // 2
273+
if self.base_alignment:
274+
y_offset = 0
275+
else:
276+
y_offset = self._get_ascent() // 2
268277

269278
right = top = bottom = 0
270279
left = None
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# SPDX-FileCopyrightText: 2021 Jose David Montoya for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
This example shows the use of base_alignment parameter.
6+
"""
7+
8+
import board
9+
import displayio
10+
from adafruit_bitmap_font import bitmap_font
11+
from adafruit_display_text import label
12+
13+
14+
display = board.DISPLAY
15+
16+
# Font definition. You can choose any two fonts available in your system
17+
MEDIUM_FONT = bitmap_font.load_font("LeagueSpartan-Bold-16.bdf")
18+
BIG_FONT = bitmap_font.load_font("IBMPlexMono-Medium-24_jep.bdf")
19+
20+
TEXT_RIGHT = "MG"
21+
TEXT_LEFT = "32.47"
22+
23+
main_group = displayio.Group()
24+
25+
# Create labels
26+
# Base Alignment parameter False
27+
left_text = label.Label(
28+
BIG_FONT,
29+
text=TEXT_LEFT,
30+
color=0x000000,
31+
background_color=0x999999,
32+
x=10,
33+
y=50,
34+
base_alignment=False,
35+
)
36+
main_group.append(left_text)
37+
38+
right_text = label.Label(
39+
MEDIUM_FONT,
40+
text=TEXT_RIGHT,
41+
color=0x000000,
42+
background_color=0x999999,
43+
x=80,
44+
y=50,
45+
base_alignment=False,
46+
)
47+
main_group.append(right_text)
48+
49+
# Base Alignment parameter True
50+
left_text_aligned = label.Label(
51+
BIG_FONT,
52+
text=TEXT_LEFT,
53+
color=0x000000,
54+
background_color=0x999999,
55+
x=10,
56+
y=100,
57+
base_alignment=True,
58+
)
59+
main_group.append(left_text_aligned)
60+
61+
right_text_aligned = label.Label(
62+
MEDIUM_FONT,
63+
text=TEXT_RIGHT,
64+
color=0x000000,
65+
background_color=0x999999,
66+
x=80,
67+
y=100,
68+
base_alignment=True,
69+
)
70+
71+
main_group.append(right_text_aligned)
72+
display.show(main_group)
73+
74+
while True:
75+
pass

0 commit comments

Comments
 (0)