add documentation for reloading font cache#26453
Conversation
| yourself if your new fonts cannot be found by Matplotlib. | ||
| The cache can be rebuilt by using :func:`matplotlib.font_manager.load_fontmanager`:: | ||
| import matplotlib as mpl | ||
| mpl.font_manager.load_fontmanager(try_read_cache=False) |
There was a problem hiding this comment.
I don't think that this will actually work?
This will make a new font manager and return it to you, but it will not change the singleton that is used by the rest of the library or update the findfont and get_font_names held at the module level.
There was a problem hiding this comment.
My testing procedure for this was:
- add a new font to
mpl-data\fonts\ttf - run a short plot with a
textinstance withfamily=new font - see that the text doesn't update to the new font after multiple runs
- run the
font_manager.load_fontmanagercode - run the plot again
Now the text is now set to the new font and a new entry in the font cache is present. The findfont and get_font_names methods both return the new font.
I did notice however, that the before and after fontmanager instances were the same object with the same ID.
There was a problem hiding this comment.
... ok, I do not understand why that works 🤯
There was a problem hiding this comment.
ok, question: are you running this all in the same process or each in its own Python process?
There was a problem hiding this comment.
Each in their own separate process.
|
Unfortunately I do not think it is this simple because we keep a singleton of this cache and then re-expose its methods at the module level. I think the steps here are:
An alternate path would be to update |
|
An alternate to my last comment is to explain why this does work as it looks like it creates a new |
|
Haven't read through the whole issue, but note that in the rebuild_if_missing branch at the end of findfont_cached, fontManager calls _load_fontmanager then replaces its |
|
That is a bit heavy handed, but makes sense (a However the way this is hidden behind a cache, we will only automatically discover the new font if it is there when we search for it. It might be worth also adding a public way to clear that cache. |
PR summary
Closes #24872. Adds documentation that describes the process of making newly added fonts available to the fontmanager.
load_fontmanagermethod which can be used in place of the deprecatedmatplotlib.font_manager._rebuild()method.Loading new fontssection to thefonts-in-matplotlibdoc with an explanation of the methods of reloading the font cache, with example code.PR checklist