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 all 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
140 changes: 87 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,12 @@ def __init__(
self.y = y

self.palette = displayio.Palette(2)
self.palette[0] = 0
self.palette.make_transparent(0)
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

self.height = self._font.get_bounding_box()[1]
Expand Down Expand Up @@ -170,58 +175,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
)
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
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
)
):
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 +309,9 @@ 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):
if self._background_color:
self._update_background_color(self._background_color)
Comment on lines +312 to +314
Copy link
Member

Choose a reason for hiding this comment

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

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


@property
def bounding_box(self):
Expand Down Expand Up @@ -349,6 +368,21 @@ def font(self):

@font.setter
def font(self, new_font):
if (
isinstance(new_font, BuiltinFont)
or not isinstance(new_font, BuiltinFont)
and self.background_color is None
):
Comment on lines +371 to +375
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (
isinstance(new_font, BuiltinFont)
or not isinstance(new_font, BuiltinFont)
and self.background_color is None
):
if self.background_color is None:

if self._added_background_tilegrid:
self.pop(0)
self._added_background_tilegrid = False
if isinstance(new_font, BuiltinFont) and (self.background_color is not None):
self.palette[0] = self.background_color
self.palette.make_opaque(0)
else:
self.palette[0] = 0
self.palette.make_transparent(0)

old_text = self._text
current_anchored_position = self.anchored_position
self._text = ""
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