8000 Added font list to fontManager with unit test and note in docs. · matplotlib/matplotlib@02c8086 · GitHub
[go: up one dir, main page]

Skip to content

Commit 02c8086

Browse files
author
ojeda-e
committed
Added font list to fontManager with unit test and note in docs.
1 parent 59732ac commit 02c8086

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

lib/matplotlib/font_manager.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,12 @@ def findfont(self, prop, fontext='ttf', directory=None,
13091309
prop, fontext, directory, fallback_to_default, rebuild_if_missing,
13101310
rc_params)
13111311

1312+
def get_font_list(self):
1313+
"""
1314+
Return list of available fonts.
1315+
"""
1316+
return list(set([font.name for font in self.ttflist]))
1317+
13121318
@lru_cache()
13131319
def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
13141320
rebuild_if_missing, rc_params):
@@ -1447,3 +1453,4 @@ def _load_fontmanager(*, try_read_cache=True):
14471453

14481454
fontManager = _load_fontmanager()
14491455
findfont = fontManager.findfont
1456+
get_font_list = fontManager.get_font_list

lib/matplotlib/tests/test_font_manager.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from matplotlib.font_manager import (
1414
findfont, findSystemFonts, FontProperties, fontManager, json_dump,
1515
json_load, get_font, is_opentype_cff_font, MSUserFontDirectories,
16-
_get_fontconfig_fonts)
16+
_get_fontconfig_fonts, ft2font, ttfFontProperty, cbook)
1717
from matplotlib import pyplot as plt, rc_context
1818

1919
has_fclist = shutil.which('fc-list') is not None
@@ -266,3 +266,21 @@ def test_fontcache_thread_safe():
266266
if proc.returncode:
267267
pytest.fail("The subprocess returned with non-zero exit status "
268268
f"{proc.returncode}.")
269+
270+
def test_get_font_list():
271+
paths_mpl = [cbook._get_data_path('fonts', subdir) for subdir in ['ttf']]
272+
fonts_mpl = findSystemFonts(paths_mpl, fontext='ttf')
273+
fonts_system = findSystemFonts(fontext='ttf')
274+
ttf_fonts = []
275+
for path in fonts_mpl + fonts_system :
276+
try:
277+
font = ft2font.FT2Font(path)
278+
prop = ttfFontProperty(font)
279+
ttf_fonts.append(prop.name)
280+
except:
281+
pass
282+
available_fonts = sorted(list(set(ttf_fonts)))
283+
mpl_font_list = sorted(fontManager.get_font_list())
284+
285+
assert len(available_fonts) == len(mpl_font_list)
286+
assert available_fonts == mpl_font_list

tutorials/text/text_props.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@
208208
#
209209
# # This is effectively translated to:
210210
# matplotlib.rcParams['font.family'] = ['Family1', 'SerifFamily1', 'SerifFamily2', 'Family2']
211+
#
212+
# .. note::
213+
# To access the full list of available fonts: ::
214+
#
215+
# matplotlib.font_manager.get_font_list()
211216
#
212217
# Text with non-latin glyphs
213218
# ==========================

0 commit comments

Comments
 (0)
0