Skip to content

Commit

Permalink
Refactor max_glyphs away (Closes #131)
Browse files Browse the repository at this point in the history
  • Loading branch information
lesamouraipourpre committed Jul 9, 2021
1 parent 4bef392 commit 3f9a6b6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 26 deletions.
21 changes: 5 additions & 16 deletions adafruit_display_text/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class Label(LabelBase):
:param Font font: A font class that has ``get_bounding_box`` and ``get_glyph``.
Must include a capital M for measuring character size.
:param str text: Text to display
:param int max_glyphs: The largest quantity of glyphs we will display
:param int color: Color of all text in RGB hex
:param int background_color: Color of the background, use `None` for transparent
:param float line_spacing: Line spacing of text to display
Expand Down Expand Up @@ -87,20 +86,13 @@ def __init__(self, font, **kwargs) -> None:

super().__init__(font, **kwargs)

max_glyphs = kwargs.get("max_glyphs", None)
text = kwargs.get("text", "")

if not max_glyphs and not text:
raise RuntimeError("Please provide a max_glyphs, or initial text")
text = self._replace_tabs(text)
if not max_glyphs:
max_glyphs = len(text)
text = self._replace_tabs(self._text)

# local_group will set the scale
self.local_group = displayio.Group(scale=kwargs.get("scale", 1))
self.append(self.local_group)

self.width = max_glyphs
self.width = len(text)
self._font = font
self._text = None

Expand Down Expand Up @@ -422,12 +414,9 @@ def _update_text(
self._set_background_color(self._background_color)

def _reset_text(self, new_text: str) -> None:
try:
current_anchored_position = self.anchored_position
self._update_text(str(self._replace_tabs(new_text)))
self.anchored_position = current_anchored_position
except RuntimeError as run_error:
raise RuntimeError("Text length exceeds max_glyphs") from run_error
current_anchored_position = self.anchored_position
self._update_text(str(self._replace_tabs(new_text)))
self.anchored_position = current_anchored_position

def _set_font(self, new_font) -> None:
old_text = self._text
Expand Down
12 changes: 4 additions & 8 deletions examples/display_text_advance_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
vertica_line = displayio.TileGrid(bitmap, pixel_shader=palette, x=0, y=110)
main_group.append(vertica_line)
# Tests
text_area = label.Label(terminalio.FONT, text="Circuit Python", max_glyphs=40)
text_area = label.Label(terminalio.FONT, text="Circuit Python")
main_group.append(text_area)
display.show(main_group)
time.sleep(TIME_PAUSE)
Expand All @@ -43,7 +43,7 @@
# Testing creating label with initial position
text_area.text = "Testing initiating without text"
try:
text_middle = label.Label(terminalio.FONT, max_glyphs=40)
text_middle = label.Label(terminalio.FONT)
except SyntaxError:
print("Fail setting-up label without text")
warning_text = label.Label(
Expand Down Expand Up @@ -121,7 +121,6 @@
BIG_FONT,
text="aaaaq~",
x=0,
max_glyphs=6,
y=display.height // 2,
color=0xFFFFFF,
background_color=0x990099,
Expand All @@ -132,7 +131,6 @@
BIG_FONT,
text="aaaaq~",
x=90,
max_glyphs=6,
y=display.height // 2,
color=0xFFFFFF,
background_color=0x990099,
Expand Down Expand Up @@ -338,7 +336,7 @@
# Testing creating label with initial position
display.show(main_group)
time.sleep(TIME_PAUSE)
text_area = bitmap_label.Label(terminalio.FONT, text="Circuit Python", max_glyphs=40)
text_area = bitmap_label.Label(terminalio.FONT, text="Circuit Python")
main_group.append(text_area)
display.show(main_group)
time.sleep(TIME_PAUSE)
Expand All @@ -349,7 +347,7 @@
time.sleep(TIME_PAUSE)
text_area.text = "Testing initiating without text"
try:
text_middle = label.Label(terminalio.FONT, max_glyphs=40)
text_middle = label.Label(terminalio.FONT)
except TypeError:
print("Fail setting-up label without text")
warning_text = label.Label(
Expand Down Expand Up @@ -426,7 +424,6 @@
BIG_FONT,
text="aaaaq~",
x=0,
max_glyphs=6,
y=display.height // 2,
color=0xFFFFFF,
background_color=0x990099,
Expand All @@ -437,7 +434,6 @@
BIG_FONT,
text="aaaaq~",
x=90,
max_glyphs=6,
y=display.height // 2,
color=0xFFFFFF,
background_color=0x990099,
Expand Down
2 changes: 0 additions & 2 deletions examples/display_text_label_vs_bitmap_label_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@
font=fontToUse,
text=long_string,
color=0x000000,
max_glyphs=len(long_string),
background_color=0xFFFF00,
padding_bottom=label2_padding,
padding_left=0,
Expand Down Expand Up @@ -192,7 +191,6 @@
font=fontToUse,
text=long_string,
color=0x000000,
max_glyphs=len(long_string),
background_color=0xFFFF00,
padding_bottom=label2_padding,
padding_left=0,
Expand Down

0 comments on commit 3f9a6b6

Please sign in to comment.