Skip to content

Commit

Permalink
fixing label padding
Browse files Browse the repository at this point in the history
  • Loading branch information
jposada202020 committed Mar 11, 2023
1 parent 966c3ad commit c844aac
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions adafruit_display_text/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def __init__(self, font: FontProtocol, **kwargs) -> None:
if text is not None:
self._reset_text(str(text))

# pylint: disable=too-many-branches
def _create_background_box(self, lines: int, y_offset: int) -> TileGrid:
"""Private Class function to create a background_box
:param lines: int number of lines
Expand All @@ -114,16 +115,27 @@ def _create_background_box(self, lines: int, y_offset: int) -> TileGrid:
else: # draw a "loose" bounding box to include any ascenders/descenders.
ascent, descent = self._ascent, self._descent

if self._label_direction in ("UPR", "DWR", "TTB"):
if self._label_direction in ("DWR", "UPR"):
box_height = (
self._bounding_box[3] + self._padding_top + self._padding_bottom
self._bounding_box[3] + self._padding_right + self._padding_left
)
x_box_offset = -self._padding_bottom
x_box_offset = -self._padding_left
box_width = (
(ascent + descent)
+ int((lines - 1) * self._width * self._line_spacing)
+ self._padding_left
+ self._padding_top
+ self._padding_bottom
)
elif self._label_direction == "TTB":
box_height = (
self._bounding_box[3] + self._padding_top + self._padding_bottom
)
x_box_offset = -self._padding_left
box_width = (
(ascent + descent)
+ int((lines - 1) * self._height * self._line_spacing)
+ self._padding_right
+ self._padding_left
)
else:
box_width = (
Expand All @@ -137,23 +149,33 @@ def _create_background_box(self, lines: int, y_offset: int) -> TileGrid:
+ self._padding_bottom
)

if self._label_direction == "DWR":
padding_to_use = self._padding_bottom
elif self._label_direction == "TTB":
padding_to_use = self._padding_top
y_offset = 0
ascent = 0
else:
padding_to_use = self._padding_top

if self._base_alignment:
y_box_offset = -ascent - self._padding_top
y_box_offset = -ascent - padding_to_use
else:
y_box_offset = -ascent + y_offset - self._padding_top

y_box_offset = -ascent + y_offset - padding_to_use

box_width = max(0, box_width) # remove any negative values
box_height = max(0, box_height) # remove any negative values

if self._label_direction == "UPR":
movx = left + x_box_offset
movx = y_box_offset
movy = -box_height - x_box_offset
elif self._label_direction == "DWR":
movx = left + x_box_offset
movx = y_box_offset
movy = x_box_offset
elif self._label_direction == "TTB":
movx = left + x_box_offset
movy = x_box_offset
movx = x_box_offset
movy = y_box_offset
else:
movx = left + x_box_offset
movy = y_box_offset
Expand All @@ -168,6 +190,7 @@ def _create_background_box(self, lines: int, y_offset: int) -> TileGrid:

return tile_grid

# pylint: enable=too-many-branches
def _set_background_color(self, new_color: Optional[int]) -> None:
"""Private class function that allows updating the font box background color
Expand Down

0 comments on commit c844aac

Please sign in to comment.