pre-process fonts for raylib (#36489)

* pre-process fonts for raylib

* it's fast!

* raylib processing

* build with scons

* padding

* happy ruff

* all exported

* cleanup

* more pad
This commit is contained in:
Adeeb Shihadeh
2025-10-26 13:20:11 -07:00
committed by GitHub
parent 03cb3e9dc0
commit 0d4b0ee116
4 changed files with 154 additions and 52 deletions
+12 -52
View File
@@ -15,7 +15,7 @@ from typing import NamedTuple
from importlib.resources import as_file, files
from openpilot.common.swaglog import cloudlog
from openpilot.system.hardware import HARDWARE, PC, TICI
from openpilot.system.ui.lib.multilang import TRANSLATIONS_DIR, UNIFONT_LANGUAGES, multilang
from openpilot.system.ui.lib.multilang import multilang
from openpilot.common.realtime import Ratekeeper
_DEFAULT_FPS = int(os.getenv("FPS", 20 if TICI else 60))
@@ -43,16 +43,16 @@ FONT_DIR = ASSETS_DIR.joinpath("fonts")
class FontWeight(StrEnum):
THIN = "Inter-Thin.ttf"
EXTRA_LIGHT = "Inter-ExtraLight.ttf"
LIGHT = "Inter-Light.ttf"
NORMAL = "Inter-Regular.ttf"
MEDIUM = "Inter-Medium.ttf"
SEMI_BOLD = "Inter-SemiBold.ttf"
BOLD = "Inter-Bold.ttf"
EXTRA_BOLD = "Inter-ExtraBold.ttf"
BLACK = "Inter-Black.ttf"
UNIFONT = "unifont.otf"
THIN = "Inter-Thin.fnt"
EXTRA_LIGHT = "Inter-ExtraLight.fnt"
LIGHT = "Inter-Light.fnt"
NORMAL = "Inter-Regular.fnt"
MEDIUM = "Inter-Medium.fnt"
SEMI_BOLD = "Inter-SemiBold.fnt"
BOLD = "Inter-Bold.fnt"
EXTRA_BOLD = "Inter-ExtraBold.fnt"
BLACK = "Inter-Black.fnt"
UNIFONT = "unifont.fnt"
def font_fallback(font: rl.Font) -> rl.Font:
@@ -392,51 +392,11 @@ class GuiApplication:
return self._height
def _load_fonts(self):
# Create a character set from our keyboard layouts
from openpilot.system.ui.widgets.keyboard import KEYBOARD_LAYOUTS
base_chars = set()
for layout in KEYBOARD_LAYOUTS.values():
base_chars.update(key for row in layout for key in row)
base_chars |= set("–‑✓×°§•")
# Load only the characters used in translations
unifont_chars = set(base_chars)
for language, code in multilang.languages.items():
unifont_chars |= set(language)
try:
with open(os.path.join(TRANSLATIONS_DIR, f"app_{code}.po")) as f:
lang_chars = set(f.read())
if code in UNIFONT_LANGUAGES:
unifont_chars |= lang_chars
else:
base_chars |= lang_chars
except FileNotFoundError:
cloudlog.warning(f"Translation file for language '{code}' not found when loading fonts.")
base_chars = "".join(base_chars)
cloudlog.debug(f"Loading fonts with {len(base_chars)} glyphs.")
unifont_chars = "".join(unifont_chars)
cloudlog.debug(f"Loading unifont with {len(unifont_chars)} glyphs.")
base_codepoint_count = rl.ffi.new("int *", 1)
base_codepoints = rl.load_codepoints(base_chars, base_codepoint_count)
unifont_codepoint_count = rl.ffi.new("int *", 1)
unifont_codepoints = rl.load_codepoints(unifont_chars, unifont_codepoint_count)
for font_weight_file in FontWeight:
with as_file(FONT_DIR.joinpath(font_weight_file)) as fspath:
if font_weight_file == FontWeight.UNIFONT:
font = rl.load_font_ex(fspath.as_posix(), 200, unifont_codepoints, unifont_codepoint_count[0])
else:
font = rl.load_font_ex(fspath.as_posix(), 200, base_codepoints, base_codepoint_count[0])
font = rl.load_font(fspath.as_posix())
rl.set_texture_filter(font.texture, rl.TextureFilter.TEXTURE_FILTER_BILINEAR)
self._fonts[font_weight_file] = font
rl.unload_codepoints(base_codepoints)
rl.unload_codepoints(unifont_codepoints)
rl.gui_set_font(self._fonts[FontWeight.NORMAL])
def _set_styles(self):