refactor(raylib): Use font file as FontWeight enum value (#35675)

* refactor(raylib): use dictionary to map font weight to file name

* refactor: add space

* refactor: format

* refactor: Switch FontWeight to a StrEnum with font file values
pull/35698/head
David 2 months ago committed by GitHub
parent 67dc69d3db
commit d913e4d349
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 41
      system/ui/lib/application.py

@ -7,7 +7,7 @@ import threading
from collections.abc import Callable from collections.abc import Callable
from collections import deque from collections import deque
from dataclasses import dataclass from dataclasses import dataclass
from enum import IntEnum from enum import StrEnum
from typing import NamedTuple from typing import NamedTuple
from importlib.resources import as_file, files from importlib.resources import as_file, files
from openpilot.common.swaglog import cloudlog from openpilot.common.swaglog import cloudlog
@ -33,16 +33,16 @@ ASSETS_DIR = files("openpilot.selfdrive").joinpath("assets")
FONT_DIR = ASSETS_DIR.joinpath("fonts") FONT_DIR = ASSETS_DIR.joinpath("fonts")
class FontWeight(IntEnum): class FontWeight(StrEnum):
THIN = 0 THIN = "Inter-Thin.ttf"
EXTRA_LIGHT = 1 EXTRA_LIGHT = "Inter-ExtraLight.ttf"
LIGHT = 2 LIGHT = "Inter-Light.ttf"
NORMAL = 3 NORMAL = "Inter-Regular.ttf"
MEDIUM = 4 MEDIUM = "Inter-Medium.ttf"
SEMI_BOLD = 5 SEMI_BOLD = "Inter-SemiBold.ttf"
BOLD = 6 BOLD = "Inter-Bold.ttf"
EXTRA_BOLD = 7 EXTRA_BOLD = "Inter-ExtraBold.ttf"
BLACK = 8 BLACK = "Inter-Black.ttf"
@dataclass @dataclass
@ -312,20 +312,9 @@ class GuiApplication:
return self._height return self._height
def _load_fonts(self): def _load_fonts(self):
font_files = (
"Inter-Thin.ttf",
"Inter-ExtraLight.ttf",
"Inter-Light.ttf",
"Inter-Regular.ttf",
"Inter-Medium.ttf",
"Inter-SemiBold.ttf",
"Inter-Bold.ttf",
"Inter-ExtraBold.ttf",
"Inter-Black.ttf",
)
# Create a character set from our keyboard layouts # Create a character set from our keyboard layouts
from openpilot.system.ui.widgets.keyboard import KEYBOARD_LAYOUTS from openpilot.system.ui.widgets.keyboard import KEYBOARD_LAYOUTS
all_chars = set() all_chars = set()
for layout in KEYBOARD_LAYOUTS.values(): for layout in KEYBOARD_LAYOUTS.values():
all_chars.update(key for row in layout for key in row) all_chars.update(key for row in layout for key in row)
@ -335,11 +324,11 @@ class GuiApplication:
codepoint_count = rl.ffi.new("int *", 1) codepoint_count = rl.ffi.new("int *", 1)
codepoints = rl.load_codepoints(all_chars, codepoint_count) codepoints = rl.load_codepoints(all_chars, codepoint_count)
for index, font_file in enumerate(font_files): for font_weight_file in FontWeight:
with as_file(FONT_DIR.joinpath(font_file)) as fspath: with as_file(FONT_DIR.joinpath(font_weight_file)) as fspath:
font = rl.load_font_ex(fspath.as_posix(), 200, codepoints, codepoint_count[0]) font = rl.load_font_ex(fspath.as_posix(), 200, codepoints, codepoint_count[0])
rl.set_texture_filter(font.texture, rl.TextureFilter.TEXTURE_FILTER_BILINEAR) rl.set_texture_filter(font.texture, rl.TextureFilter.TEXTURE_FILTER_BILINEAR)
self._fonts[index] = font self._fonts[font_weight_file] = font
rl.unload_codepoints(codepoints) rl.unload_codepoints(codepoints)
rl.gui_set_font(self._fonts[FontWeight.NORMAL]) rl.gui_set_font(self._fonts[FontWeight.NORMAL])

Loading…
Cancel
Save