ui: cache emoji font to avoid repeated loading (#36451)

cache font to avoid repleated loads
pull/36458/head
Dean Lee 2 weeks ago committed by GitHub
parent 40a1af97b9
commit f2db7f7665
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 10
      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()

Loading…
Cancel
Save