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

Skip to content

Commit c4dcf5e

Browse files
ain-sophQuLogic
andauthored
Fix Text class bug when font argument is provided without math_fontfamily (#20101)
* fix mathtext.fontset issue * fix argument order issue * better fix using alias_map * indent fix * Update lib/matplotlib/text.py indent fix Co-authored-by: Elliott Sales de Andrade <[email protected]> * add test * split into 2 test methods * fix test * add a whitespace * use cbook.normalize_kwargs * no need to copy (normalize_kwargs already copy it) Co-authored-by: Elliott Sales de Andrade <[email protected]>
1 parent 3c6375a commit c4dcf5e

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

lib/matplotlib/font_manager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ def __init__(self, family=None, style=None, variant=None, weight=None,
666666
self._stretch = rcParams['font.stretch']
667667
self._size = rcParams['font.size']
668668
self._file = None
669-
self._math_fontfamily = None
669+
self.set_math_fontfamily(math_fontfamily)
670670

671671
if isinstance(family, str):
672672
# Treat family as a fontconfig pattern if it is the only
@@ -683,7 +683,6 @@ def __init__(self, family=None, style=None, variant=None, weight=None,
683683
self.set_stretch(stretch)
684684
self.set_file(fname)
685685
self.set_size(size)
686-
self.set_math_fontfamily(math_fontfamily)
687686

688687
@classmethod
689688
def _from_any(cls, arg):

lib/matplotlib/tests/test_mathtext.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,46 @@ def test_math_fontfamily():
390390
size=24, math_fontfamily='stix')
391391

392392

393+
def test_default_math_fontfamily():
394+
mpl.rcParams['mathtext.fontset'] = 'cm'
395+
test_str = r'abc$abc\alpha$'
396+
fig, ax = plt.subplots()
397+
398+
text1 = fig.text(0.1, 0.1, test_str, font='Arial')
399+
prop1 = text1.get_fontproperties()
400+
assert prop1.get_math_fontfamily() == 'cm'
401+
text2 = fig.text(0.2, 0.2, test_str, fontproperties='Arial')
402+
prop2 = text2.get_fontproperties()
403+
assert prop2.get_math_fontfamily() == 'cm'
404+
405+
fig.draw_no_output()
406+
407+
408+
def test_argument_order():
409+
mpl.rcParams['mathtext.fontset'] = 'cm'
410+
test_str = r'abc$abc\alpha$'
411+
fig, ax = plt.subplots()
412+
413+
text1 = fig.text(0.1, 0.1, test_str,
414+
math_fontfamily='dejavusans', font='Arial')
415+
prop1 = text1.get_fontproperties()
416+
assert prop1.get_math_fontfamily() == 'dejavusans'
417+
text2 = fig.text(0.2, 0.2, test_str,
418+
math_fontfamily='dejavusans', fontproperties='Arial')
419+
prop2 = text2.get_fontproperties()
420+
assert prop2.get_math_fontfamily() == 'dejavusans'
421+
text3 = fig.text(0.3, 0.3, test_str,
422+
font='Arial', math_fontfamily='dejavusans')
423+
prop3 = text3.get_fontproperties()
424+
assert prop3.get_math_fontfamily() == 'dejavusans'
425+
text4 = fig.text(0.4, 0.4, test_str,
426+
fontproperties='Arial', math_fontfamily='dejavusans')
427+
prop4 = text4.get_fontproperties()
428+
assert prop4.get_math_fontfamily() == 'dejavusans'
429+
430+
fig.draw_no_output()
431+
432+
393433
def test_mathtext_cmr10_minus_sign():
394434
# cmr10 does not contain a minus sign and used to issue a warning
395435
# RuntimeWarning: Glyph 8722 missing from current font.

lib/matplotlib/text.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ def __init__(self,
158158

159159
def update(self, kwargs):
160160
# docstring inherited
161-
# make a copy so we do not mutate user input!
162-
kwargs = dict(kwargs)
161+
kwargs = cbook.normalize_kwargs(kwargs, Text)
163162
sentinel = object() # bbox can be None, so use another sentinel.
164163
# Update fontproperties first, as it has lowest priority.
165164
fontproperties = kwargs.pop("fontproperties", sentinel)

0 commit comments

Comments
 (0)