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

Skip to content

Commit 835220b

Browse files
authored
Merge pull request #26529 from QuLogic/fix-antialias
Fix MathText antialiasing
2 parents 267b7c0 + 09b8ee8 commit 835220b

File tree

4 files changed

+20
-62
lines changed

4 files changed

+20
-62
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 1 addition & 2 deletions
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, antialiased=None):
109+
def to_raster(self, *, antialiased):
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],
@@ -128,7 +128,6 @@ def to_raster(self, antialiased=None):
128128
# old approach and keeps baseline images backcompat.
129129
shifted = ship(self.box, (-xmin, -ymin))
130130

131-
antialiased = mpl.rcParams['text.antialiased']
132131
for ox, oy, info in shifted.glyphs:
133132
info.font.draw_glyph_to_bitmap(
134133
image, ox, oy - info.metrics.iceberg, info.glyph,

lib/matplotlib/mathtext.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ def parse(self, s, dpi=72, prop=None, *, antialiased=None):
7575
# is mutable; key the cache using an internal copy (see
7676
# text._get_text_metrics_with_cache for a similar case).
7777
prop = prop.copy() if prop is not None else None
78-
if antialiased is None:
79-
antialiased = mpl.rcParams['text.antialiased']
78+
antialiased = mpl._val_or_rc(antialiased, 'text.antialiased')
8079
return self._parse_cached(s, dpi, prop, antialiased)
8180

8281
@functools.lru_cache(50)

lib/matplotlib/tests/test_text.py

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,23 @@ def draw_box(ax, tt):
186186
ax.text(1.2, 0.1, 'Bot align, rot20', color='C2')
187187

188188

189-
@image_comparison(['antialiased.png'])
189+
@image_comparison(['antialiased.png'], style='mpl20')
190190
def test_antialiasing():
191-
mpl.rcParams['text.antialiased'] = True
191+
mpl.rcParams['text.antialiased'] = False # Passed arguments should override.
192192

193193
fig = plt.figure(figsize=(5.25, 0.75))
194-
fig.text(0.5, 0.75, "antialiased", horizontalalignment='center',
195-
verticalalignment='center')
196-
fig.text(0.5, 0.25, r"$\sqrt{x}$", horizontalalignment='center',
197-
verticalalignment='center')
198-
# NOTE: We don't need to restore the rcParams here, because the
199-
# test cleanup will do it for us. In fact, if we do it here, it
200-
# will turn antialiasing back off before the images are actually
201-
# rendered.
194+
fig.text(0.3, 0.75, "antialiased", horizontalalignment='center',
195+
verticalalignment='center', antialiased=True)
196+
fig.text(0.3, 0.25, r"$\sqrt{x}$", horizontalalignment='center',
197+
verticalalignment='center', antialiased=True)
198+
199+
mpl.rcParams['text.antialiased'] = True # Passed arguments should override.
200+
fig.text(0.7, 0.75, "not antialiased", horizontalalignment='center',
201+
verticalalignment='center', antialiased=False)
202+
fig.text(0.7, 0.25, r"$\sqrt{x}$", horizontalalignment='center',
203+
verticalalignment='center', antialiased=False)
204+
205+
mpl.rcParams['text.antialiased'] = False # Should not affect existing text.
202206

203207

204208
def test_afm_kerning():
@@ -914,29 +918,18 @@ def test_annotate_offset_fontsize():
914918
assert str(points_coords) == str(fontsize_coords)
915919

916920

917-
def test_set_antialiased():
921+
def test_get_set_antialiased():
918922
txt = Text(.5, .5, "foo\nbar")
919923
assert txt._antialiased == mpl.rcParams['text.antialiased']
924+
assert txt.get_antialiased() == mpl.rcParams['text.antialiased']
920925

921926
txt.set_antialiased(True)
922927
assert txt._antialiased is True
928+
assert txt.get_antialiased() == txt._antialiased
923929

924930
txt.set_antialiased(False)
925931
assert txt._antialiased is False
926-
927-
928-
def test_get_antialiased():
929-
930-
txt2 = Text(.5, .5, "foo\nbar", antialiased=True)
931-
assert txt2._antialiased is True
932-
assert txt2.get_antialiased() == txt2._antialiased
933-
934-
txt3 = Text(.5, .5, "foo\nbar", antialiased=False)
935-
assert txt3._antialiased is False
936-
assert txt3.get_antialiased() == txt3._antialiased
937-
938-
txt4 = Text(.5, .5, "foo\nbar")
939-
assert txt4.get_antialiased() == mpl.rcParams['text.antialiased']
932+
assert txt.get_antialiased() == txt._antialiased
940933

941934

942935
def test_annotation_antialiased():
@@ -957,39 +950,6 @@ def test_annotation_antialiased():
957950
assert annot4._antialiased == mpl.rcParams['text.antialiased']
958951

959952

960-
@check_figures_equal()
961-
def test_text_antialiased_off_default_vs_manual(fig_test, fig_ref):
962-
fig_test.text(0.5, 0.5, '6 inches x 2 inches',
963-
antialiased=False)
964-
965-
mpl.rcParams['text.antialiased'] = False
966-
fig_ref.text(0.5, 0.5, '6 inches x 2 inches')
967-
968-
969-
@check_figures_equal()
970-
def test_text_antialiased_on_default_vs_manual(fig_test, fig_ref):
971-
fig_test.text(0.5, 0.5, '6 inches x 2 inches', antialiased=True)
972-
973-
mpl.rcParams['text.antialiased'] = True
974-
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}$")
991-
992-
993953
@check_figures_equal(extensions=["png"])
994954
def test_annotate_and_offsetfrom_copy_input(fig_test, fig_ref):
995955
# Both approaches place the text (10, 0) pixels away from the center of the line.

0 commit comments

Comments
 (0)