Skip to content

Commit 4461ad7

Browse files
committed
TST: Add a test for RendererBase.draw_text
This exercises complex shaping, font features, and language support.
1 parent 9059da7 commit 4461ad7

File tree

2 files changed

+52
-29
lines changed

2 files changed

+52
-29
lines changed
66 KB
Loading

lib/matplotlib/tests/test_text.py

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,6 @@ def find_matplotlib_font(**kw):
115115
ax.set_yticks([])
116116

117117

118-
@image_comparison(['complex'], extensions=['png', 'pdf', 'svg', 'eps'], style='mpl20')
119-
def test_complex_shaping():
120-
# Raqm is Arabic for writing; note that because Arabic is RTL, the characters here
121-
# may seem to be in a different order than expected, but libraqm will order them
122-
# correctly for us.
123-
text = (
124-
'Arabic: \N{Arabic Letter REH}\N{Arabic FATHA}\N{Arabic Letter QAF}'
125-
'\N{Arabic SUKUN}\N{Arabic Letter MEEM}')
126-
math_signs = '\N{N-ary Product}\N{N-ary Coproduct}\N{N-ary summation}\N{Integral}'
127-
text = math_signs + text + math_signs
128-
fig = plt.figure(figsize=(6, 2))
129-
fig.text(0.5, 0.75, text, size=32, ha='center', va='center')
130-
# Also check fallback behaviour:
131-
# - English should use cmr10
132-
# - Math signs should use DejaVu Sans Display (and thus be larger than the rest)
133-
# - Arabic should use DejaVu Sans
134-
fig.text(0.5, 0.25, text, size=32, ha='center', va='center',
135-
family=['cmr10', 'DejaVu Sans Display', 'DejaVu Sans'])
136-
137-
138118
@image_comparison(['multiline'], style='mpl20')
139119
def test_multiline():
140120
plt.figure()
@@ -1244,10 +1224,31 @@ def test_text_tightbbox_outside_scale_domain():
12441224
assert not np.isfinite(invalid_bbox.width)
12451225

12461226

1247-
@image_comparison(['features'], remove_text=False, style='mpl20',
1248-
extensions=['png', 'pdf', 'svg', 'eps'])
1249-
def test_text_features():
1250-
fig = plt.figure(figsize=(5, 1.5))
1227+
def _test_complex_shaping(fig):
1228+
# Raqm is Arabic for writing; note that because Arabic is RTL, the characters here
1229+
# may seem to be in a different order than expected, but libraqm will order them
1230+
# correctly for us.
1231+
text = (
1232+
'Arabic: \N{Arabic Letter REH}\N{Arabic FATHA}\N{Arabic Letter QAF}'
1233+
'\N{Arabic SUKUN}\N{Arabic Letter MEEM}')
1234+
math_signs = '\N{N-ary Product}\N{N-ary Coproduct}\N{N-ary summation}\N{Integral}'
1235+
text = math_signs + text + math_signs
1236+
fig.text(0.5, 0.75, text, size=32, ha='center', va='center')
1237+
# Also check fallback behaviour:
1238+
# - English should use cmr10
1239+
# - Math signs should use DejaVu Sans Display (and thus be larger than the rest)
1240+
# - Arabic should use DejaVu Sans
1241+
fig.text(0.5, 0.25, text, size=32, ha='center', va='center',
1242+
family=['cmr10', 'DejaVu Sans Display', 'DejaVu Sans'])
1243+
1244+
1245+
@image_comparison(['complex'], extensions=['png', 'pdf', 'svg', 'eps'], style='mpl20')
1246+
def test_complex_shaping():
1247+
fig = plt.figure(figsize=(6, 2))
1248+
_test_complex_shaping(fig)
1249+
1250+
1251+
def _test_text_features(fig):
12511252
t = fig.text(1, 0.7, 'Default: fi ffi fl st',
12521253
fontsize=32, horizontalalignment='right')
12531254
assert t.get_fontfeatures() is None
@@ -1261,6 +1262,13 @@ def test_text_features():
12611262
assert t.get_fontfeatures() == ('dlig', )
12621263

12631264

1265+
@image_comparison(['features'], remove_text=False, style='mpl20',
1266+
extensions=['png', 'pdf', 'svg', 'eps'])
1267+
def test_text_features():
1268+
fig = plt.figure(figsize=(5, 1.5))
1269+
_test_text_features(fig)
1270+
1271+
12641272
@pytest.mark.parametrize(
12651273
'input, match',
12661274
[
@@ -1275,11 +1283,7 @@ def test_text_language_invalid(input, match):
12751283
Text(0, 0, 'foo', language=input)
12761284

12771285

1278-
@image_comparison(['language'], remove_text=False, style='mpl20',
1279-
extensions=['png', 'pdf', 'svg', 'eps'])
1280-
def test_text_language():
1281-
fig = plt.figure(figsize=(5, 3))
1282-
1286+
def _test_text_language(fig):
12831287
t = fig.text(0, 0.8, 'Default', fontsize=32)
12841288
assert t.get_language() is None
12851289
t = fig.text(0, 0.55, 'Lang A', fontsize=32)
@@ -1317,3 +1321,22 @@ def test_text_language():
13171321
t.set_language((('en', 0, 1), ('smn', 1, 2), ('en', 2, 3), ('smn', 3, 4)))
13181322
assert t.get_language() == (
13191323
('en', 0, 1), ('smn', 1, 2), ('en', 2, 3), ('smn', 3, 4))
1324+
1325+
1326+
@image_comparison(['language'], remove_text=False, style='mpl20',
1327+
extensions=['png', 'pdf', 'svg', 'eps'])
1328+
def test_text_language():
1329+
fig = plt.figure(figsize=(5, 3))
1330+
_test_text_language(fig)
1331+
1332+
1333+
@image_comparison(['draw_text_fallback.png'], style='mpl20')
1334+
def test_draw_text_as_path_fallback(monkeypatch):
1335+
# Delete RendererAgg.draw_text so that we use the RendererBase.draw_text fallback.
1336+
monkeypatch.delattr('matplotlib.backends.backend_agg.RendererAgg.draw_text')
1337+
heights = [2, 1.5, 3]
1338+
fig = plt.figure(figsize=(6, sum(heights)))
1339+
subfig = fig.subfigures(3, 1, height_ratios=heights)
1340+
_test_complex_shaping(subfig[0])
1341+
_test_text_features(subfig[1])
1342+
_test_text_language(subfig[2])

0 commit comments

Comments
 (0)