@@ -273,30 +273,29 @@ def update_from(self, other):
273273 self ._linespacing = other ._linespacing
274274 self .stale = True
275275
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 ,
288287 )
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 ]
289292
290293 def _get_layout (self , renderer ):
291294 """
292295 Return the extent (bbox) of the text together with
293296 multiple-alignment information. Note that it returns an extent
294297 of a rotated text when necessary.
295298 """
296- key = self ._get_layout_cache_key (renderer = renderer )
297- if key in self ._cached :
298- return self ._cached [key ]
299-
300299 thisx , thisy = 0.0 , 0.0
301300 lines = self .get_text ().split ("\n " ) # Ensures lines is not empty.
302301
@@ -306,16 +305,16 @@ def _get_layout(self, renderer):
306305 ys = []
307306
308307 # 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 ,
311310 ismath = "TeX" if self .get_usetex () else False )
312311 min_dy = (lp_h - lp_d ) * self ._linespacing
313312
314313 for i , line in enumerate (lines ):
315314 clean_line , ismath = self ._preprocess_math (line )
316315 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 )
319318 else :
320319 w = h = d = 0
321320
@@ -439,9 +438,7 @@ def _get_layout(self, renderer):
439438 # now rotate the positions around the first (x, y) position
440439 xys = M .transform (offset_layout ) - (offsetx , offsety )
441440
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
445442
446443 def set_bbox (self , rectprops ):
447444 """
0 commit comments