Use CoreText to find macOS fonts by iccir · Pull Request #31977 · matplotlib/matplotlib · GitHub
Skip to content

Use CoreText to find macOS fonts#31977

Open
iccir wants to merge 2 commits into
matplotlib:mainfrom
iccir:coretext-find-fonts
Open

Use CoreText to find macOS fonts#31977
iccir wants to merge 2 commits into
matplotlib:mainfrom
iccir:coretext-find-fonts

Conversation

@iccir

@iccir iccir commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Closes #28249
Closes #31965

Why is this change necessary?

Prior to this change, findSystemFonts() in font_manager.py called system_profiler -xml SPFontsDataType. This subprocess would load 1000+ font files in order to parse localized names. As we only need font paths, we can instead query CoreText directly. This finishes in ~63ms on my machine instead of ~7 seconds.

Differences in found fonts

system_profiler uses the private kCTFontCollectionIncludeInvisibleFontsOption option to find "invisible" fonts. These are fonts with names starting with '.'. These fonts are typically unavailable using the standard macOS font APIs. On my machine (macOS 14.8), the following fonts are "invisible":

  /System/Library/Fonts/ADTNumeric.ttc
  /System/Library/Fonts/AquaKana.ttc
  /System/Library/Fonts/HelveLTMM
  /System/Library/Fonts/Keyboard.ttf
  /System/Library/Fonts/LastResort.otf
  /System/Library/Fonts/NewYork.ttf
  /System/Library/Fonts/NewYorkItalic.ttf
  /System/Library/Fonts/SFArabic.ttf
  /System/Library/Fonts/SFArabicRounded.ttf
  /System/Library/Fonts/SFArmenian.ttf
  /System/Library/Fonts/SFArmenianRounded.ttf
  /System/Library/Fonts/SFCamera.ttf
  /System/Library/Fonts/SFCompact.ttf
  /System/Library/Fonts/SFCompactItalic.ttf
  /System/Library/Fonts/SFCompactRounded.ttf
  /System/Library/Fonts/SFGeorgian.ttf
  /System/Library/Fonts/SFGeorgianRounded.ttf
  /System/Library/Fonts/SFHebrew.ttf
  /System/Library/Fonts/SFHebrewRounded.ttf
  /System/Library/Fonts/SFNS.ttf
  /System/Library/Fonts/SFNSItalic.ttf
  /System/Library/Fonts/SFNSMono.ttf
  /System/Library/Fonts/SFNSMonoItalic.ttf
  /System/Library/Fonts/SFNSRounded.ttf
  /System/Library/Fonts/ThonburiUI.ttc
  /System/Library/Fonts/TimesLTMM

My change uses the CTFontCollectionCreateFromAvailableFonts() without this private option; hence, these fonts will be missing from the list returned by _get_macos_fonts().

However, font_manager.py later re-scans the "/System/Library/Fonts" path, so it should discover these invisible fonts at that time.

I tested this change on macOS Mojave (10.14) and macOS Sonoma (14.8). I was unable to test on 10.12 or 10.13 as I updated my 10.12 test device to 10.14. The CoreText API used has been available since 10.5, so I do not foresee any compatibility issues.

AI Disclosure

The original C-based implementation in #31965 is my own without any usage of AI. I used AI to help me convert it to C++ and return a py::set (my original implementation printed the paths). I used AI to check the resulting files and verify that I was following the correct formatting guidelines.

PR checklist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MNT]: Possibly use CoreText to find fonts for faster performance font finding in OSX produces 200+ lines of warnings

1 participant