@@ -61,37 +61,44 @@ class TexManager:
61
61
"""
62
62
63
63
texcache = os .path .join (mpl .get_cachedir (), 'tex.cache' )
64
-
65
64
_grey_arrayd = {}
66
- _font_family = 'serif'
65
+
67
66
_font_families = ('serif' , 'sans-serif' , 'cursive' , 'monospace' )
68
- _font_info = {
69
- 'new century schoolbook' : ( 'pnc' , r'\renewcommand{\rmdefault}{pnc}' ) ,
70
- 'bookman' : ( 'pbk' , r'\renewcommand{\rmdefault}{pbk}' ) ,
71
- 'times' : ( 'ptm' , r'\usepackage{mathptmx}' ) ,
72
- 'palatino' : ( 'ppl' , r'\usepackage{mathpazo}' ) ,
73
- 'zapf chancery' : ( 'pzc' , r'\usepackage{chancery}' ) ,
74
- 'cursive' : ( 'pzc' , r'\usepackage{chancery}' ) ,
75
- 'charter' : ( 'pch' , r'\usepackage{charter}' ) ,
76
- 'serif' : ( 'cmr' , '' ) ,
77
- 'sans-serif' : ( 'cmss' , '' ) ,
78
- 'helvetica' : ( 'phv' , r'\usepackage{helvet}' ) ,
79
- 'avant garde' : ( 'pag' , r'\usepackage{avant}' ) ,
80
- 'courier' : ( 'pcr' , r'\usepackage{courier}' ) ,
67
+ _font_preambles = {
68
+ 'new century schoolbook' : r'\renewcommand{\rmdefault}{pnc}' ,
69
+ 'bookman' : r'\renewcommand{\rmdefault}{pbk}' ,
70
+ 'times' : r'\usepackage{mathptmx}' ,
71
+ 'palatino' : r'\usepackage{mathpazo}' ,
72
+ 'zapf chancery' : r'\usepackage{chancery}' ,
73
+ 'cursive' : r'\usepackage{chancery}' ,
74
+ 'charter' : r'\usepackage{charter}' ,
75
+ 'serif' : '' ,
76
+ 'sans-serif' : '' ,
77
+ 'helvetica' : r'\usepackage{helvet}' ,
78
+ 'avant garde' : r'\usepackage{avant}' ,
79
+ 'courier' : r'\usepackage{courier}' ,
81
80
# Loading the type1ec package ensures that cm-super is installed, which
82
81
# is necessary for Unicode computer modern. (It also allows the use of
83
82
# computer modern at arbitrary sizes, but that's just a side effect.)
84
- 'monospace' : ('cmtt' , r'\usepackage{type1ec}' ),
85
- 'computer modern roman' : ('cmr' , r'\usepackage{type1ec}' ),
86
- 'computer modern sans serif' : ('cmss' , r'\usepackage{type1ec}' ),
87
- 'computer modern typewriter' : ('cmtt' , r'\usepackage{type1ec}' )}
83
+ 'monospace' : r'\usepackage{type1ec}' ,
84
+ 'computer modern roman' : r'\usepackage{type1ec}' ,
85
+ 'computer modern sans serif' : r'\usepackage{type1ec}' ,
86
+ 'computer modern typewriter' : r'\usepackage{type1ec}' ,
87
+ }
88
88
_font_types = {
89
- 'new century schoolbook' : 'serif' , 'bookman' : 'serif' ,
90
- 'times' : 'serif' , 'palatino' : 'serif' , 'charter' : 'serif' ,
91
- 'computer modern roman' : 'serif' , 'zapf chancery' : 'cursive' ,
92
- 'helvetica' : 'sans-serif' , 'avant garde' : 'sans-serif' ,
89
+ 'new century schoolbook' : 'serif' ,
90
+ 'bookman' : 'serif' ,
91
+ 'times' : 'serif' ,
92
+ 'palatino' : 'serif' ,
93
+ 'zapf chancery' : 'cursive' ,
94
+ 'charter' : 'serif' ,
95
+ 'helvetica' : 'sans-serif' ,
96
+ 'avant garde' : 'sans-serif' ,
97
+ 'courier' : 'monospace' ,
98
+ 'computer modern roman' : 'serif' ,
93
99
'computer modern sans serif' : 'sans-serif' ,
94
- 'courier' : 'monospace' , 'computer modern typewriter' : 'monospace' }
100
+ 'computer modern typewriter' : 'monospace' ,
101
+ }
95
102
96
103
grey_arrayd = _api .deprecate_privatize_attribute ("3.5" )
97
104
font_family = _api .deprecate_privatize_attribute ("3.5" )
@@ -103,33 +110,46 @@ def __new__(cls):
103
110
Path (cls .texcache ).mkdir (parents = True , exist_ok = True )
104
111
return object .__new__ (cls )
105
112
113
+ @_api .deprecated ("3.6" )
106
114
def get_font_config (self ):
115
+ preamble , font_cmd = self ._get_font_preamble_and_command ()
116
+ # Add a hash of the latex preamble to fontconfig so that the
117
+ # correct png is selected for strings rendered with same font and dpi
118
+ # even if the latex preamble changes within the session
119
+ preambles = preamble + font_cmd + self .get_custom_preamble ()
120
+ return hashlib .md5 (preambles .encode ('utf-8' )).hexdigest ()
121
+
122
+ def _get_font_family_and_reduced (self ):
107
123
ff = rcParams ['font.family' ]
108
124
ff_val = ff [0 ].lower () if len (ff ) == 1 else None
109
125
reduced_notation = False
110
126
if len (ff ) == 1 and ff_val in self ._font_families :
111
- self ._font_family = ff_val
112
- elif len (ff ) == 1 and ff_val in self ._font_info :
113
- reduced_notation = True
114
- self ._font_family = self ._font_types [ff_val ]
127
+ return ff_val , False
128
+ elif len (ff ) == 1 and ff_val in self ._font_preambles :
129
+ return self ._font_types [ff_val ], True
115
130
else :
116
131
_log .info ('font.family must be one of (%s) when text.usetex is '
117
132
'True. serif will be used by default.' ,
118
133
', ' .join (self ._font_families ))
119
- self ._font_family = 'serif'
134
+ return 'serif' , False
135
+
136
+ def _get_font_preamble_and_command (self ):
137
+ requested_family , is_reduced_font = self ._get_font_family_and_reduced ()
120
138
121
- fontconfig = [self ._font_family ]
122
- fonts = {}
139
+ preambles = {}
123
140
for font_family in self ._font_families :
124
- if reduced_notation and self ._font_family == font_family :
125
- fonts [font_family ] = self ._font_info [ff_val ]
141
+ if is_reduced_font and font_family == requested_family :
142
+ preambles [font_family ] = self ._font_preambles [
143
+ rcParams ['font.family' ][0 ].lower ()]
126
144
else :
127
145
for font in rcParams ['font.' + font_family ]:
128
- if font .lower () in self ._font_info :
129
- fonts [font_family ] = self ._font_info [font .lower ()]
146
+ if font .lower () in self ._font_preambles :
147
+ preambles [font_family ] = \
148
+ self ._font_preambles [font .lower ()]
130
149
_log .debug (
131
150
'family: %s, font: %s, info: %s' ,
132
- font_family , font , self ._font_info [font .lower ()])
151
+ font_family , font ,
152
+ self ._font_preambles [font .lower ()])
133
153
break
134
154
else :
135
155
_log .debug ('%s font is not compatible with usetex.' ,
@@ -138,24 +158,20 @@ def get_font_config(self):
138
158
_log .info ('No LaTeX-compatible font found for the %s font'
139
159
'family in rcParams. Using default.' ,
140
160
font_family )
141
- fonts [font_family ] = self ._font_info [font_family ]
142
- fontconfig .append (fonts [font_family ][0 ])
143
- # Add a hash of the latex preamble to fontconfig so that the
144
- # correct png is selected for strings rendered with same font and dpi
145
- # even if the latex preamble changes within the session
146
- preamble_bytes = self .get_custom_preamble ().encode ('utf-8' )
147
- fontconfig .append (hashlib .md5 (preamble_bytes ).hexdigest ())
161
+ preambles [font_family ] = self ._font_preambles [font_family ]
148
162
149
163
# The following packages and commands need to be included in the latex
150
164
# file's preamble:
151
- cmd = {fonts [family ][ 1 ]
165
+ cmd = {preambles [family ]
152
166
for family in ['serif' , 'sans-serif' , 'monospace' ]}
153
- if self . _font_family == 'cursive' :
154
- cmd .add (fonts ['cursive' ][ 1 ])
167
+ if requested_family == 'cursive' :
168
+ cmd .add (preambles ['cursive' ])
155
169
cmd .add (r'\usepackage{type1cm}' )
156
- self ._font_preamble = '\n ' .join (sorted (cmd ))
157
-
158
- return '' .join (fontconfig )
170
+ preamble = '\n ' .join (sorted (cmd ))
171
+ fontcmd = (r'\sffamily' if requested_family == 'sans-serif' else
172
+ r'\ttfamily' if requested_family == 'monospace' else
173
+ r'\rmfamily' )
174
+ return preamble , fontcmd
159
175
160
176
def get_basefile (self , tex , fontsize , dpi = None ):
161
177
"""
@@ -169,26 +185,24 @@ def get_font_preamble(self):
169
185
"""
170
186
Return a string containing font configuration for the tex preamble.
171
187
"""
172
- return self ._font_preamble
188
+ font_preamble , command = self ._get_font_preamble_and_command ()
189
+ return font_preamble
173
190
174
191
def get_custom_preamble (self ):
175
192
"""Return a string containing user additions to the tex preamble."""
176
193
return rcParams ['text.latex.preamble' ]
177
194
178
195
def _get_tex_source (self , tex , fontsize ):
179
196
"""Return the complete TeX source for processing a TeX string."""
180
- self . get_font_config () # Updates self._font_preamble.
197
+ font_preamble , fontcmd = self ._get_font_preamble_and_command ()
181
198
baselineskip = 1.25 * fontsize
182
- fontcmd = (r'\sffamily' if self ._font_family == 'sans-serif' else
183
- r'\ttfamily' if self ._font_family == 'monospace' else
184
- r'\rmfamily' )
185
199
return "\n " .join ([
186
200
r"\documentclass{article}" ,
187
201
r"% Pass-through \mathdefault, which is used in non-usetex mode" ,
188
202
r"% to use the default text font but was historically suppressed" ,
189
203
r"% in usetex mode." ,
190
204
r"\newcommand{\mathdefault}[1]{#1}" ,
191
- self . _font_preamble ,
205
+ font_preamble ,
192
206
r"\usepackage[utf8]{inputenc}" ,
193
207
r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}" ,
194
208
r"% geometry is loaded before the custom preamble as " ,
@@ -301,7 +315,7 @@ def get_grey(self, tex, fontsize=None, dpi=None):
301
315
fontsize = rcParams ['font.size' ]
302
316
if not dpi :
303
317
dpi = rcParams ['savefig.dpi' ]
304
- key = tex , self .get_font_config () , fontsize , dpi
318
+ key = self ._get_tex_source ( tex , fontsize ) , dpi
305
319
alpha = self ._grey_arrayd .get (key )
306
320
if alpha is None :
307
321
pngfile = self .make_png (tex , fontsize , dpi )
0 commit comments