Skip to content

dont use bitmap and tilegrid background for builtin fonts #71

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 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 72 additions & 53 deletions adafruit_display_text/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"""

import displayio
from fontio import BuiltinFont

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
Expand Down Expand Up @@ -98,8 +99,13 @@ def __init__(
self.y = y

self.palette = displayio.Palette(2)
self.palette[0] = 0
self.palette.make_transparent(0)
if not background_color:
self.palette[0] = 0
self.palette.make_transparent(0)
else:
if isinstance(self.font, BuiltinFont):
Copy link
Contributor

Choose a reason for hiding this comment

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

Three things, the first I know how to fix, the second needs more looking, and third item is minor.

  1. When I ran this, the palette is opaque (black), but should be transparent. For BDF loaded fonts, palette[0] should always be transparent.

I changed starting at line 101 to this:

        self.palette = displayio.Palette(2)
        if isinstance(self.font, BuiltinFont) and (background_color is not None):
            self.palette[0] = background_color
            self.palette.make_opaque(0)
        else:
            self.palette[0] = 0
            self.palette.make_transparent(0)

        self.palette[1] = color
  1. Font changing has background errors: When I start with BuiltinFont and then change to BDF fonts, then back and forth, the background doesn't update properly. I am using padding_****=-4 to better highlight these errors.

I think it has to do with palette not updating between the two font types. (Maybe move the palette updates from the __init__ into the function _update_background_color?) At the bottom is the code I'm using for debugging.

  1. I was looking through the font setter, and it looks like this line is unnecessary:
       self.height = self._font.get_bounding_box()[1]

For item #2 above, here is my debug code for changing the fonts:

"""
This examples shows the use color and background_color
"""
import time
import board
import displayio
import terminalio


# from adafruit_st7789 import ST7789
from adafruit_ili9341 import ILI9341
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font

#  Setup the SPI display

print("Starting the display...")  # goes to serial only
displayio.release_displays()


spi = board.SPI()
tft_cs = board.D9  # arbitrary, pin not used
tft_dc = board.D10
tft_backlight = board.D12
tft_reset = board.D11

while not spi.try_lock():
    spi.configure(baudrate=32000000)
spi.unlock()

display_bus = displayio.FourWire(
    spi,
    command=tft_dc,
    chip_select=tft_cs,
    reset=tft_reset,
    baudrate=32000000,
    polarity=1,
    phase=1,
)

print("spi.frequency: {}".format(spi.frequency))

DISPLAY_WIDTH = 320
DISPLAY_HEIGHT = 240

# display = ST7789(display_bus, width=240, height=240, rotation=0, rowstart=80, colstart=0)
display = ILI9341(
    display_bus,
    width=DISPLAY_WIDTH,
    height=DISPLAY_HEIGHT,
    rotation=180,
    auto_refresh=True,
)

display.show(None)

# font=terminalio.FONT # this is the Builtin fixed dimension font

#font = bitmap_font.load_font("fonts/BitstreamVeraSans-Roman-24.bdf")

font = terminalio.FONT
font2 = bitmap_font.load_font("fonts/Arial-16.bdf")
#font = bitmap_font.load_font("fonts/Fayette-HandwrittenScript-64.bdf")


text = []
text.append("none")  # no ascenders or descenders
text.append("pop quops")  # only descenders
#text.append("MONSTERs are tall")  # only ascenders
#text.append("MONSTERs ate pop quops")  # both ascenders and descenders
#text.append("MONSTER quops\nnewline quops")  # with newline

display.auto_refresh = True
myGroup = displayio.Group(max_size=6)
display.show(myGroup)

text_area = []
myPadding = -4

for i, thisText in enumerate(text):
    text_area.append(
        label.Label(
            font,
            text=thisText,
            color=0xFFFFFF,
            background_color=0xFF00FF,
            background_tight=False,
            padding_top=myPadding,
            padding_bottom=myPadding,
            padding_left=myPadding,
            padding_right=myPadding,
            max_glyphs=60, ##
            scale=1
        )
    )

    this_x = 10
    this_y = 10 + i * 40
    text_area[i].x = 10
    text_area[i].y = 3 + i * 50
    text_area[i].anchor_point = (0, 0)
    text_area[i].anchored_position = (this_x, this_y)
    myGroup.append(text_area[i])

print("background color is {}".format(text_area[0].background_color))


test_string='The quick brown fox'
starti=len(test_string)
i=starti


while True:
    #time.sleep(2)
    text_area[0].font=font
    text_area[0].text = test_string  # change some text in an existing text box
    # Note: changed text must fit within existing number of characters
    # when the Label was created
    #time.sleep(2)
    text_area[0].background_color=None
    
    time.sleep(1)
    text_area[0].text = 'None'
    text_area[0].background_color=0xFF00FF
    time.sleep(1)
    text_area[0].font=font2
    time.sleep(1)

    #i=i-1
    #if i <= 0:
    #    text_area[0].text = 'None Test'
    #    i=starti
    #    time.sleep(1)

self.palette[0] = background_color
self.palette.make_opaque(0)
self.palette[1] = color

self.height = self._font.get_bounding_box()[1]
Expand Down Expand Up @@ -170,58 +176,70 @@ def _create_background_box(self, lines, y_offset):
return tile_grid

def _update_background_color(self, new_color):

if new_color is None:
self._background_palette.make_transparent(0)
if self._added_background_tilegrid:
self.pop(0)
self._added_background_tilegrid = False
# pylint: disable=too-many-branches
if isinstance(self.font, BuiltinFont):
if new_color is not None:
self.palette[0] = new_color
self.palette.make_opaque(0)
else:
self.palette[0] = 0
self.palette.make_transparent(0)
else:
self._background_palette.make_opaque(0)
self._background_palette[0] = new_color
self._background_color = new_color

y_offset = int(
(
self._font.get_glyph(ord("M")).height
- self.text.count("\n") * self.height * self.line_spacing
)
/ 2
)
lines = self.text.count("\n") + 1

if not self._added_background_tilegrid: # no bitmap is in the self Group
# add bitmap if text is present and bitmap sizes > 0 pixels
if (
(len(self._text) > 0)
and (
self._boundingbox[2] + self._padding_left + self._padding_right > 0
if new_color is None:
self._background_palette.make_transparent(0)
if self._added_background_tilegrid:
self.pop(0)
self._added_background_tilegrid = False
else:
self._background_palette.make_opaque(0)
self._background_palette[0] = new_color
self._background_color = new_color

y_offset = int(
(
self._font.get_glyph(ord("M")).height
- self.text.count("\n") * self.height * self.line_spacing
)
and (
self._boundingbox[3] + self._padding_top + self._padding_bottom > 0
)
):
if len(self) > 0:
self.insert(0, self._create_background_box(lines, y_offset))
else:
self.append(self._create_background_box(lines, y_offset))
self._added_background_tilegrid = True

else: # a bitmap is present in the self Group
# update bitmap if text is present and bitmap sizes > 0 pixels
if (
(len(self._text) > 0)
and (
self._boundingbox[2] + self._padding_left + self._padding_right > 0
)
and (
self._boundingbox[3] + self._padding_top + self._padding_bottom > 0
)
):
self[0] = self._create_background_box(lines, y_offset)
else: # delete the existing bitmap
self.pop(0)
self._added_background_tilegrid = False
/ 2
)
lines = self.text.count("\n") + 1

if not self._added_background_tilegrid: # no bitmap is in the self Group
# add bitmap if text is present and bitmap sizes > 0 pixels
if (
(len(self._text) > 0)
and (
self._boundingbox[2] + self._padding_left + self._padding_right
> 0
)
and (
self._boundingbox[3] + self._padding_top + self._padding_bottom
> 0
)
):
if len(self) > 0:
self.insert(0, self._create_background_box(lines, y_offset))
else:
self.append(self._create_background_box(lines, y_offset))
self._added_background_tilegrid = True

else: # a bitmap is present in the self Group
# update bitmap if text is present and bitmap sizes > 0 pixels
if (
(len(self._text) > 0)
and (
self._boundingbox[2] + self._padding_left + self._padding_right
> 0
)
and (
self._boundingbox[3] + self._padding_top + self._padding_bottom
> 0
)
):
self[0] = self._create_background_box(lines, y_offset)
else: # delete the existing bitmap
self.pop(0)
self._added_background_tilegrid = False

def _update_text(
self, new_text
Expand Down Expand Up @@ -292,7 +310,8 @@ def _update_text(
self._text = new_text
self._boundingbox = (left, top, left + right, bottom - top)

self._update_background_color(self._background_color)
if not isinstance(self.font, BuiltinFont):
self._update_background_color(self._background_color)

@property
def bounding_box(self):
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# Uncomment the below if you use native CircuitPython modules such as
# digitalio, micropython and busio. List the modules you use. Without it, the
# autodoc module docs will fail to generate with a warning.
autodoc_mock_imports = ["displayio"]
autodoc_mock_imports = ["displayio", "fontio"]


intersphinx_mapping = {
Expand Down