Skip to content

Updated background color handling for label #49

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 21 commits into from
Jun 6, 2020

Conversation

kmatch98
Copy link
Contributor

@kmatch98 kmatch98 commented Jun 4, 2020

When using imported BDF font files with proportional typefaces, the current version of label with applied background color only covers the glyphs but not the whitespace. Also, spaces provide some errant dots. See image:
IMG_0653

This PR creates an additional colored bitmap tileGrid as the bottom layer below the text that is the size of the bounding box around the glyph text. The following three images show examples of the proposed PR with differing text.

Note: The bounding box is a simple minimum bounding box to fit the whole set of glyphs. This means that text with no ascenders and descenders (see second image below mere a or o) will have a smaller bounding box than other lines with taller letters.

Possible enhancements:

  • Draw a bounding box the same height for all characters, to cover the space of ascenders and descenders. This would be important when writing multiple lines of text, since it may be obvious to the viewer that the height of background text boxes differs based on the text in them.
  • To avoid excess background fill regions (see last image) print each line separately so the bounding boxes will not extend past the right edge of the text.

IMG_0659
IMG_0660
IMG_0664

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.

I confirmed this issue and tested the fix successfully on a PyPortal with this slightly modified background color example:

"""
This examples shows the use color and background_color.
Modified to use BDF font.
"""
import time
import board
import terminalio
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font

text = " Color Background Hello world"
font = bitmap_font.load_font("/Helvetica-Bold-16.bdf")
text_area = label.Label(
    font, text=text, color=0x0000FF, background_color=0xFFAA00
)
text_area.x = 10
text_area.y = 50

print("background color is {:06x}".format(text_area.background_color))

board.DISPLAY.show(text_area)

time.sleep(2)
text_area.background_color = 0xFF0000
print("background color is {:06x}".format(text_area.background_color))

while True:
    pass

I would love to eventually see options for adding padding, especially top and bottom. I think this can come in a future PR though. I am happy to have this fix in now to add immediate utility to BDF font based labels.

Thank you @kmatch98

@FoamyGuy FoamyGuy requested a review from a team June 5, 2020 01:48
Margaret Matocha added 4 commits June 5, 2020 12:30
…ed options for padding, including *background_tight* Boolean and padding_top/bottom/left/right optional parameters
@kmatch98
Copy link
Contributor Author

kmatch98 commented Jun 5, 2020

Thanks for the feedback. I eliminated the chance for possible multiple insert issue identified by @jepler .

Per @FoamyGuy request I added options for padding:

  • If padding_tight = True, then the bounding box is the minimum to cover the text. That is, the box size will depend upon what characters are inside the box. Any of the padding_* variables are ignored.
  • If padding_tight = False then the bounding box height will cover all ascenders and descenders of the current font. Also, variable padding can be added or subtracted to this padding using the following variables: padding_top, padding_bottom, padding_left, padding_right.

Limitations: For multiline text strings the background box will be the width of the widest line.

Examples:
padding_tight = True
background_tight=True

padding_tight = False and no additional padding
background_tight=False, no padding

padding_tight = False and 5 pixels of additional padding on all four sides
background_tight=False, 5 pixel padding

Margaret Matocha added 2 commits June 5, 2020 13:12
Copy link
Contributor Author

@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.

Since there are no getter/setter functions, I hid these variables inside the class definition:

background_tight
padding_top
padding_bottom
padding_left
padding_right

@kmatch98 kmatch98 requested review from FoamyGuy and jepler June 5, 2020 19:09
@kmatch98
Copy link
Contributor Author

kmatch98 commented Jun 5, 2020

@FoamyGuy the new example added uses a separately-connected display. If you have a chance, please verify it with your simpler code for starting the display for devices with built-in. (Currently my daughter is using our pyPortal full-time for her studies, so I can't really verify.) Feel free to change the example to the simpler version, since that will be easier for users to understand.

@jepler
Copy link
Contributor

jepler commented Jun 5, 2020

Thanks! This looks good at a human level but note that pylint has something to tell you. Find where "Build CI / test" is failing, and look at the Details. let us know if you get stuck, the formatting checks can be very ... specific.

@jepler
Copy link
Contributor

jepler commented Jun 5, 2020

adafruit_display_text/label.py:64:4: R0914: Too many local variables (16/15) (too-many-locals)

If there are any pylint diagnostics that are too onerous to deal with, they can be ignored. Let me know if you need a pointer to docs on how to do this.

@kmatch98
Copy link
Contributor Author

kmatch98 commented Jun 6, 2020

I resolved all the error messages. Let me know if you see any other changes required.

@FoamyGuy
Copy link
Contributor

FoamyGuy commented Jun 6, 2020

I successfully tested the new library and example code (slightly modified) on PyPortal.

The functionality looks good to me.

That comment on line 322 looks to be present still. If it's unneeded we can remove it. I'm not sure I understand fully what it does right now thought. I can tinker with it a bit in the morning perhaps.

@kmatch98
Copy link
Contributor Author

kmatch98 commented Jun 6, 2020

I don't see what you mean about line 322. I pushed a few commits just now. @jepler also said he's seeing a bunch of comments still in the file. I checked but I don't see anything in the latest revision. Let me know if I'm missing something.

That comment on line 322 looks to be present still. If it's unneeded we can remove it. I'm not sure I understand fully what it does right now thought. I can tinker with it a bit in the morning perhaps.

@FoamyGuy
Copy link
Contributor

FoamyGuy commented Jun 6, 2020

In the font setter. This line:

# bounds = self._font.get_bounding_box()

@kmatch98
Copy link
Contributor Author

kmatch98 commented Jun 6, 2020

In the font setter. This line:

# bounds = self._font.get_bounding_box()

Squashed it. Dunno how I overlooked it. Thanks for the find. I just committed the update and it passed the checks. Thanks for all of your help and patience! Hope this will be of help to someone out there. After a couple of days of this I'm ready to move on to something else...

Copy link
Contributor

@jepler jepler left a comment

Choose a reason for hiding this comment

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

Yup, somehow I got led astray about the latest content of the PR. Sorry about that.

@jepler jepler merged commit 1f2bf53 into adafruit:master Jun 6, 2020
@FoamyGuy
Copy link
Contributor

FoamyGuy commented Jun 6, 2020

@kmatch98 Thank you for sticking through all the changes. It's really nice to have the background color working correctly with BDF fonts. And I really like the new padding functionality!

@kmatch98
Copy link
Contributor Author

kmatch98 commented Jun 6, 2020

Thanks guys for helping me through this and suggesting all the improvements. Thank y’all. Maybe we’ll see more text based projects on the show n tell!

@evaherrada
Copy link
Collaborator

@kmatch98 Yeah, to echo what @FoamyGuy said this is such an awesome change. Thanks for tackling it.

adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jun 9, 2020
Updating https://github.com/adafruit/Adafruit_CircuitPython_AHTx0 to 1.0.1 from 1.0.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_AHTx0#1 from kattni/example-i2c-update

Updating https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI to 3.4.1 from 3.4.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_ESP32SPI#99 from Flameeyes/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_ILI9341 to 1.2.1 from 1.2.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_ILI9341#22 from makermelissa/master
  > Merge pull request adafruit/Adafruit_CircuitPython_ILI9341#21 from adafruit/setup-py-disabled
  > build.yml: add black formatting check

Updating https://github.com/adafruit/Adafruit_CircuitPython_LPS2X to 2.0.0 from 1.1.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_LPS2X#4 from adafruit/lps22
  > build.yml: add black formatting check

Updating https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k to 1.4.0 from 1.3.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#20 from xorbit/master
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#14 from adafruit/pylint-update

Updating https://github.com/adafruit/Adafruit_CircuitPython_AzureIoT to 2.2.1 from 2.2.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_AzureIoT#16 from jimbobbennett/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_Display_Text to 2.7.0 from 2.6.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_Display_Text#49 from kmatch98/background
  > Merge pull request adafruit/Adafruit_CircuitPython_Display_Text#48 from FoamyGuy/better_group_full_error

Updating https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation to 2.3.0 from 2.2.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_LED_Animation#39 from rhooper/bugfixes
  > Merge pull request adafruit/Adafruit_CircuitPython_LED_Animation#38 from rhooper/sequence-one-shot
  > Merge pull request adafruit/Adafruit_CircuitPython_LED_Animation#40 from rhooper/pixelgrid

Updating https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT to 3.1.0 from 3.0.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#38 from brentru/on-message-enhancements

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Added the following libraries: Adafruit_CircuitPython_ILI9341
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.

4 participants