forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_font_manager.py
More file actions
452 lines (356 loc) · 15.5 KB
/
test_font_manager.py
File metadata and controls
452 lines (356 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
from io import BytesIO, StringIO
import gc
import multiprocessing
import os
from pathlib import Path
from PIL import Image
import shutil
import sys
import warnings
import numpy as np
import pytest
import matplotlib as mpl
from matplotlib.font_manager import (
findfont, findSystemFonts, FontEntry, FontProperties, fontManager,
json_dump, json_load, get_font, is_opentype_cff_font,
MSUserFontDirectories, ttfFontProperty,
_get_fontconfig_fonts, _normalize_weight)
from matplotlib import cbook, ft2font, pyplot as plt, rc_context, figure as mfigure
from matplotlib.testing import subprocess_run_helper, subprocess_run_for_testing
has_fclist = sys.platform != 'emscripten' and shutil.which('fc-list') is not None
def test_font_priority():
with rc_context(rc={
'font.sans-serif':
['cmmi10', 'Bitstream Vera Sans']}):
fontfile = findfont(FontProperties(family=["sans-serif"]))
assert Path(fontfile).name == 'cmmi10.ttf'
# Smoketest get_charmap, which isn't used internally anymore
font = get_font(fontfile)
cmap = font.get_charmap()
assert len(cmap) == 131
assert cmap[8729] == 30
def test_score_weight():
assert 0 == fontManager.score_weight("regular", "regular")
assert 0 == fontManager.score_weight("bold", "bold")
assert (0 < fontManager.score_weight(400, 400) <
fontManager.score_weight("normal", "bold"))
assert (0 < fontManager.score_weight("normal", "regular") <
fontManager.score_weight("normal", "bold"))
assert (fontManager.score_weight("normal", "regular") ==
fontManager.score_weight(400, 400))
def test_json_serialization(tmp_path):
# Can't open a NamedTemporaryFile twice on Windows, so use a temporary
# directory instead.
json_dump(fontManager, tmp_path / "fontlist.json")
copy = json_load(tmp_path / "fontlist.json")
with warnings.catch_warnings():
warnings.filterwarnings('ignore', 'findfont: Font family.*not found')
for prop in ({'family': 'STIXGeneral'},
{'family': 'Bitstream Vera Sans', 'weight': 700},
{'family': 'no such font family'}):
fp = FontProperties(**prop)
assert (fontManager.findfont(fp, rebuild_if_missing=False) ==
copy.findfont(fp, rebuild_if_missing=False))
def test_otf():
fname = '/usr/share/fonts/opentype/freefont/FreeMono.otf'
if Path(fname).exists():
assert is_opentype_cff_font(fname)
for f in fontManager.ttflist:
if 'otf' in f.fname:
with open(f.fname, 'rb') as fd:
res = fd.read(4) == b'OTTO'
assert res == is_opentype_cff_font(f.fname)
@pytest.mark.skipif(sys.platform == "win32" or not has_fclist,
reason='no fontconfig installed')
def test_get_fontconfig_fonts():
assert len(_get_fontconfig_fonts()) > 1
@pytest.mark.parametrize('factor', [2, 4, 6, 8])
def test_hinting_factor(factor):
font = findfont(FontProperties(family=["sans-serif"]))
font1 = get_font(font, hinting_factor=1)
font1.clear()
font1.set_size(12, 100)
font1.set_text('abc')
expected = font1.get_width_height()
hinted_font = get_font(font, hinting_factor=factor)
hinted_font.clear()
hinted_font.set_size(12, 100)
hinted_font.set_text('abc')
# Check that hinting only changes text layout by a small (10%) amount.
np.testing.assert_allclose(hinted_font.get_width_height(), expected,
rtol=0.1)
def test_utf16m_sfnt():
try:
# seguisbi = Microsoft Segoe UI Semibold
entry = next(entry for entry in fontManager.ttflist
if Path(entry.fname).name == "seguisbi.ttf")
except StopIteration:
pytest.skip("Couldn't find seguisbi.ttf font to test against.")
else:
# Check that we successfully read "semibold" from the font's sfnt table
# and set its weight accordingly.
assert entry.weight == 600
def test_find_ttc():
fp = FontProperties(family=["WenQuanYi Zen Hei"])
if Path(findfont(fp)).name != "wqy-zenhei.ttc":
pytest.skip("Font wqy-zenhei.ttc may be missing")
fig, ax = plt.subplots()
ax.text(.5, .5, "\N{KANGXI RADICAL DRAGON}", fontproperties=fp)
for fmt in ["raw", "svg", "pdf", "ps"]:
fig.savefig(BytesIO(), format=fmt)
def test_find_noto():
fp = FontProperties(family=["Noto Sans CJK SC", "Noto Sans CJK JP"])
name = Path(findfont(fp)).name
if name not in ("NotoSansCJKsc-Regular.otf", "NotoSansCJK-Regular.ttc"):
pytest.skip(f"Noto Sans CJK SC font may be missing (found {name})")
fig, ax = plt.subplots()
ax.text(0.5, 0.5, 'Hello, 你好', fontproperties=fp)
for fmt in ["raw", "svg", "pdf", "ps"]:
fig.savefig(BytesIO(), format=fmt)
def test_find_invalid(tmp_path):
with pytest.raises(FileNotFoundError):
get_font(tmp_path / 'non-existent-font-name.ttf')
with pytest.raises(FileNotFoundError):
get_font(str(tmp_path / 'non-existent-font-name.ttf'))
with pytest.raises(FileNotFoundError):
get_font(bytes(tmp_path / 'non-existent-font-name.ttf'))
# Not really public, but get_font doesn't expose non-filename constructor.
from matplotlib.ft2font import FT2Font
with pytest.raises(TypeError, match='font file or a binary-mode file'):
FT2Font(StringIO()) # type: ignore[arg-type]
@pytest.mark.skipif(sys.platform != 'linux' or not has_fclist,
reason='only Linux with fontconfig installed')
def test_user_fonts_linux(tmpdir, monkeypatch):
font_test_file = 'mpltest.ttf'
# Precondition: the test font should not be available
fonts = findSystemFonts()
if any(font_test_file in font for font in fonts):
pytest.skip(f'{font_test_file} already exists in system fonts')
# Prepare a temporary user font directory
user_fonts_dir = tmpdir.join('fonts')
user_fonts_dir.ensure(dir=True)
shutil.copyfile(Path(__file__).parent / 'data' / font_test_file,
user_fonts_dir.join(font_test_file))
with monkeypatch.context() as m:
m.setenv('XDG_DATA_HOME', str(tmpdir))
_get_fontconfig_fonts.cache_clear()
# Now, the font should be available
fonts = findSystemFonts()
assert any(font_test_file in font for font in fonts)
# Make sure the temporary directory is no longer cached.
_get_fontconfig_fonts.cache_clear()
def test_addfont_as_path():
"""Smoke test that addfont() accepts pathlib.Path."""
font_test_file = 'mpltest.ttf'
path = Path(__file__).parent / 'data' / font_test_file
try:
fontManager.addfont(path)
added, = (font for font in fontManager.ttflist
if font.fname.endswith(font_test_file))
fontManager.ttflist.remove(added)
finally:
to_remove = [font for font in fontManager.ttflist
if font.fname.endswith(font_test_file)]
for font in to_remove:
fontManager.ttflist.remove(font)
@pytest.mark.skipif(sys.platform != 'win32', reason='Windows only')
def test_user_fonts_win32():
if not (os.environ.get('APPVEYOR') or os.environ.get('TF_BUILD')):
pytest.xfail("This test should only run on CI (appveyor or azure) "
"as the developer's font directory should remain "
"unchanged.")
pytest.xfail("We need to update the registry for this test to work")
font_test_file = 'mpltest.ttf'
# Precondition: the test font should not be available
fonts = findSystemFonts()
if any(font_test_file in font for font in fonts):
pytest.skip(f'{font_test_file} already exists in system fonts')
user_fonts_dir = MSUserFontDirectories[0]
# Make sure that the user font directory exists (this is probably not the
# case on Windows versions < 1809)
os.makedirs(user_fonts_dir)
# Copy the test font to the user font directory
shutil.copy(Path(__file__).parent / 'data' / font_test_file, user_fonts_dir)
# Now, the font should be available
fonts = findSystemFonts()
assert any(font_test_file in font for font in fonts)
def _model_handler(_):
fig, ax = plt.subplots()
fig.savefig(BytesIO(), format="pdf")
plt.close()
@pytest.mark.skipif(sys.platform == 'emscripten',
reason='emscripten does not support subprocesses')
@pytest.mark.skipif(not hasattr(os, "register_at_fork"),
reason="Cannot register at_fork handlers")
def test_fork():
_model_handler(0) # Make sure the font cache is filled.
ctx = multiprocessing.get_context("fork")
with ctx.Pool(processes=2) as pool:
pool.map(_model_handler, range(2))
def test_missing_family(caplog):
plt.rcParams["font.sans-serif"] = ["this-font-does-not-exist"]
with caplog.at_level("WARNING"):
findfont("sans")
assert [rec.getMessage() for rec in caplog.records] == [
"findfont: Font family ['sans'] not found. "
"Falling back to DejaVu Sans.",
"findfont: Generic family 'sans' not found because none of the "
"following families were found: this-font-does-not-exist",
]
def _test_threading():
import threading
from matplotlib.ft2font import LoadFlags
import matplotlib.font_manager as fm
def loud_excepthook(args):
raise RuntimeError("error in thread!")
threading.excepthook = loud_excepthook
N = 10
b = threading.Barrier(N)
def bad_idea(n):
b.wait(timeout=5)
for j in range(100):
font = fm.get_font(fm.findfont("DejaVu Sans"))
font.set_text(str(n), 0.0, flags=LoadFlags.NO_HINTING)
threads = [
threading.Thread(target=bad_idea, name=f"bad_thread_{j}", args=(j,))
for j in range(N)
]
for t in threads:
t.start()
for t in threads:
t.join(timeout=9)
if t.is_alive():
raise RuntimeError("thread failed to join")
def test_fontcache_thread_safe():
pytest.importorskip('threading')
subprocess_run_helper(_test_threading, timeout=10)
def test_lockfilefailure(tmp_path):
# The logic here:
# 1. get a temp directory from pytest
# 2. import matplotlib which makes sure it exists
# 3. get the cache dir (where we check it is writable)
# 4. make it not writable
# 5. try to write into it via font manager
proc = subprocess_run_for_testing(
[
sys.executable,
"-c",
"import matplotlib;"
"import os;"
"p = matplotlib.get_cachedir();"
"os.chmod(p, 0o555);"
"import matplotlib.font_manager;"
],
env={**os.environ, 'MPLCONFIGDIR': str(tmp_path)},
check=True
)
def test_fontentry_dataclass():
fontent = FontEntry(name='font-name')
png = fontent._repr_png_()
img = Image.open(BytesIO(png))
assert img.width > 0
assert img.height > 0
html = fontent._repr_html_()
assert html.startswith("<img src=\"data:image/png;base64")
def test_fontentry_dataclass_invalid_path():
with pytest.raises(FileNotFoundError):
fontent = FontEntry(fname='/random', name='font-name')
fontent._repr_html_()
@pytest.mark.skipif(sys.platform == 'win32', reason='Linux or OS only')
def test_get_font_names():
paths_mpl = [cbook._get_data_path('fonts', subdir) for subdir in ['ttf']]
fonts_mpl = findSystemFonts(paths_mpl, fontext='ttf')
fonts_system = findSystemFonts(fontext='ttf')
ttf_fonts = []
for path in fonts_mpl + fonts_system:
try:
font = ft2font.FT2Font(path)
prop = ttfFontProperty(font)
ttf_fonts.append(prop.name)
except Exception:
pass
available_fonts = sorted(list(set(ttf_fonts)))
mpl_font_names = sorted(fontManager.get_font_names())
assert set(available_fonts) == set(mpl_font_names)
assert len(available_fonts) == len(mpl_font_names)
assert available_fonts == mpl_font_names
def test_donot_cache_tracebacks():
class SomeObject:
pass
def inner():
x = SomeObject()
fig = mfigure.Figure()
ax = fig.subplots()
fig.text(.5, .5, 'aardvark', family='doesnotexist')
with BytesIO() as out:
with warnings.catch_warnings():
warnings.filterwarnings('ignore')
fig.savefig(out, format='raw')
inner()
for obj in gc.get_objects():
if isinstance(obj, SomeObject):
pytest.fail("object from inner stack still alive")
def test_fontproperties_init_deprecation():
"""
Test the deprecated API of FontProperties.__init__.
The deprecation does not change behavior, it only adds a deprecation warning
via a decorator. Therefore, the purpose of this test is limited to check
which calls do and do not issue deprecation warnings. Behavior is still
tested via the existing regular tests.
"""
with pytest.warns(mpl.MatplotlibDeprecationWarning):
# multiple positional arguments
FontProperties("Times", "italic")
with pytest.warns(mpl.MatplotlibDeprecationWarning):
# Mixed positional and keyword arguments
FontProperties("Times", size=10)
with pytest.warns(mpl.MatplotlibDeprecationWarning):
# passing a family list positionally
FontProperties(["Times"])
# still accepted:
FontProperties(family="Times", style="italic")
FontProperties(family="Times")
FontProperties("Times") # works as pattern and family
FontProperties("serif-24:style=oblique:weight=bold") # pattern
# also still accepted:
# passing as pattern via family kwarg was not covered by the docs but
# historically worked. This is left unchanged for now.
# AFAICT, we cannot detect this: We can determine whether a string
# works as pattern, but that doesn't help, because there are strings
# that are both pattern and family. We would need to identify, whether
# a string is *not* a valid family.
# Since this case is not covered by docs, I've refrained from jumping
# extra hoops to detect this possible API misuse.
FontProperties(family="serif-24:style=oblique:weight=bold")
def test_normalize_weights():
assert _normalize_weight(300) == 300 # passthrough
assert _normalize_weight('ultralight') == 100
assert _normalize_weight('light') == 200
assert _normalize_weight('normal') == 400
assert _normalize_weight('regular') == 400
assert _normalize_weight('book') == 400
assert _normalize_weight('medium') == 500
assert _normalize_weight('roman') == 500
assert _normalize_weight('semibold') == 600
assert _normalize_weight('demibold') == 600
assert _normalize_weight('demi') == 600
assert _normalize_weight('bold') == 700
assert _normalize_weight('heavy') == 800
assert _normalize_weight('extra bold') == 800
assert _normalize_weight('black') == 900
with pytest.raises(KeyError):
_normalize_weight('invalid')
def test_font_match_warning(caplog):
findfont(FontProperties(family=["DejaVu Sans"], weight=750))
logs = [rec.message for rec in caplog.records]
assert 'findfont: Failed to find font weight 750, now using 700.' in logs
def test_mutable_fontproperty_cache_invalidation():
fp = FontProperties()
assert findfont(fp).endswith("DejaVuSans.ttf")
fp.set_weight("bold")
assert findfont(fp).endswith("DejaVuSans-Bold.ttf")
def test_fontproperty_default_cache_invalidation():
mpl.rcParams["font.weight"] = "normal"
assert findfont("DejaVu Sans").endswith("DejaVuSans.ttf")
mpl.rcParams["font.weight"] = "bold"
assert findfont("DejaVu Sans").endswith("DejaVuSans-Bold.ttf")