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

Commit85f30cb

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. In particular,this fixes problematic unicode minus support with the "cm" fontset.This patch also reverts a significant part of52003e4, as LogFormattersno longer need to pass unicode minuses in mathtext -- everything getsconverted by mathtext. Likewise, this change also invalidates thetest_log_scales baseline image (old, buggy wrt. unicode minus); replaceit by a test that the drawn ticks are as expected (which was the intentin90c1aa3).
1 parentf278686 commit85f30cb

File tree

10 files changed

+84
-719
lines changed

10 files changed

+84
-719
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: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
QuotedString,Regex,StringEnd,ZeroOrMore,pyparsing_common)
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):# Publicly 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,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(
@@ -314,7 +311,7 @@ def __init__(self, *args, **kwargs):
314311

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

317-
def_get_glyph(self,fontname,font_class,sym,fontsize,math=True):
314+
def_get_glyph(self,fontname,font_class,sym,fontsize):
318315
font=None
319316
iffontnameinself.fontmapandsyminlatex_to_bakoma:
320317
basename,num=latex_to_bakoma[sym]
@@ -329,7 +326,7 @@ def _get_glyph(self, fontname, font_class, sym, fontsize, math=True):
329326
returnfont,num,slanted
330327
else:
331328
returnself._stix_fallback._get_glyph(
332-
fontname,font_class,sym,fontsize,math)
329+
fontname,font_class,sym,fontsize)
333330

334331
# The Bakoma fonts contain many pre-sized alternatives for the
335332
# delimiters. The AutoSizedChar class will use these alternatives
@@ -442,9 +439,9 @@ def __init__(self, *args, **kwargs):
442439
def_map_virtual_font(self,fontname,font_class,uniindex):
443440
returnfontname,uniindex
444441

445-
def_get_glyph(self,fontname,font_class,sym,fontsize,math=True):
442+
def_get_glyph(self,fontname,font_class,sym,fontsize):
446443
try:
447-
uniindex=get_unicode_index(sym,math)
444+
uniindex=get_unicode_index(sym)
448445
found_symbol=True
449446
exceptValueError:
450447
uniindex=ord('?')
@@ -536,23 +533,20 @@ def __init__(self, *args, **kwargs):
536533
self.fontmap[key]=fullpath
537534
self.fontmap[name]=fullpath
538535

539-
def_get_glyph(self,fontname,font_class,sym,fontsize,math=True):
536+
def_get_glyph(self,fontname,font_class,sym,fontsize):
540537
# Override prime symbol to use Bakoma.
541538
ifsym==r'\prime':
542-
returnself.bakoma._get_glyph(
543-
fontname,font_class,sym,fontsize,math)
539+
returnself.bakoma._get_glyph(fontname,font_class,sym,fontsize)
544540
else:
545541
# check whether the glyph is available in the display font
546542
uniindex=get_unicode_index(sym)
547543
font=self._get_font('ex')
548544
iffontisnotNone:
549545
glyphindex=font.get_char_index(uniindex)
550546
ifglyphindex!=0:
551-
returnsuper()._get_glyph(
552-
'ex',font_class,sym,fontsize,math)
547+
returnsuper()._get_glyph('ex',font_class,sym,fontsize)
553548
# otherwise return regular glyph
554-
returnsuper()._get_glyph(
555-
fontname,font_class,sym,fontsize,math)
549+
returnsuper()._get_glyph(fontname,font_class,sym,fontsize)
556550

557551

558552
classDejaVuSerifFonts(DejaVuFonts):
@@ -913,15 +907,14 @@ class Char(Node):
913907
`Hlist`.
914908
"""
915909

916-
def__init__(self,c,state,math=True):
910+
def__init__(self,c,state):
917911
super().__init__()
918912
self.c=c
919913
self.font_output=state.font_output
920914
self.font=state.font
921915
self.font_class=state.font_class
922916
self.fontsize=state.fontsize
923917
self.dpi=state.dpi
924-
self.math=math
925918
# The real width, height and depth will be set during the
926919
# pack phase, after we know the real fontsize
927920
self._update_metrics()
@@ -931,8 +924,7 @@ def __repr__(self):
931924

932925
def_update_metrics(self):
933926
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)
936928
ifself.c==' ':
937929
self.width=metrics.advance
938930
else:
@@ -1624,8 +1616,9 @@ class _MathStyle(enum.Enum):
16241616
SCRIPTSTYLE=enum.auto()
16251617
SCRIPTSCRIPTSTYLE=enum.auto()
16261618

1627-
_binary_operators=set(r'''
1628-
+ * -
1619+
_binary_operators=set(
1620+
'+ * -\N{MINUS SIGN}'
1621+
r'''
16291622
\pm \sqcap \rhd
16301623
\mp \sqcup \unlhd
16311624
\times \vee \unrhd
@@ -1922,7 +1915,7 @@ def math(self, s, loc, toks):
19221915

19231916
defnon_math(self,s,loc,toks):
19241917
s=toks[0].replace(r'\$','$')
1925-
symbols= [Char(c,self.get_state(),math=False)forcins]
1918+
symbols= [Char(c,self.get_state())forcins]
19261919
hlist=Hlist(symbols)
19271920
# We're going into math now, so set font to 'it'
19281921
self.push_state()
@@ -1969,6 +1962,13 @@ def customspace(self, s, loc, toks):
19691962

19701963
defsymbol(self,s,loc,toks):
19711964
c=toks["sym"]
1965+
ifc=="-":
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}"
19721972
try:
19731973
char=Char(c,self.get_state())
19741974
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),
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp