已安装字体列表 OS X / C
我想通过编程的方式获取已安装字体的列表,使用的是C语言或Python。我需要在OS X系统上做到这一点,有人知道怎么做吗?
5 个回答
4
虽然不是C语言,但在Objective-C中,你可以很简单地通过Cocoa框架获取已安装字体的列表:
// 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];
如果你想的话,可以通过PyObjC在Python中访问Cocoa框架。
在C语言中,我觉得可以通过Carbon的ATSUI库做类似的事情,虽然我不太确定具体怎么做,因为我之前没有在Carbon中处理过字体。不过,从我浏览ATSUI文档的经验来看,我建议你看看ATSUGetFontIDs
和ATSUGetIndFontName
这两个函数。想了解更多信息,可以查看ATSUI文档。
9
为什么不使用终端呢?
系统字体:
ls -R /System/Library/Fonts | grep ttf
用户字体:
ls -R ~/Library/Fonts | grep ttf
Mac OS X 默认字体:
ls -R /Library/Fonts | grep ttf
如果你需要在你的C程序里运行它:
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);
}
14
在安装了PyObjC的Python环境下(这在Mac OS X 10.5及以上版本中是默认的,所以这段代码可以直接使用,不需要额外安装任何东西):
import Cocoa
manager = Cocoa.NSFontManager.sharedFontManager()
font_families = list(manager.availableFontFamilies())
(这个内容是基于htw的回答)