Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit35442a2

Browse files
committed
TST: Use placeholders for text in layout tests
1 parent662239c commit35442a2

File tree

57 files changed

+3713
-6367
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+3713
-6367
lines changed

‎lib/matplotlib/testing/conftest.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,46 @@ def test_imshow_xarray(xr):
125125

126126
xr=pytest.importorskip('xarray')
127127
returnxr
128+
129+
130+
@pytest.fixture
131+
def_text_placeholders(monkeypatch):
132+
frommatplotlib.patchesimportRectangle
133+
134+
defpatched_get_text_metrics_with_cache(renderer,text,fontprop,ismath,dpi):
135+
"""
136+
Replace ``_get_text_metrics_with_cache`` with fixed results.
137+
138+
The usual ``renderer.get_text_width_height_descent`` would depend on font
139+
metrics; instead the fixed results are based on font size and the length of the
140+
string only.
141+
"""
142+
# While get_window_extent returns pixels and font size is in points, font size
143+
# includes ascenders and descenders. Leaving out this factor and setting
144+
# descent=0 ends up with a box that is relatively close to DejaVu Sans.
145+
height=fontprop.get_size()
146+
width=len(text)*height/1.618# Golden ratio for character size.
147+
descent=0
148+
returnwidth,height,descent
149+
150+
defpatched_text_draw(self,renderer):
151+
"""
152+
Replace ``Text.draw`` with a fixed bounding box Rectangle.
153+
154+
The bounding box corresponds to ``Text.get_window_extent``, which ultimately
155+
depends on the above patched ``_get_text_metrics_with_cache``.
156+
"""
157+
ifrendererisnotNone:
158+
self._renderer=renderer
159+
ifnotself.get_visible():
160+
return
161+
ifself.get_text()=='':
162+
return
163+
bbox=self.get_window_extent()
164+
rect=Rectangle(bbox.p0,bbox.width,bbox.height,
165+
facecolor=(self.get_color(),0.5),edgecolor='none')
166+
rect.draw(renderer)
167+
168+
monkeypatch.setattr('matplotlib.text._get_text_metrics_with_cache',
169+
patched_get_text_metrics_with_cache)
170+
monkeypatch.setattr('matplotlib.text.Text.draw',patched_text_draw)

‎lib/matplotlib/testing/conftest.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ def mpl_test_settings(request: pytest.FixtureRequest) -> None: ...
1010
defpd()->ModuleType: ...
1111
@pytest.fixture
1212
defxr()->ModuleType: ...
13+
def_text_placeholders(monkeypatch:pytest.MonkeyPatch)->None: ...
Binary file not shown.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp