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,21 @@ 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
+ # Cached based on a copy of fontprop so that later in-place mutations of
96
+ # the passed-in argument do not mess up the cache.
97
+ return _get_text_metrics_with_cache_impl (
98
+ weakref .ref (renderer ), text , fontprop .copy (), ismath , dpi )
99
+
100
+
101
+ @functools .lru_cache (4096 )
102
+ def _get_text_metrics_with_cache_impl (
103
+ renderer_ref , text , fontprop , ismath , dpi ):
104
+ # dpi is unused, but participates in cache invalidation (via the renderer).
105
+ return renderer_ref ().get_text_width_height_descent (text , fontprop , ismath )
106
+
107
+
92
108
@docstring .interpd
93
109
@cbook ._define_aliases ({
94
110
"color" : ["c" ],
@@ -108,7 +124,6 @@ class Text(Artist):
108
124
"""Handle storing and drawing of text in window or data coordinates."""
109
125
110
126
zorder = 3
111
- _cached = cbook .maxdict (50 )
112
127
113
128
def __repr__ (self ):
114
129
return "Text(%s, %s, %s)" % (self ._x , self ._y , repr (self ._text ))
@@ -273,23 +288,6 @@ def update_from(self, other):
273
288
self ._linespacing = other ._linespacing
274
289
self .stale = True
275
290
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
291
def _get_layout (self , renderer ):
294
292
"""
295
293
Return the extent (bbox) of the text together with
@@ -305,16 +303,17 @@ def _get_layout(self, renderer):
305
303
ys = []
306
304
307
305
# Full vertical extent of font, including ascenders and descenders:
308
- _ , lp_h , lp_d = self . _get_text_metrics_with_cache (
306
+ _ , lp_h , lp_d = _get_text_metrics_with_cache (
309
307
renderer , "lp" , self ._fontproperties ,
310
- ismath = "TeX" if self .get_usetex () else False )
308
+ ismath = "TeX" if self .get_usetex () else False , dpi = self . figure . dpi )
311
309
min_dy = (lp_h - lp_d ) * self ._linespacing
312
310
313
311
for i , line in enumerate (lines ):
314
312
clean_line , ismath = self ._preprocess_math (line )
315
313
if clean_line :
316
- w , h , d = self ._get_text_metrics_with_cache (
317
- renderer , clean_line , self ._fontproperties , ismath )
314
+ w , h , d = _get_text_metrics_with_cache (
315
+ renderer , clean_line , self ._fontproperties ,
316
+ ismath = ismath , dpi = self .figure .dpi )
318
317
else :
319
318
w = h = d = 0
320
319
0 commit comments