Skip to content

Query macOS for available system fonts. #27230

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 1 commit into from
Jan 12, 2024
Merged
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
12 changes: 11 additions & 1 deletion lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from numbers import Number
import os
from pathlib import Path
import plistlib
import re
import subprocess
import sys
Expand Down Expand Up @@ -261,6 +262,14 @@ def _get_fontconfig_fonts():
return [Path(os.fsdecode(fname)) for fname in out.split(b'\n')]


@lru_cache
def _get_macos_fonts():
"""Cache and list the font paths known to ``system_profiler SPFontsDataType``."""
d, = plistlib.loads(
subprocess.check_output(["system_profiler", "-xml", "SPFontsDataType"]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this output portable across different system languages?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a quick check, the answer appears to be yes.

return [Path(entry["path"]) for entry in d["_items"]]


def findSystemFonts(fontpaths=None, fontext='ttf'):
"""
Search for fonts in the specified font paths. If no paths are
Expand All @@ -279,6 +288,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
else:
installed_fonts = _get_fontconfig_fonts()
if sys.platform == 'darwin':
installed_fonts += _get_macos_fonts()
fontpaths = [*X11FontDirectories, *OSXFontDirectories]
else:
fontpaths = X11FontDirectories
Expand Down Expand Up @@ -1011,7 +1021,7 @@ class FontManager:
# Increment this version number whenever the font cache data
# format or behavior has changed and requires an existing font
# cache files to be rebuilt.
__version__ = 330
__version__ = 390

def __init__(self, size=None, weight='normal'):
self._version = self.__version__
Expand Down