()
| 840 | |
| 841 | @needs_usetex |
| 842 | def test_metrics_cache(): |
| 843 | # dig into the signature to get the mutable default used as a cache |
| 844 | renderer_cache = inspect.signature( |
| 845 | mpl.text._get_text_metrics_function |
| 846 | ).parameters['_cache'].default |
| 847 | |
| 848 | renderer_cache.clear() |
| 849 | |
| 850 | fig = plt.figure() |
| 851 | fig.text(.3, .5, "foo\nbar") |
| 852 | fig.text(.3, .5, "foo\nbar", usetex=True) |
| 853 | fig.text(.5, .5, "foo\nbar", usetex=True) |
| 854 | fig.canvas.draw() |
| 855 | renderer = fig._get_renderer() |
| 856 | assert renderer in renderer_cache |
| 857 | |
| 858 | ys = {} # mapping of strings to where they were drawn in y with draw_tex. |
| 859 | |
| 860 | def call(*args, **kwargs): |
| 861 | renderer, x, y, s, *_ = args |
| 862 | ys.setdefault(s, set()).add(y) |
| 863 | |
| 864 | renderer.draw_tex = call |
| 865 | fig.canvas.draw() |
| 866 | assert [*ys] == ["foo", "bar"] |
| 867 | # Check that both TeX strings were drawn with the same y-position for both |
| 868 | # single-line substrings. Previously, there used to be an incorrect cache |
| 869 | # collision with the non-TeX string (drawn first here) whose metrics would |
| 870 | # get incorrectly reused by the first TeX string. |
| 871 | assert len(ys["foo"]) == len(ys["bar"]) == 1 |
| 872 | |
| 873 | info = renderer_cache[renderer].cache_info() |
| 874 | # Every string gets a miss for the first layouting (extents), then a hit |
| 875 | # when drawing, but "foo\nbar" gets two hits as it's drawn twice. |
| 876 | assert info.hits > info.misses |
| 877 | |
| 878 | |
| 879 | def test_metrics_cache2(): |
nothing calls this directly
no test coverage detected
searching dependent graphs…