Skip to content

Get Mathtext \times symbol from cmsy10 when using cmr10. #23702

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 2 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/matplotlib/_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,14 @@ class UnicodeFonts(TruetypeFonts):
symbol can not be found in the font.
"""

# Some glyphs are not present in the `cmr10` font, and must be brought in
# from `cmsy10`. Map the Unicode indices of those glyphs to the indices at
# which they are found in `cmsy10`.
_cmr10_substitutions = {
0x00D7: 0x00A3, # Multiplication sign.
0x2212: 0x00A1, # Minus sign.
}

def __init__(self, *args, **kwargs):
# This must come first so the backend's owner is set correctly
fallback_rc = mpl.rcParams['mathtext.fallback']
Expand Down Expand Up @@ -541,11 +549,11 @@ def _get_glyph(self, fontname, font_class, sym):
found_symbol = False
font = self._get_font(new_fontname)
if font is not None:
if font.family_name == "cmr10" and uniindex == 0x2212:
# minus sign exists in cmsy10 (not cmr10)
if (uniindex in self._cmr10_substitutions
and font.family_name == "cmr10"):
font = get_font(
cbook._get_data_path("fonts/ttf/cmsy10.ttf"))
uniindex = 0xa1
uniindex = self._cmr10_substitutions[uniindex]
glyphindex = font.get_char_index(uniindex)
if glyphindex != 0:
found_symbol = True
Expand Down
19 changes: 19 additions & 0 deletions lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from contextlib import nullcontext
import itertools
import locale
import logging
import re

import numpy as np
Expand Down Expand Up @@ -725,6 +726,24 @@ def test_mathtext_ticks(self):
ax.set_xticks([-1, 0, 1])
fig.canvas.draw()

def test_cmr10_substitutions(self, caplog):
mpl.rcParams.update({
'font.family': 'cmr10',
'mathtext.fontset': 'cm',
'axes.formatter.use_mathtext': True,
})

# Test that it does not log a warning about missing glyphs.
with caplog.at_level(logging.WARNING, logger='matplotlib.mathtext'):
fig, ax = plt.subplots()
ax.plot([-0.03, 0.05], [40, 0.05])
ax.set_yscale('log')
yticks = [0.02, 0.3, 4, 50]
formatter = mticker.LogFormatterSciNotation()
ax.set_yticks(yticks, map(formatter, yticks))
fig.canvas.draw()
assert not caplog.text

def test_empty_locs(self):
sf = mticker.ScalarFormatter()
sf.set_locs([])
Expand Down