Skip to content

write addfont example #24866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 31, 2023
Merged

write addfont example #24866

merged 7 commits into from
Jan 31, 2023

Conversation

saranti
Copy link
Contributor

@saranti saranti commented Jan 2, 2023

PR Summary

Fixes #24419. Adds an example to the addfont function in the fontManager reference docs.

PR Checklist

Documentation and Tests

  • Has pytest style unit tests (and pytest passes)
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • New plotting related features are documented with examples.

Release Notes

  • New features are marked with a .. versionadded:: directive in the docstring and documented in doc/users/next_whats_new/
  • API changes are marked with a .. versionchanged:: directive in the docstring and documented in doc/api/next_api_changes/
  • Release notes conform with instructions in next_whats_new/README.rst or next_api_changes/README.rst

Copy link
Member

@story645 story645 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for tackling this - sorry I didn't get back to you, but I don't know much about fonts either.

Comment on lines 1047 to 1048
for font_file in font_files:
font_manager.fontManager.addfont(font_file)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs indent


`addfont` must be called on the global `fontManager` instance::

import matplotlib.pyplot as plt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I think since this is API reference, you can drop the plotting example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. I added the plot because i thought you wanted to show how it works in the example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can honestly go either way when it's a parameter docstring so now I'm leaning again towards keeping it so folks know how to test if they did it correctly 😅

or a combination of both.
(see :doc:`font tutorial </tutorials/text/text_props>`)

See `~.font_manager.FontManager.addfont` for adding a font family from a file.
Copy link
Contributor Author

@saranti saranti Jan 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a weird one. I only meant to add line 12

@tacaswell
Copy link
Member

I continue to be extremely skeptical that we want to document this or "here is how to do something we think is a really bad idea".

If we really do want to document this (which again I have doubts about), it should go in the comment at the top of https://matplotlib.org/stable/api/font_manager_api.html#module-matplotlib.font_manager or in the docstring for https://matplotlib.org/stable/api/font_manager_api.html#matplotlib.font_manager.fontManager

@anntzer
Copy link
Contributor

anntzer commented Jan 4, 2023

@tacaswell Do you mean you think addfont() should not have been public API? See discussion at #15068.

@tacaswell
Copy link
Member

Not fair to quote past me against my current me!

My thinking has moved more towards "we should not roll our own font discovery" given that we have gotten pushed that way (and there are system APIs for doing it) on windows and osx. Saying "put the fonts where your system expects" and then rebuilding our cache seems like both the correct and easiest (for us) path forward.

That said, addFont has been around for a while and is a pragmatic solution (I have been thinking about packaging in general recently and hence am very down on pragmatic solutions that could be abused because they will be abused in ways that will cause great pain) so I am not in favor of ripping it out at this point. If it were proposed today I would be weakly against it.

On a bit more consideration, that addFont does not modify the cache seems correct to avoid some really confusing action-at-a-distance issues with runtime state getting latched too strongly (if you want to use addFont you have to call it every process).

I think the information that needs to be clear:

  • the FontManager instance that matters is the singleton at mpl.font_manager.fontmanager
  • how to rebuild the cache
  • addFont does not update the cache

but we should not suggest things (abusing pypi to distribute fonts (which we ourselves do)) that we think are a bad idea!

@story645
Copy link
Member

story645 commented Jan 5, 2023

it should go in the comment at the top of https://matplotlib.org/stable/api/font_manager_api.html#module-matplotlib.font_manager

I'd pull that out of a comment into docs since it seems to be a summary of the module, but I think adding this info to the top makes sense. Maybe w/ a nice big note section w/ all the caveats. I was thinking something in the style of the animation or color docs.

@saranti
Copy link
Contributor Author

saranti commented Jan 6, 2023

Maybe I should put a bookmark in this issue for the time being.

@jklymak
Copy link
Member

jklymak commented Jan 12, 2023

@saranti. We discussed this at the weekly meeting. I don't personally understand all the ins and outs, so I won't suggest wording, but the consensus was that the addfont doctoring should have a couple sentences of explanation, and then a link to a longer explanation in font_manager.py API docs.

I believe some of the technical details that have to get across are that addfont does not add the font to our font cache, and hence needs to be rendered each time the script is run, and hence folks should only use this as an escape route if they cannot properly install a font on their machine.

@saranti
Copy link
Contributor Author

saranti commented Jan 14, 2023

@saranti. We discussed this at the weekly meeting. I don't personally understand all the ins and outs, so I won't suggest wording, but the consensus was that the addfont doctoring should have a couple sentences of explanation, and then a link to a longer explanation in font_manager.py API docs.

I believe some of the technical details that have to get across are that addfont does not add the font to our font cache, and hence needs to be rendered each time the script is run, and hence folks should only use this as an escape route if they cannot properly install a font on their machine.

Would it be a good idea to add the longer explanation with an example in the text tutorials, just below Default Font?

Copy link
Member

@story645 story645 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit about wording but solves my documentation concerns.

Comment on lines 986 to 987
However, fonts added with the `FontManager.addfont` method will not persist
in the cache and hence will need to be called at every process. This method
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
However, fonts added with the `FontManager.addfont` method will not persist
in the cache and hence will need to be called at every process. This method
Fonts added with the `FontManager.addfont` method will not persist
in the cache; therefore *addfont* will need to be called every time matplotlib is imported. This method

not quite sure, but something a little more explicit

@jklymak jklymak merged commit f2100d5 into matplotlib:main Jan 31, 2023
@jklymak
Copy link
Member

jklymak commented Jan 31, 2023

Thanks @saranti - I took the liberty of squash merging....

@saranti saranti deleted the addfont branch February 14, 2023 08:25
@QuLogic QuLogic added this to the v3.8.0 milestone Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Doc]: add from file to font family example
7 participants