@@ -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
try :
96
99
import alarm # pylint: disable=import-outside-toplevel
97
100
@@ -157,10 +160,7 @@ def add_text(
157
160
position is relative to the label
158
161
:param bool is_data: If True, fetch will attempt to update the label
159
162
"""
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
+
164
164
if not text_wrap :
165
165
text_wrap = 0
166
166
if not text_maxlen :
@@ -179,6 +179,7 @@ def add_text(
179
179
if self ._debug :
180
180
print ("Init text area" )
181
181
self ._text .append (None )
182
+ self ._text_font .append (self ._load_font (text_font ))
182
183
self ._text_color .append (self .html_color_convert (text_color ))
183
184
self ._text_position .append (text_position )
184
185
self ._text_wrap .append (text_wrap )
@@ -193,6 +194,19 @@ def add_text(
193
194
194
195
# pylint: enable=too-many-arguments
195
196
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
+
196
210
@staticmethod
197
211
def html_color_convert (color ):
198
212
"""Convert an HTML color code to an integer
@@ -233,8 +247,8 @@ def preload_font(self, glyphs=None, index=0):
233
247
if not glyphs :
234
248
glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-!,. \" '?!"
235
249
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 )
238
252
239
253
def set_text_color (self , color , index = 0 ):
240
254
"""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):
281
295
282
296
if len (string ) > 0 :
283
297
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 ],
285
301
)
286
302
self ._text [index ].color = self ._text_color [index ]
287
303
self ._text [index ].anchor_point = self ._text_anchor_point [index ]
0 commit comments