Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Added _repr_html_ for fonts #21904

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 9 commits into from
Dec 16, 2021
Merged

Added _repr_html_ for fonts #21904

merged 9 commits into from
Dec 16, 2021

Conversation

ambi7
Copy link
Contributor

@ambi7 ambi7 commented Dec 10, 2021

PR Summary

Resolves #18039

Invoking in IPython

import matplotlib.font_manager
from IPython.core.display import HTML

font = matplotlib.font_manager.fontManager.ttflist[0]       # any FontEntry

HTML(font._repr_html_())

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • [N/A] New features are documented, with examples if plot related.
  • [N/A] New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • [N/A] API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • [N/A] Documentation is sphinx and numpydoc compliant (the docs should build without error).

Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

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

As noted in #18039 I don't think this implementation is satisfactory; for example STIXGeneral is only provided by matplotlib on my computer and therefore trying to display in with this mechanism (which uses the browser's OS integration to lookup the font) silently renders a different font instead. I guess(?) a strategy may be to use something like https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face to explicitly specify a local font path instead.

@ambi7
Copy link
Contributor Author

ambi7 commented Dec 12, 2021

As noted in #18039 I don't think this implementation is satisfactory; for example STIXGeneral is only provided by matplotlib on my computer and therefore trying to display in with this mechanism (which uses the browser's OS integration to lookup the font) silently renders a different font instead. I guess(?) a strategy may be to use something like https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face to explicitly specify a local font path instead.

@anntzer Thank you for the review! I agree, the prior implementation will not render the correct font. I updated the PR to load the font using font-face and updated tests. Can you take another look?

@ambi7 ambi7 requested a review from anntzer December 12, 2021 19:51
@anntzer
Copy link
Contributor

anntzer commented Dec 12, 2021

Does this actually render the right font e.g. from a jupyter notebook? From local testing it doesn't seem so?

@tacaswell
Copy link
Member

I think it would be better to render to make a temporary Figure and put a base64 encodeding png (or a svg with the paths embeded) in the html repr (and also add _repr_png_).

@tacaswell
Copy link
Member

@ambi7 Thank you for working on this! I hope you are not discouraged by the initial feedback.

@tacaswell tacaswell added this to the v3.6.0 milestone Dec 13, 2021
@ambi7
Copy link
Contributor Author

ambi7 commented Dec 13, 2021

Does this actually render the right font e.g. from a jupyter notebook? From local testing it doesn't seem so?

Yes, font-face was having issues loading fonts from file when rendering from a jupyter notebook. I took @tacaswell suggestion and rendered the font via a png image. Looks like it is now rendering the right font from a jupyter notebook by using a temporary Figure

@anntzer
Copy link
Contributor

anntzer commented Dec 13, 2021

I like png as well here, but @timhoffm didn't (#18039 (comment)).

@ambi7
Copy link
Contributor Author

ambi7 commented Dec 14, 2021

@timhoffm Any additional comments in regards to this implementation?

@timhoffm
Copy link
Member

I can see the argument for images, that decouple the font rendering from the display.

So, this seems the defensive and sane approach. Still I'd like to see HiDPI-support for the rendered images, but that doesn't have to be necessarily part of this PR.

@anntzer anntzer removed their request for review December 14, 2021 23:15
Copy link
Contributor Author

@ambi7 ambi7 left a comment

Choose a reason for hiding this comment

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

.

@tacaswell tacaswell requested a review from anntzer December 15, 2021 20:31
@tacaswell
Copy link
Member

Cleared @anntzer 's review because it was of the "ask the browser to render it" implementation.

@tacaswell tacaswell dismissed anntzer’s stale review December 15, 2021 20:34

The implementation has changed to render a png rather than ask the browser to find the font.

@timhoffm timhoffm merged commit 951f752 into matplotlib:main Dec 16, 2021
@timhoffm
Copy link
Member

Thanks @ambi7!

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.

Add _repr_html_ for fonts
5 participants