Repository navigation
Load font files passed as font_name, and accept lists of names - #2922
Merged
Merged
Conversation
- A path or resource handle passed as font_name was turned into the file name (e.g. "Kenney_Future.ttf") and never loaded, so pyglet drew the default font. The file is now loaded the first time it's used, and its family name is used. Each file is loaded once. - The family name comes from pyglet's on_font_loaded event, which uses the platform's name for the font. If the file was already added to pyglet some other way, it's read from the file instead. - Lists of font names raised TypeError, although the docs allow them. - A path no longer takes priority over names listed before it. - The Text.font_name setter now resolves paths too. - load_font() records the family name, so a later path is cheap. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pvcraven
force-pushed
the
fix/text-font-paths
branch
from
October 6, 2026 15:30
5ad0cce to
0877dca
Compare
This was referenced Oct 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PR 5 of the text improvements:
font_namepaths to font files, and lists of names.Bugs
Text(..., font_name=":system:fonts/ttf/Kenney/Kenney_Future.ttf")(or the real path) passed"Kenney_Future.ttf"to pyglet as the font name, and never loaded the file. pyglet found no such font and used Segoe UI"Kenney Future"font_namesetter ignored pathstext.font_name = pathdrew the default fontTypeErrorfont_name=["NoSuchFont", "arial"]raised inTextanddraw_text, even though the docs say lists are accepted("Liberation Sans", path)used the file, because any path found anywhere in the tuple was returned right awaycreate_text_sprite()anddraw_text()go throughText, so they're fixed too.How
_attempt_font_name_resolutionchecks each name. If it resolves to a file, the file is loaded withpyglet.font.add_fileand the name is replaced by the font's family name. The resulting list goes topyglet.font.load, as before, which picks the first font that exists.on_font_loadedevent duringadd_file. That event gives the name the platform registers the font under, which is the name pyglet can load it by. If the file was already added to pyglet some other way, there's no event, so the name is read from the file withpyglet.font.ttf.TruetypeInfoinstead.arcade.load_font()uses the same code, so a path to a font loaded earlier (for example byload_kenney_fonts()) doesn't add the file again.Creating a
Textcosts the same as before: about 690 µs with the default("calibri", "arial"), measured alternating old and new.Tests
New
tests/unit/text/test_text_fonts.py(14 tests). Each test starts with no remembered font files.Textanddraw_text.Textobjects andload_font(). A file already added to pyglet directly gets its name from the file.On
development, 9 of them fail. The tests load every bundled font up front, so a path in the tests always points to a font that's already loaded. I checked the first-time load separately, in a fresh process: a path to the unloaded Kenney Future file draws Kenney Future.Full suite on pyglet 3.0.dev11: 1557 passed, no warnings, plus the same 3 render tests that fail only on my machine. Ruff is clean, and mypy has no new errors.
The changelog has an entry under Unreleased → Fixes.
🤖 Generated with Claude Code