Skip to content

Added get_font_names() to fontManager #21799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/clean_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
base="$(git merge-base "origin/$GITHUB_BASE_REF" 'HEAD^2')"
am="$(git log "$base..HEAD^2" --pretty=tformat: --name-status --diff-filter=AM |
cut --fields 2 | sort | uniq --repeated |
grep -E '.(png|pdf|ps|eps|svg)' || true)"
grep -E '\.(png|pdf|ps|eps|svg)' || true)"
if [[ -n "$am" ]]; then
printf 'The following images were both added and modified in this PR:\n%s\n' "$am"
exit 1
Expand Down
10 changes: 10 additions & 0 deletions doc/users/next_whats_new/list_font_names.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
List of available font names
------------------------------

The list of available fonts are now easily accesible. Get a list of the
available font names in matplotlib with:

.. code-block:: python

from matplotlib import font_manager
font_manager.get_font_names()
5 changes: 5 additions & 0 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,10 @@ def findfont(self, prop, fontext='ttf', directory=None,
prop, fontext, directory, fallback_to_default, rebuild_if_missing,
rc_params)

def get_font_names(self):
"""Return the list of available fonts."""
return list(set([font.name for font in self.ttflist]))

@lru_cache()
def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
rebuild_if_missing, rc_params):
Expand Down Expand Up @@ -1447,3 +1451,4 @@ def _load_fontmanager(*, try_read_cache=True):

fontManager = _load_fontmanager()
findfont = fontManager.findfont
get_font_names = fontManager.get_font_names
21 changes: 20 additions & 1 deletion lib/matplotlib/tests/test_font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from matplotlib.font_manager import (
findfont, findSystemFonts, FontProperties, fontManager, json_dump,
json_load, get_font, is_opentype_cff_font, MSUserFontDirectories,
_get_fontconfig_fonts)
_get_fontconfig_fonts, ft2font, ttfFontProperty, cbook)
from matplotlib import pyplot as plt, rc_context

has_fclist = shutil.which('fc-list') is not None
Expand Down Expand Up @@ -266,3 +266,22 @@ def test_fontcache_thread_safe():
if proc.returncode:
pytest.fail("The subprocess returned with non-zero exit status "
f"{proc.returncode}.")


@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:
pass
available_fonts = sorted(list(set(ttf_fonts)))
mpl_font_names = sorted(fontManager.get_font_names())
assert len(available_fonts) == len(mpl_font_names)
assert available_fonts == mpl_font_names
6 changes: 6 additions & 0 deletions tutorials/text/text_props.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@
# generic-family aliases like (``{'cursive', 'fantasy', 'monospace',
# 'sans', 'sans serif', 'sans-serif', 'serif'}``).
#
# .. note::
# To access the full list of available fonts: ::
#
# matplotlib.font_manager.get_font_names()
#
# The mapping between the generic family aliases and actual font families
# (mentioned at :doc:`default rcParams </tutorials/introductory/customizing>`)
# is controlled by the following rcParams:
Expand Down Expand Up @@ -209,6 +214,7 @@
# # This is effectively translated to:
# matplotlib.rcParams['font.family'] = ['Family1', 'SerifFamily1', 'SerifFamily2', 'Family2']
#
#
# Text with non-latin glyphs
# ==========================
#
Expand Down