Skip to content

Commit d488d50

Browse files
authored
Merge pull request #32 from makermelissa/fontcache
Cache fonts for lower memory usage
2 parents d27b2c6 + d9d70c9 commit d488d50

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

adafruit_magtag/magtag.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ def __init__(
9292
self._json_path = None
9393
self.json_path = json_path
9494

95+
# Font Cache
96+
self._fonts = {}
97+
9598
try:
9699
import alarm # pylint: disable=import-outside-toplevel
97100

@@ -157,10 +160,7 @@ def add_text(
157160
position is relative to the label
158161
:param bool is_data: If True, fetch will attempt to update the label
159162
"""
160-
if text_font is terminalio.FONT:
161-
self._text_font.append(text_font)
162-
else:
163-
self._text_font.append(bitmap_font.load_font(text_font))
163+
164164
if not text_wrap:
165165
text_wrap = 0
166166
if not text_maxlen:
@@ -179,6 +179,7 @@ def add_text(
179179
if self._debug:
180180
print("Init text area")
181181
self._text.append(None)
182+
self._text_font.append(self._load_font(text_font))
182183
self._text_color.append(self.html_color_convert(text_color))
183184
self._text_position.append(text_position)
184185
self._text_wrap.append(text_wrap)
@@ -193,6 +194,19 @@ def add_text(
193194

194195
# pylint: enable=too-many-arguments
195196

197+
def _load_font(self, font):
198+
"""
199+
Load and cache a font if not previously loaded
200+
Return the key of the cached font
201+
"""
202+
if font is terminalio.FONT:
203+
if "terminal" not in self._fonts:
204+
self._fonts["terminal"] = terminalio.FONT
205+
return "terminal"
206+
if font not in self._fonts:
207+
self._fonts[font] = bitmap_font.load_font(font)
208+
return font
209+
196210
@staticmethod
197211
def html_color_convert(color):
198212
"""Convert an HTML color code to an integer
@@ -233,8 +247,8 @@ def preload_font(self, glyphs=None, index=0):
233247
if not glyphs:
234248
glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-!,. \"'?!"
235249
print("Preloading font glyphs:", glyphs)
236-
if self._text_font[index] is not terminalio.FONT:
237-
self._text_font[index].load_glyphs(glyphs)
250+
if self._fonts[self._text_font[index]] is not terminalio.FONT:
251+
self._fonts[self._text_font[index]].load_glyphs(glyphs)
238252

239253
def set_text_color(self, color, index=0):
240254
"""Update the text color, with indexing into our list of text boxes.
@@ -281,7 +295,9 @@ def set_text(self, val, index=0, auto_refresh=True):
281295

282296
if len(string) > 0:
283297
self._text[index] = Label(
284-
self._text_font[index], text=string, scale=self._text_scale[index]
298+
self._fonts[self._text_font[index]],
299+
text=string,
300+
scale=self._text_scale[index],
285301
)
286302
self._text[index].color = self._text_color[index]
287303
self._text[index].anchor_point = self._text_anchor_point[index]

0 commit comments

Comments
 (0)