Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f11c92c

Browse files
antialias for agg
1 parent fc350ea commit f11c92c

File tree

5 files changed

+25
-8
lines changed

5 files changed

+25
-8
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def to_vector(self):
9999
for x1, y1, x2, y2 in self.rects]
100100
return VectorParse(w, h + d, d, gs, rs)
101101

102-
def to_raster(self):
102+
def to_raster(self, antialiased=None):
103103
# Metrics y's and mathtext y's are oriented in opposite directions,
104104
# hence the switch between ymin and ymax.
105105
xmin = min([*[ox + info.metrics.xmin for ox, oy, info in self.glyphs],
@@ -124,7 +124,7 @@ def to_raster(self):
124124
for ox, oy, info in shifted.glyphs:
125125
info.font.draw_glyph_to_bitmap(
126126
image, ox, oy - info.metrics.iceberg, info.glyph,
127-
antialiased=mpl.rcParams['text.antialiased'])
127+
antialiased=antialiased)
128128
for x1, y1, x2, y2 in shifted.rects:
129129
height = max(int(y2 - y1) - 1, 0)
130130
if height == 0:

lib/matplotlib/backends/backend_agg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def draw_path(self, gc, path, transform, rgbFace=None):
189189
def draw_mathtext(self, gc, x, y, s, prop, angle):
190190
"""Draw mathtext using :mod:`matplotlib.mathtext`."""
191191
ox, oy, width, height, descent, font_image = \
192-
self.mathtext_parser.parse(s, self.dpi, prop)
192+
self.mathtext_parser.parse(s, self.dpi, prop, gc.get_antialiased())
193193

194194
xd = descent * sin(radians(angle))
195195
yd = descent * cos(radians(angle))

lib/matplotlib/mathtext.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(self, output):
5858
{"path": "vector", "agg": "raster", "macosx": "raster"},
5959
output=output.lower())
6060

61-
def parse(self, s, dpi=72, prop=None):
61+
def parse(self, s, dpi=72, prop=None, antialiased=None):
6262
"""
6363
Parse the given math expression *s* at the given *dpi*. If *prop* is
6464
provided, it is a `.FontProperties` object specifying the "default"
@@ -74,10 +74,10 @@ def parse(self, s, dpi=72, prop=None):
7474
# is mutable; key the cache using an internal copy (see
7575
# text._get_text_metrics_with_cache for a similar case).
7676
prop = prop.copy() if prop is not None else None
77-
return self._parse_cached(s, dpi, prop)
77+
return self._parse_cached(s, dpi, prop, antialiased)
7878

7979
@functools.lru_cache(50)
80-
def _parse_cached(self, s, dpi, prop):
80+
def _parse_cached(self, s, dpi, prop, antialiased):
8181
from matplotlib.backends import backend_agg
8282

8383
if prop is None:
@@ -100,7 +100,7 @@ def _parse_cached(self, s, dpi, prop):
100100
if self._output_type == "vector":
101101
return output.to_vector()
102102
elif self._output_type == "raster":
103-
return output.to_raster()
103+
return output.to_raster(antialiased=antialiased)
104104

105105

106106
def math_to_image(s, filename_or_obj, prop=None, dpi=None, format=None,

lib/matplotlib/mathtext.pyi

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

1414
class MathTextParser:
1515
def __init__(self, output: Literal["path", "raster", "macosx"]) -> None: ...
16-
def parse(self, s: str, dpi: float = ..., prop: FontProperties | None = ...): ...
16+
def parse(self, s: str, dpi: float = ..., prop: FontProperties | None = ...,
17+
antialiased: bool | None = ...): ...
1718

1819
def math_to_image(
1920
s: str,

lib/matplotlib/tests/test_text.py

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

963963
mpl.rcParams['text.antialiased'] = True
964964
fig_ref.text(0.5, 0.5, '6 inches x 2 inches')
965+
966+
967+
@check_figures_equal()
968+
def test_text_math_antialiased_on_default_vs_manual(fig_test, fig_ref):
969+
fig_test.text(0.5, 0.5, r"OutsideMath $I\'m \sqrt{2}$", antialiased=True)
970+
971+
mpl.rcParams['text.antialiased'] = True
972+
fig_ref.text(0.5, 0.5, r"OutsideMath $I\'m \sqrt{2}$")
973+
974+
975+
@check_figures_equal()
976+
def test_text_math_antialiased_off_default_vs_manual(fig_test, fig_ref):
977+
fig_test.text(0.5, 0.5, r"OutsideMath $I\'m \sqrt{2}$", antialiased=False)
978+
979+
mpl.rcParams['text.antialiased'] = False
980+
fig_ref.text(0.5, 0.5, r"OutsideMath $I\'m \sqrt{2}$")

0 commit comments

Comments
 (0)