OS X / C 已安装字体列表

发布于 2024-07-26 05:35:30 字数 71 浏览 3 评论 0原文

我正在尝试以编程方式获取 C 或 Python 中已安装字体的列表。 我需要能够在 OS X 上执行此操作,有人知道怎么做吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

jJeQQOZ5 2024-08-02 05:35:30

安装了 PyObjC 的 Python(Mac OS X 10.5+ 就是这种情况,因此此代码无需安装任何东西即可运行):(

import Cocoa
manager = Cocoa.NSFontManager.sharedFontManager()
font_families = list(manager.availableFontFamilies())

基于 htw 的答案)

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)

暗地喜欢 2024-08-02 05:35:30

为什么不使用终端?

系统字体:

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);
}

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);
}
盛装女皇 2024-08-02 05:35:30

不完全是 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,如果你愿意的话。

在 C 中,我认为你可以使用 ATSUI 库在 Carbon 中做类似的事情,尽管我不完全确定如何做到这一点,因为我以前没有在 Carbon 中使用过字体。 尽管如此,通过浏览 ATSUI 文档,我建议查看 ATSUGetFontIDsATSUGetIndFontName 函数。 以下是 ATSUI 文档的链接,了解更多信息。

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.

李不 2024-08-02 05:35:30

您想要编写一个程序来完成它,还是想要使用一个程序来完成它? 有很多列出字体的程序,我想到的是 xlsfonts。

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.

抽个烟儿 2024-08-02 05:35:30

您可以使用 Objective-C 和 Cocoa 获取一系列可用字体。 您正在寻找的方法是 NSFontManager可用字体

我不相信有一个标准方法来确定系统字体使用纯 C 语言。但是,您可以自由地混合 C 和 Objective-C,所以使用这种方法来做您想做的事情确实不应该太难想要。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文