Skip to content

Commit 6a18806

Browse files
committed
Pylint and Black
1 parent 8478f71 commit 6a18806

File tree

1 file changed

+39
-172
lines changed

1 file changed

+39
-172
lines changed

adafruit_display_text/label.py

Lines changed: 39 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ class Label(LabelBase):
6666
This is helpful when two or more labels need to be aligned to the same baseline
6767
:param: (int,str) tab_replacement: tuple with tab character replace information. When
6868
(4, " ") will indicate a tab replacement of 4 spaces, defaults to 4 spaces by
69-
tab character
70-
:param: str label_direction: string defining the label text orientation. There are 5
71-
configurations possibles ``LTR``:Left-To-Right ``RTL``:Right-To-Left
72-
``TTB``:TTB Top-To-Bottom ``UPR``:Upwards ``DWR``:Downwards. It defaults to ``STR``"""
69+
tab character"""
7370

7471
# pylint: disable=too-many-instance-attributes, too-many-locals
7572
# This has a lot of getters/setters, maybe it needs cleanup.
@@ -125,7 +122,6 @@ def __init__(self, font, **kwargs) -> None:
125122
self._padding_left = kwargs.get("padding_left", 0)
126123
self._padding_right = kwargs.get("padding_right", 0)
127124
self.base_alignment = kwargs.get("base_alignment", False)
128-
self.label_type = kwargs.get("label_direction", "LTR")
129125

130126
if text is not None:
131127
self._update_text(str(text))
@@ -140,6 +136,7 @@ def _create_background_box(self, lines: int, y_offset: int) -> None:
140136
:param y_offset: int y pixel bottom coordinate for the background_box"""
141137

142138
left = self._bounding_box[0]
139+
143140
if self._background_tight: # draw a tight bounding box
144141
box_width = self._bounding_box[2]
145142
box_height = self._bounding_box[3]
@@ -149,33 +146,14 @@ def _create_background_box(self, lines: int, y_offset: int) -> None:
149146
else: # draw a "loose" bounding box to include any ascenders/descenders.
150147
ascent, descent = self._get_ascent_descent()
151148

152-
if (
153-
self.label_type == "UPR"
154-
or self.label_type == "DWR"
155-
or self.label_type == "TTB"
156-
):
157-
box_height = (
158-
self._bounding_box[3] + self._padding_top + self._padding_bottom
159-
)
160-
x_box_offset = -self._padding_bottom
161-
box_width = (
162-
(ascent + descent)
163-
+ int((lines - 1) * self.width * self._line_spacing)
164-
+ self._padding_left
165-
+ self._padding_right
166-
)
167-
else:
168-
box_width = (
169-
self._bounding_box[2] + self._padding_left + self._padding_right
170-
)
171-
x_box_offset = -self._padding_left
172-
box_height = (
173-
(ascent + descent)
174-
+ int((lines - 1) * self.height * self._line_spacing)
175-
+ self._padding_top
176-
+ self._padding_bottom
177-
)
178-
149+
box_width = self._bounding_box[2] + self._padding_left + self._padding_right
150+
x_box_offset = -self._padding_left
151+
box_height = (
152+
(ascent + descent)
153+
+ int((lines - 1) * self.height * self._line_spacing)
154+
+ self._padding_top
155+
+ self._padding_bottom
156+
)
179157
if self.base_alignment:
180158
y_box_offset = -ascent - self._padding_top
181159
else:
@@ -184,30 +162,17 @@ def _create_background_box(self, lines: int, y_offset: int) -> None:
184162
box_width = max(0, box_width) # remove any negative values
185163
box_height = max(0, box_height) # remove any negative values
186164

187-
if self.label_type == "UPR":
188-
movx = left + x_box_offset
189-
movy = -box_height - x_box_offset
190-
elif self.label_type == "DWR":
191-
movx = left + x_box_offset
192-
movy = x_box_offset
193-
elif self.label_type == "TTB":
194-
movx = left + x_box_offset
195-
movy = x_box_offset
196-
else:
197-
movx = left + x_box_offset
198-
movy = y_box_offset
199-
200165
background_bitmap = displayio.Bitmap(box_width, box_height, 1)
201166
tile_grid = displayio.TileGrid(
202167
background_bitmap,
203168
pixel_shader=self._background_palette,
204-
x=movx,
205-
y=movy,
169+
x=left + x_box_offset,
170+
y=y_box_offset,
206171
)
207172

208173
return tile_grid
209174

210-
def _update_background_color(self, new_color: int) -> None:
175+
def _update_background_color(self, new_color):
211176
"""Private class function that allows updating the font box background color
212177
:param new_color: int color as an RGB hex number."""
213178

@@ -262,10 +227,9 @@ def _update_background_color(self, new_color: int) -> None:
262227
self.local_group.pop(0)
263228
self._added_background_tilegrid = False
264229

265-
# pylint: disable = too-many-branches, too-many-statements
266230
def _update_text(
267-
self, new_text: str
268-
) -> None: # pylint: disable=too-many-locals ,too-many-branches, too-many-statements
231+
self, new_text
232+
): # pylint: disable=too-many-locals ,too-many-branches, too-many-statements
269233
x = 0
270234
y = 0
271235
if self._added_background_tilegrid:
@@ -274,19 +238,12 @@ def _update_text(
274238
i = 0
275239
tilegrid_count = i
276240
if self.base_alignment:
277-
y_offset = 0
241+
self._y_offset = 0
278242
else:
279-
y_offset = self._get_ascent() // 2
280-
281-
if self.label_type == "RTL":
282-
left = top = bottom = 0
283-
right = None
284-
elif self.label_type == "LTR":
285-
right = top = bottom = 0
286-
left = None
287-
else:
288-
top = right = left = 0
289-
bottom = 0
243+
self._y_offset = self._get_ascent() // 2
244+
245+
right = top = bottom = 0
246+
left = None
290247

291248
for character in new_text:
292249
if character == "\n":
@@ -296,74 +253,17 @@ def _update_text(
296253
glyph = self._font.get_glyph(ord(character))
297254
if not glyph:
298255
continue
299-
300-
if self.label_type == "LTR" or self.label_type == "RTL":
301-
bottom = max(bottom, y - glyph.dy + y_offset)
302-
if y == 0: # first line, find the Ascender height
303-
top = min(top, -glyph.height - glyph.dy + y_offset)
304-
position_y = y - glyph.height - glyph.dy + y_offset
305-
306-
if self.label_type == "LTR":
307-
right = max(right, x + glyph.shift_x, x + glyph.width + glyph.dx)
308-
if x == 0:
309-
if left is None:
310-
left = glyph.dx
311-
else:
312-
left = min(left, glyph.dx)
313-
position_x = x + glyph.dx
256+
right = max(right, x + glyph.shift_x, x + glyph.width + glyph.dx)
257+
if x == 0:
258+
if left is None:
259+
left = glyph.dx
314260
else:
315-
left = max(
316-
left, abs(x) + glyph.shift_x, abs(x) + glyph.width + glyph.dx
317-
)
318-
if x == 0:
319-
if right is None:
320-
right = glyph.dx
321-
else:
322-
right = max(right, glyph.dx)
323-
position_x = x - glyph.width
324-
325-
if self.label_type == "TTB":
326-
if x == 0:
327-
if left is None:
328-
left = glyph.dx
329-
else:
330-
left = min(left, glyph.dx)
331-
if y == 0:
332-
top = min(top, -glyph.dy)
333-
334-
bottom = max(bottom, y + glyph.height, y + glyph.height + glyph.dy)
335-
right = max(
336-
right, x + glyph.width + glyph.dx, x + glyph.shift_x + glyph.dx
337-
)
338-
position_y = y + glyph.dy
339-
position_x = x - glyph.width // 2 + y_offset
340-
341-
if self.label_type == "UPR":
342-
if x == 0:
343-
if bottom is None:
344-
bottom = -glyph.dx
345-
346-
if y == 0: # first line, find the Ascender height
347-
bottom = min(bottom, -glyph.dy)
348-
left = min(left, x - glyph.height + y_offset)
349-
top = min(top, y - glyph.width - glyph.dx, y - glyph.shift_x)
350-
right = max(right, x + glyph.height, x + glyph.height - glyph.dy)
351-
position_y = y - glyph.width - glyph.dx
352-
position_x = x - glyph.height - glyph.dy + y_offset
353-
354-
if self.label_type == "DWR":
355-
if y == 0:
356-
if top is None:
357-
top = -glyph.dx
358-
top = min(top, -glyph.dx)
359-
if x == 0:
360-
left = min(left, -glyph.dy)
361-
left = min(left, x, x - glyph.dy - y_offset)
362-
bottom = max(bottom, y + glyph.width + glyph.dx, y + glyph.shift_x)
363-
right = max(right, x + glyph.height)
364-
position_y = y + glyph.dx
365-
position_x = x + glyph.dy - y_offset
366-
261+
left = min(left, glyph.dx)
262+
if y == 0: # first line, find the Ascender height
263+
top = min(top, -glyph.height - glyph.dy + self._y_offset)
264+
bottom = max(bottom, y - glyph.dy + self._y_offset)
265+
position_y = y - glyph.height - glyph.dy + self._y_offset
266+
position_x = x + glyph.dx
367267
if glyph.width > 0 and glyph.height > 0:
368268
try:
369269
# pylint: disable=unexpected-keyword-arg
@@ -385,60 +285,27 @@ def _update_text(
385285
x=position_x,
386286
y=position_y,
387287
)
388-
389-
if self.label_type == "UPR":
390-
face.transpose_xy = True
391-
face.flip_x = True
392-
if self.label_type == "DWR":
393-
face.transpose_xy = True
394-
face.flip_y = True
395-
396288
if tilegrid_count < len(self.local_group):
397289
self.local_group[tilegrid_count] = face
398290
else:
399291
self.local_group.append(face)
400292
tilegrid_count += 1
401-
if self.label_type == "RTL":
402-
x = x - glyph.shift_x
403-
if self.label_type == "TTB":
404-
if glyph.height < 2:
405-
y = y + glyph.shift_x
406-
else:
407-
y = y + glyph.height + 1
408-
if self.label_type == "UPR":
409-
y = y - glyph.shift_x
410-
if self.label_type == "DWR":
411-
y = y + glyph.shift_x
412-
if self.label_type == "LTR":
413-
x = x + glyph.shift_x
293+
x += glyph.shift_x
414294
i += 1
295+
# Remove the rest
415296

416-
if self.label_type == "LTR" and left is None:
297+
if left is None:
417298
left = 0
418-
if self.label_type == "RTL" and right is None:
419-
right = 0
420-
if self.label_type == "TTB" and top is None:
421-
top = 0
422299

423300
while len(self.local_group) > tilegrid_count: # i:
424301
self.local_group.pop()
425-
# pylint: disable=invalid-unary-operand-type
426-
if self.label_type == "RTL":
427-
self._bounding_box = (-left, top, left - right, bottom - top)
428-
if self.label_type == "TTB":
429-
self._bounding_box = (left, top, right - left, bottom - top)
430-
if self.label_type == "UPR":
431-
self._bounding_box = (left, top, right, bottom - top)
432-
if self.label_type == "DWR":
433-
self._bounding_box = (left, top, right, bottom - top)
434-
if self.label_type == "LTR":
435-
self._bounding_box = (left, top, right - left, bottom - top)
436-
437302
self._text = new_text
303+
self._bounding_box = (left, top, right - left, bottom - top)
304+
438305
if self.background_color is not None:
439306
self._update_background_color(self._background_color)
440307

441-
def _reset_text(self, new_text: str) -> None:
308+
def _reset_text(self, new_text):
442309
new_text = self._tab_text.join(new_text.split("\t"))
443310
try:
444311
current_anchored_position = self.anchored_position
@@ -447,7 +314,7 @@ def _reset_text(self, new_text: str) -> None:
447314
except RuntimeError as run_error:
448315
raise RuntimeError("Text length exceeds max_glyphs") from run_error
449316

450-
def _set_font(self, new_font) -> None:
317+
def _set_font(self, new_font):
451318
old_text = self._text
452319
current_anchored_position = self.anchored_position
453320
self._text = ""
@@ -456,9 +323,9 @@ def _set_font(self, new_font) -> None:
456323
self._update_text(str(old_text))
457324
self.anchored_position = current_anchored_position
458325

459-
def _set_line_spacing(self, new_line_spacing: float) -> None:
326+
def _set_line_spacing(self, new_line_spacing):
460327
self._line_spacing = new_line_spacing
461328
self.text = self._text # redraw the box
462329

463-
def _set_text(self, new_text: str, scale: int) -> None:
330+
def _set_text(self, new_text, scale):
464331
self._reset_text(new_text)

0 commit comments

Comments
 (0)