8000 Merge pull request #21799 from ojeda-e/issue21761 · matplotlib/matplotlib@801d1b7 · GitHub
[go: up one dir, main page]

Skip to content
Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 801d1b7

Browse files
authored
Merge pull request #21799 from ojeda-e/issue21761
Added get_font_names() to fontManager
2 parents 8d85a71 + 242955d commit 801d1b7

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

.github/workflows/clean_pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
base="$(git merge-base "origin/$GITHUB_BASE_REF" 'HEAD^2')"
2626
am="$(git log "$base..HEAD^2" --pretty=tformat: --name-status --diff-filter=AM |
2727
cut --fields 2 | sort | uniq --repeated |
28-
grep -E '.(png|pdf|ps|eps|svg)' || true)"
28+
grep -E '\.(png|pdf|ps|eps|svg)' || true)"
2929
if [[ -n "$am" ]]; then
3030
printf 'The following images were both added and modified in this PR:\n%s\n' "$am"
3131
exit 1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
List of available font names
2+
------------------------------
3+
4+
The list of available fonts are now easily accesible. Get a list of the
5+
available font names in matplotlib with:
6+
7+
.. code-block:: python
8+
9+
from matplotlib import font_manager
10+
font_manager.get_font_names()

lib/matplotlib/font_manager.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,10 @@ 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_names(self):
1313+
"""Return the list of available fonts."""
1314+
return list(set([font.name for font in self.ttflist]))
1315+
13121316
@lru_cache()
13131317
def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
13141318
rebuild_if_missing, rc_params):
@@ -1447,3 +1451,4 @@ def _load_fontmanager(*, try_read_cache=True):
14471451

14481452
fontManager = _load_fontmanager()
14491453
findfont = fontManager.findfont
1454+
get_font_names = fontManager.get_font_names

lib/matplotlib/tests/test_font_manager.py

Lines changed: 20 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,22 @@ 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+
271+
@pytest.mark.skipif(sys.platform == 'win32', reason='Linux or OS only')
272+
def test_get_font_names():
273+
paths_mpl = [cbook._get_data_path('fonts', subdir) for subdir in ['ttf']]
274+
fonts_mpl = findSystemFonts(paths_mpl, fontext='ttf')
275+
fonts_system = findSystemFonts(fontext='ttf')
276+
ttf_fonts = []
277+
for path in fonts_mpl + fonts_system:
278+
try:
279+
font = ft2font.FT2Font(path)
280+
prop = ttfFontProperty(font)
281+
ttf_fonts.append(prop.name)
282+
except:
283+
pass
284+
available_fonts = sorted(list(set(ttf_fonts)))
285+
mpl_font_names = sorted(fontManager.get_font_names())
286+
assert len(available_fonts) == len(mpl_font_names)
287+
assert available_fonts == mpl_font_names

tutorials/text/text_props.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@
179179
# generic-family aliases like (``{'cursive', 'fantasy', 'monospace',
180180
# 'sans', 'sans serif', 'sans-serif', 'serif'}``).
181181
#
182+
# .. note::
183+
# To access the full list of available fonts: ::
184+
#
185+
# matplotlib.font_manager.get_font_names()
186+
#
182187
# The mapping between the generic family aliases and actual font families
183188
# (mentioned at :doc:`default rcParams </tutorials/introductory/customizing>`)
184189
# is controlled by the following rcParams:
@@ -209,6 +214,7 @@
209214
# # This is effectively translated to:
210215
# matplotlib.rcParams['font.family'] = ['Family1', 'SerifFamily1', 'SerifFamily2', 'Family2']
211216
#
217+
#
212218
# Text with non-latin glyphs
213219
# ==========================
214220
#

0 commit comments

Comments
 (0)
0