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

Skip to content

Commit c93ee7f

Browse files
committed
Respect antialiasing settings in cairo backends as well.
I don't think there's many people who want non-antialiased output for cairo; rather, this is mostly so that I can remove the "agg-only" point in the docs (as mplcairo already supports these).
1 parent e429603 commit c93ee7f

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

lib/matplotlib/backends/backend_cairo.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"cairo backend requires that pycairo>=1.11.0 or cairocffi "
2626
"is installed") from err
2727

28+
import matplotlib as mpl
2829
from .. import _api, cbook, font_manager
2930
from matplotlib.backend_bases import (
3031
_Backend, _check_savefig_extra_args, FigureCanvasBase, FigureManagerBase,
@@ -244,9 +245,14 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
244245
ctx.new_path()
245246
ctx.move_to(x, y)
246247

247-
ctx.select_font_face(*_cairo_font_args_from_font_prop(prop))
248248
ctx.save()
249+
ctx.select_font_face(*_cairo_font_args_from_font_prop(prop))
249250
ctx.set_font_size(prop.get_size_in_points() * self.dpi / 72)
251+
opts = cairo.FontOptions()
252+
opts.set_antialias(
253+
cairo.Antialias.DEFAULT if mpl.rcParams["text.antialiased"]
254+
else cairo.Antialias.NONE)
255+
ctx.set_font_options(opts)
250256
if angle:
251257
ctx.rotate(np.deg2rad(-angle))
252258
ctx.show_text(s)
@@ -349,9 +355,9 @@ def set_alpha(self, alpha):
349355
else:
350356
self.ctx.set_source_rgba(rgb[0], rgb[1], rgb[2], rgb[3])
351357

352-
# def set_antialiased(self, b):
353-
# cairo has many antialiasing modes, we need to pick one for True and
354-
# one for False.
358+
def set_antialiased(self, b):
359+
self.ctx.set_antialias(
360+
cairo.Antialias.DEFAULT if b else cairo.Antialias.NONE)
355361

356362
def set_capstyle(self, cs):
357363
self.ctx.set_line_cap(_api.check_getitem(self._capd, capstyle=cs))

matplotlibrc.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@
326326
# unchanged. Set to 6 to obtain previous behavior. Values
327327
# other than 0 or 6 have no defined meaning.
328328
#text.antialiased: True # If True (default), the text will be antialiased.
329-
# This only affects the Agg backend.
329+
# This only affects raster outputs.
330330

331331
## The following settings allow you to select the fonts in math mode.
332332
#mathtext.fontset: dejavusans # Should be 'dejavusans' (default),

0 commit comments

Comments
 (0)