Skip to content

fixing label padding #187

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 1 commit into from
Mar 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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