diff --git a/adafruit_display_text/bitmap_label.py b/adafruit_display_text/bitmap_label.py index bb5c723..f90a56b 100755 --- a/adafruit_display_text/bitmap_label.py +++ b/adafruit_display_text/bitmap_label.py @@ -68,7 +68,9 @@ class Label(displayio.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 tuple(int, str): 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 @@ -96,6 +98,7 @@ def __init__( save_text=True, # can reduce memory use if save_text = False scale=1, base_alignment=False, + tab_replacement=(4, " "), **kwargs, ): @@ -121,7 +124,8 @@ def __init__( ) # the local_group will always stay in the self Group self._font = font - self._text = text + self.tab_text = tab_replacement[1] * tab_replacement[0] + self._text = self.tab_text.join(text.split("\t")) # Create the two-color palette self.palette = displayio.Palette(2) @@ -150,6 +154,7 @@ def __init__( save_text=save_text, scale=scale, base_alignment=base_alignment, + tab_replacement=tab_replacement, ) def _reset_text( @@ -169,6 +174,7 @@ def _reset_text( save_text=None, scale=None, base_alignment=None, + tab_replacement=None, ): # Store all the instance variables @@ -198,13 +204,14 @@ 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 @@ -239,7 +246,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 @@ -262,7 +269,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, @@ -632,6 +639,7 @@ def text(self): @text.setter # Cannot set color or background color with text setter, use separate setter def text(self, new_text): + new_text = self.tab_text.join(new_text.split("\t")) self._reset_text(text=new_text, scale=self.scale) @property diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 5549871..7d449a3 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -61,7 +61,9 @@ class Label(displayio.Group): 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 tuple(int, str): 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. @@ -86,14 +88,17 @@ def __init__( anchored_position=None, scale=1, base_alignment=False, + tab_replacement=(4, " "), **kwargs ): if not max_glyphs and not text: raise RuntimeError("Please provide a max size, or initial text") + self.tab_text = tab_replacement[1] * 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 + # add one to max_size for the background bitmap tileGrid # instance the Group # self Group will contain a single local_group which contains a Group (self.local_group) # which contains a TileGrid @@ -386,6 +391,7 @@ def text(self): @text.setter def 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))