8000 Support not embedding glyphs in svg mathtests. · matplotlib/matplotlib@6d9fc6e · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d9fc6e

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

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

lib/matplotlib/testing/compare.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,15 @@ def __del__(self):
214214
self._tmpdir.cleanup()
215215

216216

217+
class _SVGWithMatplotlibFontsConverter(_SVGConverter):
218+
def __call__(self, orig, dest):
219+
if not hasattr(self, "_tmpdir"):
220+
self._tmpdir = TemporaryDirectory()
221+
shutil.copytree(cbook._get_data_path("fonts/ttf"),
222+
Path(self._tmpdir.name, "fonts"))
223+
return super().__call__(orig, dest)
224+
225+
217226
def _update_converter():
218227
try:
219228
mpl._get_executable_info("gs")
@@ -235,6 +244,7 @@ def _update_converter():
235244
#: extension to png format.
236245
converter = {}
237246
_update_converter()
247+
_svg_with_matplotlib_fonts_converter = _SVGWithMatplotlibFontsConverter()
238248

239249

240250
def comparable_formats():
@@ -284,7 +294,12 @@ def convert(filename, cache):
284294
return str(newpath)
285295

286296
_log.debug("For %s: converting to png.", filename)
287-
converter[path.suffix[1:]](path, newpath)
297+
convert = converter[path.suffix[1:]]
298+
if path.suffix == ".svg":
299+
contents = path.read_text()
300+
if 'style="font:' in contents: # for svg.fonttype = none.
301+
convert = _svg_with_matplotlib_fonts_converter
302+
convert(path, newpath)
288303

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

lib/matplotlib/tests/test_mathtext.py

Lines changed: 23 additions & 1 deletion
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)
0