-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add _repr_html_
for fonts
#18039
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
Comments
Put no thought into this, but you could make it similar to |
I'm not sure how IPython invokes the diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py
index a5fdc32b2..767b62020 100644
--- a/lib/matplotlib/font_manager.py
+++ b/lib/matplotlib/font_manager.py
@@ -373,6 +373,9 @@ class FontEntry:
self.name, os.path.basename(self.fname), self.style, self.variant,
self.weight, self.stretch)
+ def _repr_html_(self):
+ return f"<span style='font-family:{self.name}'>{self.name}</span>"
+
def ttfFontProperty(font):
""" Invoking it 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_()) Only direct invocation works, since if we do a |
Another issue is, if fonts have a Let's say someday someone wants to use a custom font, through matplotlib they'd be able to generate text via that font, but if we use this |
Rendering to png is not worth the added complexity. You'll run into HiDPI scaling issues. Overall, I consider this feature super-optional. There are various ways of showing fonts (without or with matplotlib). Rendering font examples is not a primary or necessary feature for Matplotlib. I'm ok if we add the two-line repr_html as a nicety, but anything beyond is overkill. |
Yeah, I think the follow on to this then is probably deciding if there'd be interest/utility in a font.available type thing that prints everything to screen? |
We dropped our own We can add a |
Adding diff --git i/lib/matplotlib/font_manager.py w/lib/matplotlib/font_manager.py
index 5a9ed79535..aa277ef0f7 100644
--- i/lib/matplotlib/font_manager.py
+++ w/lib/matplotlib/font_manager.py
@@ -25,6 +25,7 @@ Future versions may implement the Level 2 or 2.1 specifications.
import dataclasses
from functools import lru_cache
+from io import BytesIO
import json
import logging
from numbers import Number
@@ -343,8 +344,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
return [fname for fname in fontfiles if os.path.exists(fname)]
-FontEntry = dataclasses.make_dataclass(
- 'FontEntry', [
+class FontEntry(dataclasses.make_dataclass('FontEntry', [
('fname', str, dataclasses.field(default='')),
('name', str, dataclasses.field(default='')),
('style', str, dataclasses.field(default='normal')),
@@ -352,13 +352,20 @@ FontEntry = dataclasses.make_dataclass(
('weight', str, dataclasses.field(default='normal')),
('stretch', str, dataclasses.field(default='normal')),
('size', str, dataclasses.field(default='medium')),
- ],
- namespace={
- '__doc__': """
+])):
+ """
A class for storing Font properties.
It is used when populating the font lookup dictionary.
- """})
+ """
+
+ def _repr_png_(self):
+ from matplotlib.figure import Figure # Circular import.
+ fig = Figure()
+ fig.text(0, 0, self.name, font=Path(self.fname))
+ with BytesIO() as buf:
+ fig.savefig(buf, bbox_inches="tight", transparent=True)
+ return buf.getvalue()
def ttfFontProperty(font): but even then there's the problem that some of the fonts we ship (e.g. computer modern symbol) or some system fonts (wingdings-like fonts) don't even have all the glyphs to represent their own names... |
But should that be a problem? I think |
PNG has the problem of hidpi (more generally dpi scaling) handling. Without that the PNGs look ugly, particularly with detailed graphics such as fibers. Dpi scaling will become more and more common. I do don‘t think we want to get into that business just for font visualization. So, I recommend not to use PNG. |
Thanks again for this folks! |
I'm not totally sure which font object this would go on, but in the vein of colormaps (#15616), it be helpful if you could get a rendering of the font and pull up a font_list (realized I couldn't get a font working 'cause I spelled it wrong).
Was inspired by this blog post that does this: jonathansoma.com/lede/data-studio/matplotlib/list-all-fonts-available-in-matplotlib-plus-samples/
The hard part here is deciding if it's something that would be accepted and on which object it should go.
The text was updated successfully, but these errors were encountered: