Skip to content

Commit

Permalink
Backport PR matplotlib#26529: Fix MathText antialiasing
Browse files Browse the repository at this point in the history
  • Loading branch information
oscargus authored and meeseeksmachine committed Aug 16, 2023
1 parent 2841c76 commit f5f4b5e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 62 deletions.
3 changes: 1 addition & 2 deletions lib/matplotlib/_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Binary file modified lib/matplotlib/tests/baseline_images/test_text/antialiased.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 18 additions & 58 deletions lib/matplotlib/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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():
Expand All @@ -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.
Expand Down

0 comments on commit f5f4b5e

Please sign in to comment.