Skip to content

Commit d0bef3b

Browse files
authored
Merge pull request #22507 from anntzer/mathtextunmath
Remove *math* parameter of various mathtext internal APIs.
2 parents 4cc7ef3 + 85f30cb commit d0bef3b

File tree

10 files changed

+84
-719
lines changed

10 files changed

+84
-719
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The *math* parameter of ``mathtext.get_unicode_index``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
In math mode, ASCII hyphens (U+002D) are now replaced by unicode minus signs
5+
(U+2212) at the parsing stage.

lib/matplotlib/_mathtext.py

+30-30
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
QuotedString, Regex, StringEnd, ZeroOrMore, pyparsing_common)
1919

2020
import matplotlib as mpl
21-
from . import cbook
21+
from . import _api, cbook
2222
from ._mathtext_data import (
2323
latex_to_bakoma, stix_glyph_fixes, stix_virtual_fonts, tex2uni)
2424
from .font_manager import FontProperties, findfont, get_font
@@ -33,7 +33,8 @@
3333
# FONTS
3434

3535

36-
def get_unicode_index(symbol, math=True):
36+
@_api.delete_parameter("3.6", "math")
37+
def get_unicode_index(symbol, math=True): # Publicly exported.
3738
r"""
3839
Return the integer index (from the Unicode table) of *symbol*.
3940
@@ -45,15 +46,13 @@ def get_unicode_index(symbol, math=True):
4546
math : bool, default: True
4647
If False, always treat as a single Unicode character.
4748
"""
48-
# for a non-math symbol, simply return its Unicode index
49-
if not math:
50-
return ord(symbol)
5149
# From UTF #25: U+2212 minus sign is the preferred
5250
# representation of the unary and binary minus sign rather than
5351
# the ASCII-derived U+002D hyphen-minus, because minus sign is
5452
# unambiguous and because it is rendered with a more desirable
5553
# length, usually longer than a hyphen.
56-
if symbol == '-':
54+
# Remove this block when the 'math' parameter is deleted.
55+
if math and symbol == '-':
5756
return 0x2212
5857
try: # This will succeed if symbol is a single Unicode char
5958
return ord(symbol)
@@ -98,7 +97,7 @@ def get_kern(self, font1, fontclass1, sym1, fontsize1,
9897
"""
9998
return 0.
10099

101-
def get_metrics(self, font, font_class, sym, fontsize, dpi, math=True):
100+
def get_metrics(self, font, font_class, sym, fontsize, dpi):
102101
r"""
103102
Parameters
104103
----------
@@ -117,8 +116,6 @@ def get_metrics(self, font, font_class, sym, fontsize, dpi, math=True):
117116
Font size in points.
118117
dpi : float
119118
Rendering dots-per-inch.
120-
math : bool
121-
Whether we are currently in math mode or not.
122119
123120
Returns
124121
-------
@@ -136,7 +133,7 @@ def get_metrics(self, font, font_class, sym, fontsize, dpi, math=True):
136133
- *slanted*: Whether the glyph should be considered as "slanted"
137134
(currently used for kerning sub/superscripts).
138135
"""
139-
info = self._get_info(font, font_class, sym, fontsize, dpi, math)
136+
info = self._get_info(font, font_class, sym, fontsize, dpi)
140137
return info.metrics
141138

142139
def render_glyph(self, ox, oy, font, font_class, sym, fontsize, dpi):
@@ -217,14 +214,14 @@ def _get_offset(self, font, glyph, fontsize, dpi):
217214
return (glyph.height / 64 / 2) + (fontsize/3 * dpi/72)
218215
return 0.
219216

220-
def _get_info(self, fontname, font_class, sym, fontsize, dpi, math=True):
217+
def _get_info(self, fontname, font_class, sym, fontsize, dpi):
221218
key = fontname, font_class, sym, fontsize, dpi
222219
bunch = self.glyphd.get(key)
223220
if bunch is not None:
224221
return bunch
225222

226223
font, num, slanted = self._get_glyph(
227-
fontname, font_class, sym, fontsize, math)
224+
fontname, font_class, sym, fontsize)
228225

229226
font.set_size(fontsize, dpi)
230227
glyph = font.load_char(
@@ -314,7 +311,7 @@ def __init__(self, *args, **kwargs):
314311

315312
_slanted_symbols = set(r"\int \oint".split())
316313

317-
def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
314+
def _get_glyph(self, fontname, font_class, sym, fontsize):
318315
font = None
319316
if fontname in self.fontmap and sym in latex_to_bakoma:
320317
basename, num = latex_to_bakoma[sym]
@@ -329,7 +326,7 @@ def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
329326
return font, num, slanted
330327
else:
331328
return self._stix_fallback._get_glyph(
332-
fontname, font_class, sym, fontsize, math)
329+
fontname, font_class, sym, fontsize)
333330

334331
# The Bakoma fonts contain many pre-sized alternatives for the
335332
# delimiters. The AutoSizedChar class will use these alternatives
@@ -442,9 +439,9 @@ def __init__(self, *args, **kwargs):
442439
def _map_virtual_font(self, fontname, font_class, uniindex):
443440
return fontname, uniindex
444441

445-
def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
442+
def _get_glyph(self, fontname, font_class, sym, fontsize):
446443
try:
447-
uniindex = get_unicode_index(sym, math)
444+
uniindex = get_unicode_index(sym)
448445
found_symbol = True
449446
except ValueError:
450447
uniindex = ord('?')
@@ -536,23 +533,20 @@ def __init__(self, *args, **kwargs):
536533
self.fontmap[key] = fullpath
537534
self.fontmap[name] = fullpath
538535

539-
def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
536+
def _get_glyph(self, fontname, font_class, sym, fontsize):
540537
# Override prime symbol to use Bakoma.
541538
if sym == r'\prime':
542-
return self.bakoma._get_glyph(
543-
fontname, font_class, sym, fontsize, math)
539+
return self.bakoma._get_glyph(fontname, font_class, sym, fontsize)
544540
else:
545541
# check whether the glyph is available in the display font
546542
uniindex = get_unicode_index(sym)
547543
font = self._get_font('ex')
548544
if font is not None:
549545
glyphindex = font.get_char_index(uniindex)
550546
if glyphindex != 0:
551-
return super()._get_glyph(
552-
'ex', font_class, sym, fontsize, math)
547+
return super()._get_glyph('ex', font_class, sym, fontsize)
553548
# otherwise return regular glyph
554-
return super()._get_glyph(
555-
fontname, font_class, sym, fontsize, math)
549+
return super()._get_glyph(fontname, font_class, sym, fontsize)
556550

557551

558552
class DejaVuSerifFonts(DejaVuFonts):
@@ -913,15 +907,14 @@ class Char(Node):
913907
`Hlist`.
914908
"""
915909

916-
def __init__(self, c, state, math=True):
910+
def __init__(self, c, state):
917911
super().__init__()
918912
self.c = c
919913
self.font_output = state.font_output
920914
self.font = state.font
921915
self.font_class = state.font_class
922916
self.fontsize = state.fontsize
923917
self.dpi = state.dpi
924-
self.math = math
925918
# The real width, height and depth will be set during the
926919
# pack phase, after we know the real fontsize
927920
self._update_metrics()
@@ -931,8 +924,7 @@ def __repr__(self):
931924

932925
def _update_metrics(self):
933926
metrics = self._metrics = self.font_output.get_metrics(
934-
self.font, self.font_class, self.c, self.fontsize, self.dpi,
935-
self.math)
927+
self.font, self.font_class, self.c, self.fontsize, self.dpi)
936928
if self.c == ' ':
937929
self.width = metrics.advance
938930
else:
@@ -1624,8 +1616,9 @@ class _MathStyle(enum.Enum):
16241616
SCRIPTSTYLE = enum.auto()
16251617
SCRIPTSCRIPTSTYLE = enum.auto()
16261618

1627-
_binary_operators = set(r'''
1628-
+ * -
1619+
_binary_operators = set(
1620+
'+ * - \N{MINUS SIGN}'
1621+
r'''
16291622
\pm \sqcap \rhd
16301623
\mp \sqcup \unlhd
16311624
\times \vee \unrhd
@@ -1922,7 +1915,7 @@ def math(self, s, loc, toks):
19221915

19231916
def non_math(self, s, loc, toks):
19241917
s = toks[0].replace(r'\$', '$')
1925-
symbols = [Char(c, self.get_state(), math=False) for c in s]
1918+
symbols = [Char(c, self.get_state()) for c in s]
19261919
hlist = Hlist(symbols)
19271920
# We're going into math now, so set font to 'it'
19281921
self.push_state()
@@ -1969,6 +1962,13 @@ def customspace(self, s, loc, toks):
19691962

19701963
def symbol(self, s, loc, toks):
19711964
c = toks["sym"]
1965+
if c == "-":
1966+
# "U+2212 minus sign is the preferred representation of the unary
1967+
# and binary minus sign rather than the ASCII-derived U+002D
1968+
# hyphen-minus, because minus sign is unambiguous and because it
1969+
# is rendered with a more desirable length, usually longer than a
1970+
# hyphen." (https://www.unicode.org/reports/tr25/)
1971+
c = "\N{MINUS SIGN}"
19721972
try:
19731973
char = Char(c, self.get_state())
19741974
except ValueError as err:

lib/matplotlib/_mathtext_data.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
']' : ('cmr10', 0x5d),
133133

134134
'*' : ('cmsy10', 0xa4),
135-
'-' : ('cmsy10', 0xa1),
135+
'\N{MINUS SIGN}' : ('cmsy10', 0xa1),
136136
'\\Downarrow' : ('cmsy10', 0x2b),
137137
'\\Im' : ('cmsy10', 0x3d),
138138
'\\Leftarrow' : ('cmsy10', 0x28),
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)