Skip to content

New parameter and example creation for testing and future reference #115

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 10 commits into from
Feb 19, 2021
17 changes: 13 additions & 4 deletions adafruit_display_text/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ class Label(displayio.Group):
(E.g. (0,0) is top left, (1.0, 0.5): is middle right.)
:param (int,int) anchored_position: Position relative to the anchor_point. Tuple
containing x,y pixel coordinates.
:param int scale: Integer value of the pixel scaling"""
:param int scale: Integer value of the pixel scaling
:param bool base_alignment: when True allows to align text label to the baseline.
This is helpful when two or more labels need to be aligned to the same baseline"""

# pylint: disable=too-many-instance-attributes, too-many-locals
# This has a lot of getters/setters, maybe it needs cleanup.
Expand All @@ -83,6 +85,7 @@ def __init__(
anchor_point=None,
anchored_position=None,
scale=1,
base_alignment=False,
**kwargs
):
if not max_glyphs and not text:
Expand Down Expand Up @@ -129,6 +132,7 @@ def __init__(
self._padding_bottom = padding_bottom
self._padding_left = padding_left
self._padding_right = padding_right
self.base_alignment = base_alignment

if text is not None:
self._update_text(str(text))
Expand Down Expand Up @@ -159,7 +163,10 @@ def _create_background_box(self, lines, y_offset):
+ self._padding_top
+ self._padding_bottom
)
y_box_offset = -ascent + y_offset - self._padding_top
if self.base_alignment:
y_box_offset = -ascent - self._padding_top
else:
y_box_offset = -ascent + y_offset - self._padding_top

box_width = max(0, box_width) # remove any negative values
box_height = max(0, box_height) # remove any negative values
Expand Down Expand Up @@ -263,8 +270,10 @@ def _update_text(
else:
i = 0
tilegrid_count = i

y_offset = self._get_ascent() // 2
if self.base_alignment:
y_offset = 0
else:
y_offset = self._get_ascent() // 2

right = top = bottom = 0
left = None
Expand Down
75 changes: 75 additions & 0 deletions examples/display_text_label_align_baseline_comparison.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# SPDX-FileCopyrightText: 2021 Jose David Montoya for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
This example shows the use of base_alignment parameter.
"""

import board
import displayio
from adafruit_bitmap_font import bitmap_font
from adafruit_display_text import label


display = board.DISPLAY

# Font definition. You can choose any two fonts available in your system
MEDIUM_FONT = bitmap_font.load_font("LeagueSpartan-Bold-16.bdf")
BIG_FONT = bitmap_font.load_font("IBMPlexMono-Medium-24_jep.bdf")

TEXT_RIGHT = "MG"
TEXT_LEFT = "32.47"

main_group = displayio.Group()

# Create labels
# Base Alignment parameter False
left_text = label.Label(
BIG_FONT,
text=TEXT_LEFT,
color=0x000000,
background_color=0x999999,
x=10,
y=50,
base_alignment=False,
)
main_group.append(left_text)

right_text = label.Label(
MEDIUM_FONT,
text=TEXT_RIGHT,
color=0x000000,
background_color=0x999999,
x=80,
y=50,
base_alignment=False,
)
main_group.append(right_text)

# Base Alignment parameter True
left_text_aligned = label.Label(
BIG_FONT,
text=TEXT_LEFT,
color=0x000000,
background_color=0x999999,
x=10,
y=100,
base_alignment=True,
)
main_group.append(left_text_aligned)

right_text_aligned = label.Label(
MEDIUM_FONT,
text=TEXT_RIGHT,
color=0x000000,
background_color=0x999999,
x=80,
y=100,
base_alignment=True,
)

main_group.append(right_text_aligned)
display.show(main_group)

while True:
pass