-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Matplotlib would not try to apply all the font in font list to draw all characters in the given string. #18883
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
Your understanding of the problem is correct, but as noted in #15260 supporting multiple fonts per text element would require pretty big surgery on the text handling machinery. I'm labeling as "hard" not because the implementation is hard, but because it's a lot of work. So I guess PRs are welcome :-) A partial solution (which may be much simpler to implement) may be to just scan the list of fonts to see whether there's one that supports all requested glyphs (so in the case given above we'd always use SimHei for everything). |
I don't think that the implementation is not hard 😢... The font fallback problem is always complex and hard for any software, you may figure out so many questions about it on the internet even from 10 years ago. e.g. servo, Discord, Figma, LaTeX... and so on. It's a possible (which may be sufficient for
Do you have any further solutions? The possible solution I mentioned above is simple to implement but its complexity would be higher than current state. |
As stated above, I think autosearching for a font that covers all characters is reasonable. Still quite a bit of work, but should be doable. Switching fonts mid-text is much harder because of the way Matplotlib is architected (a single font "object" is in charge of laying out the text, but having multiple fonts per text would require inverting this architecture all across the library, which is what I meant by "big surgery"). Perhaps that architecture choice was not optimal, but it was made a long time ago and that's what we are stuck with. Moreover, if we get support for multi-font text, then it seems quite natural that the next question people will ask is whether they can arbitrarily switch fonts in the middle of a text object (it seems a bit unfair if you can use DejaVu for character 1 and SimHei for character 2 if character 1 is latin and character 2 is chinese, but cannot use DejaVu for character 1 and Times for character 2 which are both latin); but then comes the question of whether we really want to have an API to specify that, which doesn't look too appetizing... |
It's no need to please anyone for further questions. That's what the order of font-family list actually means. As the figure I mentioned in the first thread in this issue:
But it's still really a quite bit of work to deal with the single font "object" using in architecture... |
Just chiming in here to express enthusiastic support for per-character font fallback. The lack of fallback support is really hamstringing some display features in librosa that would require music notation. The symbols in question are, AFAIK, only supported by FreeSerif, and forcing a full-string level fallback to FreeSerif would result in some pretty weird looking figures if the preference is otherwise for sans fonts. |
Bug report
Bug summary
The document page Configuring the font family said that:
But the mechanism behind the strings rendering with the font-family is not as same as what most users think.
matplotlib
just finds the font in the font list and apply the first valid one (can be found in the given path) to all characters in the given string.Would
matplotlib
have any plan to implement this feature? There were so many users got into the trouble of missing fonts when we want to show the string mixed with CJK characters and Latin characters.Code for reproduction
Just take a look at this Colab Notebook.
use
['DejaVu Sans', 'SimHei', 'sans-serif']
電壓
and電流
can't be correctly rendered with the current fontDejaVu Sans
.use
['SimHei', 'DejaVu Sans', 'sans-serif']
電壓
and電流
can be correctly rendered with the current fontSimHei
.Voltage
andCurrent
are also be rendered with the current fontSimHei
.Expected outcome
We would like to apply the Chinese font
SimHei
to the Chinese characters and the English fontDejaVu Sans
to the English characters. Just like what we usefont-family
style in CSS:Similar Issues
The text was updated successfully, but these errors were encountered: