Skip to content

Cache fonts for lower memory usage #32

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 4 commits into from
Dec 5, 2020
Merged
Changes from 1 commit
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
30 changes: 23 additions & 7 deletions adafruit_magtag/magtag.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ def __init__(
self._json_path = None
self.json_path = json_path

# Font Cache
self._fonts = {}

self._regexp_path = regexp_path

self.splash = self.graphics.splash
Expand Down Expand Up @@ -150,10 +153,7 @@ def add_text(
position is relative to the label
:param bool is_data: If True, fetch will attempt to update the label
"""
if text_font is terminalio.FONT:
self._text_font.append(text_font)
else:
self._text_font.append(bitmap_font.load_font(text_font))

if not text_wrap:
text_wrap = 0
if not text_maxlen:
Expand All @@ -172,6 +172,7 @@ def add_text(
if self._debug:
print("Init text area")
self._text.append(None)
self._text_font.append(self._load_font(text_font))
self._text_color.append(self.html_color_convert(text_color))
self._text_position.append(text_position)
self._text_wrap.append(text_wrap)
Expand All @@ -186,6 +187,19 @@ def add_text(

# pylint: enable=too-many-arguments

def _load_font(self, font):
"""
Load and cache a font if not previously loaded
Return the key of the cached font
"""
if font is terminalio.FONT:
if "terminal" not in self._fonts:
self._fonts["terminal"] = terminalio.FONT
return "terminal"
if font not in self._fonts:
self._fonts[font] = bitmap_font.load_font(font)
return font

@staticmethod
def html_color_convert(color):
"""Convert an HTML color code to an integer
Expand Down Expand Up @@ -226,8 +240,8 @@ def preload_font(self, glyphs=None, index=0):
if not glyphs:
glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-!,. \"'?!"
print("Preloading font glyphs:", glyphs)
if self._text_font[index] is not terminalio.FONT:
self._text_font[index].load_glyphs(glyphs)
if self._fonts[self._text_font[index]] is not terminalio.FONT:
self._fonts[self._text_font[index]].load_glyphs(glyphs)

def set_text_color(self, color, index=0):
"""Update the text color, with indexing into our list of text boxes.
Expand Down Expand Up @@ -274,7 +288,9 @@ def set_text(self, val, index=0, auto_refresh=True):

if len(string) > 0:
self._text[index] = Label(
self._text_font[index], text=string, scale=self._text_scale[index]
self._fonts[self._text_font[index]],
text=string,
scale=self._text_scale[index],
)
self._text[index].color = self._text_color[index]
self._text[index].anchor_point = self._text_anchor_point[index]
Expand Down