From ba8980c9fe31a2b4f64094b3ada765a6ff4dea1e Mon Sep 17 00:00:00 2001 From: tfpf <19171016+tfpf@users.noreply.github.com> Date: Mon, 22 Aug 2022 22:04:47 +0530 Subject: [PATCH 1/2] When glyph is not found in `cmr10`, substitute from `cmsy10`. --- lib/matplotlib/_mathtext.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/_mathtext.py b/lib/matplotlib/_mathtext.py index 44bb5a9e8fd8..5cc2dc052ce1 100644 --- a/lib/matplotlib/_mathtext.py +++ b/lib/matplotlib/_mathtext.py @@ -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'] @@ -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 From f53301d2db02708452e18618237b716595c3a803 Mon Sep 17 00:00:00 2001 From: tfpf <19171016+tfpf@users.noreply.github.com> Date: Mon, 22 Aug 2022 23:20:11 +0530 Subject: [PATCH 2/2] Test that `cmr10` in Mathtext does not warn about missing `\times`. --- lib/matplotlib/tests/test_ticker.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index e91d3236020d..b474dfdd5eaa 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -1,6 +1,7 @@ from contextlib import nullcontext import itertools import locale +import logging import re import numpy as np @@ -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([])