From f2db7f7665426d93060ecb3f314187bfccd02833 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Sat, 25 Oct 2025 10:13:34 +0800 Subject: [PATCH] ui: cache emoji font to avoid repeated loading (#36451) cache font to avoid repleated loads --- system/ui/lib/emoji.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/system/ui/lib/emoji.py b/system/ui/lib/emoji.py index 54f742952e..37228e2d45 100644 --- a/system/ui/lib/emoji.py +++ b/system/ui/lib/emoji.py @@ -6,6 +6,7 @@ import pyray as rl from openpilot.system.ui.lib.application import FONT_DIR +_emoji_font: ImageFont.FreeTypeFont | None = None _cache: dict[str, rl.Texture] = {} EMOJI_REGEX = re.compile( @@ -32,6 +33,12 @@ EMOJI_REGEX = re.compile( flags=re.UNICODE ) +def _load_emoji_font() -> ImageFont.FreeTypeFont | None: + global _emoji_font + if _emoji_font is None: + _emoji_font = ImageFont.truetype(str(FONT_DIR.joinpath("NotoColorEmoji.ttf")), 109) + return _emoji_font + def find_emoji(text): return [(m.start(), m.end(), m.group()) for m in EMOJI_REGEX.finditer(text)] @@ -39,8 +46,7 @@ def emoji_tex(emoji): if emoji not in _cache: img = Image.new("RGBA", (128, 128), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) - font = ImageFont.truetype(FONT_DIR.joinpath("NotoColorEmoji.ttf"), 109) - draw.text((0, 0), emoji, font=font, embedded_color=True) + draw.text((0, 0), emoji, font=_load_emoji_font(), embedded_color=True) with io.BytesIO() as buffer: img.save(buffer, format="PNG") l = buffer.tell()