Skip to content

Commit 2865d41

Browse files
committed
variable change name to self._line_direction. Setter and getter changes. Error checking in both initial setup and setter module.
1 parent db44aa0 commit 2865d41

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-30
lines changed

adafruit_display_text/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ def __init__(
230230
self.local_group = None
231231

232232
self._text = text
233+
234+
if label_direction not in ["LTR", "RTL", "UPR", "DWR", "TTB"]:
235+
raise RuntimeError("Please provide a valid text direction")
236+
self._label_direction = label_direction
237+
233238
self.baseline = -1.0
234239

235240
self.base_alignment = base_alignment
@@ -388,3 +393,19 @@ def _set_line_spacing(self, new_line_spacing: float) -> None:
388393
@line_spacing.setter
389394
def line_spacing(self, new_line_spacing: float) -> None:
390395
self._set_line_spacing(new_line_spacing)
396+
397+
@property
398+
def label_direction(self) -> str:
399+
"""Set the text direction of the label"""
400+
return self._label_direction
401+
402+
def _set_label_direction(self, new_label_direction: str) -> None:
403+
# subclass should override this.
404+
pass
405+
406+
@label_direction.setter
407+
def label_direction(self, new_label_direction: str) -> None:
408+
"""Set the text direction of the label"""
409+
if new_label_direction not in ["LTR", "RTL", "UPR", "DWR", "TTB"]:
410+
raise RuntimeError("Please provide a valid text direction")
411+
self._set_label_direction(new_label_direction)

adafruit_display_text/label.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def __init__(self, font, **kwargs) -> None:
125125
self._padding_left = kwargs.get("padding_left", 0)
126126
self._padding_right = kwargs.get("padding_right", 0)
127127
self.base_alignment = kwargs.get("base_alignment", False)
128-
self.label_type = kwargs.get("label_direction", "LTR")
128+
self._label_direction = kwargs.get("label_direction", "LTR")
129129

130130
if text is not None:
131131
self._update_text(str(text))
@@ -150,9 +150,9 @@ def _create_background_box(self, lines: int, y_offset: int) -> None:
150150
ascent, descent = self._get_ascent_descent()
151151

152152
if (
153-
self.label_type == "UPR"
154-
or self.label_type == "DWR"
155-
or self.label_type == "TTB"
153+
self._label_direction == "UPR"
154+
or self._label_direction == "DWR"
155+
or self._label_direction == "TTB"
156156
):
157157
box_height = (
158158
self._bounding_box[3] + self._padding_top + self._padding_bottom
@@ -184,13 +184,13 @@ def _create_background_box(self, lines: int, y_offset: int) -> None:
184184
box_width = max(0, box_width) # remove any negative values
185185
box_height = max(0, box_height) # remove any negative values
186186

187-
if self.label_type == "UPR":
187+
if self._label_direction == "UPR":
188188
movx = left + x_box_offset
189189
movy = -box_height - x_box_offset
190-
elif self.label_type == "DWR":
190+
elif self._label_direction == "DWR":
191191
movx = left + x_box_offset
192192
movy = x_box_offset
193-
elif self.label_type == "TTB":
193+
elif self._label_direction == "TTB":
194194
movx = left + x_box_offset
195195
movy = x_box_offset
196196
else:
@@ -278,10 +278,10 @@ def _update_text(
278278
else:
279279
self._y_offset = self._get_ascent() // 2
280280

281-
if self.label_type == "RTL":
281+
if self._label_direction== "RTL":
282282
left = top = bottom = 0
283283
right = None
284-
elif self.label_type == "LTR":
284+
elif self._label_direction == "LTR":
285285
right = top = bottom = 0
286286
left = None
287287
else:
@@ -297,13 +297,13 @@ def _update_text(
297297
if not glyph:
298298
continue
299299

300-
if self.label_type == "LTR" or self.label_type == "RTL":
300+
if self._label_direction == "LTR" or self._label_direction == "RTL":
301301
bottom = max(bottom, y - glyph.dy + self._y_offset)
302302
if y == 0: # first line, find the Ascender height
303303
top = min(top, -glyph.height - glyph.dy + self._y_offset)
304304
position_y = y - glyph.height - glyph.dy + self._y_offset
305305

306-
if self.label_type == "LTR":
306+
if self._label_direction == "LTR":
307307
right = max(right, x + glyph.shift_x, x + glyph.width + glyph.dx)
308308
if x == 0:
309309
if left is None:
@@ -322,7 +322,7 @@ def _update_text(
322322
right = max(right, glyph.dx)
323323
position_x = x - glyph.width
324324

325-
if self.label_type == "TTB":
325+
if self._label_direction == "TTB":
326326
if x == 0:
327327
if left is None:
328328
left = glyph.dx
@@ -338,7 +338,7 @@ def _update_text(
338338
position_y = y + glyph.dy
339339
position_x = x - glyph.width // 2 + self._y_offset
340340

341-
if self.label_type == "UPR":
341+
if self._label_direction == "UPR":
342342
if x == 0:
343343
if bottom is None:
344344
bottom = -glyph.dx
@@ -351,7 +351,7 @@ def _update_text(
351351
position_y = y - glyph.width - glyph.dx
352352
position_x = x - glyph.height - glyph.dy + self._y_offset
353353

354-
if self.label_type == "DWR":
354+
if self._label_direction == "DWR":
355355
if y == 0:
356356
if top is None:
357357
top = -glyph.dx
@@ -386,10 +386,10 @@ def _update_text(
386386
y=position_y,
387387
)
388388

389-
if self.label_type == "UPR":
389+
if self._label_direction == "UPR":
390390
face.transpose_xy = True
391391
face.flip_x = True
392-
if self.label_type == "DWR":
392+
if self._label_direction == "DWR":
393393
face.transpose_xy = True
394394
face.flip_y = True
395395

@@ -399,41 +399,41 @@ def _update_text(
399399
self.local_group.append(face)
400400
tilegrid_count += 1
401401

402-
if self.label_type == "RTL":
402+
if self._label_direction == "RTL":
403403
x = x - glyph.shift_x
404-
if self.label_type == "TTB":
404+
if self._label_direction == "TTB":
405405
if glyph.height < 2:
406406
y = y + glyph.shift_x
407407
else:
408408
y = y + glyph.height + 1
409-
if self.label_type == "UPR":
409+
if self._label_direction == "UPR":
410410
y = y - glyph.shift_x
411-
if self.label_type == "DWR":
411+
if self._label_direction == "DWR":
412412
y = y + glyph.shift_x
413-
if self.label_type == "LTR":
413+
if self._label_direction == "LTR":
414414
x = x + glyph.shift_x
415415

416416
i += 1
417417

418-
if self.label_type == "LTR" and left is None:
418+
if self._label_direction == "LTR" and left is None:
419419
left = 0
420-
if self.label_type == "RTL" and right is None:
420+
if self._label_direction == "RTL" and right is None:
421421
right = 0
422-
if self.label_type == "TTB" and top is None:
422+
if self._label_direction == "TTB" and top is None:
423423
top = 0
424424

425425
while len(self.local_group) > tilegrid_count: # i:
426426
self.local_group.pop()
427427
# pylint: disable=invalid-unary-operand-type
428-
if self.label_type == "RTL":
428+
if self._label_direction == "RTL":
429429
self._bounding_box = (-left, top, left - right, bottom - top)
430-
if self.label_type == "TTB":
430+
if self._label_direction == "TTB":
431431
self._bounding_box = (left, top, right - left, bottom - top)
432-
if self.label_type == "UPR":
432+
if self._label_direction == "UPR":
433433
self._bounding_box = (left, top, right, bottom - top)
434-
if self.label_type == "DWR":
434+
if self._label_direction == "DWR":
435435
self._bounding_box = (left, top, right, bottom - top)
436-
if self.label_type == "LTR":
436+
if self._label_direction == "LTR":
437437
self._bounding_box = (left, top, right - left, bottom - top)
438438

439439
self._text = new_text
@@ -466,5 +466,10 @@ def _set_line_spacing(self, new_line_spacing: float) -> None:
466466
def _set_text(self, new_text: str, scale: int) -> None:
467467
self._reset_text(new_text)
468468

469-
def _set_background_color(self, new_color):
469+
def _set_background_color(self, new_color: int) -> None:
470470
self._update_background_color(new_color)
471+
472+
def _set_label_direction(self, new_line_direction: str) -> None:
473+
self._label_direction = new_line_direction
474+
old_text = self._text
475+
self._update_text(str(old_text))

0 commit comments

Comments
 (0)