diff --git a/lib/matplotlib/_mathtext.py b/lib/matplotlib/_mathtext.py index a2002c971296..324ba2af7a5f 100644 --- a/lib/matplotlib/_mathtext.py +++ b/lib/matplotlib/_mathtext.py @@ -106,7 +106,7 @@ def to_vector(self): for x1, y1, x2, y2 in self.rects] return VectorParse(w, h + d, d, gs, rs) - def to_raster(self, antialiased=None): + def to_raster(self, *, antialiased): # Metrics y's and mathtext y's are oriented in opposite directions, # hence the switch between ymin and ymax. xmin = min([*[ox + info.metrics.xmin for ox, oy, info in self.glyphs], @@ -128,7 +128,6 @@ def to_raster(self, antialiased=None): # old approach and keeps baseline images backcompat. shifted = ship(self.box, (-xmin, -ymin)) - antialiased = mpl.rcParams['text.antialiased'] for ox, oy, info in shifted.glyphs: info.font.draw_glyph_to_bitmap( image, ox, oy - info.metrics.iceberg, info.glyph, diff --git a/lib/matplotlib/mathtext.py b/lib/matplotlib/mathtext.py index 7f639d2c0987..e25b76c4037c 100644 --- a/lib/matplotlib/mathtext.py +++ b/lib/matplotlib/mathtext.py @@ -75,8 +75,7 @@ def parse(self, s, dpi=72, prop=None, *, antialiased=None): # is mutable; key the cache using an internal copy (see # text._get_text_metrics_with_cache for a similar case). prop = prop.copy() if prop is not None else None - if antialiased is None: - antialiased = mpl.rcParams['text.antialiased'] + antialiased = mpl._val_or_rc(antialiased, 'text.antialiased') return self._parse_cached(s, dpi, prop, antialiased) @functools.lru_cache(50) diff --git a/lib/matplotlib/tests/baseline_images/test_text/antialiased.png b/lib/matplotlib/tests/baseline_images/test_text/antialiased.png index 11ba1b082df9..06c007591d96 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_text/antialiased.png and b/lib/matplotlib/tests/baseline_images/test_text/antialiased.png differ diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index 45bc2d2cc24d..7222d5b00cf7 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -186,19 +186,23 @@ def draw_box(ax, tt): ax.text(1.2, 0.1, 'Bot align, rot20', color='C2') -@image_comparison(['antialiased.png']) +@image_comparison(['antialiased.png'], style='mpl20') def test_antialiasing(): - mpl.rcParams['text.antialiased'] = True + mpl.rcParams['text.antialiased'] = False # Passed arguments should override. fig = plt.figure(figsize=(5.25, 0.75)) - fig.text(0.5, 0.75, "antialiased", horizontalalignment='center', - verticalalignment='center') - fig.text(0.5, 0.25, r"$\sqrt{x}$", horizontalalignment='center', - verticalalignment='center') - # NOTE: We don't need to restore the rcParams here, because the - # test cleanup will do it for us. In fact, if we do it here, it - # will turn antialiasing back off before the images are actually - # rendered. + fig.text(0.3, 0.75, "antialiased", horizontalalignment='center', + verticalalignment='center', antialiased=True) + fig.text(0.3, 0.25, r"$\sqrt{x}$", horizontalalignment='center', + verticalalignment='center', antialiased=True) + + mpl.rcParams['text.antialiased'] = True # Passed arguments should override. + fig.text(0.7, 0.75, "not antialiased", horizontalalignment='center', + verticalalignment='center', antialiased=False) + fig.text(0.7, 0.25, r"$\sqrt{x}$", horizontalalignment='center', + verticalalignment='center', antialiased=False) + + mpl.rcParams['text.antialiased'] = False # Should not affect existing text. def test_afm_kerning(): @@ -914,29 +918,18 @@ def test_annotate_offset_fontsize(): assert str(points_coords) == str(fontsize_coords) -def test_set_antialiased(): +def test_get_set_antialiased(): txt = Text(.5, .5, "foo\nbar") assert txt._antialiased == mpl.rcParams['text.antialiased'] + assert txt.get_antialiased() == mpl.rcParams['text.antialiased'] txt.set_antialiased(True) assert txt._antialiased is True + assert txt.get_antialiased() == txt._antialiased txt.set_antialiased(False) assert txt._antialiased is False - - -def test_get_antialiased(): - - txt2 = Text(.5, .5, "foo\nbar", antialiased=True) - assert txt2._antialiased is True - assert txt2.get_antialiased() == txt2._antialiased - - txt3 = Text(.5, .5, "foo\nbar", antialiased=False) - assert txt3._antialiased is False - assert txt3.get_antialiased() == txt3._antialiased - - txt4 = Text(.5, .5, "foo\nbar") - assert txt4.get_antialiased() == mpl.rcParams['text.antialiased'] + assert txt.get_antialiased() == txt._antialiased def test_annotation_antialiased(): @@ -957,39 +950,6 @@ def test_annotation_antialiased(): assert annot4._antialiased == mpl.rcParams['text.antialiased'] -@check_figures_equal() -def test_text_antialiased_off_default_vs_manual(fig_test, fig_ref): - fig_test.text(0.5, 0.5, '6 inches x 2 inches', - antialiased=False) - - mpl.rcParams['text.antialiased'] = False - fig_ref.text(0.5, 0.5, '6 inches x 2 inches') - - -@check_figures_equal() -def test_text_antialiased_on_default_vs_manual(fig_test, fig_ref): - fig_test.text(0.5, 0.5, '6 inches x 2 inches', antialiased=True) - - mpl.rcParams['text.antialiased'] = True - fig_ref.text(0.5, 0.5, '6 inches x 2 inches') - - -@check_figures_equal() -def test_text_math_antialiased_on_default_vs_manual(fig_test, fig_ref): - fig_test.text(0.5, 0.5, r"OutsideMath $I\'m \sqrt{2}$", antialiased=True) - - mpl.rcParams['text.antialiased'] = True - fig_ref.text(0.5, 0.5, r"OutsideMath $I\'m \sqrt{2}$") - - -@check_figures_equal() -def test_text_math_antialiased_off_default_vs_manual(fig_test, fig_ref): - fig_test.text(0.5, 0.5, r"OutsideMath $I\'m \sqrt{2}$", antialiased=False) - - mpl.rcParams['text.antialiased'] = False - fig_ref.text(0.5, 0.5, r"OutsideMath $I\'m \sqrt{2}$") - - @check_figures_equal(extensions=["png"]) def test_annotate_and_offsetfrom_copy_input(fig_test, fig_ref): # Both approaches place the text (10, 0) pixels away from the center of the line.