@@ -125,3 +125,54 @@ def test_imshow_xarray(xr):
125
125
126
126
xr = pytest .importorskip ('xarray' )
127
127
return xr
128
+
129
+
130
+ @pytest .fixture
131
+ def text_placeholders (monkeypatch ):
132
+ """
133
+ Replace texts with placeholder rectangles.
134
+
135
+ The rectangle size only depends on the font size and the number of characters. It is
136
+ thus insensitive to font properties and rendering details. This should be used for
137
+ tests that depend on text geometries but not the actual text rendering, e.g. layout
138
+ tests.
139
+ """
140
+ from matplotlib .patches import Rectangle
141
+
142
+ def patched_get_text_metrics_with_cache (renderer , text , fontprop , ismath , dpi ):
143
+ """
144
+ Replace ``_get_text_metrics_with_cache`` with fixed results.
145
+
146
+ The usual ``renderer.get_text_width_height_descent`` would depend on font
147
+ metrics; instead the fixed results are based on font size and the length of the
148
+ string only.
149
+ """
150
+ # While get_window_extent returns pixels and font size is in points, font size
151
+ # includes ascenders and descenders. Leaving out this factor and setting
152
+ # descent=0 ends up with a box that is relatively close to DejaVu Sans.
153
+ height = fontprop .get_size ()
154
+ width = len (text ) * height / 1.618 # Golden ratio for character size.
155
+ descent = 0
156
+ return width , height , descent
157
+
158
+ def patched_text_draw (self , renderer ):
159
+ """
160
+ Replace ``Text.draw`` with a fixed bounding box Rectangle.
161
+
162
+ The bounding box corresponds to ``Text.get_window_extent``, which ultimately
163
+ depends on the above patched ``_get_text_metrics_with_cache``.
164
+ """
165
+ if renderer is not None :
166
+ self ._renderer = renderer
167
+ if not self .get_visible ():
168
+ return
169
+ if self .get_text () == '' :
170
+ return
171
+ bbox = self .get_window_extent ()
172
+ rect = Rectangle (bbox .p0 , bbox .width , bbox .height ,
173
+ facecolor = self .get_color (), edgecolor = 'none' )
174
+ rect .draw (renderer )
175
+
176
+ monkeypatch .setattr ('matplotlib.text._get_text_metrics_with_cache' ,
177
+ patched_get_text_metrics_with_cache )
178
+ monkeypatch .setattr ('matplotlib.text.Text.draw' , patched_text_draw )
0 commit comments