1
1
import numpy as np
2
2
from io import BytesIO
3
3
import os
4
+ import re
4
5
import tempfile
5
6
import warnings
6
7
import xml .parsers .expat
7
8
8
9
import pytest
9
10
11
+ import matplotlib as mpl
12
+ from matplotlib import dviread
13
+ from matplotlib .figure import Figure
10
14
import matplotlib .pyplot as plt
11
15
from matplotlib .testing .decorators import image_comparison
12
- import matplotlib
13
- from matplotlib import dviread
14
16
15
17
16
18
with warnings .catch_warnings ():
17
19
warnings .simplefilter ('ignore' )
18
20
needs_usetex = pytest .mark .skipif (
19
- not matplotlib .checkdep_usetex (True ),
21
+ not mpl .checkdep_usetex (True ),
20
22
reason = "This test needs a TeX installation" )
21
23
22
24
@@ -100,15 +102,10 @@ def test_bold_font_output_with_none_fonttype():
100
102
101
103
def _test_determinism_save (filename , usetex ):
102
104
# This function is mostly copy&paste from "def test_visibility"
103
- # To require no GUI, we use Figure and FigureCanvasSVG
104
- # instead of plt.figure and fig.savefig
105
- from matplotlib .figure import Figure
106
- from matplotlib .backends .backend_svg import FigureCanvasSVG
107
- from matplotlib import rc
108
- rc ('svg' , hashsalt = 'asdf' )
109
- rc ('text' , usetex = usetex )
105
+ mpl .rc ('svg' , hashsalt = 'asdf' )
106
+ mpl .rc ('text' , usetex = usetex )
110
107
111
- fig = Figure ()
108
+ fig = Figure () # Require no GUI.
112
109
ax = fig .add_subplot (111 )
113
110
114
111
x = np .linspace (0 , 4 * np .pi , 50 )
@@ -122,7 +119,7 @@ def _test_determinism_save(filename, usetex):
122
119
ax .set_xlabel ('A string $1+2+\\ sigma$' )
123
120
ax .set_ylabel ('A string $1+2+\\ sigma$' )
124
121
125
- FigureCanvasSVG ( fig ). print_svg (filename )
122
+ fig . savefig (filename , format = "svg" )
126
123
127
124
128
125
@pytest .mark .parametrize (
@@ -165,36 +162,30 @@ def test_determinism(filename, usetex):
165
162
@needs_usetex
166
163
def test_missing_psfont (monkeypatch ):
167
164
"""An error is raised if a TeX font lacks a Type-1 equivalent"""
168
- from matplotlib import rc
169
165
170
166
def psfont (* args , ** kwargs ):
171
167
return dviread .PsFont (texname = 'texfont' , psname = 'Some Font' ,
172
168
effects = None , encoding = None , filename = None )
173
169
174
170
monkeypatch .setattr (dviread .PsfontsMap , '__getitem__' , psfont )
175
- rc ('text' , usetex = True )
171
+ mpl . rc ('text' , usetex = True )
176
172
fig , ax = plt .subplots ()
177
173
ax .text (0.5 , 0.5 , 'hello' )
178
174
with tempfile .TemporaryFile () as tmpfile , pytest .raises (ValueError ):
179
175
fig .savefig (tmpfile , format = 'svg' )
180
176
181
177
182
- @needs_tex
178
+ # Use Computer Modern Sans Serif, not Helvetica (which has no \textwon).
179
+ @pytest .mark .style ('default' )
180
+ @needs_usetex
183
181
def test_unicode_won ():
184
- from pylab import rcParams , plot , ylabel , savefig
185
- rcParams .update ({'text.usetex' : True , 'text.latex.unicode' : True })
186
-
187
- plot (1 , 1 )
188
- ylabel (r'\textwon' )
182
+ fig = Figure ()
183
+ fig .text (.5 , .5 , r'\textwon' , usetex = True )
189
184
190
- fd = BytesIO ()
191
- savefig (fd , format = 'svg' )
192
- fd .seek (0 )
193
- buf = fd .read ().decode ()
194
- fd .close ()
185
+ with BytesIO () as fd :
186
+ fig .savefig (fd , format = 'svg' )
187
+ buf = fd .getvalue ().decode ('ascii' )
195
188
196
189
won_id = 'Computer_Modern_Sans_Serif-142'
197
- def_regex = re .compile (r'<path d=(.|\s)*?id="{0}"/>' .format (won_id ))
198
- use_regex = re .compile (r'<use[^/>]*? xlink:href="#{0}"/>' .format (won_id ))
199
- assertTrue (bool (def_regex .search (buf )))
200
- assertTrue (bool (use_regex .search (buf )))
190
+ assert re .search (r'<path d=(.|\s)*?id="{0}"/>' .format (won_id ), buf )
191
+ assert re .search (r'<use[^/>]*? xlink:href="#{0}"/>' .format (won_id ), buf )
0 commit comments