-
Notifications
You must be signed in to change notification settings - Fork 38
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
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
865e6a7
dont use bitmap and tilegrid background for builtin fonts
FoamyGuy 18d43fe
black and pylint
FoamyGuy 33fe638
only set background palette color if its a builtin font in constructor.
FoamyGuy 8ea95e8
update the background as needed when changing fonts. Set the backgrou…
FoamyGuy 71d210b
dont create a background bitmap if background_color is None
FoamyGuy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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" | ||||||||||||||
|
@@ -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] | ||||||||||||||
|
@@ -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 | ||||||||||||||
|
@@ -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) | ||||||||||||||
|
||||||||||||||
@property | ||||||||||||||
def bounding_box(self): | ||||||||||||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
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 = "" | ||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.