0

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

我已经从我的桌面上复制了该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)

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

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

4

1 回答 1

0

您必须使用CTFontManagerCreateFontDescriptorsFromURL(fontURL)而不是CTFontDescriptorCreateWithAttributes(@{ (id)kCTFontURLAttribute: fontURL }). 该CTFontManagerCreateFontDescriptorsFromURL函数返回一个对象数组CTFontDescriptorRef

于 2013-04-11T08:27:51.040 回答