Skip to content

Commit 81998a6

Browse files
authored
Merge pull request #187 from jposada202020/fixing_label_padding
fixing label padding
2 parents 784a80b + c844aac commit 81998a6

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

adafruit_display_text/label.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def __init__(self, font: FontProtocol, **kwargs) -> None:
9999
if text is not None:
100100
self._reset_text(str(text))
101101

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

117-
if self._label_direction in ("UPR", "DWR", "TTB"):
118+
if self._label_direction in ("DWR", "UPR"):
118119
box_height = (
119-
self._bounding_box[3] + self._padding_top + self._padding_bottom
120+
self._bounding_box[3] + self._padding_right + self._padding_left
120121
)
121-
x_box_offset = -self._padding_bottom
122+
x_box_offset = -self._padding_left
122123
box_width = (
123124
(ascent + descent)
124125
+ int((lines - 1) * self._width * self._line_spacing)
125-
+ self._padding_left
126+
+ self._padding_top
127+
+ self._padding_bottom
128+
)
129+
elif self._label_direction == "TTB":
130+
box_height = (
131+
self._bounding_box[3] + self._padding_top + self._padding_bottom
132+
)
133+
x_box_offset = -self._padding_left
134+
box_width = (
135+
(ascent + descent)
136+
+ int((lines - 1) * self._height * self._line_spacing)
126137
+ self._padding_right
138+
+ self._padding_left
127139
)
128140
else:
129141
box_width = (
@@ -137,23 +149,33 @@ def _create_background_box(self, lines: int, y_offset: int) -> TileGrid:
137149
+ self._padding_bottom
138150
)
139151

152+
if self._label_direction == "DWR":
153+
padding_to_use = self._padding_bottom
154+
elif self._label_direction == "TTB":
155+
padding_to_use = self._padding_top
156+
y_offset = 0
157+
ascent = 0
158+
else:
159+
padding_to_use = self._padding_top
160+
140161
if self._base_alignment:
141-
y_box_offset = -ascent - self._padding_top
162+
y_box_offset = -ascent - padding_to_use
142163
else:
143-
y_box_offset = -ascent + y_offset - self._padding_top
164+
165+
y_box_offset = -ascent + y_offset - padding_to_use
144166

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

148170
if self._label_direction == "UPR":
149-
movx = left + x_box_offset
171+
movx = y_box_offset
150172
movy = -box_height - x_box_offset
151173
elif self._label_direction == "DWR":
152-
movx = left + x_box_offset
174+
movx = y_box_offset
153175
movy = x_box_offset
154176
elif self._label_direction == "TTB":
155-
movx = left + x_box_offset
156-
movy = x_box_offset
177+
movx = x_box_offset
178+
movy = y_box_offset
157179
else:
158180
movx = left + x_box_offset
159181
movy = y_box_offset
@@ -168,6 +190,7 @@ def _create_background_box(self, lines: int, y_offset: int) -> TileGrid:
168190

169191
return tile_grid
170192

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

0 commit comments

Comments
 (0)