28
28
:rc:`text.usetex` to True.
29
29
"""
30
30
31
- import copy
32
31
import functools
33
32
import glob
34
33
import hashlib
@@ -58,10 +57,6 @@ class TexManager:
58
57
rgba_arrayd = {}
59
58
grey_arrayd = {}
60
59
61
- serif = ('cmr' , '' )
62
- sans_serif = ('cmss' , '' )
63
- monospace = ('cmtt' , '' )
64
- cursive = ('pzc' , r'\usepackage{chancery}' )
65
60
font_family = 'serif'
66
61
font_families = ('serif' , 'sans-serif' , 'cursive' , 'monospace' )
67
62
@@ -86,24 +81,39 @@ class TexManager:
86
81
'computer modern sans serif' : ('cmss' , r'\usepackage{type1ec}' ),
87
82
'computer modern typewriter' : ('cmtt' , r'\usepackage{type1ec}' )}
88
83
89
- _rc_cache = None
90
- _rc_cache_keys = [
91
- 'text.latex.preamble' , 'text.latex.preview' , 'font.family' ,
92
- * ['font.' + n for n in font_families ]]
93
-
94
84
@cbook .deprecated ("3.3" , alternative = "matplotlib.get_cachedir()" )
95
85
@property
96
86
def cachedir (self ):
97
87
return mpl .get_cachedir ()
98
88
99
89
@functools .lru_cache () # Always return the same instance.
100
90
def __new__ (cls ):
101
- self = object .__new__ (cls )
102
- self ._reinit ()
103
- return self
91
+ Path (cls .texcache ).mkdir (parents = True , exist_ok = True )
92
+ return object .__new__ (cls )
93
+
94
+ _fonts = {} # Only for deprecation period.
95
+
96
+ @cbook .deprecated ("3.3" )
97
+ @property
98
+ def serif (self ):
99
+ return self ._fonts .get ("serif" , ('cmr' , '' ))
100
+
101
+ @cbook .deprecated ("3.3" )
102
+ @property
103
+ def sans_serif (self ):
104
+ return self ._fonts .get ("sans-serif" , ('cmss' , '' ))
105
+
106
+ @cbook .deprecated ("3.3" )
107
+ @property
108
+ def cursive (self ):
109
+ return self ._fonts .get ("cursive" , ('pzc' , r'\usepackage{chancery}' ))
110
+
111
+ @cbook .deprecated ("3.3" )
112
+ @property
113
+ def monospace (self ):
114
+ return self ._fonts .get ("monospace" , ('cmtt' , '' ))
104
115
105
- def _reinit (self ):
106
- Path (self .texcache ).mkdir (parents = True , exist_ok = True )
116
+ def get_font_config (self ):
107
117
ff = rcParams ['font.family' ]
108
118
if len (ff ) == 1 and ff [0 ].lower () in self .font_families :
109
119
self .font_family = ff [0 ].lower ()
@@ -115,11 +125,9 @@ def _reinit(self):
115
125
116
126
fontconfig = [self .font_family ]
117
127
for font_family in self .font_families :
118
- font_family_attr = font_family .replace ('-' , '_' )
119
128
for font in rcParams ['font.' + font_family ]:
120
129
if font .lower () in self .font_info :
121
- setattr (self , font_family_attr ,
122
- self .font_info [font .lower ()])
130
+ self ._fonts [font_family ] = self .font_info [font .lower ()]
123
131
_log .debug ('family: %s, font: %s, info: %s' ,
124
132
font_family , font , self .font_info [font .lower ()])
125
133
break
@@ -129,22 +137,25 @@ def _reinit(self):
129
137
else :
130
138
_log .info ('No LaTeX-compatible font found for the %s font '
131
139
'family in rcParams. Using default.' , font_family )
132
- setattr ( self , font_family_attr , self .font_info [font_family ])
133
- fontconfig .append (getattr ( self , font_family_attr ) [0 ])
134
- # Add a hash of the latex preamble to self._fontconfig so that the
140
+ self . _fonts [ font_family ] = self .font_info [font_family ]
141
+ fontconfig .append (self . _fonts [ font_family ] [0 ])
142
+ # Add a hash of the latex preamble to fontconfig so that the
135
143
# correct png is selected for strings rendered with same font and dpi
136
144
# even if the latex preamble changes within the session
137
145
preamble_bytes = self .get_custom_preamble ().encode ('utf-8' )
138
146
fontconfig .append (hashlib .md5 (preamble_bytes ).hexdigest ())
139
- self ._fontconfig = '' .join (fontconfig )
140
147
141
148
# The following packages and commands need to be included in the latex
142
149
# file's preamble:
143
- cmd = [self .serif [1 ], self .sans_serif [1 ], self .monospace [1 ]]
150
+ cmd = [self ._fonts ['serif' ][1 ],
151
+ self ._fonts ['sans-serif' ][1 ],
152
+ self ._fonts ['monospace' ][1 ]]
144
153
if self .font_family == 'cursive' :
145
- cmd .append (self .cursive [1 ])
154
+ cmd .append (self ._fonts [ ' cursive' ] [1 ])
146
155
self ._font_preamble = '\n ' .join (
147
- [r'\usepackage{type1cm}' ] + cmd + [r'\usepackage{textcomp}' ])
156
+ [r'\usepackage{type1cm}' , * cmd , r'\usepackage{textcomp}' ])
157
+
158
+ return '' .join (fontconfig )
148
159
149
160
def get_basefile (self , tex , fontsize , dpi = None ):
150
161
"""
@@ -155,24 +166,6 @@ def get_basefile(self, tex, fontsize, dpi=None):
155
166
return os .path .join (
156
167
self .texcache , hashlib .md5 (s .encode ('utf-8' )).hexdigest ())
157
168
158
- def get_font_config (self ):
159
- """Reinitializes self if relevant rcParams on have changed."""
160
- if self ._rc_cache is None :
161
- self ._rc_cache = dict .fromkeys (self ._rc_cache_keys )
162
- changed = [par for par in self ._rc_cache_keys
163
- if rcParams [par ] != self ._rc_cache [par ]]
164
- if changed :
165
- _log .debug ('following keys changed: %s' , changed )
166
- for k in changed :
167
- _log .debug ('%-20s: %-10s -> %-10s' ,
168
- k , self ._rc_cache [k ], rcParams [k ])
169
- # deepcopy may not be necessary, but feels more future-proof
170
- self ._rc_cache [k ] = copy .deepcopy (rcParams [k ])
171
- _log .debug ('RE-INIT\n old fontconfig: %s' , self ._fontconfig )
172
- self ._reinit ()
173
- _log .debug ('fontconfig: %s' , self ._fontconfig )
174
- return self ._fontconfig
175
-
176
169
def get_font_preamble (self ):
177
170
"""
178
171
Return a string containing font configuration for the tex preamble.
0 commit comments