Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
44d6aed
Raise an error if a required method if not overridden when called
lesamouraipourpre Jul 8, 2021
381a890
Remove references to 'max_glyphs' from __init__ and bitmap_label, it …
lesamouraipourpre Jul 8, 2021
60590f1
Refactor line_spacing
lesamouraipourpre Jul 8, 2021
9552fdc
Refactor background_tight
lesamouraipourpre Jul 8, 2021
64a7d3e
Refactor x & y
lesamouraipourpre Jul 8, 2021
889679e
Refactor padding
lesamouraipourpre Jul 8, 2021
6eb5dd0
Refactor padding
lesamouraipourpre Jul 8, 2021
ff473a4
Refactor save_text
lesamouraipourpre Jul 8, 2021
31ad121
Refactor tab_replacement
lesamouraipourpre Jul 8, 2021
b259e6b
Refactor label_direction
lesamouraipourpre Jul 9, 2021
c4d43e2
Refactor color, background_color & palette
lesamouraipourpre Jul 9, 2021
fb75d66
Refactor base_alignment
lesamouraipourpre Jul 9, 2021
4bef392
Refactor anchor_point & anchored_position
lesamouraipourpre Jul 9, 2021
3f9a6b6
Refactor max_glyphs away (Closes #131)
lesamouraipourpre Jul 9, 2021
48357d4
Refactor local_group to a protected variable.
lesamouraipourpre Jul 9, 2021
068599c
Remove and update all pylint disables
lesamouraipourpre Jul 9, 2021
aedccd9
Reduce unnecessary kwarg usage
lesamouraipourpre Jul 10, 2021
915b360
Minor tweaks to advanced example
lesamouraipourpre Jul 10, 2021
95a822a
Refactor label __init__
lesamouraipourpre Jul 10, 2021
46fec3a
Refactor baseline
lesamouraipourpre Jul 10, 2021
d557cf8
Refactor bitmap
lesamouraipourpre Jul 10, 2021
a001c02
Refactor width & height
lesamouraipourpre Jul 10, 2021
5014b3d
Refactor tilegrid
lesamouraipourpre Jul 10, 2021
58c6644
Refactor boolean to bool
lesamouraipourpre Jul 10, 2021
fbe1fbd
Tweak the PyPortal example to specify the Font directory
lesamouraipourpre Jul 15, 2021
adddcf2
Add height & width properties to LabelBase
lesamouraipourpre Jul 15, 2021
b3acf4f
Print a message if max_glyphs is passed in
lesamouraipourpre Jul 15, 2021
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
Prev Previous commit
Next Next commit
Refactor color, background_color & palette
  • Loading branch information
lesamouraipourpre committed Jul 9, 2021
commit c4d43e260cd17c3d23a23baa861e50821360ab49
41 changes: 21 additions & 20 deletions adafruit_display_text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,36 +213,36 @@ def __init__(
super().__init__(x=x, y=y, scale=1)

self._font = font
self._text = text
self._palette = Palette(2)
self._color = 0xFFFFFF
self._background_color = None
self._line_spacing = line_spacing
self._background_tight = background_tight
self._padding_top = padding_top
self._padding_bottom = padding_bottom
self._padding_left = padding_left
self._padding_right = padding_right
self._ascent, self._descent = self._get_ascent_descent()
self.palette = Palette(2)
self._color = color
self._background_color = background_color

self._bounding_box = None
self._anchor_point = anchor_point
self._anchored_position = anchored_position
self.base_alignment = base_alignment
self._label_direction = label_direction
self._tab_replacement = tab_replacement
self._tab_text = self._tab_replacement[1] * self._tab_replacement[0]

self._ascent, self._descent = self._get_ascent_descent()
self._bounding_box = None

self.color = color
self.background_color = background_color

# local group will hold background and text
# the self group scale should always remain at 1, the self.local_group will
# be used to set the scale of the label
self.local_group = None

self._text = text

self._label_direction = label_direction

self.baseline = -1.0

self.base_alignment = base_alignment
self._tab_replacement = tab_replacement
self._tab_text = self._tab_replacement[1] * self._tab_replacement[0]

if self.base_alignment:
self._y_offset = 0
else:
Expand Down Expand Up @@ -290,20 +290,21 @@ def color(self) -> int:
def color(self, new_color: int):
self._color = new_color
if new_color is not None:
self.palette[1] = new_color
self.palette.make_opaque(1)
self._palette[1] = new_color
self._palette.make_opaque(1)
else:
self.palette[1] = 0
self.palette.make_transparent(1)
self._palette[1] = 0
self._palette.make_transparent(1)

@property
def background_color(self) -> int:
"""Color of the background as an RGB hex number."""
return self._background_color

def _set_background_color(self, new_color):
# subclasses should override this
pass
raise NotImplementedError(
"{} MUST override '_set_background_color'".format(type(self))
)

@background_color.setter
def background_color(self, new_color: int) -> None:
Expand Down
16 changes: 6 additions & 10 deletions adafruit_display_text/bitmap_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ def __init__(self, font, save_text=True, **kwargs) -> None:
self._save_text = save_text
self._text = self._replace_tabs(text)

# Create the two-color palette
self.color = kwargs.get("color", 0xFFFFFF)
self.background_color = kwargs.get("background_color", None)

if self._label_direction == "RTL":
self._text = "".join(reversed(self._text))

Expand Down Expand Up @@ -200,7 +196,7 @@ def _reset_text(
box_y = box_y + self._padding_top + self._padding_bottom

# Create the bitmap and TileGrid
self.bitmap = displayio.Bitmap(box_x, box_y, len(self.palette))
self.bitmap = displayio.Bitmap(box_x, box_y, len(self._palette))

# Place the text into the Bitmap
self._place_text(
Expand All @@ -218,7 +214,7 @@ def _reset_text(

self.tilegrid = displayio.TileGrid(
self.bitmap,
pixel_shader=self.palette,
pixel_shader=self._palette,
width=1,
height=1,
tile_width=box_x,
Expand Down Expand Up @@ -546,11 +542,11 @@ def _set_text(self, new_text: str, scale: int) -> None:
def _set_background_color(self, new_color):
self._background_color = new_color
if new_color is not None:
self.palette[0] = new_color
self.palette.make_opaque(0)
self._palette[0] = new_color
self._palette.make_opaque(0)
else:
self.palette[0] = 0
self.palette.make_transparent(0)
self._palette[0] = 0
self._palette.make_transparent(0)

def _set_label_direction(self, new_label_direction: str) -> None:
self._label_direction = new_label_direction
Expand Down
33 changes: 16 additions & 17 deletions adafruit_display_text/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Label(LabelBase):
:param str text: Text to display
:param int max_glyphs: The largest quantity of glyphs we will display
:param int color: Color of all text in RGB hex
:param int background_color: Color of the background, use `None` for transparent
:param float line_spacing: Line spacing of text to display
:param bool background_tight: Set `True` only if you want background box to tightly
surround text. When set to 'True' Padding parameters will be ignored.
Expand Down Expand Up @@ -81,6 +82,9 @@ class Label(LabelBase):
# This has a lot of getters/setters, maybe it needs cleanup.

def __init__(self, font, **kwargs) -> None:
self._background_palette = displayio.Palette(1)
self._added_background_tilegrid = False

super().__init__(font, **kwargs)

max_glyphs = kwargs.get("max_glyphs", None)
Expand All @@ -105,14 +109,8 @@ def __init__(self, font, **kwargs) -> None:
self._bounding_box = None

# Create the two-color text palette
self.palette = displayio.Palette(2)
self.palette[0] = 0
self.palette.make_transparent(0)
self.color = kwargs.get("color", 0xFFFFFF)

self._background_color = kwargs.get("background_color", None)
self._background_palette = displayio.Palette(1)
self._added_background_tilegrid = False
self._palette[0] = 0
self._palette.make_transparent(0)

self.base_alignment = kwargs.get("base_alignment", False)

Expand Down Expand Up @@ -192,9 +190,9 @@ def _create_background_box(self, lines: int, y_offset: int) -> None:

return tile_grid

def _update_background_color(self, new_color: int) -> None:
def _set_background_color(self, new_color: int) -> None:
"""Private class function that allows updating the font box background color
:param new_color: int color as an RGB hex number."""
:param int new_color: color as an RGB hex number."""

if new_color is None:
self._background_palette.make_transparent(0)
Expand All @@ -209,6 +207,10 @@ def _update_background_color(self, new_color: int) -> None:
lines = self._text.rstrip("\n").count("\n") + 1
y_offset = self._ascent // 2

if self._bounding_box is None:
# Still in initialization
return

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 (
Expand Down Expand Up @@ -354,7 +356,7 @@ def _update_text(
# pylint: disable=unexpected-keyword-arg
face = displayio.TileGrid(
glyph.bitmap,
pixel_shader=self.palette,
pixel_shader=self._palette,
default_tile=glyph.tile_index,
tile_width=glyph.width,
tile_height=glyph.height,
Expand All @@ -363,7 +365,7 @@ def _update_text(
except TypeError:
face = displayio.TileGrid(
glyph.bitmap,
pixel_shader=self.palette,
pixel_shader=self._palette,
default_tile=glyph.tile_index,
tile_width=glyph.width,
tile_height=glyph.height,
Expand Down Expand Up @@ -423,8 +425,8 @@ def _update_text(

self._text = new_text

if self.background_color is not None:
self._update_background_color(self._background_color)
if self._background_color is not None:
self._set_background_color(self._background_color)

def _reset_text(self, new_text: str) -> None:
try:
Expand All @@ -450,9 +452,6 @@ def _set_line_spacing(self, new_line_spacing: float) -> None:
def _set_text(self, new_text: str, scale: int) -> None:
self._reset_text(new_text)

def _set_background_color(self, new_color: int) -> None:
self._update_background_color(new_color)

def _set_label_direction(self, new_label_direction: str) -> None:
self._label_direction = new_label_direction
self._update_text(str(self._text))
Expand Down