Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15f6905

Browse files
committed
Add machinery for svg-only mathtext tests.
1 parent 5fd8ae8 commit 15f6905

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

lib/matplotlib/testing/compare.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def __call__(self, orig, dest):
181181
# just be reported as a regular exception below).
182182
"DISPLAY": "",
183183
# Do not load any user options.
184-
"INKSCAPE_PROFILE_DIR": os.devnull,
184+
"INKSCAPE_PROFILE_DIR": self._tmpdir.name,
185185
}
186186
# Old versions of Inkscape (e.g. 0.48.3.1) seem to sometimes
187187
# deadlock when stderr is redirected to a pipe, so we redirect it
@@ -233,6 +233,15 @@ def __del__(self):
233233
self._tmpdir.cleanup()
234234

235235

236+
class _SVGWithMatplotlibFontsConverter(_SVGConverter):
237+
def __call__(self, orig, dest):
238+
if not hasattr(self, "_tmpdir"):
239+
self._tmpdir = TemporaryDirectory()
240+
shutil.copytree(cbook._get_data_path("fonts/ttf"),
241+
Path(self._tmpdir.name, "fonts"))
242+
return super().__call__(orig, dest)
243+
244+
236245
def _update_converter():
237246
try:
238247
mpl._get_executable_info("gs")
@@ -254,6 +263,7 @@ def _update_converter():
254263
#: extension to png format.
255264
converter = {}
256265
_update_converter()
266+
_svg_with_matplotlib_fonts_converter = _SVGWithMatplotlibFontsConverter()
257267

258268

259269
def comparable_formats():
@@ -303,7 +313,12 @@ def convert(filename, cache):
303313
return str(newpath)
304314

305315
_log.debug("For %s: converting to png.", filename)
306-
converter[path.suffix[1:]](path, newpath)
316+
convert = converter[path.suffix[1:]]
317+
if path.suffix == ".svg":
318+
contents = path.read_text()
319+
if 'style="font:' in contents: # for svg.fonttype = none.
320+
convert = _svg_with_matplotlib_fonts_converter
321+
convert(path, newpath)
307322

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

lib/matplotlib/tests/test_mathtext.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
r'$\left(X\right)_{a}^{b}$', # github issue 7615
113113
r'$\dfrac{\$100.00}{y}$', # github issue #1888
114114
]
115+
svg_only_math_tests = [
116+
]
115117

116118
digits = "0123456789"
117119
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@@ -167,8 +169,6 @@
167169
for set in chars:
168170
font_tests.append(wrapper % set)
169171

170-
font_tests = list(filter(lambda x: x[1] is not None, enumerate(font_tests)))
171-
172172

173173
@pytest.fixture
174174
def baseline_images(request, fontset, index):
@@ -192,6 +192,32 @@ def test_mathtext_rendering(baseline_images, fontset, index, test):
192192
horizontalalignment='center', verticalalignment='center')
193193

194194

195+
cur_svg_only_math_tests = list(
196+
filter(lambda x: x[1] is not None, enumerate(svg_only_math_tests)))
197+
198+
199+
@pytest.mark.parametrize('index, test', cur_svg_only_math_tests,
200+
ids=[str(idx) for idx, _ in cur_svg_only_math_tests])
201+
@pytest.mark.parametrize('fontset', ['all'])
202+
@pytest.mark.parametrize('baseline_images', ['mathtext1'], indirect=True)
203+
@image_comparison(
204+
baseline_images=None, extensions=['svg'],
205+
savefig_kwarg={
206+
'metadata': { # Minimize svg size.
207+
'Creator': None, 'Date': None, 'Format': None, 'Type': None}})
208+
def test_mathtext_rendering_svg_only(baseline_images, fontset, index, test):
209+
mpl.rcParams['svg.fonttype'] = 'none'
210+
fig = plt.figure(figsize=(5.25, 5.25))
211+
fig.patch.set_visible(False) # Minimize svg size.
212+
fontsets = ['cm', 'stix', 'stixsans', 'dejavusans', 'dejavuserif']
213+
for i, fontset in enumerate(fontsets):
214+
fig.text(0.5, (i + .5) / len(fontsets), test, math_fontfamily=fontset,
215+
horizontalalignment='center', verticalalignment='center')
216+
217+
218+
font_tests = list(filter(lambda x: x[1] is not None, enumerate(font_tests)))
219+
220+
195221
@pytest.mark.parametrize('index, test', font_tests,
196222
ids=[str(index) for index, _ in font_tests])
197223
@pytest.mark.parametrize('fontset',

0 commit comments

Comments
 (0)