diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index f87dc17..5549871 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -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. @@ -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: @@ -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)) @@ -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 @@ -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 diff --git a/examples/display_text_label_align_baseline_comparison.py b/examples/display_text_label_align_baseline_comparison.py new file mode 100644 index 0000000..5ea1ee4 --- /dev/null +++ b/examples/display_text_label_align_baseline_comparison.py @@ -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