From 865e6a7c784ea740f4f78e522e58ad510acad43c Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Thu, 16 Jul 2020 21:00:57 -0500 Subject: [PATCH 1/5] dont use bitmap and tilegrid background for builtin fonts --- adafruit_display_text/label.py | 121 ++++++++++++++++++--------------- docs/conf.py | 2 +- 2 files changed, 68 insertions(+), 55 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 251ba17..0765eaa 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -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 not background_color: + self.palette[0] = 0 + self.palette.make_transparent(0) + else: + self.palette[0] = background_color + self.palette.make_opaque(0) self.palette[1] = color self.height = self._font.get_bounding_box()[1] @@ -170,58 +175,65 @@ 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 + 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 @@ -292,7 +304,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): @@ -395,4 +408,4 @@ def anchored_position(self, new_position): + round((self._boundingbox[3] * self._scale) / 2.0) ) self.x = new_x - self.y = new_y + self.y = new_y \ No newline at end of file diff --git a/docs/conf.py b/docs/conf.py index 004c2d1..df546df 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -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 = { From 18d43fea8fc48dce9ce709cae8178cef2afe9463 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Fri, 17 Jul 2020 20:37:33 -0500 Subject: [PATCH 2/5] black and pylint --- adafruit_display_text/label.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 0765eaa..a051da2 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -175,6 +175,7 @@ def _create_background_box(self, lines, y_offset): return tile_grid def _update_background_color(self, new_color): + # pylint: disable=too-many-branches if isinstance(self.font, BuiltinFont): if new_color is not None: self.palette[0] = new_color @@ -207,10 +208,12 @@ def _update_background_color(self, new_color): if ( (len(self._text) > 0) and ( - self._boundingbox[2] + self._padding_left + self._padding_right > 0 + self._boundingbox[2] + self._padding_left + self._padding_right + > 0 ) and ( - self._boundingbox[3] + self._padding_top + self._padding_bottom > 0 + self._boundingbox[3] + self._padding_top + self._padding_bottom + > 0 ) ): if len(self) > 0: @@ -224,10 +227,12 @@ def _update_background_color(self, new_color): if ( (len(self._text) > 0) and ( - self._boundingbox[2] + self._padding_left + self._padding_right > 0 + self._boundingbox[2] + self._padding_left + self._padding_right + > 0 ) and ( - self._boundingbox[3] + self._padding_top + self._padding_bottom > 0 + self._boundingbox[3] + self._padding_top + self._padding_bottom + > 0 ) ): self[0] = self._create_background_box(lines, y_offset) @@ -408,4 +413,4 @@ def anchored_position(self, new_position): + round((self._boundingbox[3] * self._scale) / 2.0) ) self.x = new_x - self.y = new_y \ No newline at end of file + self.y = new_y From 33fe6380514087816f29f6d261357dba2b2893aa Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Sat, 18 Jul 2020 16:12:33 -0500 Subject: [PATCH 3/5] only set background palette color if its a builtin font in constructor. --- adafruit_display_text/label.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index a051da2..670c5ae 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -103,8 +103,9 @@ def __init__( self.palette[0] = 0 self.palette.make_transparent(0) else: - self.palette[0] = background_color - self.palette.make_opaque(0) + if isinstance(self.font, BuiltinFont): + self.palette[0] = background_color + self.palette.make_opaque(0) self.palette[1] = color self.height = self._font.get_bounding_box()[1] From 8ea95e8a2db929af49a5ab54d07afd2322606f8e Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Sun, 19 Jul 2020 17:44:46 -0500 Subject: [PATCH 4/5] update the background as needed when changing fonts. Set the background correctly in init --- adafruit_display_text/label.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 670c5ae..389ff53 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -99,13 +99,12 @@ def __init__( self.y = y self.palette = displayio.Palette(2) - if not background_color: + 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) - else: - if isinstance(self.font, BuiltinFont): - self.palette[0] = background_color - self.palette.make_opaque(0) self.palette[1] = color self.height = self._font.get_bounding_box()[1] @@ -368,6 +367,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 + ): + 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 = "" From 71d210b75c82b04151c70e6fcf800bac5d4bc1cf Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Tue, 21 Jul 2020 20:52:22 -0500 Subject: [PATCH 5/5] dont create a background bitmap if background_color is None --- adafruit_display_text/label.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 389ff53..cdd08c1 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -310,7 +310,8 @@ def _update_text( self._boundingbox = (left, top, left + right, bottom - top) if not isinstance(self.font, BuiltinFont): - self._update_background_color(self._background_color) + if self._background_color: + self._update_background_color(self._background_color) @property def bounding_box(self):