如何使用 CTFontRef 获取 FontFormat

发布于 2024-12-01 00:23:01 字数 149 浏览 0 评论 0原文

有旧的碳代码使用 FMGetFontFormat 来确定字体是否为“True Type”。由于该 API 已弃用,有没有办法使用 CTFontRef 找到相同的 API。我使用了 CTFontCopyAttribute,但它返回 CTTypeRef 并且我无法获取该值?有什么建议吗?

There is old carbon code using FMGetFontFormat to determine if the Font is of "True Type". Since the API is deprecated, is there a way to find the same using CTFontRef. I used CTFontCopyAttribute, but it is returning CTTypeRef and i am not able to get the value? Any Suggestions?

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

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

发布评论

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

评论(1

司马昭之心 2024-12-08 00:23:01

CTTypeRef 是通用类型。如果您阅读 kCTFontFormatAttribute 常量的文档,它们会指出:

与此键关联的值是一个整数,表示为
CFNumberRef 对象包含“字体格式”中的常量之一
常数。”

这意味着您需要将该属性视为数字,然后可以将其转换为 short 并根据 CTFontFormat 的已知值进行检查:

//get an array of all the available font names
CFArrayRef fontFamilies = CTFontManagerCopyAvailableFontFamilyNames();

//loop through the array
for(CFIndex i = 0; i < CFArrayGetCount(fontFamilies); i++)
{
    //get the current name
    CFStringRef fontName = CFArrayGetValueAtIndex(fontFamilies, i);

    //create a CTFont with the current font name
    CTFontRef font = CTFontCreateWithName(fontName, 12.0, NULL);

    //ask it for its font format attribute
    CFNumberRef fontFormat = CTFontCopyAttribute(font, kCTFontFormatAttribute);

    //release the font because we're done with it
    CFRelease(font);

    //if there is no format attribute just skip this one
    if(fontFormat == NULL)
    {
        NSLog(@"Could not determine the font format for font named %@.", fontName);
        continue;
    }

    //get the font format as a short
    SInt16 format;
    CFNumberGetValue(fontFormat, kCFNumberSInt16Type, &format);

    //release the number because we're done with it
    CFRelease(fontFormat);

    //create a human-readable string based on the format of the font
    NSString* formatName = nil;
    switch (format) {
        case kCTFontFormatOpenTypePostScript:
            formatName = @"OpenType PostScript";
            break;
        case kCTFontFormatOpenTypeTrueType:
            formatName = @"OpenType TrueType";
            break;
        case kCTFontFormatTrueType:
            formatName = @"TrueType";
            break;
        case kCTFontFormatPostScript:
            formatName = @"PostScript";
            break;
        case kCTFontFormatBitmap:
            formatName = @"Bitmap";
            break;
        case kCTFontFormatUnrecognized:
        default:
            formatName = @"Unrecognized";
            break;
    }
    NSLog(@"Font: '%@' Format: '%@'", fontName, formatName);
}

A CTTypeRef is a generic type. If you read the docs for the kCTFontFormatAttribute constant, they state:

The value associated with this key is an integer represented as a
CFNumberRef object containing one of the constants in “Font Format
Constants.”

That means that you need to treat the attribute as a number, which you can then convert to a short and check it against the known values for CTFontFormat:

//get an array of all the available font names
CFArrayRef fontFamilies = CTFontManagerCopyAvailableFontFamilyNames();

//loop through the array
for(CFIndex i = 0; i < CFArrayGetCount(fontFamilies); i++)
{
    //get the current name
    CFStringRef fontName = CFArrayGetValueAtIndex(fontFamilies, i);

    //create a CTFont with the current font name
    CTFontRef font = CTFontCreateWithName(fontName, 12.0, NULL);

    //ask it for its font format attribute
    CFNumberRef fontFormat = CTFontCopyAttribute(font, kCTFontFormatAttribute);

    //release the font because we're done with it
    CFRelease(font);

    //if there is no format attribute just skip this one
    if(fontFormat == NULL)
    {
        NSLog(@"Could not determine the font format for font named %@.", fontName);
        continue;
    }

    //get the font format as a short
    SInt16 format;
    CFNumberGetValue(fontFormat, kCFNumberSInt16Type, &format);

    //release the number because we're done with it
    CFRelease(fontFormat);

    //create a human-readable string based on the format of the font
    NSString* formatName = nil;
    switch (format) {
        case kCTFontFormatOpenTypePostScript:
            formatName = @"OpenType PostScript";
            break;
        case kCTFontFormatOpenTypeTrueType:
            formatName = @"OpenType TrueType";
            break;
        case kCTFontFormatTrueType:
            formatName = @"TrueType";
            break;
        case kCTFontFormatPostScript:
            formatName = @"PostScript";
            break;
        case kCTFontFormatBitmap:
            formatName = @"Bitmap";
            break;
        case kCTFontFormatUnrecognized:
        default:
            formatName = @"Unrecognized";
            break;
    }
    NSLog(@"Font: '%@' Format: '%@'", fontName, formatName);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文