From 740235060519c8330e6e733a10d8795e40e19b54 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 17 Apr 2022 13:09:27 +0200 Subject: [PATCH] Support not embedding glyphs in svg mathtests. --- lib/matplotlib/testing/compare.py | 25 ++++++++++++++++++++++++- lib/matplotlib/tests/test_mathtext.py | 24 +++++++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/testing/compare.py b/lib/matplotlib/testing/compare.py index 5e0ddc593e03..665e055f123a 100644 --- a/lib/matplotlib/testing/compare.py +++ b/lib/matplotlib/testing/compare.py @@ -214,6 +214,21 @@ def __del__(self): self._tmpdir.cleanup() +class _SVGWithMatplotlibFontsConverter(_SVGConverter): + """ + A SVG converter which explicitly adds the fonts shipped by Matplotlib to + Inkspace's font search path, to better support `svg.fonttype = "none"` + (which is in particular used by certain mathtext tests). + """ + + def __call__(self, orig, dest): + if not hasattr(self, "_tmpdir"): + self._tmpdir = TemporaryDirectory() + shutil.copytree(cbook._get_data_path("fonts/ttf"), + Path(self._tmpdir.name, "fonts")) + return super().__call__(orig, dest) + + def _update_converter(): try: mpl._get_executable_info("gs") @@ -235,6 +250,7 @@ def _update_converter(): #: extension to png format. converter = {} _update_converter() +_svg_with_matplotlib_fonts_converter = _SVGWithMatplotlibFontsConverter() def comparable_formats(): @@ -284,7 +300,14 @@ def convert(filename, cache): return str(newpath) _log.debug("For %s: converting to png.", filename) - converter[path.suffix[1:]](path, newpath) + convert = converter[path.suffix[1:]] + if path.suffix == ".svg": + contents = path.read_text() + if 'style="font:' in contents: + # for svg.fonttype = none, we explicitly patch the font search + # path so that fonts shipped by Matplotlib are found. + convert = _svg_with_matplotlib_fonts_converter + convert(path, newpath) if cache_dir is not None: _log.debug("For %s: caching conversion result.", filename) diff --git a/lib/matplotlib/tests/test_mathtext.py b/lib/matplotlib/tests/test_mathtext.py index abe023495953..9a0e30990e9c 100644 --- a/lib/matplotlib/tests/test_mathtext.py +++ b/lib/matplotlib/tests/test_mathtext.py @@ -116,7 +116,11 @@ r'$\left(X\right)_{a}^{b}$', # github issue 7615 r'$\dfrac{\$100.00}{y}$', # github issue #1888 ] -# 'Lightweight' tests test only a single fontset (dejavusans, which is the +# 'svgastext' tests switch svg output to embed text as text (rather than as +# paths). +svgastext_math_tests = [ +] +# 'lightweight' tests test only a single fontset (dejavusans, which is the # default) and only png outputs, in order to minimize the size of baseline # images. lightweight_math_tests = [ @@ -199,6 +203,24 @@ def test_mathtext_rendering(baseline_images, fontset, index, text): horizontalalignment='center', verticalalignment='center') +@pytest.mark.parametrize('index, text', enumerate(svgastext_math_tests), + ids=range(len(svgastext_math_tests))) +@pytest.mark.parametrize( + 'fontset', ['cm', 'stix', 'stixsans', 'dejavusans', 'dejavuserif']) +@pytest.mark.parametrize('baseline_images', ['mathtext0'], indirect=True) +@image_comparison( + baseline_images=None, + savefig_kwarg={'metadata': { # Minimize image size. + 'Creator': None, 'Date': None, 'Format': None, 'Type': None}}) +def test_mathtext_rendering_svgastext(baseline_images, fontset, index, text): + mpl.rcParams['mathtext.fontset'] = fontset + mpl.rcParams['svg.fonttype'] = 'none' # Minimize image size. + fig = plt.figure(figsize=(5.25, 0.75)) + fig.patch.set(visible=False) # Minimize image size. + fig.text(0.5, 0.5, text, + horizontalalignment='center', verticalalignment='center') + + @pytest.mark.parametrize('index, text', enumerate(lightweight_math_tests), ids=range(len(lightweight_math_tests))) @pytest.mark.parametrize('fontset', ['dejavusans'])