adafruit / Adafruit_CircuitPython_Display_Text

Library to display text using displayio
MIT License
57 stars 38 forks source link

Changing logic to include tab replacement and glyph calculation #121

Closed jposada202020 closed 3 years ago

jposada202020 commented 3 years ago

Present changes allows the library to change the "\t" tabs by 4 spaces. The library then will use all the normal logic to take care of these. Present solution was chosen for two main reasons:

  1. This could be done by a re expression in the form of
    if re.search('\t',  text):
    text = text.replace('\t', '    ')

    However this will imply import the re library. Not sure that this is what why want.

  2. We could use a Post treatment using glyph.shift_x in the _update_text module
    if not glyph:
    if character == 't':
        glyph = self._font.get_glyph(ord(" "))
        glyph.shift_x = glyph.shift_x * 4
    else:
    continue

    At the end, we need to return the parameter to the original value with. This needs to be done as the library uses glyph<s parameters for calculations and display:

    if character == '\t':
    glyph.shift_x = glyph.shift_x // 4

    This is a hack to disguise a tab as an space with a different shift_x parameter as "\t" will not be found as a glyph. So less Pythonic.

This was tested with the following code:

# SPDX-FileCopyrightText: 2021 Jose David Montoya
# SPDX-License-Identifier: MIT

"""
This example shows the changes to verify the tab.
"""

from blinka_displayio_pygamedisplay import PyGameDisplay
import displayio
from adafruit_bitmap_font import bitmap_font
import terminalio
from adafruit_display_text import label

display = PyGameDisplay(width=320, height=240)

# Test parameters
TEXT_BACKGROUND_COLOR = [0xe0433, 0x990099, 0xd6d714, 0xd73014, 0x1415d7, 0x71646f]
TEXT_COLOR = 0x00FF00
FONT_TESTS = [bitmap_font.load_font("IBMPlexMono-Medium-24_jep.bdf"),
              bitmap_font.load_font("LeagueSpartan-Bold-16.bdf"),
              terminalio.FONT]

main_group = displayio.Group(max_size=8)
text = ["\tñ    \t Mp", "    ñ\tp     M", "\tp Mñ\t    ", " tab\ttest "]
ypos = [100, 130, 160, 190]
text_testing = list()
[text_testing.append(label.Label(FONT_TESTS[0],
                                 text=text_iter,
                                 background_color=TEXT_BACKGROUND_COLOR[1],
                                 x=40, y=y_iter))
                                 for text_iter, y_iter in zip(text, ypos)]

[main_group.append(_) for _ in text_testing]

display.show(main_group)

while True:
    pass

This produce the following result image

jposada202020 commented 3 years ago

@FoamyGuy @kmatch98 please review the followings changes. This will hopefully address partially issue #118. Thanks for your comments.

jposada202020 commented 3 years ago

@kmatch98 yes I am in, I will do the bitmap_label too. just wanted to be open about the different approaches and let you folks discuss if needed :).

kmatch98 commented 3 years ago

Looks good to me. Thanks for the quick update!

jposada202020 commented 3 years ago

@FoamyGuy Changes are made for the tab parameter. @kmatch98 I changed also for the bitmap_label :). We discuss in the stream after a suggestion from Hugo, that would be good to have that, that way the user can decide how many spaces, dashes or other characters he want the replace the \t for. I was thinking about the setter, but in the same line of thought as Foamyguy, I would like to keep the library simple, and only work if some users ask for that feature as a setter. Thanks.

jposada202020 commented 3 years ago

PR closed as changes were introduced in release 2.15.4 - Fix for tab characters