Skip to content

Commit 5113e33

Browse files
Add dynamic row heights based on font used. (Closes #3)
Deprecate 'title_length', ignore it for now. It is no longer needed with the update to Adafruit_CircuitPython_Display_Text removing 'max_glyphs'. Refactor the scaling so that the 'text_scale' is not a multiple of the 'title_scale'. Correctly add multiple rows with the color-cycling effect. Previously all new rows had the same color.
1 parent a48e24e commit 5113e33

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

adafruit_simple_text_display.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,18 @@ class SimpleTextDisplay:
5656
VIOLET = (255, 0, 255)
5757
SKY = (0, 180, 255)
5858

59-
def __init__( # pylint: disable=too-many-arguments
59+
def __init__(
6060
self,
6161
title=None,
6262
title_color=(255, 255, 255),
6363
title_scale: int = 1,
64-
title_length: int = 80,
64+
title_length: int = 0, # Ignored - will be removed in a future version
6565
text_scale: int = 1,
6666
font=None,
6767
colors=None,
6868
display=None,
6969
):
70+
# pylint: disable=too-many-arguments, unused-argument
7071
"""Display lines of text on a display using displayio. Lines of text are created in order as
7172
shown in the example below. If you skip a number, the line will be shown blank on the
7273
display, e.g. if you include ``[0]`` and ``[2]``, the second line on the display will be
@@ -85,8 +86,7 @@ def __init__( # pylint: disable=too-many-arguments
8586
title is provided. Defaults to white (255, 255, 255).
8687
:param int title_scale: Scale the size of the title. Not necessary if no title is provided.
8788
Defaults to 1.
88-
:param int title_length: The maximum number of characters allowed in the title. Only
89-
necessary if the title is longer than the default 80 characters. Defaults to 80.
89+
:param int title_length: DEPRECATED/IGNORED - This will be removed in a future version.
9090
:param int text_scale: Scale the size of the data lines. Scales the title as well.
9191
Defaults to 1.
9292
:param ~fontio.BuiltinFont,~adafruit_bitmap_font.bdf.BDF,~adafruit_bitmap_font.pcf.PCF font:
@@ -141,56 +141,55 @@ def __init__( # pylint: disable=too-many-arguments
141141
)
142142

143143
self._colors = colors
144-
self._label = label
145144
if display is None:
146145
display = board.DISPLAY
147146
self._display = display
148147
self._font = font if font else terminalio.FONT
148+
self._text_scale = text_scale
149149

150-
self.text_group = displayio.Group(scale=text_scale)
150+
self.text_group = displayio.Group()
151151

152152
if title:
153-
# Fail gracefully if title is longer than title_length characters. Defaults to 80.
154-
if len(title) > title_length:
155-
raise ValueError(
156-
"Title character count must be less than or equal to title_length."
157-
" Default is 80."
158-
)
159-
160-
title = label.Label(
153+
title_label = label.Label(
161154
self._font,
162155
text=title,
163-
max_glyphs=title_length,
164156
color=title_color,
165157
scale=title_scale,
158+
anchor_point=(0, 0),
159+
anchored_position=(0, 0),
166160
)
167-
title.x = 0
168-
title.y = 8
169-
self._y = title.y + 18
161+
self._next_y = title_label.bounding_box[3] * title_scale
170162

171-
self.text_group.append(title)
163+
self.text_group.append(title_label)
172164
else:
173-
self._y = 3
165+
self._next_y = 0
174166

175167
self._lines = []
176-
for num in range(1):
177-
self._lines.append(self.add_text_line(color=colors[num % len(colors)]))
168+
# Add first line
169+
self._lines.append(self.add_text_line(color=colors[0]))
178170

179171
def __getitem__(self, item):
180172
"""Fetch the Nth text line Group"""
181173
if len(self._lines) - 1 < item:
182-
for _ in range(item - (len(self._lines) - 1)):
174+
for i in range(len(self._lines), item + 1):
183175
self._lines.append(
184-
self.add_text_line(color=self._colors[item % len(self._colors)])
176+
self.add_text_line(color=self._colors[i % len(self._colors)])
185177
)
186178
return self._lines[item]
187179

188180
def add_text_line(self, color=(255, 255, 255)):
189181
"""Adds a line on the display of the specified color and returns the label object."""
190-
text_label = self._label.Label(self._font, text="", color=color)
191-
text_label.x = 0
192-
text_label.y = self._y
193-
self._y = text_label.y + 13
182+
183+
text_label = label.Label(
184+
self._font,
185+
text="Myj", # Dummy value to allow bounding_box to calculate
186+
color=color,
187+
scale=self._text_scale,
188+
anchor_point=(0, 0),
189+
anchored_position=(0, self._next_y),
190+
)
191+
self._next_y += text_label.bounding_box[3] * text_label.scale
192+
text_label.text = "" # Erase the dummy value after using bounding_box
194193
self.text_group.append(text_label)
195194

196195
return text_label

0 commit comments

Comments
 (0)