Skip to content

Commit 7402350

Browse files
committed
Support not embedding glyphs in svg mathtests.
1 parent e077394 commit 7402350

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

lib/matplotlib/testing/compare.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,21 @@ def __del__(self):
214214
self._tmpdir.cleanup()
215215

216216

217+
class _SVGWithMatplotlibFontsConverter(_SVGConverter):
218+
"""
219+
A SVG converter which explicitly adds the fonts shipped by Matplotlib to
220+
Inkspace's font search path, to better support `svg.fonttype = "none"`
221+
(which is in particular used by certain mathtext tests).
222+
"""
223+
224+
def __call__(self, orig, dest):
225+
if not hasattr(self, "_tmpdir"):
226+
self._tmpdir = TemporaryDirectory()
227+
shutil.copytree(cbook._get_data_path("fonts/ttf"),
228+
Path(self._tmpdir.name, "fonts"))
229+
return super().__call__(orig, dest)
230+
231+
217232
def _update_converter():
218233
try:
219234
mpl._get_executable_info("gs")
@@ -235,6 +250,7 @@ def _update_converter():
235250
#: extension to png format.
236251
converter = {}
237252
_update_converter()
253+
_svg_with_matplotlib_fonts_converter = _SVGWithMatplotlibFontsConverter()
238254

239255

240256
def comparable_formats():
@@ -284,7 +300,14 @@ def convert(filename, cache):
284300
return str(newpath)
285301

286302
_log.debug("For %s: converting to png.", filename)
287-
converter[path.suffix[1:]](path, newpath)
303+
convert = converter[path.suffix[1:]]
304+
if path.suffix == ".svg":
305+
contents = path.read_text()
306+
if 'style="font:' in contents:
307+
# for svg.fonttype = none, we explicitly patch the font search
308+
# path so that fonts shipped by Matplotlib are found.
309+
convert = _svg_with_matplotlib_fonts_converter
310+
convert(path, newpath)
288311

289312
if cache_dir is not None:
290313
_log.debug("For %s: caching conversion result.", filename)

lib/matplotlib/tests/test_mathtext.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@
116116
r'$\left(X\right)_{a}^{b}$', # github issue 7615
117117
r'$\dfrac{\$100.00}{y}$', # github issue #1888
118118
]
119-
# 'Lightweight' tests test only a single fontset (dejavusans, which is the
119+
# 'svgastext' tests switch svg output to embed text as text (rather than as
120+
# paths).
121+
svgastext_math_tests = [
122+
]
123+
# 'lightweight' tests test only a single fontset (dejavusans, which is the
120124
# default) and only png outputs, in order to minimize the size of baseline
121125
# images.
122126
lightweight_math_tests = [
@@ -199,6 +203,24 @@ def test_mathtext_rendering(baseline_images, fontset, index, text):
199203
horizontalalignment='center', verticalalignment='center')
200204

201205

206+
@pytest.mark.parametrize('index, text', enumerate(svgastext_math_tests),
207+
ids=range(len(svgastext_math_tests)))
208+
@pytest.mark.parametrize(
209+
'fontset', ['cm', 'stix', 'stixsans', 'dejavusans', 'dejavuserif'])
210+
@pytest.mark.parametrize('baseline_images', ['mathtext0'], indirect=True)
211+
@image_comparison(
212+
baseline_images=None,
213+
savefig_kwarg={'metadata': { # Minimize image size.
214+
'Creator': None, 'Date': None, 'Format': None, 'Type': None}})
215+
def test_mathtext_rendering_svgastext(baseline_images, fontset, index, text):
216+
mpl.rcParams['mathtext.fontset'] = fontset
217+
mpl.rcParams['svg.fonttype'] = 'none' # Minimize image size.
218+
fig = plt.figure(figsize=(5.25, 0.75))
219+
fig.patch.set(visible=False) # Minimize image size.
220+
fig.text(0.5, 0.5, text,
221+
horizontalalignment='center', verticalalignment='center')
222+
223+
202224
@pytest.mark.parametrize('index, text', enumerate(lightweight_math_tests),
203225
ids=range(len(lightweight_math_tests)))
204226
@pytest.mark.parametrize('fontset', ['dejavusans'])

0 commit comments

Comments
 (0)