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

Commit98007a5

Browse files
Text antialiasing for mathtext (#26376)
1 parentb5149fd commit98007a5

File tree

6 files changed

+35
-9
lines changed

6 files changed

+35
-9
lines changed

‎doc/users/next_whats_new/antialiasing_text_annotation.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ Examples:
1212
plt.text(0.5, 0.5, '6 inches x 2 inches', antialiased=True)
1313
ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5), antialiased=False)
1414
15-
If the text contains math expression, then antialiasing will be set by:rc:`text.antialiased`, and *antialiased* will have no effect
16-
This applies to the whole text.
15+
If the text contains math expression, *antialiased* applies to the whole text.
1716
Examples:
1817

1918
..code-block::

‎lib/matplotlib/_mathtext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def to_vector(self):
106106
forx1,y1,x2,y2inself.rects]
107107
returnVectorParse(w,h+d,d,gs,rs)
108108

109-
defto_raster(self):
109+
defto_raster(self,antialiased=None):
110110
# Metrics y's and mathtext y's are oriented in opposite directions,
111111
# hence the switch between ymin and ymax.
112112
xmin=min([*[ox+info.metrics.xminforox,oy,infoinself.glyphs],

‎lib/matplotlib/backends/backend_agg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ def draw_path(self, gc, path, transform, rgbFace=None):
175175
defdraw_mathtext(self,gc,x,y,s,prop,angle):
176176
"""Draw mathtext using :mod:`matplotlib.mathtext`."""
177177
ox,oy,width,height,descent,font_image= \
178-
self.mathtext_parser.parse(s,self.dpi,prop)
178+
self.mathtext_parser.parse(s,self.dpi,prop,
179+
antialiased=gc.get_antialiased())
179180

180181
xd=descent*sin(radians(angle))
181182
yd=descent*cos(radians(angle))

‎lib/matplotlib/mathtext.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
importfunctools
1919
importlogging
2020

21+
importmatplotlibasmpl
2122
frommatplotlibimport_api,_mathtext
2223
frommatplotlib.ft2fontimportLOAD_NO_HINTING
2324
frommatplotlib.font_managerimportFontProperties
@@ -58,7 +59,7 @@ def __init__(self, output):
5859
{"path":"vector","agg":"raster","macosx":"raster"},
5960
output=output.lower())
6061

61-
defparse(self,s,dpi=72,prop=None):
62+
defparse(self,s,dpi=72,prop=None,*,antialiased=None):
6263
"""
6364
Parse the given math expression *s* at the given *dpi*. If *prop* is
6465
provided, it is a `.FontProperties` object specifying the "default"
@@ -74,10 +75,12 @@ def parse(self, s, dpi=72, prop=None):
7475
# is mutable; key the cache using an internal copy (see
7576
# text._get_text_metrics_with_cache for a similar case).
7677
prop=prop.copy()ifpropisnotNoneelseNone
77-
returnself._parse_cached(s,dpi,prop)
78+
ifantialiasedisNone:
79+
antialiased=mpl.rcParams['text.antialiased']
80+
returnself._parse_cached(s,dpi,prop,antialiased)
7881

7982
@functools.lru_cache(50)
80-
def_parse_cached(self,s,dpi,prop):
83+
def_parse_cached(self,s,dpi,prop,antialiased):
8184
frommatplotlib.backendsimportbackend_agg
8285

8386
ifpropisNone:
@@ -100,7 +103,7 @@ def _parse_cached(self, s, dpi, prop):
100103
ifself._output_type=="vector":
101104
returnoutput.to_vector()
102105
elifself._output_type=="raster":
103-
returnoutput.to_raster()
106+
returnoutput.to_raster(antialiased=antialiased)
104107

105108

106109
defmath_to_image(s,filename_or_obj,prop=None,dpi=None,format=None,

‎lib/matplotlib/mathtext.pyi

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ from matplotlib.typing import ColorType
1313

1414
classMathTextParser:
1515
def__init__(self,output:Literal["path","agg","raster","macosx"])->None: ...
16-
defparse(self,s:str,dpi:float= ...,prop:FontProperties|None= ...): ...
16+
defparse(
17+
self,
18+
s:str,
19+
dpi:float= ...,
20+
prop:FontProperties|None= ...,
21+
*,
22+
antialiased:bool|None= ...
23+
): ...
1724

1825
defmath_to_image(
1926
s:str,

‎lib/matplotlib/tests/test_text.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,3 +972,19 @@ def test_text_antialiased_on_default_vs_manual(fig_test, fig_ref):
972972

973973
mpl.rcParams['text.antialiased']=True
974974
fig_ref.text(0.5,0.5,'6 inches x 2 inches')
975+
976+
977+
@check_figures_equal()
978+
deftest_text_math_antialiased_on_default_vs_manual(fig_test,fig_ref):
979+
fig_test.text(0.5,0.5,r"OutsideMath $I\'m \sqrt{2}$",antialiased=True)
980+
981+
mpl.rcParams['text.antialiased']=True
982+
fig_ref.text(0.5,0.5,r"OutsideMath $I\'m \sqrt{2}$")
983+
984+
985+
@check_figures_equal()
986+
deftest_text_math_antialiased_off_default_vs_manual(fig_test,fig_ref):
987+
fig_test.text(0.5,0.5,r"OutsideMath $I\'m \sqrt{2}$",antialiased=False)
988+
989+
mpl.rcParams['text.antialiased']=False
990+
fig_ref.text(0.5,0.5,r"OutsideMath $I\'m \sqrt{2}$")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp