I'm trying to programatically get a list of installed fonts in C or Python. I need to be able to do this on OS X, does anyone know how?
Do you want to write a program to do it, or do you want to use a program to do it? There are many programs that list fonts, xlsfonts comes to mind.
Not exactly C, but in Objective-C, you can easily get a list of installed fonts via the Cocoa framework:
// This returns an array of NSStrings that gives you each font installed on the system
NSArray *fonts = [[NSFontManager sharedFontManager] availableFontFamilies];
// Does the same as the above, but includes each available font style (e.g. you get
// Verdana, "Verdana-Bold", "Verdana-BoldItalic", and "Verdana-Italic" for Verdana).
NSArray *fonts = [[NSFontManager sharedFontManager] availableFonts];
You can access the Cocoa framework from Python via PyObjC, if you want.
In C, I think you can do something similar in Carbon with the ATSUI library, although I'm not entirely sure how to do this, since I haven't worked with fonts in Carbon before. Nevertheless, from browsing the ATSUI docs, I'd recommend looking into the ATSUGetFontIDs
and the ATSUGetIndFontName
functions. Here's a link to the ATSUI documentation for more information.
You can get an array of available fonts using Objective-C and Cocoa. The method you are looking for is NSFontManager
's availableFonts
.
I don't believe there is a standard way to determine what the system fonts are using pure C. However, you can freely mix C and Objective-C, so it really shouldn't be to hard to use this method to do what you'd like.
Why not use the Terminal?
System Fonts:
ls -R /System/Library/Fonts | grep ttf
User Fonts:
ls -R ~/Library/Fonts | grep ttf
Mac OS X Default fonts:
ls -R /Library/Fonts | grep ttf
If you need to run it inside your C program:
void main()
{
printf("System fonts: ");
execl("/bin/ls","ls -R /System/Library/Fonts | grep ttf", "-l",0);
printf("Mac OS X Default fonts: ");
execl("/bin/ls","ls -R /Library/Fonts | grep ttf", "-l",0);
printf("User fonts: ");
execl("/bin/ls","ls -R ~/Library/Fonts | grep ttf", "-l",0);
}
Python with PyObjC installed (which is the case for Mac OS X 10.5+, so this code will work without having to install anything):
import Cocoa
manager = Cocoa.NSFontManager.sharedFontManager()
font_families = list(manager.availableFontFamilies())
(based on htw's answer)