[font] Catch file load exception (#10058)

Co-authored-by: clydeps <U5yx99dok9>
This commit is contained in:
Clyde Stubbs 2025-08-04 14:55:54 +10:00 committed by GitHub
parent 3007ca4d57
commit bb3ebaf955
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -15,6 +15,7 @@ from freetype import (
FT_LOAD_RENDER, FT_LOAD_RENDER,
FT_LOAD_TARGET_MONO, FT_LOAD_TARGET_MONO,
Face, Face,
FT_Exception,
ft_pixel_mode_mono, ft_pixel_mode_mono,
) )
import requests import requests
@ -94,7 +95,14 @@ class FontCache(MutableMapping):
return self.store[self._keytransform(item)] return self.store[self._keytransform(item)]
def __setitem__(self, key, value): def __setitem__(self, key, value):
self.store[self._keytransform(key)] = Face(str(value)) transformed = self._keytransform(key)
try:
self.store[transformed] = Face(str(value))
except FT_Exception as exc:
file = transformed.split(":", 1)
raise cv.Invalid(
f"{file[0].capitalize()} {file[1]} is not a valid font file"
) from exc
FONT_CACHE = FontCache() FONT_CACHE = FontCache()