@@ -273,30 +273,29 @@ def update_from(self, other):
273
273
self ._linespacing = other ._linespacing
274
274
self .stale = True
275
275
276
- def _get_layout_cache_key (self ,renderer ):
277
- """
278
- Return a hashable tuple of properties that lets `_get_layout` know
279
- whether a previously computed layout can be reused.
280
- """
281
- return (
282
- self .get_unitless_position (),self .get_text (),
283
- hash (self ._fontproperties ),
284
- self ._verticalalignment ,self ._horizontalalignment ,
285
- self ._linespacing ,
286
- self ._rotation ,self ._rotation_mode ,self ._transform_rotates_text ,
287
- self .figure .dpi ,weakref .ref (renderer ),
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 ,
288
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 ]
289
292
290
293
def _get_layout (self ,renderer ):
291
294
"""
292
295
Return the extent (bbox) of the text together with
293
296
multiple-alignment information. Note that it returns an extent
294
297
of a rotated text when necessary.
295
298
"""
296
- key = self ._get_layout_cache_key (renderer = renderer )
297
- if key in self ._cached :
298
- return self ._cached [key ]
299
-
300
299
thisx ,thisy = 0.0 ,0.0
301
300
lines = self .get_text ().split ("\n " )# Ensures lines is not empty.
302
301
@@ -306,16 +305,16 @@ def _get_layout(self, renderer):
306
305
ys = []
307
306
308
307
# Full vertical extent of font, including ascenders and descenders:
309
- _ ,lp_h ,lp_d = renderer . get_text_width_height_descent (
310
- "lp" ,self ._fontproperties ,
308
+ _ ,lp_h ,lp_d = self . _get_text_metrics_with_cache (
309
+ renderer , "lp" ,self ._fontproperties ,
311
310
ismath = "TeX" if self .get_usetex ()else False )
312
311
min_dy = (lp_h - lp_d )* self ._linespacing
313
312
314
313
for i ,line in enumerate (lines ):
315
314
clean_line ,ismath = self ._preprocess_math (line )
316
315
if clean_line :
317
- w ,h ,d = renderer . get_text_width_height_descent (
318
- clean_line ,self ._fontproperties ,ismath = ismath )
316
+ w ,h ,d = self . _get_text_metrics_with_cache (
317
+ renderer , clean_line ,self ._fontproperties ,ismath )
319
318
else :
320
319
w = h = d = 0
321
320
@@ -439,9 +438,7 @@ def _get_layout(self, renderer):
439
438
# now rotate the positions around the first (x, y) position
440
439
xys = M .transform (offset_layout )- (offsetx ,offsety )
441
440
442
- ret = bbox ,list (zip (lines ,zip (ws ,hs ),* xys .T )),descent
443
- self ._cached [key ]= ret
444
- return ret
441
+ return bbox ,list (zip (lines ,zip (ws ,hs ),* xys .T )),descent
445
442
446
443
def set_bbox (self ,rectprops ):
447
444
"""