Skip to content
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

[Bug]: Font fallback doesn't work on \mathdefault #27838

Open
dedebenui opened this issue Feb 29, 2024 · 3 comments
Open

[Bug]: Font fallback doesn't work on \mathdefault #27838

dedebenui opened this issue Feb 29, 2024 · 3 comments

Comments

@dedebenui
Copy link

dedebenui commented Feb 29, 2024

Bug summary

Per character font fallback (#20740) works great, except in tick labels.

Code for reproduction

import matplotlib.pyplot as plt

# CMU fonts available here : https://www.1001fonts.com/download/cmu.zip
plt.rcParams["font.family"] = ["CMU Bright", "DejaVu Sans"]

s = "There are \u2212 \u00D7 in between!"
plt.figtext(0.18, 0.45, s)
plt.gcf().suptitle(s)
plt.gca().set_title(s)
plt.xscale("log")
plt.xlim(1e-2, 6e-2)
plt.show()

Actual outcome

In the tick labels, characters not covered by CMU Bright are rendered with a placeholder instead of falling back to DejaVu
image

Expected outcome

Tick labels should fall back to DejaVu for \u2212 (minus sign) and \u00D7 (times sign) as in the other text instances.

Additional information

It seems to happen only with tick labels, other instances of text rendering work as intended in #20740. I get the same outcome with both macosx and TkAgg backends

I get the following warnings:

Font 'default' does not have a glyph for '\u2212' [U+2212], substituting with a dummy symbol.
Font 'default' does not have a glyph for '\\times' [U+d7], substituting with a dummy symbol.

Operating system

macOS 14.3.1

Matplotlib Version

3.8.3

Matplotlib Backend

MacOSX

Python version

3.11.7

Jupyter version

No response

Installation

pip

@QuLogic
Copy link
Member

QuLogic commented Mar 21, 2024

This is some specific thing to do with mathtext which seems to some special glyph mapping itself, but the odd thing is that if you wrap the other text in $, that doesn't repeat the bug.

@QuLogic
Copy link
Member

QuLogic commented Mar 21, 2024

Ah, the difference is that the ticks wrap their text in \mathdefault which picks up CMU Bright that you set, but regular math text would use the fonts from the mathtext.* rcParams, so that's why the regular text appears to work even when wrapped in $.

So the bug is that in mathtext, \mathdefault text only uses the first font specified as the default. A shortened reproducer is:

import matplotlib.pyplot as plt

# CMU fonts available here : https://www.1001fonts.com/download/cmu.zip
plt.rcParams["font.family"] = ["CMU Bright", "DejaVu Sans"]

s = "$\\mathdefault{\u2212 \u00D7 \\times}$"
plt.figtext(0.5, 0.5, s, fontsize=30, horizontalalignment='center')
plt.show()

which "works" if you replace \mathdefault with \mathrm.

@QuLogic QuLogic changed the title [Bug]: Font fallback doesn't work on tick labels [Bug]: Font fallback doesn't work on \mathdefault Mar 21, 2024
@dedebenui
Copy link
Author

It seems that mathdefault also ignores axes.unicode_minus:

import matplotlib.pyplot as plt

# CMU fonts available here : https://www.1001fonts.com/download/cmu.zip
plt.rcParams["font.family"] = ["CMU Bright", "DejaVu Sans"]
plt.rcParams["axes.unicode_minus"] = False
s = "$\\mathdefault{\u2212 \u00D7 \\times - } - $ - "
plt.figtext(0.5, 0.5, s, fontsize=30, horizontalalignment='center')
plt.show()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants