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

Skip to content

Remove *math* parameter of various mathtext internal APIs. #22507

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 1 commit into from
Jun 11, 2022
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
5 changes: 5 additions & 0 deletions doc/api/next_api_changes/deprecations/22507-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The *math* parameter of ``mathtext.get_unicode_index``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In math mode, ASCII hyphens (U+002D) are now replaced by unicode minus signs
(U+2212) at the parsing stage.
60 changes: 30 additions & 30 deletions lib/matplotlib/_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
QuotedString, Regex, StringEnd, ZeroOrMore, pyparsing_common)

import matplotlib as mpl
from . import cbook
from . import _api, cbook
from ._mathtext_data import (
latex_to_bakoma, stix_glyph_fixes, stix_virtual_fonts, tex2uni)
from .font_manager import FontProperties, findfont, get_font
Expand All @@ -33,7 +33,8 @@
# FONTS


def get_unicode_index(symbol, math=True):
@_api.delete_parameter("3.6", "math")
def get_unicode_index(symbol, math=True): # Publicly exported.
r"""
Return the integer index (from the Unicode table) of *symbol*.

Expand All @@ -45,15 +46,13 @@ def get_unicode_index(symbol, math=True):
math : bool, default: True
If False, always treat as a single Unicode character.
"""
# for a non-math symbol, simply return its Unicode index
if not math:
return ord(symbol)
# From UTF #25: U+2212 minus sign is the preferred
# representation of the unary and binary minus sign rather than
# the ASCII-derived U+002D hyphen-minus, because minus sign is
# unambiguous and because it is rendered with a more desirable
# length, usually longer than a hyphen.
if symbol == '-':
# Remove this block when the 'math' parameter is deleted.
if math and symbol == '-':
return 0x2212
try: # This will succeed if symbol is a single Unicode char
return ord(symbol)
Expand Down Expand Up @@ -98,7 +97,7 @@ def get_kern(self, font1, fontclass1, sym1, fontsize1,
"""
return 0.

def get_metrics(self, font, font_class, sym, fontsize, dpi, math=True):
def get_metrics(self, font, font_class, sym, fontsize, dpi):
r"""
Parameters
----------
Expand All @@ -117,8 +116,6 @@ def get_metrics(self, font, font_class, sym, fontsize, dpi, math=True):
Font size in points.
dpi : float
Rendering dots-per-inch.
math : bool
Whether we are currently in math mode or not.

Returns
-------
Expand All @@ -136,7 +133,7 @@ def get_metrics(self, font, font_class, sym, fontsize, dpi, math=True):
- *slanted*: Whether the glyph should be considered as "slanted"
(currently used for kerning sub/superscripts).
"""
info = self._get_info(font, font_class, sym, fontsize, dpi, math)
info = self._get_info(font, font_class, sym, fontsize, dpi)
return info.metrics

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

def _get_info(self, fontname, font_class, sym, fontsize, dpi, math=True):
def _get_info(self, fontname, font_class, sym, fontsize, dpi):
key = fontname, font_class, sym, fontsize, dpi
bunch = self.glyphd.get(key)
if bunch is not None:
return bunch

font, num, slanted = self._get_glyph(
fontname, font_class, sym, fontsize, math)
fontname, font_class, sym, fontsize)

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

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

def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
def _get_glyph(self, fontname, font_class, sym, fontsize):
font = None
if fontname in self.fontmap and sym in latex_to_bakoma:
basename, num = latex_to_bakoma[sym]
Expand All @@ -329,7 +326,7 @@ def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
return font, num, slanted
else:
return self._stix_fallback._get_glyph(
fontname, font_class, sym, fontsize, math)
fontname, font_class, sym, fontsize)

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

def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
def _get_glyph(self, fontname, font_class, sym, fontsize):
try:
uniindex = get_unicode_index(sym, math)
uniindex = get_unicode_index(sym)
found_symbol = True
except ValueError:
uniindex = ord('?')
Expand Down Expand Up @@ -536,23 +533,20 @@ def __init__(self, *args, **kwargs):
self.fontmap[key] = fullpath
self.fontmap[name] = fullpath

def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
def _get_glyph(self, fontname, font_class, sym, fontsize):
# Override prime symbol to use Bakoma.
if sym == r'\prime':
return self.bakoma._get_glyph(
fontname, font_class, sym, fontsize, math)
return self.bakoma._get_glyph(fontname, font_class, sym, fontsize)
else:
# check whether the glyph is available in the display font
uniindex = get_unicode_index(sym)
font = self._get_font('ex')
if font is not None:
glyphindex = font.get_char_index(uniindex)
if glyphindex != 0:
return super()._get_glyph(
'ex', font_class, sym, fontsize, math)
return super()._get_glyph('ex', font_class, sym, fontsize)
# otherwise return regular glyph
return super()._get_glyph(
fontname, font_class, sym, fontsize, math)
return super()._get_glyph(fontname, font_class, sym, fontsize)


class DejaVuSerifFonts(DejaVuFonts):
Expand Down Expand Up @@ -913,15 +907,14 @@ class Char(Node):
`Hlist`.
"""

def __init__(self, c, state, math=True):
def __init__(self, c, state):
super().__init__()
self.c = c
self.font_output = state.font_output
self.font = state.font
self.font_class = state.font_class
self.fontsize = state.fontsize
self.dpi = state.dpi
self.math = math
# The real width, height and depth will be set during the
# pack phase, after we know the real fontsize
self._update_metrics()
Expand All @@ -931,8 +924,7 @@ def __repr__(self):

def _update_metrics(self):
metrics = self._metrics = self.font_output.get_metrics(
self.font, self.font_class, self.c, self.fontsize, self.dpi,
self.math)
self.font, self.font_class, self.c, self.fontsize, self.dpi)
if self.c == ' ':
self.width = metrics.advance
else:
Expand Down Expand Up @@ -1624,8 +1616,9 @@ class _MathStyle(enum.Enum):
SCRIPTSTYLE = enum.auto()
SCRIPTSCRIPTSTYLE = enum.auto()

_binary_operators = set(r'''
+ * -
_binary_operators = set(
'+ * - \N{MINUS SIGN}'
r'''
\pm \sqcap \rhd
\mp \sqcup \unlhd
\times \vee \unrhd
Expand Down Expand Up @@ -1922,7 +1915,7 @@ def math(self, s, loc, toks):

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

def symbol(self, s, loc, toks):
c = toks["sym"]
if c == "-":
# "U+2212 minus sign is the preferred representation of the unary
# and binary minus sign rather than the ASCII-derived U+002D
# hyphen-minus, because minus sign is unambiguous and because it
# is rendered with a more desirable length, usually longer than a
# hyphen." (https://www.unicode.org/reports/tr25/)
c = "\N{MINUS SIGN}"
try:
char = Char(c, self.get_state())
except ValueError as err:
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/_mathtext_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
']' : ('cmr10', 0x5d),

'*' : ('cmsy10', 0xa4),
'-' : ('cmsy10', 0xa1),
'\N{MINUS SIGN}' : ('cmsy10', 0xa1),
'\\Downarrow' : ('cmsy10', 0x2b),
'\\Im' : ('cmsy10', 0x3d),
'\\Leftarrow' : ('cmsy10', 0x28),
Expand Down
Binary file not shown.
Binary file not shown.
Loading