From 42e0d6ba4499772eebe87fa9e2510b5edf852eb8 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Wed, 22 Jul 2026 18:01:25 -0400 Subject: [PATCH] Cairo: Switch back to character codes for mathtext Since f630b483e86d703afa37f5eaa25c88b8c753c639, the Cairo backend was switched to use glyph indices for mathtext, just as the Agg backend. Unfortunately, neither Pycairo nor cairocffi expose anything other than the toy font face API. This means the Cairo backend can only access fonts by family name and not explicit path. Apparently, the version of DejaVu Sans that we ship may differ from the system copy WRT glyph indices, so we need to switch back to character codes, which are standardized and won't cause the wrong glyph to be rendered accidentally. Fixes #32084 --- lib/matplotlib/backends/backend_cairo.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/backends/backend_cairo.py b/lib/matplotlib/backends/backend_cairo.py index a16c7a25aec2..5201959151da 100644 --- a/lib/matplotlib/backends/backend_cairo.py +++ b/lib/matplotlib/backends/backend_cairo.py @@ -8,7 +8,6 @@ import functools import gzip -import itertools import math import numpy as np @@ -249,15 +248,12 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle): if angle: ctx.rotate(np.deg2rad(-angle)) - for (font, fontsize), font_glyphs in itertools.groupby( - glyphs, key=lambda info: (info[0], info[1])): + for font, fontsize, ccode, _glyph_index, ox, oy in glyphs: ctx.new_path() + ctx.move_to(ox, -oy) ctx.select_font_face(*_cairo_font_args_from_font_prop(ttfFontProperty(font))) ctx.set_font_size(self.points_to_pixels(fontsize)) - ctx.show_glyphs([ - (glyph_index, ox, -oy) - for _font, _size, _ccode, glyph_index, ox, oy in font_glyphs - ]) + ctx.show_text(chr(ccode)) for ox, oy, w, h in rects: ctx.new_path()