18
18
QuotedString , Regex , StringEnd , ZeroOrMore , pyparsing_common )
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 ): # Publicly 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 , 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 (
@@ -314,7 +311,7 @@ def __init__(self, *args, **kwargs):
314
311
315
312
_slanted_symbols = set (r"\int \oint" .split ())
316
313
317
- def _get_glyph (self , fontname , font_class , sym , fontsize , math = True ):
314
+ def _get_glyph (self , fontname , font_class , sym , fontsize ):
318
315
font = None
319
316
if fontname in self .fontmap and sym in latex_to_bakoma :
320
317
basename , num = latex_to_bakoma [sym ]
@@ -329,7 +326,7 @@ def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
329
326
return font , num , slanted
330
327
else :
331
328
return self ._stix_fallback ._get_glyph (
332
- fontname , font_class , sym , fontsize , math )
329
+ fontname , font_class , sym , fontsize )
333
330
334
331
# The Bakoma fonts contain many pre-sized alternatives for the
335
332
# delimiters. The AutoSizedChar class will use these alternatives
@@ -442,9 +439,9 @@ def __init__(self, *args, **kwargs):
442
439
def _map_virtual_font (self , fontname , font_class , uniindex ):
443
440
return fontname , uniindex
444
441
445
- def _get_glyph (self , fontname , font_class , sym , fontsize , math = True ):
442
+ def _get_glyph (self , fontname , font_class , sym , fontsize ):
446
443
try :
447
- uniindex = get_unicode_index (sym , math )
444
+ uniindex = get_unicode_index (sym )
448
445
found_symbol = True
449
446
except ValueError :
450
447
uniindex = ord ('?' )
@@ -536,23 +533,20 @@ def __init__(self, *args, **kwargs):
536
533
self .fontmap [key ] = fullpath
537
534
self .fontmap [name ] = fullpath
538
535
539
- def _get_glyph (self , fontname , font_class , sym , fontsize , math = True ):
536
+ def _get_glyph (self , fontname , font_class , sym , fontsize ):
540
537
# Override prime symbol to use Bakoma.
541
538
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 )
544
540
else :
545
541
# check whether the glyph is available in the display font
546
542
uniindex = get_unicode_index (sym )
547
543
font = self ._get_font ('ex' )
548
544
if font is not None :
549
545
glyphindex = font .get_char_index (uniindex )
550
546
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 )
553
548
# 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 )
556
550
557
551
558
552
class DejaVuSerifFonts (DejaVuFonts ):
@@ -913,15 +907,14 @@ class Char(Node):
913
907
`Hlist`.
914
908
"""
915
909
916
- def __init__ (self , c , state , math = True ):
910
+ def __init__ (self , c , state ):
917
911
super ().__init__ ()
918
912
self .c = c
919
913
self .font_output = state .font_output
920
914
self .font = state .font
921
915
self .font_class = state .font_class
922
916
self .fontsize = state .fontsize
923
917
self .dpi = state .dpi
924
- self .math = math
925
918
# The real width, height and depth will be set during the
926
919
# pack phase, after we know the real fontsize
927
920
self ._update_metrics ()
@@ -931,8 +924,7 @@ def __repr__(self):
931
924
932
925
def _update_metrics (self ):
933
926
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 )
936
928
if self .c == ' ' :
937
929
self .width = metrics .advance
938
930
else :
@@ -1624,8 +1616,9 @@ class _MathStyle(enum.Enum):
1624
1616
SCRIPTSTYLE = enum .auto ()
1625
1617
SCRIPTSCRIPTSTYLE = enum .auto ()
1626
1618
1627
- _binary_operators = set (r'''
1628
- + * -
1619
+ _binary_operators = set (
1620
+ '+ * - \N{MINUS SIGN} '
1621
+ r'''
1629
1622
\pm \sqcap \rhd
1630
1623
\mp \sqcup \unlhd
1631
1624
\times \vee \unrhd
@@ -1922,7 +1915,7 @@ def math(self, s, loc, toks):
1922
1915
1923
1916
def non_math (self , s , loc , toks ):
1924
1917
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 ]
1926
1919
hlist = Hlist (symbols )
1927
1920
# We're going into math now, so set font to 'it'
1928
1921
self .push_state ()
@@ -1969,6 +1962,13 @@ def customspace(self, s, loc, toks):
1969
1962
1970
1963
def symbol (self , s , loc , toks ):
1971
1964
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} "
1972
1972
try :
1973
1973
char = Char (c , self .get_state ())
1974
1974
except ValueError as err :
0 commit comments