Skip to content

initial Commit for draft Bitmap Label directional label #145

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 13 commits into from
Mar 28, 2021
2 changes: 0 additions & 2 deletions adafruit_display_text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,6 @@ def __init__(

self._text = text

if label_direction not in ["LTR", "RTL", "UPR", "DWR", "TTB"]:
raise RuntimeError("Please provide a valid text direction")
self._label_direction = label_direction

self.baseline = -1.0
Expand Down
28 changes: 27 additions & 1 deletion adafruit_display_text/bitmap_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ class Label(LabelBase):
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"""
tab character
:param str label_direction: string defining the label text orientation. There are 5
configurations possibles ``LTR``-Left-To-Right ``RTL``-Right-To-Left
``UPD``-Upside Down ``UPR``-Upwards ``DWR``-Downwards. It defaults to ``LTR``"""

# 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 @@ -103,6 +106,13 @@ def __init__(self, font, **kwargs) -> None:

self.color = kwargs.get("color", 0xFFFFFF)
self.background_color = kwargs.get("background_color", None)
self._label_direction = kwargs.get("label_direction", "LTR")

if self._label_direction not in ["LTR", "RTL", "UPD", "UPR", "DWR"]:
raise RuntimeError("Please provide a valid text direction")

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

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

Expand All @@ -124,6 +134,7 @@ def __init__(self, font, **kwargs) -> None:
scale=kwargs.get("scale", 1),
base_alignment=kwargs.get("base_alignment", False),
tab_replacement=kwargs.get("tab_replacement", (4, " ")),
label_direction=kwargs.get("label_direction", "LTR"),
)

def _reset_text(
Expand All @@ -144,6 +155,7 @@ def _reset_text(
scale: int = None,
base_alignment: bool = None,
tab_replacement: Tuple[int, str] = None,
label_direction: str = "LTR",
) -> None:

# Store all the instance variables
Expand Down Expand Up @@ -175,13 +187,17 @@ def _reset_text(
self.base_alignment = base_alignment
if tab_replacement is not None:
self._tab_replacement = tab_replacement
if label_direction is not None:
self._label_direction = label_direction

# 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 = self._tab_text.join(text.split("\t"))
if self._label_direction == "RTL":
self._text = "".join(reversed(self._text))
else:
self._text = None # save a None value since text string is not saved

Expand Down Expand Up @@ -263,6 +279,16 @@ def _reset_text(
y=label_position_yoffset - y_offset - self._padding_top,
)

if self._label_direction == "UPR":
self.tilegrid.transpose_xy = True
self.tilegrid.flip_x = True
if self._label_direction == "DWR":
self.tilegrid.transpose_xy = True
self.tilegrid.flip_y = True
if self._label_direction == "UPD":
self.tilegrid.flip_x = True
self.tilegrid.flip_y = True

# Clear out any items in the local_group Group, in case this is an update to
# the bitmap_label
for _ in self.local_group:
Expand Down
3 changes: 3 additions & 0 deletions adafruit_display_text/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ def __init__(self, font, **kwargs) -> None:
self.base_alignment = kwargs.get("base_alignment", False)
self._label_direction = kwargs.get("label_direction", "LTR")

if self._label_direction not in ["LTR", "RTL", "UPR", "DWR", "TTB"]:
raise RuntimeError("Please provide a valid text direction")

if text is not None:
self._update_text(str(text))
if (kwargs.get("anchored_position", None) is not None) and (
Expand Down
18 changes: 18 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,21 @@ Ensure your device works with this simple test.
.. literalinclude:: ../examples/display_text_simpletest.py
:caption: examples/display_text_simpletest.py
:linenos:

Bitmap_label Simple test
------------------------

Simple test using bitmap_label to display text

.. literalinclude:: ../examples/display_text_bitmap_label_simpletest.py
:caption: examples/display_text_bitmap_label_simpletest.py
:linenos:

Label vs Bitmap_label Comparison
--------------------------------

Example to compare Label and Bitmap_Label characteristics

.. literalinclude:: ../examples/display_text_label_vs_bitmap_label_comparison.py
:caption: examples/display_text_label_vs_bitmap_label_comparison.py
:linenos: