A font we installed ourselves has to be loaded before it can be selected
Found by testing the second launch rather than the first. AddFontResourceW adds a font to the running process's font table; the HKCU registration beside it is what makes it permanent, and Windows reads that at the next logon. So between an install and a logoff there is a window where a new MeshTerm process finds its own registration, believes the font is ready, and selects nothing -- SetCurrentConsoleFontEx reports success and quietly keeps the old font. consolefont.use() retries once after loading our own copy, which is a no-op for the Cascadia that came with Windows and the fix for the one we put there. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
f06b593a0f
commit
5158268a73
3 files changed
+56
-3
No files matched your search
+1
-1
@@ -305,7 +305,7 @@ def _use_a_better_console_font(console: Console, installed: str | None) -> None:
|
||||
return
|
||||
face = consolefont.BUNDLED_FACE
|
||||
|
||||
if consolefont.select(face):
|
||||
if consolefont.use(face):
|
||||
console.print(f"[ok]✓[/ok] Now drawing with [accent]{face}[/accent].")
|
||||
get_logger().info("console font set to %s", face)
|
||||
else:
|
||||
|
||||
@@ -92,8 +92,7 @@ def install_bundled_font() -> bool:
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(BUNDLED_FONT, target)
|
||||
|
||||
gdi32 = ctypes.WinDLL("gdi32", use_last_error=True)
|
||||
if not gdi32.AddFontResourceW(ctypes.c_wchar_p(str(target))):
|
||||
if not _add_font_resource(target):
|
||||
return False
|
||||
|
||||
# The registration is what survives a logout; AddFontResourceW alone lasts only
|
||||
@@ -122,6 +121,54 @@ def install_bundled_font() -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def _add_font_resource(path: Path) -> bool:
|
||||
"""Make a font file usable by this process, without installing anything.
|
||||
|
||||
``AddFontResourceW`` adds to the font table for the running process; the ``HKCU``
|
||||
registration beside it is what makes the font permanent, and Windows reads that at the
|
||||
*next logon*. Between those two facts sits the case this exists for: a second MeshTerm
|
||||
launch in the same session finds its own registration and believes the font is ready,
|
||||
when nothing has loaded it into this new process yet.
|
||||
|
||||
Args:
|
||||
path: The font file, wherever it is.
|
||||
|
||||
Returns:
|
||||
Whether at least one face was added.
|
||||
"""
|
||||
if sys.platform != "win32" or not path.is_file():
|
||||
return False
|
||||
try:
|
||||
import ctypes
|
||||
|
||||
gdi32 = ctypes.WinDLL("gdi32", use_last_error=True)
|
||||
return bool(gdi32.AddFontResourceW(ctypes.c_wchar_p(str(path))))
|
||||
except Exception: # pragma: no cover - best effort, like everything else here
|
||||
return False
|
||||
|
||||
|
||||
def use(face: str) -> bool:
|
||||
"""Draw this console with ``face``, loading our own copy first if that is what it is.
|
||||
|
||||
The plain :func:`select` is enough for a font the system installed — the Cascadia that
|
||||
comes with Windows 11 or with Windows Terminal. It is *not* enough for the copy
|
||||
MeshTerm installed itself in an earlier run of the same session: that one is registered
|
||||
but not yet loaded (see :func:`_add_font_resource`), and selecting it silently keeps
|
||||
the old font. So a refusal is retried once, after loading it.
|
||||
|
||||
Args:
|
||||
face: The family to draw with.
|
||||
|
||||
Returns:
|
||||
Whether the console is now drawing with ``face``.
|
||||
"""
|
||||
if select(face):
|
||||
return True
|
||||
if face == BUNDLED_FACE and _add_font_resource(user_font_dir() / BUNDLED_FONT.name):
|
||||
return select(face)
|
||||
return False
|
||||
|
||||
|
||||
def current_face() -> str | None:
|
||||
"""The face this console is drawing with, or ``None`` off a console."""
|
||||
return _console_font(None)
|
||||
|
||||
@@ -205,3 +205,9 @@ def test_installing_is_a_no_op_where_there_are_no_windows_fonts() -> None:
|
||||
"""Off Windows the offer is never made, and the install refuses rather than pretends."""
|
||||
assert consolefont.install_bundled_font() is False
|
||||
assert consolefont.select("Cascadia Mono PL") is False
|
||||
assert consolefont.use("Cascadia Mono PL") is False
|
||||
|
||||
|
||||
def test_a_font_resource_is_only_added_from_a_file_that_exists(tmp_path: Path) -> None:
|
||||
"""The retry path takes a path that may well not be there, and must not raise on it."""
|
||||
assert consolefont._add_font_resource(tmp_path / "nothing.ttf") is False
|
||||
Reference in new issue
Block a user