Skip to content

Commit 7f758c6

Browse files
authored
Merge pull request matplotlib#23702 from tfpf/cmr10_cmsy10_substitutions
Get Mathtext `\times` symbol from `cmsy10` when using `cmr10`.
2 parents 60d3d29 + f53301d commit 7f758c6

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,14 @@ class UnicodeFonts(TruetypeFonts):
475475
symbol can not be found in the font.
476476
"""
477477

478+
# Some glyphs are not present in the `cmr10` font, and must be brought in
479+
# from `cmsy10`. Map the Unicode indices of those glyphs to the indices at
480+
# which they are found in `cmsy10`.
481+
_cmr10_substitutions = {
482+
0x00D7: 0x00A3, # Multiplication sign.
483+
0x2212: 0x00A1, # Minus sign.
484+
}
485+
478486
def __init__(self, *args, **kwargs):
479487
# This must come first so the backend's owner is set correctly
480488
fallback_rc = mpl.rcParams['mathtext.fallback']
@@ -541,11 +549,11 @@ def _get_glyph(self, fontname, font_class, sym):
541549
found_symbol = False
542550
font = self._get_font(new_fontname)
543551
if font is not None:
544-
if font.family_name == "cmr10" and uniindex == 0x2212:
545-
# minus sign exists in cmsy10 (not cmr10)
552+
if (uniindex in self._cmr10_substitutions
553+
and font.family_name == "cmr10"):
546554
font = get_font(
547555
cbook._get_data_path("fonts/ttf/cmsy10.ttf"))
548-
uniindex = 0xa1
556+
uniindex = self._cmr10_substitutions[uniindex]
549557
glyphindex = font.get_char_index(uniindex)
550558
if glyphindex != 0:
551559
found_symbol = True

lib/matplotlib/tests/test_ticker.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from contextlib import nullcontext
22
import itertools
33
import locale
4+
import logging
45
import re
56

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

729+
def test_cmr10_substitutions(self, caplog):
730+
mpl.rcParams.update({
731+
'font.family': 'cmr10',
732+
'mathtext.fontset': 'cm',
733+
'axes.formatter.use_mathtext': True,
734+
})
735+
736+
# Test that it does not log a warning about missing glyphs.
737+
with caplog.at_level(logging.WARNING, logger='matplotlib.mathtext'):
738+
fig, ax = plt.subplots()
739+
ax.plot([-0.03, 0.05], [40, 0.05])
740+
ax.set_yscale('log')
741+
yticks = [0.02, 0.3, 4, 50]
742+
formatter = mticker.LogFormatterSciNotation()
743+
ax.set_yticks(yticks, map(formatter, yticks))
744+
fig.canvas.draw()
745+
assert not caplog.text
746+
728747
def test_empty_locs(self):
729748
sf = mticker.ScalarFormatter()
730749
sf.set_locs([])

0 commit comments

Comments
 (0)