Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita0135d5

Browse files
committed
Remove *math* parameter of various mathtext internal APIs.
The *math* parameter is passed through many layers of the call stackbut is ultimately only used for a single purpose: deciding whether toreplace the ASCII hyphen by a (longer) unicode minus. Instead of doingthat, just do the substitution at the parsing stage.
1 parent53c5f88 commita0135d5

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*math* parameter of various mathtext APIs
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
The *math* parameter of `mathtext.get_unicode_index`. (In math mode, ASCII
5+
hyphens (U+002D) are now replaced by unicode minus signs (U+2212) at the
6+
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
importmatplotlibasmpl
21-
from .importcbook
21+
from .import_api,cbook
2222
from ._mathtext_dataimport (
2323
latex_to_bakoma,stix_glyph_fixes,stix_virtual_fonts,tex2uni)
2424
from .font_managerimportFontProperties,findfont,get_font
@@ -33,7 +33,8 @@
3333
# FONTS
3434

3535

36-
defget_unicode_index(symbol,math=True):
36+
@_api.delete_parameter("3.6","math")
37+
defget_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-
ifnotmath:
50-
returnord(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-
ifsymbol=='-':
54+
# Remove this block when the 'math' parameter is deleted.
55+
ifmathandsymbol=='-':
5756
return0x2212
5857
try:# This will succeed if symbol is a single Unicode char
5958
returnord(symbol)
@@ -98,7 +97,7 @@ def get_kern(self, font1, fontclass1, sym1, fontsize1,
9897
"""
9998
return0.
10099

101-
defget_metrics(self,font,font_class,sym,fontsize,dpi,math=True):
100+
defget_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
returninfo.metrics
141138

142139
defrender_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
return0.
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
ifbunchisnotNone:
224221
returnbunch
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
iffontnameinself.fontmapandsyminlatex_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
returnfont,num,fontsize,slanted
331328
else:
332329
returnself._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
returnfontname,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
exceptValueError:
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
ifsym==r'\prime':
543-
returnself.bakoma._get_glyph(
544-
fontname,font_class,sym,fontsize,math)
540+
returnself.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
iffontisnotNone:
550546
glyphindex=font.get_char_index(uniindex)
551547
ifglyphindex!=0:
552-
returnsuper()._get_glyph(
553-
'ex',font_class,sym,fontsize,math)
548+
returnsuper()._get_glyph('ex',font_class,sym,fontsize)
554549
# otherwise return regular glyph
555-
returnsuper()._get_glyph(
556-
fontname,font_class,sym,fontsize,math)
550+
returnsuper()._get_glyph(fontname,font_class,sym,fontsize)
557551

558552

559553
classDejaVuSerifFonts(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
ifself.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
defnon_math(self,s,loc,toks):
19961988
s=toks[0].replace(r'\$','$')
1997-
symbols= [Char(c,self.get_state(),math=False)forcins]
1989+
symbols= [Char(c,self.get_state())forcins]
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
defsymbol(self,s,loc,toks):
20422034
c,=toks
2035+
ifc=="-":
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
exceptValueErroraserr:

‎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)

[8]ページ先頭

©2009-2025 Movatter.jp