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

Skip to content

Fix Text class bug when font argument is provided without math_fontfamily #20101

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 17 commits into from
May 26, 2021
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
3 changes: 1 addition & 2 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def __init__(self, family=None, style=None, variant=None, weight=None,
self._stretch = rcParams['font.stretch']
self._size = rcParams['font.size']
self._file = None
self._math_fontfamily = None
self.set_math_fontfamily(math_fontfamily)

if isinstance(family, str):
# Treat family as a fontconfig pattern if it is the only
Expand All @@ -646,7 +646,6 @@ def __init__(self, family=None, style=None, variant=None, weight=None,
self.set_stretch(stretch)
self.set_file(fname)
self.set_size(size)
self.set_math_fontfamily(math_fontfamily)

@classmethod
def _from_any(cls, arg):
Expand Down
40 changes: 40 additions & 0 deletions lib/matplotlib/tests/test_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,46 @@ def test_math_fontfamily():
size=24, math_fontfamily='stix')


def test_default_math_fontfamily():
mpl.rcParams['mathtext.fontset'] = 'cm'
test_str = r'abc$abc\alpha$'
fig, ax = plt.subplots()

text1 = fig.text(0.1, 0.1, test_str, font='Arial')
prop1 = text1.get_fontproperties()
assert prop1.get_math_fontfamily() == 'cm'
text2 = fig.text(0.2, 0.2, test_str, fontproperties='Arial')
prop2 = text2.get_fontproperties()
assert prop2.get_math_fontfamily() == 'cm'

fig.draw_no_output()


def test_argument_order():
mpl.rcParams['mathtext.fontset'] = 'cm'
test_str = r'abc$abc\alpha$'
fig, ax = plt.subplots()

text1 = fig.text(0.1, 0.1, test_str,
math_fontfamily='dejavusans', font='Arial')
prop1 = text1.get_fontproperties()
assert prop1.get_math_fontfamily() == 'dejavusans'
text2 = fig.text(0.2, 0.2, test_str,
math_fontfamily='dejavusans', fontproperties='Arial')
prop2 = text2.get_fontproperties()
assert prop2.get_math_fontfamily() == 'dejavusans'
text3 = fig.text(0.3, 0.3, test_str,
font='Arial', math_fontfamily='dejavusans')
prop3 = text3.get_fontproperties()
assert prop3.get_math_fontfamily() == 'dejavusans'
text4 = fig.text(0.4, 0.4, test_str,
fontproperties='Arial', math_fontfamily='dejavusans')
prop4 = text4.get_fontproperties()
assert prop4.get_math_fontfamily() == 'dejavusans'

fig.draw_no_output()


def test_mathtext_cmr10_minus_sign():
# cmr10 does not contain a minus sign and used to issue a warning
# RuntimeWarning: Glyph 8722 missing from current font.
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ def __init__(self,

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