Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee790a3

Browse files
committed
feat: mathtext.fallback genralized
1 parent ce57855 commit ee790a3

File tree

5 files changed

+53
-23
lines changed

5 files changed

+53
-23
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Add generalized "mathtext.fallback" rcParam
2+
------------------------------------------------------------------------
3+
New "mathtext.fallback" rcParam. Takes "cm", "stix", "stixsans"
4+
or "none" to turn fallback off. "mathtext.fallback_to_cm" is
5+
deprecated, but if used, will override new fallback.

lib/matplotlib/mathtext.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -769,18 +769,18 @@ class UnicodeFonts(TruetypeFonts):
769769

770770
def __init__(self, *args, **kwargs):
771771
# This must come first so the backend's owner is set correctly
772-
_fallback_rc = rcParams['mathtext.fallback']
772+
fallback_rc = rcParams['mathtext.fallback']
773773
if rcParams['mathtext.fallback_to_cm'] is not None:
774774
if rcParams['mathtext.fallback_to_cm']:
775-
_fallback_rc = 'cm'
775+
fallback_rc = 'cm'
776776
else:
777-
_fallback_rc = None
777+
fallback_rc = None
778778

779-
if _fallback_rc == 'stix':
779+
if fallback_rc == 'stix':
780780
self.cm_fallback = StixFonts(*args, **kwargs)
781-
elif _fallback_rc == 'stixsans':
781+
elif fallback_rc == 'stixsans':
782782
self.cm_fallback = StixSansFonts(*args, **kwargs)
783-
elif _fallback_rc == 'cm':
783+
elif fallback_rc == 'cm':
784784
self.cm_fallback = BakomaFonts(*args, **kwargs)
785785
else:
786786
self.cm_fallback = None
@@ -856,19 +856,18 @@ def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
856856

857857
if not found_symbol:
858858
if self.cm_fallback:
859-
if type(self.cm_fallback) is BakomaFonts:
860-
_log.warning("Substituting with a symbol from Computer "
861-
"Modern.")
862-
elif type(self.cm_fallback) is StixFonts:
863-
_log.warning("Substituting with a symbol from STIX.")
864-
elif type(self.cm_fallback) is StixSansFonts:
865-
_log.warning("Substituting with a symbol from STIX Sans.")
866859
if (fontname in ('it', 'regular')
867860
and isinstance(self.cm_fallback, StixFonts)):
868861
fontname = 'rm'
869862

870-
return self.cm_fallback._get_glyph(
871-
fontname, font_class, sym, fontsize)
863+
g = self.cm_fallback._get_glyph(fontname, font_class,
864+
sym, fontsize)
865+
fname = g[0].family_name
866+
if fname in list(BakomaFonts._fontmap.values()):
867+
fname = "Computer Modern"
868+
_log.warning("Substituting symbol {} "
869+
"from {}".format(sym, fname))
870+
return g
872871

873872
else:
874873
if (fontname in ('it', 'regular')

lib/matplotlib/rcsetup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def validate_font_properties(s):
413413
return s
414414

415415

416-
def validate_mathtext_fallback_to_cm(b):
416+
def _validate_mathtext_fallback_to_cm(b):
417417
"""
418418
Temporary validate for fallback_to_cm, while deprecated
419419
@@ -430,7 +430,7 @@ def validate_mathtext_fallback_to_cm(b):
430430
return validate_bool_maybe_none(b)
431431

432432

433-
def validate_mathtext_fallback(s):
433+
def _validate_mathtext_fallback(s):
434434
_fallback_fonts = ['cm', 'stix', 'stixsans']
435435
if isinstance(s, str):
436436
s = s.lower()
@@ -1182,8 +1182,8 @@ def _convert_validator_spec(key, conv):
11821182
'mathtext.default': [
11831183
'it',
11841184
['rm', 'cal', 'it', 'tt', 'sf', 'bf', 'default', 'bb', 'frak', 'scr', 'regular']],
1185-
'mathtext.fallback_to_cm': [None, validate_mathtext_fallback_to_cm],
1186-
'mathtext.fallback': ['cm', validate_mathtext_fallback],
1185+
'mathtext.fallback_to_cm': [None, _validate_mathtext_fallback_to_cm],
1186+
'mathtext.fallback': ['cm', _validate_mathtext_fallback],
11871187

11881188
'image.aspect': ['equal', validate_aspect], # equal, auto, a number
11891189
'image.interpolation': ['antialiased', validate_string],

lib/matplotlib/tests/test_mathtext.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import io
2+
import os
23
import re
34

45
import numpy as np
56
import pytest
6-
import warnings
77

88
import matplotlib
99
import matplotlib as mpl
@@ -303,3 +303,29 @@ def test_mathtext_fallback_to_cm_invalid():
303303
mpl.rcParams['mathtext.fallback_to_cm'] = fallback
304304

305305

306+
@pytest.mark.parametrize(
307+
"fallback,fontlist",
308+
[("cm", ['DejaVu Sans', 'mpltest', 'STIXGeneral', 'cmr10', 'STIXGeneral']),
309+
("stix", ['DejaVu Sans', 'mpltest', 'STIXGeneral'])])
310+
def test_mathtext_fallback_cm(fallback, fontlist):
311+
mpl.font_manager.fontManager.addfont(
312+
os.path.join((os.path.dirname(os.path.realpath(__file__))), 'mpltest.ttf'))
313+
mpl.rcParams["svg.fonttype"] = 'none'
314+
mpl.rcParams['mathtext.fontset'] = 'custom'
315+
mpl.rcParams['mathtext.rm'] = 'mpltest'
316+
mpl.rcParams['mathtext.it'] = 'mpltest:italic'
317+
mpl.rcParams['mathtext.bf'] = 'mpltest:bold'
318+
mpl.rcParams['mathtext.fallback'] = fallback
319+
320+
test_str = r'a$A\AA\breve\gimel$'
321+
322+
buff = io.BytesIO()
323+
fig, ax = plt.subplots()
324+
fig.text(.5, .5, test_str, fontsize=40, ha='center')
325+
fig.savefig(buff, format="svg")
326+
char_fonts = [
327+
line.split("font-family:")[-1].split(";")[0]
328+
for line in str(buff.getvalue()).split(r"\n") if "tspan" in line
329+
]
330+
assert char_fonts == fontlist
331+
mpl.font_manager.fontManager.ttflist = mpl.font_manager.fontManager.ttflist[:-1]

tutorials/text/mathtext.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,9 @@
296296
The fonts used should have a Unicode mapping in order to find any
297297
non-Latin characters, such as Greek. If you want to use a math symbol
298298
that is not contained in your custom fonts, you can set
299-
:rc:`mathtext.fallback` to either ``'cm'`` or ``'stix'``
300-
which ``'stixsans'`` will cause the mathtext system to use
301-
characters from an alternative font whenever a particular
299+
:rc:`mathtext.fallback` to either ``'cm'``, ``'stix'`` or ``'stixsans'``
300+
which will cause the mathtext system to use
301+
characters from an alternative font whenever a particular
302302
character can not be found in the custom font.
303303
304304
Note that the math glyphs specified in Unicode have evolved over time, and many

0 commit comments

Comments
 (0)