Skip to content

Commit 1da9501

Browse files
authored
Merge pull request #145 from jposada202020/master
initial Commit for draft Bitmap Label directional label
2 parents 76460f4 + cc7f478 commit 1da9501

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

adafruit_display_text/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,6 @@ def __init__(
235235

236236
self._text = text
237237

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

242240
self.baseline = -1.0

adafruit_display_text/bitmap_label.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ class Label(LabelBase):
7676
This is helpful when two or more labels need to be aligned to the same baseline
7777
:param (int,str) tab_replacement: tuple with tab character replace information. When
7878
(4, " ") will indicate a tab replacement of 4 spaces, defaults to 4 spaces by
79-
tab character"""
79+
tab character
80+
:param str label_direction: string defining the label text orientation. There are 5
81+
configurations possibles ``LTR``-Left-To-Right ``RTL``-Right-To-Left
82+
``UPD``-Upside Down ``UPR``-Upwards ``DWR``-Downwards. It defaults to ``LTR``"""
8083

8184
# pylint: disable=unused-argument, too-many-instance-attributes, too-many-locals, too-many-arguments
8285
# pylint: disable=too-many-branches, no-self-use, too-many-statements
@@ -103,6 +106,13 @@ def __init__(self, font, **kwargs) -> None:
103106

104107
self.color = kwargs.get("color", 0xFFFFFF)
105108
self.background_color = kwargs.get("background_color", None)
109+
self._label_direction = kwargs.get("label_direction", "LTR")
110+
111+
if self._label_direction not in ["LTR", "RTL", "UPD", "UPR", "DWR"]:
112+
raise RuntimeError("Please provide a valid text direction")
113+
114+
if self._label_direction == "RTL":
115+
self._text = "".join(reversed(self._text))
106116

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

@@ -124,6 +134,7 @@ def __init__(self, font, **kwargs) -> None:
124134
scale=kwargs.get("scale", 1),
125135
base_alignment=kwargs.get("base_alignment", False),
126136
tab_replacement=kwargs.get("tab_replacement", (4, " ")),
137+
label_direction=kwargs.get("label_direction", "LTR"),
127138
)
128139

129140
def _reset_text(
@@ -144,6 +155,7 @@ def _reset_text(
144155
scale: int = None,
145156
base_alignment: bool = None,
146157
tab_replacement: Tuple[int, str] = None,
158+
label_direction: str = "LTR",
147159
) -> None:
148160

149161
# Store all the instance variables
@@ -175,13 +187,17 @@ def _reset_text(
175187
self.base_alignment = base_alignment
176188
if tab_replacement is not None:
177189
self._tab_replacement = tab_replacement
190+
if label_direction is not None:
191+
self._label_direction = label_direction
178192

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

183197
if self._save_text: # text string will be saved
184198
self._text = self._tab_text.join(text.split("\t"))
199+
if self._label_direction == "RTL":
200+
self._text = "".join(reversed(self._text))
185201
else:
186202
self._text = None # save a None value since text string is not saved
187203

@@ -263,6 +279,16 @@ def _reset_text(
263279
y=label_position_yoffset - y_offset - self._padding_top,
264280
)
265281

282+
if self._label_direction == "UPR":
283+
self.tilegrid.transpose_xy = True
284+
self.tilegrid.flip_x = True
285+
if self._label_direction == "DWR":
286+
self.tilegrid.transpose_xy = True
287+
self.tilegrid.flip_y = True
288+
if self._label_direction == "UPD":
289+
self.tilegrid.flip_x = True
290+
self.tilegrid.flip_y = True
291+
266292
# Clear out any items in the local_group Group, in case this is an update to
267293
# the bitmap_label
268294
for _ in self.local_group:

adafruit_display_text/label.py

+3
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ def __init__(self, font, **kwargs) -> None:
127127
self.base_alignment = kwargs.get("base_alignment", False)
128128
self._label_direction = kwargs.get("label_direction", "LTR")
129129

130+
if self._label_direction not in ["LTR", "RTL", "UPR", "DWR", "TTB"]:
131+
raise RuntimeError("Please provide a valid text direction")
132+
130133
if text is not None:
131134
self._update_text(str(text))
132135
if (kwargs.get("anchored_position", None) is not None) and (

docs/examples.rst

+18
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,21 @@ Ensure your device works with this simple test.
66
.. literalinclude:: ../examples/display_text_simpletest.py
77
:caption: examples/display_text_simpletest.py
88
:linenos:
9+
10+
Bitmap_label Simple test
11+
------------------------
12+
13+
Simple test using bitmap_label to display text
14+
15+
.. literalinclude:: ../examples/display_text_bitmap_label_simpletest.py
16+
:caption: examples/display_text_bitmap_label_simpletest.py
17+
:linenos:
18+
19+
Label vs Bitmap_label Comparison
20+
--------------------------------
21+
22+
Example to compare Label and Bitmap_Label characteristics
23+
24+
.. literalinclude:: ../examples/display_text_label_vs_bitmap_label_comparison.py
25+
:caption: examples/display_text_label_vs_bitmap_label_comparison.py
26+
:linenos:

0 commit comments

Comments
 (0)