@@ -806,11 +806,26 @@ def test_metrics_cache():
806806
807807 fig = plt .figure ()
808808 fig .text (.3 , .5 , "foo\n bar" )
809- fig .text (.5 , .5 , "foo\n bar" )
810809 fig .text (.3 , .5 , "foo\n bar" , usetex = True )
811810 fig .text (.5 , .5 , "foo\n bar" , usetex = True )
812811 fig .canvas .draw ()
812+ renderer = fig ._get_renderer ()
813+ ys = {} # mapping of strings to where they were drawn in y with draw_tex.
814+
815+ def call (* args , ** kwargs ):
816+ renderer , x , y , s , * _ = args
817+ ys .setdefault (s , set ()).add (y )
818+
819+ renderer .draw_tex = call
820+ fig .canvas .draw ()
821+ assert [* ys ] == ["foo" , "bar" ]
822+ # Check that both TeX strings were drawn with the same y-position for both
823+ # single-line substrings. Previously, there used to be an incorrect cache
824+ # collision with the non-TeX string (drawn first here) whose metrics would
825+ # get incorrectly reused by the first TeX string.
826+ assert len (ys ["foo" ]) == len (ys ["bar" ]) == 1
813827
814828 info = mpl .text ._get_text_metrics_with_cache_impl .cache_info ()
815- # Each string gets drawn twice, so the second draw results in a hit.
816- assert info .hits == info .misses
829+ # Every string gets a miss for the first layouting (extents), then a hit
830+ # when drawing, but "foo\nbar" gets two hits as it's drawn twice.
831+ assert info .hits > info .misses
0 commit comments