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 @@ -131,6 +134,7 @@ def __init__(
self._padding_right = padding_right

self._scale = scale
self.base_alignment = base_alignment

if text is not None:
self._update_text(str(text))
Expand Down Expand Up @@ -161,7 +165,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 @@ -265,8 +272,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
79 changes: 79 additions & 0 deletions examples/display_text_label_align_baseline_comparison.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

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

import time
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("Helvetica-Bold-16.bdf")
BIG_FONT = bitmap_font.load_font("LeagueSpartan-Bold-16.bdf")

# Test parameters
TEXT_BACKGROUND_COLOR = 0x990099
TEXT_COLOR = 0x00FF00
X_COL_POS = [10, 90] # X position of the Label boxes
TEST_TEXT = [
"aApPqQ.",
"ppppppp",
"llllll",
"oooooo",
"ñúÇèüß",
] # Iteration text for testing
LOCALISATION_Y = [40, 80, 120, 160, 200] # Y coordinate of the text labels
NUMBER_TEXT_LABELS = len(LOCALISATION_Y) * len(
X_COL_POS
) # Calculation for Display.Group function
TIME_BETWEEN_TEXTS = 5 # Seconds
BASE_ALIGNMENT_OPTIONS = [False, True]

# Test
for behaviour in BASE_ALIGNMENT_OPTIONS:
for text_test in TEST_TEXT:
main_group = displayio.Group(max_size=NUMBER_TEXT_LABELS + 1)
for position in LOCALISATION_Y:
text = label.Label(
font=MEDIUM_FONT,
text=text_test,
color=TEXT_COLOR,
background_tight=True,
background_color=TEXT_BACKGROUND_COLOR,
x=X_COL_POS[0],
y=position,
base_alignment=behaviour,
)
main_group.append(text)
text = label.Label(
font=BIG_FONT,
text=text_test,
color=TEXT_COLOR,
background_tight=False,
background_color=TEXT_BACKGROUND_COLOR,
x=X_COL_POS[1],
y=position,
base_alignment=behaviour,
)
main_group.append(text)
display.show(main_group)
display.refresh()
time.sleep(TIME_BETWEEN_TEXTS)


bitmap = displayio.Bitmap(280, 5, 2)
palette = displayio.Palette(2)
palette[0] = 0x004400
palette[1] = 0x00FFFF
tile_grid2 = displayio.TileGrid(bitmap, pixel_shader=palette, x=10, y=160)
main_group.append(tile_grid2)

while True:
pass