Skip to content

Commit 95d012c

Browse files
committed
mathtext: implement \mathnormal and distinguish between normal and it
To replicate LaTeX behaviour, distinguish between "italic" and "normal" math. In particular, digits should be set italic in the \mathit env. For `cm`, use cmti font for "it" For general UnicodeFont (stix, DejaVu, ...), maps digits to roman or italic alphabet, depending on "normal" or "it" environment.
1 parent 7aacab5 commit 95d012c

File tree

4 files changed

+354
-19
lines changed

4 files changed

+354
-19
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,9 @@ def get_metrics(self, font: str, font_class: str, sym: str, fontsize: float,
269269
----------
270270
font : str
271271
One of the TeX font names: "tt", "it", "rm", "cal", "sf", "bf",
272-
"default", "regular", "bb", "frak", "scr". "default" and "regular"
273-
are synonyms and use the non-math font.
272+
"default", "regular", "normal", "bb", "frak", "scr". "default"
273+
and "regular" are synonyms and use the non-math font.
274+
"normal" denotes the normal math font.
274275
font_class : str
275276
One of the TeX font names (as for *font*), but **not** "bb",
276277
"frak", or "scr". This is used to combine two font classes. The
@@ -457,10 +458,11 @@ class BakomaFonts(TruetypeFonts):
457458
its own proprietary 8-bit encoding.
458459
"""
459460
_fontmap = {
461+
'normal': 'cmmi10',
460462
'cal': 'cmsy10',
461463
'rm': 'cmr10',
462464
'tt': 'cmtt10',
463-
'it': 'cmmi10',
465+
'it': 'cmti10',
464466
'bf': 'cmb10',
465467
'sf': 'cmss10',
466468
'ex': 'cmex10',
@@ -480,11 +482,18 @@ def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlag
480482
def _get_glyph(self, fontname: str, font_class: str,
481483
sym: str) -> tuple[FT2Font, CharacterCodeType, bool]:
482484
font = None
485+
483486
if fontname in self.fontmap and sym in latex_to_bakoma:
484487
basename, num = latex_to_bakoma[sym]
485-
slanted = (basename == "cmmi10") or sym in self._slanted_symbols
488+
slanted = (basename in ("cmmi10", "cmti10")) or sym in self._slanted_symbols
486489
font = self._get_font(basename)
487490
elif len(sym) == 1:
491+
if fontname == "normal" and sym.isdigit():
492+
# use digits from cmr (roman alphabet) instead of cmm (math alphabet),
493+
# same as LaTeX does.
494+
# This was previously mapped via `latex_to_bakoma`.
495+
fontname = "rm"
496+
488497
slanted = (fontname == "it")
489498
font = self._get_font(fontname)
490499
if font is not None:
@@ -616,11 +625,14 @@ def _get_glyph(self, fontname: str, font_class: str,
616625
# Only characters in the "Letter" class should be italicized in 'it'
617626
# mode. Greek capital letters should be Roman.
618627
if found_symbol:
619-
if fontname == 'it' and uniindex < 0x10000:
628+
if fontname == 'normal' and uniindex < 0x10000:
629+
# normal mathematics font
620630
char = chr(uniindex)
621631
if (unicodedata.category(char)[0] != "L"
622632
or unicodedata.name(char).startswith("GREEK CAPITAL")):
623633
new_fontname = 'rm'
634+
else:
635+
new_fontname = 'it'
624636

625637
slanted = (new_fontname == 'it') or sym in self._slanted_symbols
626638
found_symbol = False
@@ -637,7 +649,7 @@ def _get_glyph(self, fontname: str, font_class: str,
637649

638650
if not found_symbol:
639651
if self._fallback_font:
640-
if (fontname in ('it', 'regular')
652+
if (fontname in ('it', 'regular', 'normal')
641653
and isinstance(self._fallback_font, StixFonts)):
642654
fontname = 'rm'
643655

@@ -649,7 +661,7 @@ def _get_glyph(self, fontname: str, font_class: str,
649661
return g
650662

651663
else:
652-
if (fontname in ('it', 'regular')
664+
if (fontname in ('it', 'regular', 'normal')
653665
and isinstance(self, StixFonts)):
654666
return self._get_glyph('rm', font_class, sym)
655667
_log.warning("Font %r does not have a glyph for %a [U+%x], "
@@ -2010,7 +2022,7 @@ class _MathStyle(enum.Enum):
20102022
_dropsub_symbols = set(r'\int \oint \iint \oiint \iiint \oiiint \iiiint'.split())
20112023

20122024
_fontnames = set("rm cal it tt sf bf bfit "
2013-
"default bb frak scr regular".split())
2025+
"default bb frak scr regular normal".split())
20142026

20152027
_function_names = set("""
20162028
arccos csc ker min arcsin deg lg Pr arctan det lim sec arg dim
@@ -2270,7 +2282,7 @@ def non_math(self, toks: ParseResults) -> T.Any:
22702282
hlist = Hlist(symbols)
22712283
# We're going into math now, so set font to 'it'
22722284
self.push_state()
2273-
self.get_state().font = mpl.rcParams['mathtext.default']
2285+
self.get_state().font = "normal"
22742286
return [hlist]
22752287

22762288
float_literal = staticmethod(pyparsing_common.convert_to_float)

lib/matplotlib/_mathtext_data.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,6 @@
161161
'(' : ('cmr10', 0x28),
162162
')' : ('cmr10', 0x29),
163163
'+' : ('cmr10', 0x2b),
164-
'0' : ('cmr10', 0x30),
165-
'1' : ('cmr10', 0x31),
166-
'2' : ('cmr10', 0x32),
167-
'3' : ('cmr10', 0x33),
168-
'4' : ('cmr10', 0x34),
169-
'5' : ('cmr10', 0x35),
170-
'6' : ('cmr10', 0x36),
171-
'7' : ('cmr10', 0x37),
172-
'8' : ('cmr10', 0x38),
173-
'9' : ('cmr10', 0x39),
174164
':' : ('cmr10', 0x3a),
175165
';' : ('cmr10', 0x3b),
176166
'=' : ('cmr10', 0x3d),

0 commit comments

Comments
 (0)