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

Skip to content

Commit 63b94ef

Browse files
committed
Globally cache a single TexManager instance.
This allows sharing its caches across renderer instances. (If it was up to me this class would be replaced by module-level functions and a module-level cache, but heh.)
1 parent de38ef2 commit 63b94ef

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lib/matplotlib/texmanager.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"""
3030

3131
import copy
32+
import functools
3233
import glob
3334
import hashlib
3435
import logging
@@ -48,6 +49,8 @@
4849
class TexManager(object):
4950
"""
5051
Convert strings to dvi files using TeX, caching the results to a directory.
52+
53+
Repeated calls to this constructor always return the same instance.
5154
"""
5255

5356
cachedir = mpl.get_cachedir()
@@ -95,8 +98,13 @@ class TexManager(object):
9598
('text.latex.preamble', 'text.latex.unicode', 'text.latex.preview',
9699
'font.family') + tuple('font.' + n for n in font_families))
97100

98-
def __init__(self):
101+
@functools.lru_cache() # Always return the same instance.
102+
def __new__(cls):
103+
self = object.__new__(cls)
104+
self._reinit()
105+
return self
99106

107+
def _reinit(self):
100108
if self.texcache is None:
101109
raise RuntimeError('Cannot create TexManager, as there is no '
102110
'cache directory available')
@@ -146,6 +154,8 @@ def __init__(self):
146154
self._font_preamble = '\n'.join(
147155
[r'\usepackage{type1cm}'] + cmd + [r'\usepackage{textcomp}'])
148156

157+
return self
158+
149159
def get_basefile(self, tex, fontsize, dpi=None):
150160
"""
151161
Return a filename based on a hash of the string, fontsize, and dpi.
@@ -169,7 +179,7 @@ def get_font_config(self):
169179
# deepcopy may not be necessary, but feels more future-proof
170180
self._rc_cache[k] = copy.deepcopy(rcParams[k])
171181
_log.debug('RE-INIT\nold fontconfig: %s', self._fontconfig)
172-
self.__init__()
182+
self._reinit()
173183
_log.debug('fontconfig: %s', self._fontconfig)
174184
return self._fontconfig
175185

lib/matplotlib/textpath.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ def _get_adobe_standard_encoding():
2424

2525

2626
class TextToPath(object):
27-
"""
28-
A class that convert a given text to a path using ttf fonts.
29-
"""
27+
"""A class that converts strings to paths."""
3028

3129
FONT_SCALE = 100.
3230
DPI = 72

0 commit comments

Comments
 (0)