Skip to content

Changing logic to include tab replacement and glyph calculation #121

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

Closed
wants to merge 7 commits into from
Closed

Changing logic to include tab replacement and glyph calculation #121

wants to merge 7 commits into from

Conversation

jposada202020
Copy link
Contributor

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
Copy link
Contributor Author

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

@@ -91,7 +91,9 @@ def __init__(
if not max_glyphs and not text:
raise RuntimeError("Please provide a max size, or initial text")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could save a line and just put the text = " ".join(text.split("\t")) before the if not max_glyphs: statement.

It does this calculation either way, so just do it once and then do the max_glyphs check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, good point, I will change it and re-do the test just to be 100% sure. Thanks @kmatch98 :)

Copy link
Contributor

@kmatch98 kmatch98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jposada202020 this works as expected.

Just one minor suggestion on the code. Also, if you are interested, will you please apply similar "\t" handling for bitmap_label?

Thanks for adding this in!

@jposada202020
Copy link
Contributor Author

@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
Copy link
Contributor

Looks good to me. Thanks for the quick update!

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. I tested with Blinka_Displayio and PyPortal.

I wonder if it will throw someone off that the string they get back might be different from the input string if the access the text property. It may be worthwhile eventually to add a parameter to control this. But I'm not sure if the complexity of that would be worth it unless folks are really having a problem with it.

I think we need to wait for the pylint fix to merge maybe so that actions will pass.

@jposada202020
Copy link
Contributor Author

@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
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants