@@ -92,6 +92,9 @@ def __init__(
92
92
self ._json_path = None
93
93
self .json_path = json_path
94
94
95
+ # Font Cache
96
+ self ._fonts = {}
97
+
95
98
self ._regexp_path = regexp_path
96
99
97
100
self .splash = self .graphics .splash
@@ -150,10 +153,7 @@ def add_text(
150
153
position is relative to the label
151
154
:param bool is_data: If True, fetch will attempt to update the label
152
155
"""
153
- if text_font is terminalio .FONT :
154
- self ._text_font .append (text_font )
155
- else :
156
- self ._text_font .append (bitmap_font .load_font (text_font ))
156
+
157
157
if not text_wrap :
158
158
text_wrap = 0
159
159
if not text_maxlen :
@@ -172,6 +172,7 @@ def add_text(
172
172
if self ._debug :
173
173
print ("Init text area" )
174
174
self ._text .append (None )
175
+ self ._text_font .append (self ._load_font (text_font ))
175
176
self ._text_color .append (self .html_color_convert (text_color ))
176
177
self ._text_position .append (text_position )
177
178
self ._text_wrap .append (text_wrap )
@@ -186,6 +187,19 @@ def add_text(
186
187
187
188
# pylint: enable=too-many-arguments
188
189
190
+ def _load_font (self , font ):
191
+ """
192
+ Load and cache a font if not previously loaded
193
+ Return the key of the cached font
194
+ """
195
+ if font is terminalio .FONT :
196
+ if "terminal" not in self ._fonts :
197
+ self ._fonts ["terminal" ] = terminalio .FONT
198
+ return "terminal"
199
+ if font not in self ._fonts :
200
+ self ._fonts [font ] = bitmap_font .load_font (font )
201
+ return font
202
+
189
203
@staticmethod
190
204
def html_color_convert (color ):
191
205
"""Convert an HTML color code to an integer
@@ -226,8 +240,8 @@ def preload_font(self, glyphs=None, index=0):
226
240
if not glyphs :
227
241
glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-!,. \" '?!"
228
242
print ("Preloading font glyphs:" , glyphs )
229
- if self ._text_font [index ] is not terminalio .FONT :
230
- self ._text_font [index ].load_glyphs (glyphs )
243
+ if self ._fonts [ self . _text_font [index ] ] is not terminalio .FONT :
244
+ self ._fonts [ self . _text_font [index ] ].load_glyphs (glyphs )
231
245
232
246
def set_text_color (self , color , index = 0 ):
233
247
"""Update the text color, with indexing into our list of text boxes.
@@ -274,7 +288,9 @@ def set_text(self, val, index=0, auto_refresh=True):
274
288
275
289
if len (string ) > 0 :
276
290
self ._text [index ] = Label (
277
- self ._text_font [index ], text = string , scale = self ._text_scale [index ]
291
+ self ._fonts [self ._text_font [index ]],
292
+ text = string ,
293
+ scale = self ._text_scale [index ],
278
294
)
279
295
self ._text [index ].color = self ._text_color [index ]
280
296
self ._text [index ].anchor_point = self ._text_anchor_point [index ]
0 commit comments