Skip to content

Support not embedding glyphs in svg mathtests. #22881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion lib/matplotlib/testing/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -235,6 +250,7 @@ def _update_converter():
#: extension to png format.
converter = {}
_update_converter()
_svg_with_matplotlib_fonts_converter = _SVGWithMatplotlibFontsConverter()


def comparable_formats():
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 23 additions & 1 deletion lib/matplotlib/tests/test_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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'])
Expand Down