Clear font caches on config reload so newly installed fonts are picked up without restart

This commit is contained in:
zhaolei
2026-05-19 13:51:53 +08:00
parent 4cef90d4c7
commit a76bb0829b
5 changed files with 30 additions and 0 deletions

View File

@@ -3200,6 +3200,8 @@ class Boss:
opts = load_config(*paths, overrides=final_overrides or None, accumulate_bad_lines=bad_lines)
if bad_lines:
self.show_bad_config_lines(bad_lines)
from .fonts.render import clear_font_caches
clear_font_caches()
self.apply_new_options(opts)
from .open_actions import clear_caches
clear_caches()

View File

@@ -65,6 +65,13 @@ else:
cache_for_variable_data_by_path: dict[str, VariableData] = {}
def clear_caches() -> None:
cache_for_variable_data_by_path.clear()
actually_variable_cache.clear()
attr_map = {(False, False): 'font_family', (True, False): 'bold_font', (False, True): 'italic_font', (True, True): 'bold_italic_font'}

View File

@@ -49,6 +49,11 @@ def create_font_map(all_fonts: Iterable[CoreTextFont]) -> FontMap:
return ans
def clear_caches() -> None:
all_fonts_map.cache_clear()
weight_range_for_family.cache_clear()
@lru_cache(maxsize=2)
def all_fonts_map(monospaced: bool = True) -> FontMap:
return create_font_map(coretext_all_fonts(monospaced))

View File

@@ -44,6 +44,12 @@ def create_font_map(all_fonts: tuple[FontConfigPattern, ...]) -> FontMap:
return ans
def clear_caches() -> None:
all_fonts_map.cache_clear()
fc_match.cache_clear()
weight_range_for_family.cache_clear()
@lru_cache(maxsize=2)
def all_fonts_map(monospaced: bool = True) -> FontMap:
if monospaced:

View File

@@ -187,6 +187,16 @@ def dump_font_debug() -> None:
log_error(' ' + s.identify_for_debug())
def clear_font_caches() -> None:
from .common import clear_caches as clear_common_caches
clear_common_caches()
if is_macos:
from .core_text import clear_caches as clear_platform_caches
else:
from .fontconfig import clear_caches as clear_platform_caches
clear_platform_caches()
def set_font_family(opts: Options | None = None, override_font_size: float | None = None, add_builtin_nerd_font: bool = False) -> None:
global current_faces, builtin_nerd_font_descriptor
opts = opts or defaults