Add support for loading all fonts from collections #30334
Draft
+112
−27
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.
PR summary
This turned out to be more straightforward than I expected, but it will probably need a few API decisions to be fully complete.
From bottom to top of the API:
FT2Font
accepts aface_index
parameter to specify which face to load in a collection, and a correspondingface_index
property to check what's loaded.FontManager.get_font
accepts a tuple of font path and face index in places it would accept just a path. But also see the next point.FontManager.findfont
returns astr
-like classFontPath
(name up for debate) which has aface_index
attribute and is accepted byget_font
much like the tuple. If anyone uses them as strings though, it should act pretty much the same.For example, now I can see all variants of Noto Sans CJK:
or all variants of WenQuanYi that we use for tests:
Fixes #3135
TODO
API questions
str | bytes | Path
, I wonder if we should change toos.PathLike
?FontPath
, I'm thinking maybe that's redundant and we should just stick with theFontPath
class only. Do we want to accept the tuple form as well, or should I drop it?FontPath
is a subclass ofstr
which allows using it as astr
as one normally would. That was the minimum implementation needed, but we probably want to flesh that out a bit. At minimum, I think we should implement__eq__
and__hash__
so that you can use it as a dictionary key without clashing with an equivalentstr
. But then do we want to add a deprecation warning when making those comparisons? And after thinking about it a bit more, would anamedtuple
with__eq__
instead be a better choice?PR checklist