Skip to content

Tab replacement corrections #132

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

Merged
merged 2 commits into from
Mar 10, 2021
Merged
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
6 changes: 5 additions & 1 deletion adafruit_display_text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ class LabelBase(Group):
:param bool save_text: Set True to save the text string as a constant in the
label structure. Set False to reduce memory use.
:param: bool base_alignment: when True allows to align text label to the baseline.
This is helpful when two or more labels need to be aligned to the same baseline"""
This is helpful when two or more labels need to be aligned to the same baseline
:param: (int,str) tab_replacement: tuple with tab character replace information. When
(4, " ") will indicate a tab replacement of 4 spaces, defaults to 4 spaces by
tab character"""

# pylint: disable=unused-argument, too-many-instance-attributes, too-many-locals, too-many-arguments
def __init__(
Expand All @@ -198,6 +201,7 @@ def __init__(
save_text=True, # can reduce memory use if save_text = False
scale=1,
base_alignment=False,
tab_replacement=(4, " "),
**kwargs,
):
super().__init__(max_size=1, x=x, y=y, scale=1)
Expand Down
21 changes: 16 additions & 5 deletions adafruit_display_text/bitmap_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ class Label(LabelBase):
:param bool save_text: Set True to save the text string as a constant in the
label structure. Set False to reduce memory use.
:param: bool base_alignment: when True allows to align text label to the baseline.
This is helpful when two or more labels need to be aligned to the same baseline"""
This is helpful when two or more labels need to be aligned to the same baseline
:param: (int,str) tab_replacement: tuple with tab character replace information. When
(4, " ") will indicate a tab replacement of 4 spaces, defaults to 4 spaces by
tab character"""

# pylint: disable=unused-argument, too-many-instance-attributes, too-many-locals, too-many-arguments
# pylint: disable=too-many-branches, no-self-use, too-many-statements
Expand All @@ -88,7 +91,10 @@ def __init__(self, font, **kwargs):
self.local_group
) # the local_group will always stay in the self Group

self._text = kwargs.get("text", "")
self._tab_replacement = kwargs.get("tab_replacement", (4, " "))
self._tab_text = self._tab_replacement[1] * self._tab_replacement[0]
text = kwargs.get("text", "")
self._text = self._tab_text.join(text.split("\t"))

# Create the two-color palette

Expand All @@ -114,6 +120,7 @@ def __init__(self, font, **kwargs):
save_text=kwargs.get("save_text", True),
scale=kwargs.get("scale", 1),
base_alignment=kwargs.get("base_alignment", False),
tab_replacement=kwargs.get("tab_replacement", (4, " ")),
)

def _reset_text(
Expand All @@ -133,6 +140,7 @@ def _reset_text(
save_text=None,
scale=None,
base_alignment=None,
tab_replacement=None,
):

# Store all the instance variables
Expand Down Expand Up @@ -162,13 +170,15 @@ def _reset_text(
self._save_text = save_text
if base_alignment is not None:
self.base_alignment = base_alignment
if tab_replacement is not None:
self._tab_replacement = tab_replacement

# if text is not provided as a parameter (text is None), use the previous value.
if (text is None) and self._save_text:
text = self._text

if self._save_text: # text string will be saved
self._text = text
self._text = self._tab_text.join(text.split("\t"))
else:
self._text = None # save a None value since text string is not saved

Expand Down Expand Up @@ -203,7 +213,7 @@ def _reset_text(
loose_box_y,
loose_y_offset,
) = self._text_bounding_box(
text,
self._text,
self._font,
self._line_spacing,
) # calculate the box size for a tight and loose backgrounds
Expand All @@ -226,7 +236,7 @@ def _reset_text(
# Place the text into the Bitmap
self._place_text(
self.bitmap,
text,
self._text,
self._font,
self._line_spacing,
self._padding_left - x_offset,
Expand Down Expand Up @@ -542,4 +552,5 @@ def _set_font(self, new_font):
raise RuntimeError("font is immutable when save_text is False")

def _set_text(self, new_text, scale):
new_text = self._tab_text.join(new_text.split("\t"))
self._reset_text(text=new_text, scale=self.scale)
9 changes: 8 additions & 1 deletion adafruit_display_text/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ class Label(LabelBase):
containing x,y pixel coordinates.
:param int scale: Integer value of the pixel scaling
:param bool base_alignment: when True allows to align text label to the baseline.
This is helpful when two or more labels need to be aligned to the same baseline"""
This is helpful when two or more labels need to be aligned to the same baseline
:param: (int,str) tab_replacement: tuple with tab character replace information. When
(4, " ") will indicate a tab replacement of 4 spaces, defaults to 4 spaces by
tab character"""

# pylint: disable=too-many-instance-attributes, too-many-locals
# This has a lot of getters/setters, maybe it needs cleanup.
Expand All @@ -76,6 +79,9 @@ def __init__(self, font, **kwargs):

if not max_glyphs and not text:
raise RuntimeError("Please provide a max size, or initial text")
self._tab_replacement = kwargs.get("tab_replacement", (4, " "))
self._tab_text = self._tab_replacement[1] * self._tab_replacement[0]
text = self._tab_text.join(text.split("\t"))
if not max_glyphs:
max_glyphs = len(text)
# add one to max_size for the background bitmap tileGrid
Expand Down Expand Up @@ -300,6 +306,7 @@ def _update_text(
self._update_background_color(self._background_color)

def _reset_text(self, new_text):
new_text = self._tab_text.join(new_text.split("\t"))
try:
current_anchored_position = self.anchored_position
self._update_text(str(new_text))
Expand Down