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

Skip to content

Commit 5d82059

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 5d82059

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

lib/matplotlib/texmanager.py

Lines changed: 10 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')
@@ -169,7 +177,7 @@ def get_font_config(self):
169177
# deepcopy may not be necessary, but feels more future-proof
170178
self._rc_cache[k] = copy.deepcopy(rcParams[k])
171179
_log.debug('RE-INIT\nold fontconfig: %s', self._fontconfig)
172-
self.__init__()
180+
self._reinit()
173181
_log.debug('fontconfig: %s', self._fontconfig)
174182
return self._fontconfig
175183

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)