-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Add feature to fallback to stix font in mathtext #11644
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
Changes from 4 commits
37020f8
3ae26b7
0791122
b95bf17
7a26f16
09f4e6d
0b5ee3a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -758,7 +758,10 @@ class UnicodeFonts(TruetypeFonts): | |
|
||
def __init__(self, *args, **kwargs): | ||
# This must come first so the backend's owner is set correctly | ||
if rcParams['mathtext.fallback_to_cm']: | ||
if rcParams['mathtext.fallback'] == 'stix': | ||
self.cm_fallback = StixFonts(*args, **kwargs) | ||
elif (rcParams['mathtext.fallback'] == 'cm' or | ||
rcParams['mathtext.fallback_to_cm']): | ||
self.cm_fallback = BakomaFonts(*args, **kwargs) | ||
else: | ||
self.cm_fallback = None | ||
|
@@ -771,6 +774,13 @@ def __init__(self, *args, **kwargs): | |
prop = FontProperties('cmex10') | ||
font = findfont(prop) | ||
self.fontmap['ex'] = font | ||
# include stix sized alternatives for glyphs if fallback is stix | ||
if isinstance(self.cm_fallback, StixFonts): | ||
stixsizedaltfonts = "STIXGeneral STIXSizeOneSym STIXSizeTwoSym "\ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
"STIXSizeThreeSym STIXSizeFourSym STIXSizeFiveSym".split() | ||
for i, stixfont in enumerate(stixsizedaltfonts): | ||
font = findfont(stixfont) | ||
self.fontmap[i] = font | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the numbering is essential, have you thought about turning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went for splitting a string here because that is also done in line 770. for texfont in "cal rm tt it bf sf".split(): # ... But of course, there is no line break and the order does not matter. So, I agree that a dict is more appropriate here. I also made sure to add the STIX alternatives with their names to the fontmap self.fontmap[name] = fullpath Not sure whether this is really necessary, but it is also the case for the |
||
|
||
_slanted_symbols = set(r"\int \oint".split()) | ||
|
||
|
@@ -825,13 +835,17 @@ def _get_glyph(self, fontname, font_class, sym, fontsize, math=True): | |
warnings.warn( | ||
"Substituting with a symbol from Computer Modern.", | ||
MathTextWarning) | ||
elif isinstance(self.cm_fallback, StixFonts): | ||
warnings.warn("Substituting with a symbol from STIX.", | ||
MathTextWarning) | ||
|
||
if (fontname in ('it', 'regular') and | ||
isinstance(self.cm_fallback, StixFonts)): | ||
return self.cm_fallback._get_glyph( | ||
'rm', font_class, sym, fontsize) | ||
else: | ||
return self.cm_fallback._get_glyph( | ||
fontname, font_class, sym, fontsize) | ||
fontname = 'rm' | ||
|
||
return self.cm_fallback._get_glyph( | ||
fontname, font_class, sym, fontsize) | ||
|
||
else: | ||
if (fontname in ('it', 'regular') | ||
and isinstance(self, StixFonts)): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -446,6 +446,20 @@ def validate_font_properties(s): | |
return s | ||
|
||
|
||
def validate_mathtext_fallback(s): | ||
fallback_fonts = ['cm', 'stix'] | ||
if s is None: | ||
return s | ||
|
||
if isinstance(s, str): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Valid values of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed this. Not sure, I understand this completely, since def validate_fontsize(s): fontsizes = ['xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'smaller', 'larger'] if isinstance(s, str): s = s.lower() if s in fontsizes: return s try: return float(s) except ValueError: raise ValueError("%s is not a valid font size. Valid font sizes " "are %s." % (s, ", ".join(fontsizes))) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I see. But I might want to go with your initial suggestion then. Currently, the following code mpl.rcParams['mathtext.fallback'] = 1 leads to the error
Of course the user should only supply a string, but the error message might not be very helpful in finding a potential bug in your own code accessing I added the following if not isinstance(s, str): raise ValueError("Must be a string or None.") which yields the following error message
I hope that's ok. |
||
s = s.lower() | ||
|
||
if s in fallback_fonts: | ||
return s | ||
|
||
raise ValueError("%s is not a valid fallback font name. Valid fallback " | ||
"font names are %s." % (s, ", ".join(fallback_fonts))) | ||
|
||
validate_fontset = ValidateInStrings( | ||
'fontset', | ||
['dejavusans', 'dejavuserif', 'cm', 'stix', 'stixsans', 'custom']) | ||
|
@@ -1117,6 +1131,7 @@ def _validate_linestyle(ls): | |
'mathtext.fontset': ['dejavusans', validate_fontset], | ||
'mathtext.default': ['it', validate_mathtext_default], | ||
'mathtext.fallback_to_cm': [True, validate_bool], | ||
'mathtext.fallback': [None, validate_mathtext_fallback], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs some documentation. In particular it seems Preferably, we would deprecate This needs to be in |
||
|
||
'image.aspect': ['equal', validate_aspect], # equal, auto, a number | ||
'image.interpolation': ['nearest', validate_string], | ||
|