Skip to content

Commit 98007a5

Browse files
Text antialiasing for mathtext (matplotlib#26376)
1 parent b5149fd commit 98007a5

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
for x1, y1, x2, y2 in self.rects]
107107
return VectorParse(w, h + d, d, gs, rs)
108108

109-
def to_raster(self):
109+
def to_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.xmin for ox, oy, info in self.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
def draw_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
import functools
1919
import logging
2020

21+
import matplotlib as mpl
2122
from matplotlib import _api, _mathtext
2223
from matplotlib.ft2font import LOAD_NO_HINTING
2324
from matplotlib.font_manager import FontProperties
@@ -58,7 +59,7 @@ def __init__(self, output):
5859
{"path": "vector", "agg": "raster", "macosx": "raster"},
5960
output=output.lower())
6061

61-
def parse(self, s, dpi=72, prop=None):
62+
def parse(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() if prop is not None else None
77-
return self._parse_cached(s, dpi, prop)
78+
if antialiased is None:
79+
antialiased = mpl.rcParams['text.antialiased']
80+
return self._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
from matplotlib.backends import backend_agg
8285

8386
if prop is None:
@@ -100,7 +103,7 @@ def _parse_cached(self, s, dpi, prop):
100103
if self._output_type == "vector":
101104
return output.to_vector()
102105
elif self._output_type == "raster":
103-
return output.to_raster()
106+
return output.to_raster(antialiased=antialiased)
104107

105108

106109
def math_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
class MathTextParser:
1515
def __init__(self, output: Literal["path", "agg", "raster", "macosx"]) -> None: ...
16-
def parse(self, s: str, dpi: float = ..., prop: FontProperties | None = ...): ...
16+
def parse(
17+
self,
18+
s: str,
19+
dpi: float = ...,
20+
prop: FontProperties | None = ...,
21+
*,
22+
antialiased: bool | None = ...
23+
): ...
1724

1825
def math_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+
def test_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+
def test_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)