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

Skip to content

Commit bba4a0a

Browse files
committed
Remove *math* parameter of various mathtext internal APIs.
The *math* parameter is passed through many layers of the call stack but is ultimately only used for a single purpose: deciding whether to replace the ASCII hyphen by a (longer) unicode minus. Instead of doing that, just do the substitution at the parsing stage.
1 parent 53c5f88 commit bba4a0a

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed
Lines changed: 5 additions & 0 deletions
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

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
ParseResults, QuotedString, Regex, StringEnd, Suppress, White, ZeroOrMore)
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): # Publically 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, fontsize, 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(
@@ -315,7 +312,7 @@ def __init__(self, *args, **kwargs):
315312

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

318-
def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
315+
def _get_glyph(self, fontname, font_class, sym, fontsize):
319316
font = None
320317
if fontname in self.fontmap and sym in latex_to_bakoma:
321318
basename, num = latex_to_bakoma[sym]
@@ -330,7 +327,7 @@ def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
330327
return font, num, fontsize, slanted
331328
else:
332329
return self._stix_fallback._get_glyph(
333-
fontname, font_class, sym, fontsize, math)
330+
fontname, font_class, sym, fontsize)
334331

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

446-
def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
443+
def _get_glyph(self, fontname, font_class, sym, fontsize):
447444
try:
448-
uniindex = get_unicode_index(sym, math)
445+
uniindex = get_unicode_index(sym)
449446
found_symbol = True
450447
except ValueError:
451448
uniindex = ord('?')
@@ -537,23 +534,20 @@ def __init__(self, *args, **kwargs):
537534
self.fontmap[key] = fullpath
538535
self.fontmap[name] = fullpath
539536

540-
def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
537+
def _get_glyph(self, fontname, font_class, sym, fontsize):
541538
# Override prime symbol to use Bakoma.
542539
if sym == r'\prime':
543-
return self.bakoma._get_glyph(
544-
fontname, font_class, sym, fontsize, math)
540+
return self.bakoma._get_glyph(fontname, font_class, sym, fontsize)
545541
else:
546542
# check whether the glyph is available in the display font
547543
uniindex = get_unicode_index(sym)
548544
font = self._get_font('ex')
549545
if font is not None:
550546
glyphindex = font.get_char_index(uniindex)
551547
if glyphindex != 0:
552-
return super()._get_glyph(
553-
'ex', font_class, sym, fontsize, math)
548+
return super()._get_glyph('ex', font_class, sym, fontsize)
554549
# otherwise return regular glyph
555-
return super()._get_glyph(
556-
fontname, font_class, sym, fontsize, math)
550+
return super()._get_glyph(fontname, font_class, sym, fontsize)
557551

558552

559553
class DejaVuSerifFonts(DejaVuFonts):
@@ -914,15 +908,14 @@ class Char(Node):
914908
`Hlist`.
915909
"""
916910

917-
def __init__(self, c, state, math=True):
911+
def __init__(self, c, state):
918912
super().__init__()
919913
self.c = c
920914
self.font_output = state.font_output
921915
self.font = state.font
922916
self.font_class = state.font_class
923917
self.fontsize = state.fontsize
924918
self.dpi = state.dpi
925-
self.math = math
926919
# The real width, height and depth will be set during the
927920
# pack phase, after we know the real fontsize
928921
self._update_metrics()
@@ -932,8 +925,7 @@ def __repr__(self):
932925

933926
def _update_metrics(self):
934927
metrics = self._metrics = self.font_output.get_metrics(
935-
self.font, self.font_class, self.c, self.fontsize, self.dpi,
936-
self.math)
928+
self.font, self.font_class, self.c, self.fontsize, self.dpi)
937929
if self.c == ' ':
938930
self.width = metrics.advance
939931
else:
@@ -1626,7 +1618,7 @@ class _MathStyle(enum.Enum):
16261618
SCRIPTSCRIPTSTYLE = enum.auto()
16271619

16281620
_binary_operators = set('''
1629-
+ * -
1621+
+ * - \N{MINUS SIGN}
16301622
\\pm \\sqcap \\rhd
16311623
\\mp \\sqcup \\unlhd
16321624
\\times \\vee \\unrhd
@@ -1994,7 +1986,7 @@ def math(self, s, loc, toks):
19941986

19951987
def non_math(self, s, loc, toks):
19961988
s = toks[0].replace(r'\$', '$')
1997-
symbols = [Char(c, self.get_state(), math=False) for c in s]
1989+
symbols = [Char(c, self.get_state()) for c in s]
19981990
hlist = Hlist(symbols)
19991991
# We're going into math now, so set font to 'it'
20001992
self.push_state()
@@ -2040,6 +2032,13 @@ def customspace(self, s, loc, toks):
20402032

20412033
def symbol(self, s, loc, toks):
20422034
c, = toks
2035+
if c == "-":
2036+
# "U+2212 minus sign is the preferred representation of the unary
2037+
# and binary minus sign rather than the ASCII-derived U+002D
2038+
# hyphen-minus, because minus sign is unambiguous and because it
2039+
# is rendered with a more desirable length, usually longer than a
2040+
# hyphen." (https://www.unicode.org/reports/tr25/)
2041+
c = "\N{MINUS SIGN}"
20432042
try:
20442043
char = Char(c, self.get_state())
20452044
except ValueError as err:

lib/matplotlib/_mathtext_data.py

Lines changed: 1 addition & 1 deletion
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),

0 commit comments

Comments
 (0)