如何在OS X上检索未安装字体的属性?

时间:2013-04-11 08:17:43

标签: macos fonts core-text

我正在尝试读取已卸载的字体的属性(字体名称,字体格式等)。

我已将Menlo.ttc文件从/System/Library/Fonts/Menlo.ttc复制到桌面上,我已下载并安装了Caviar Dreams字体。

我尝试了以下代码:

#import <Foundation/Foundation.h>
#import <CoreText/CoreText.h>

static void printFontInfo(NSURL *fontURL)
{
    NSLog(@"%@", [fontURL path]);
    NSLog(@"    file size: %lld", [[[NSFileManager defaultManager] attributesOfItemAtPath:[fontURL path] error:NULL] fileSize]);

    NSDictionary *attributes = @{ (id)kCTFontURLAttribute: fontURL };
    CTFontDescriptorRef fontDescriptor = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attributes);
    CFNumberRef format = CTFontDescriptorCopyAttribute(fontDescriptor, kCTFontFormatAttribute);
    CFStringRef fontName = CTFontDescriptorCopyAttribute(fontDescriptor, kCTFontNameAttribute);
    CFStringRef displayName = CTFontDescriptorCopyAttribute(fontDescriptor, kCTFontDisplayNameAttribute);
    NSLog(@"    fontDescriptor: %p", fontDescriptor);
    NSLog(@"    format: %@", format);
    NSLog(@"    font name: %@", fontName);
    NSLog(@"    display name: %@", displayName);
}

int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSURL *desktopURL = [fileManager URLForDirectory:NSDesktopDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:NULL];
        NSURL *downloadsURL = [fileManager URLForDirectory:NSDownloadsDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:NULL];
        NSURL *fontsURL = [[fileManager URLForDirectory:NSLibraryDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:NULL] URLByAppendingPathComponent:@"Fonts"];

        NSURL *menloURL = [NSURL fileURLWithPath:@"/System/Library/Fonts/Menlo.ttc"];
        NSURL *copiedMenloURL = [desktopURL URLByAppendingPathComponent:@"Menlo.ttc"];
        NSURL *installedCaviarDreamsURL = [fontsURL URLByAppendingPathComponent:@"CaviarDreams.ttf"];
        NSURL *downloadedCaviarDreamsURL = [downloadsURL URLByAppendingPathComponent:@"caviar_dreams/CaviarDreams.ttf"];

        printFontInfo(menloURL);
        printFontInfo(copiedMenloURL);
        printFontInfo(installedCaviarDreamsURL);
        printFontInfo(downloadedCaviarDreamsURL);
    }
    return 0;
}

运行此代码会产生以下结果:

/System/Library/Fonts/Menlo.ttc
    file size: 2144600
    fontDescriptor: 0x100111550
    format: 3
    font name: Menlo-Regular
    display name: Menlo Regular
/Users/0xced/Desktop/Menlo.ttc
    file size: 2144600
    fontDescriptor: 0x100117fa0
    format: (null)
    font name: (null)
    display name: (null)
/Users/0xced/Library/Fonts/CaviarDreams.ttf
    file size: 58864
    fontDescriptor: 0x100118f30
    format: 3
    font name: CaviarDreams
    display name: Caviar Dreams
/Users/0xced/Downloads/caviar_dreams/CaviarDreams.ttf
    file size: 58864
    fontDescriptor: 0x1001186c0
    format: (null)
    font name: (null)
    display name: (null)

我们看到我们可以在安装字体时(在/System/Library/Fonts~/Library/Fonts内)读取属性,但是当字体不在系统字体目录中时CTFontDescriptorCopyAttribute函数会失败

有没有办法在未安装字体时读取字体属性

1 个答案:

答案 0 :(得分:0)

您必须使用CTFontManagerCreateFontDescriptorsFromURL(fontURL)代替CTFontDescriptorCreateWithAttributes(@{ (id)kCTFontURLAttribute: fontURL })CTFontManagerCreateFontDescriptorsFromURL函数返回CTFontDescriptorRef个对象的数组。