Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Summary
Currently, FontProperties (FPs) are mutable, which makes caching based on them a bit tricky (#22323,#22487). Manipulation is also not so nice; e.g. the fonts_demo.py example needs to repeatedly copy a base FP instance and call setters on the copies. Moreover, if one writes
fp = FontProperties(...)textobj.set_fontproperties(fp)
then later mutation offp
doesn't modify the fontproperties ontextobj
, becauseset_fontproperties
explicitly makes a copy of its argument. While this kind of "isolate-from-later-changes" is something we typically aim for in Matplotlib (see recent PRs likewise isolating artists from later changes in passed-in data arrays), this also means that there is basically no functionality gain coming from mutable FPs (and at least one complication, in the case of caching). (Ifset_fontproperties
did not copy, then mutability would provide a way for two text objects to share exactly the same FP while having to mutate only one of them.)
I would therefore argue that the various property setters on FP should be deprecated to ultimately make FPs immutable; instead, we should usewith_foo()
methods that return new instances with just a single property being changed (similar to the pathlib API, for example). More precisely, we can either havewith_family(family)
,with_style(style)
,with_variant(variant)
, etc., or instead we could also have a singlewith_props(**kwargs)
(called e.g. aswith_props(family="Times", ...)
); I don't have a very strong opinion there.
Proposed fix
No response