2
2
Classes for including text in a figure.
3
3
"""
4
4
5
+ import functools
5
6
import logging
6
7
import math
7
8
import numbers
@@ -89,6 +90,22 @@ def _get_textbox(text, renderer):
89
90
return x_box , y_box , w_box , h_box
90
91
91
92
93
+ def _get_text_metrics_with_cache (renderer , text , fontprop , ismath , dpi ):
94
+ """Call ``renderer.get_text_width_height_descent``, caching the results."""
95
+ # Resolve the actual path corresponding to fontprop so that later in-place
96
+ # mutations of fontprop do not mess up the cache. dpi is not used, but
97
+ # participates in cache invalidation (via the renderer).
98
+ normalized_fp = FontProperties (fname = mpl .font_manager .findfont (fontprop ))
99
+ return _get_text_metrics_with_cache_impl (
100
+ weakref .ref (renderer ), text , normalized_fp , ismath , dpi )
101
+
102
+
103
+ @functools .lru_cache (4096 )
104
+def _get_text_metrics_with_cache_impl (
105
+ renderer_ref , text , fontprop , ismath , dpi ):
106
+ return renderer_ref ().get_text_width_height_descent (text , fontprop , ismath )
107
+
108
+
92
109
@docstring .interpd
93
110
@cbook ._define_aliases ({
94
111
"color" : ["c" ],
@@ -108,7 +125,6 @@ class Text(Artist):
108
125
"""Handle storing and drawing of text in window or data coordinates."""
109
126
110
127
zorder = 3
111
- _cached = cbook .maxdict (50 )
112
128
113
129
def __repr__ (self ):
114
130
return "Text(%s, %s, %s)" % (self ._x , self ._y , repr (self ._text ))
@@ -273,23 +289,6 @@ def update_from(self, other):
273
289
self ._linespacing = other ._linespacing
274
290
self .stale = True
275
291
276
- def _get_text_metrics_with_cache (
277
- self , renderer , text , fontproperties , ismath ):
278
- """
279
- Call ``renderer.get_text_width_height_descent``, caching the results.
280
- """
281
- cache_key = (
282
- weakref .ref (renderer ),
283
- text ,
284
- hash (fontproperties ),
285
- ismath ,
286
- self .figure .dpi ,
287
- )
288
- if cache_key not in self ._cached :
289
- self ._cached [cache_key ] = renderer .get_text_width_height_descent (
290
- text , fontproperties , ismath )
291
- return self ._cached [cache_key ]
292
-
293
292
def _get_layout (self , renderer ):
294
293
"""
295
294
Return the extent (bbox) of the text together with
@@ -305,16 +304,17 @@ def _get_layout(self, renderer):
305
304
ys = []
306
305
307
306
# Full vertical extent of font, including ascenders and descenders:
308
- _ , lp_h , lp_d = self . _get_text_metrics_with_cache (
307
+ _ , lp_h , lp_d = _get_text_metrics_with_cache (
309
308
renderer , "lp" , self ._fontproperties ,
310
- ismath = "TeX" if self .get_usetex () else False )
309
+ ismath = "TeX" if self .get_usetex () else False , dpi = self . figure . dpi )
311
310
min_dy = (lp_h - lp_d ) * self ._linespacing
312
311
313
312
for i , line in enumerate (lines ):
314
313
clean_line , ismath = self ._preprocess_math (line )
315
314
if clean_line :
316
- w , h , d = self ._get_text_metrics_with_cache (
317
- renderer , clean_line , self ._fontproperties , ismath )
315
+ w , h , d = _get_text_metrics_with_cache (
316
+ renderer , clean_line , self ._fontproperties ,
317
+ ismath = ismath , dpi = self .figure .dpi )
318
318
else :
319
319
w = h = d = 0
320
320
0 commit comments