我对字体和编码有基本的了解,但最近我不得不做一些超出我舒适区的事情:将字符 ✖ ( 0x2716
"heavy multiplication x") 变成 CGPathRef
.
我使用了 Core Text 的 CTFontGetGlyphsForCharacters
来完成这项工作。据我所知 CGGlyph
实际上是给定字体支持的字形集的索引。我很快发现 iOS 上只有极少数字体支持这种字形,包括 Zapf Dingbats,我选择了它。
这是我的问题:我可以在任何字体中使用 ✖,只需将其粘贴进去。我在 Pages 中做了一个小实验,将 ✖ 粘贴到文档中,选择它,然后不断更改字体。我做了一个类似的程序化实验。字形始终正确显示(并且从未更改)。如果这些字体中很少有 ✖,这是如何工作的?如果给定的字体没有这样的符号,是否有一些“后备字形”(我猜这将是“后备字体”)?如果是这样,有没有办法以编程方式访问这些字形?
最佳答案
Core Text 中的后备字体列表称为“级联列表”,它是 CTFontDescriptor
的一个属性。 ( kCTFontCascadeListAttribute
)。
可以使用 CTFontCopyDefaultCascadeListForLanguages()
访问系统的默认级联列表.出于某种原因,Apple 的网站上还没有记录此功能,但这里是“CTFont.h”头文件中的文档:
/*!
@function CTFontCopyDefaultCascadeListForLanguages
@abstract Return an ordered list of CTFontDescriptorRef's for font
fallback derived from the system default fallback region
according to the given language preferences. The style of
the given is also matched as well as the weight and width
of the font is not one of the system UI font, otherwise
the UI font fallback is applied.
@param font
The font reference.
@param languagePrefList
The language preference list - ordered array of
CFStringRef's of ISO language codes.
@result The ordered list of fallback fonts - ordered array of
CTFontDescriptors.
*/
CFArrayRef CTFontCopyDefaultCascadeListForLanguages(
CTFontRef font,
CFArrayRef languagePrefList ) CT_AVAILABLE(10_8, 6_0);
在 Mac OS X 10.10 上,默认的英文级联列表包含 26 种字体,涵盖了广泛的语言和符号集。
您可以为自己的级联列表/后备字体自定义
CTFont
实例通过设置 kCTFontCascadeListAttribute
自定义属性 CTFontDescriptor
到后备数组 CTFontDescriptor
对象。然后,把它变成 CTFont
与 CTFontCreateWithFontDescriptor()
.如果您没有在 CTFontDescriptor
上设置自定义级联列表,默认使用全局列表。
关于unicode - Apple 的文本渲染如何绘制字体没有的字形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29069362/