diff --git a/.github/workflows/clean_pr.yml b/.github/workflows/clean_pr.yml index 18af5253d58d..05cc4f5039e5 100644 --- a/.github/workflows/clean_pr.yml +++ b/.github/workflows/clean_pr.yml @@ -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 diff --git a/doc/users/next_whats_new/list_font_names.rst b/doc/users/next_whats_new/list_font_names.rst new file mode 100644 index 000000000000..bc7634fd7710 --- /dev/null +++ b/doc/users/next_whats_new/list_font_names.rst @@ -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() \ No newline at end of file diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index 9716e72b5330..cfa5629d30ff 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -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): @@ -1447,3 +1451,4 @@ def _load_fontmanager(*, try_read_cache=True): fontManager = _load_fontmanager() findfont = fontManager.findfont +get_font_names = fontManager.get_font_names diff --git a/lib/matplotlib/tests/test_font_manager.py b/lib/matplotlib/tests/test_font_manager.py index ee62120c1e37..a07c68051c8a 100644 --- a/lib/matplotlib/tests/test_font_manager.py +++ b/lib/matplotlib/tests/test_font_manager.py @@ -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 @@ -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 diff --git a/tutorials/text/text_props.py b/tutorials/text/text_props.py index fba757979c8f..7b0537a3ac26 100644 --- a/tutorials/text/text_props.py +++ b/tutorials/text/text_props.py @@ -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 `) # is controlled by the following rcParams: @@ -209,6 +214,7 @@ # # This is effectively translated to: # matplotlib.rcParams['font.family'] = ['Family1', 'SerifFamily1', 'SerifFamily2', 'Family2'] # +# # Text with non-latin glyphs # ========================== #