18
18
ParseResults , QuotedString , Regex , StringEnd , Suppress , White , ZeroOrMore )
19
19
20
20
import matplotlib as mpl
21
- from . import cbook
21
+ from . import _api , cbook
22
22
from ._mathtext_data import (
23
23
latex_to_bakoma , stix_glyph_fixes , stix_virtual_fonts , tex2uni )
24
24
from .font_manager import FontProperties , findfont , get_font
33
33
# FONTS
34
34
35
35
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.
37
38
r"""
38
39
Return the integer index (from the Unicode table) of *symbol*.
39
40
@@ -45,15 +46,13 @@ def get_unicode_index(symbol, math=True):
45
46
math : bool, default: True
46
47
If False, always treat as a single Unicode character.
47
48
"""
48
- # for a non-math symbol, simply return its Unicode index
49
- if not math :
50
- return ord (symbol )
51
49
# From UTF #25: U+2212 minus sign is the preferred
52
50
# representation of the unary and binary minus sign rather than
53
51
# the ASCII-derived U+002D hyphen-minus, because minus sign is
54
52
# unambiguous and because it is rendered with a more desirable
55
53
# length, usually longer than a hyphen.
56
- if symbol == '-' :
54
+ # Remove this block when the 'math' parameter is deleted.
55
+ if math and symbol == '-' :
57
56
return 0x2212
58
57
try : # This will succeed if symbol is a single Unicode char
59
58
return ord (symbol )
@@ -98,7 +97,7 @@ def get_kern(self, font1, fontclass1, sym1, fontsize1,
98
97
"""
99
98
return 0.
100
99
101
- def get_metrics (self , font , font_class , sym , fontsize , dpi , math = True ):
100
+ def get_metrics (self , font , font_class , sym , fontsize , dpi ):
102
101
r"""
103
102
Parameters
104
103
----------
@@ -117,8 +116,6 @@ def get_metrics(self, font, font_class, sym, fontsize, dpi, math=True):
117
116
Font size in points.
118
117
dpi : float
119
118
Rendering dots-per-inch.
120
- math : bool
121
- Whether we are currently in math mode or not.
122
119
123
120
Returns
124
121
-------
@@ -136,7 +133,7 @@ def get_metrics(self, font, font_class, sym, fontsize, dpi, math=True):
136
133
- *slanted*: Whether the glyph should be considered as "slanted"
137
134
(currently used for kerning sub/superscripts).
138
135
"""
139
- info = self ._get_info (font , font_class , sym , fontsize , dpi , math )
136
+ info = self ._get_info (font , font_class , sym , fontsize , dpi )
140
137
return info .metrics
141
138
142
139
def render_glyph (self , ox , oy , font , font_class , sym , fontsize , dpi ):
@@ -217,14 +214,14 @@ def _get_offset(self, font, glyph, fontsize, dpi):
217
214
return (glyph .height / 64 / 2 ) + (fontsize / 3 * dpi / 72 )
218
215
return 0.
219
216
220
- def _get_info (self , fontname , font_class , sym , fontsize , dpi , math = True ):
217
+ def _get_info (self , fontname , font_class , sym , fontsize , dpi ):
221
218
key = fontname , font_class , sym , fontsize , dpi
222
219
bunch = self .glyphd .get (key )
223
220
if bunch is not None :
224
221
return bunch
225
222
226
223
font , num , fontsize , slanted = self ._get_glyph (
227
- fontname , font_class , sym , fontsize , math )
224
+ fontname , font_class , sym , fontsize )
228
225
229
226
font .set_size (fontsize , dpi )
230
227
glyph = font .load_char (
@@ -315,7 +312,7 @@ def __init__(self, *args, **kwargs):
315
312
316
313
_slanted_symbols = set (r"\int \oint" .split ())
317
314
318
- def _get_glyph (self , fontname , font_class , sym , fontsize , math = True ):
315
+ def _get_glyph (self , fontname , font_class , sym , fontsize ):
319
316
font = None
320
317
if fontname in self .fontmap and sym in latex_to_bakoma :
321
318
basename , num = latex_to_bakoma [sym ]
@@ -330,7 +327,7 @@ def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
330
327
return font , num , fontsize , slanted
331
328
else :
332
329
return self ._stix_fallback ._get_glyph (
333
- fontname , font_class , sym , fontsize , math )
330
+ fontname , font_class , sym , fontsize )
334
331
335
332
# The Bakoma fonts contain many pre-sized alternatives for the
336
333
# delimiters. The AutoSizedChar class will use these alternatives
@@ -443,9 +440,9 @@ def __init__(self, *args, **kwargs):
443
440
def _map_virtual_font (self , fontname , font_class , uniindex ):
444
441
return fontname , uniindex
445
442
446
- def _get_glyph (self , fontname , font_class , sym , fontsize , math = True ):
443
+ def _get_glyph (self , fontname , font_class , sym , fontsize ):
447
444
try :
448
- uniindex = get_unicode_index (sym , math )
445
+ uniindex = get_unicode_index (sym )
449
446
found_symbol = True
450
447
except ValueError :
451
448
uniindex = ord ('?' )
@@ -537,23 +534,20 @@ def __init__(self, *args, **kwargs):
537
534
self .fontmap [key ] = fullpath
538
535
self .fontmap [name ] = fullpath
539
536
540
- def _get_glyph (self , fontname , font_class , sym , fontsize , math = True ):
537
+ def _get_glyph (self , fontname , font_class , sym , fontsize ):
541
538
# Override prime symbol to use Bakoma.
542
539
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 )
545
541
else :
546
542
# check whether the glyph is available in the display font
547
543
uniindex = get_unicode_index (sym )
548
544
font = self ._get_font ('ex' )
549
545
if font is not None :
550
546
glyphindex = font .get_char_index (uniindex )
551
547
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 )
554
549
# 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 )
557
551
558
552
559
553
class DejaVuSerifFonts (DejaVuFonts ):
@@ -914,15 +908,14 @@ class Char(Node):
914
908
`Hlist`.
915
909
"""
916
910
917
- def __init__ (self , c , state , math = True ):
911
+ def __init__ (self , c , state ):
918
912
super ().__init__ ()
919
913
self .c = c
920
914
self .font_output = state .font_output
921
915
self .font = state .font
922
916
self .font_class = state .font_class
923
917
self .fontsize = state .fontsize
924
918
self .dpi = state .dpi
925
- self .math = math
926
919
# The real width, height and depth will be set during the
927
920
# pack phase, after we know the real fontsize
928
921
self ._update_metrics ()
@@ -932,8 +925,7 @@ def __repr__(self):
932
925
933
926
def _update_metrics (self ):
934
927
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 )
937
929
if self .c == ' ' :
938
930
self .width = metrics .advance
939
931
else :
@@ -1626,7 +1618,7 @@ class _MathStyle(enum.Enum):
1626
1618
SCRIPTSCRIPTSTYLE = enum .auto ()
1627
1619
1628
1620
_binary_operators = set ('''
1629
- + * -
1621
+ + * - \N{MINUS SIGN}
1630
1622
\\ pm \\ sqcap \\ rhd
1631
1623
\\ mp \\ sqcup \\ unlhd
1632
1624
\\ times \\ vee \\ unrhd
@@ -1994,7 +1986,7 @@ def math(self, s, loc, toks):
1994
1986
1995
1987
def non_math (self , s , loc , toks ):
1996
1988
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 ]
1998
1990
hlist = Hlist (symbols )
1999
1991
# We're going into math now, so set font to 'it'
2000
1992
self .push_state ()
@@ -2040,6 +2032,13 @@ def customspace(self, s, loc, toks):
2040
2032
2041
2033
def symbol (self , s , loc , toks ):
2042
2034
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} "
2043
2042
try :
2044
2043
char = Char (c , self .get_state ())
2045
2044
except ValueError as err :
0 commit comments