苹果公司的文字渲染如何绘制字体所没有的字形? [英] How does Apple's text rendering draw a glyph that a font doesn't have?

查看:185
本文介绍了苹果公司的文字渲染如何绘制字体所没有的字形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对字体和编码有一个基本的了解,但最近我不得不在我的舒适区之外做一些事情:打开字符✖ ( 0x2716 multiplication x)转换为 CGPathRef

我使用了Core Text的 CTFontGetGlyphsForCharacters 来完成这项工作。我知道 CGGlyph 实际上是给定字体支持的字形集合的索引。我很快就发现这个字形只有iOS上很少的字体支持,包括Zapf Dingbats,这是我选择的字体。



这是我的问题:我可以使用✖在任何字体中,只需粘贴它。我在粘贴的页面上做了一个小小的实验✖放入一个文档,选中它,然后不断地改变字体。我做了一个类似的程序实验。字形总是显示正确(并且永远不会改变)。如果这些字体很少有&#x2716 ;,这是如何工作的?如果一个给定的字体没有这样的符号,是否有一些后备字形(我猜会是一个后备字体)?如果是这样,是否有任何方法获得这些字形的编程访问?

解决方案

核心文本中的回退字体的列表是已知的作为级联列表,它是 CTFontDescriptor kCTFontCascadeListAttribute )的一个属性。 b
$ b

系统的默认级联列表可以使用 CTFontCopyDefaultCascadeListForLanguages()来访问。由于某种原因,这个函数在Apple的网站上没有记录,但是这里是来自'CTFont.h'头文件的文档:

  / *! 
@function CTFontCopyDefaultCascadeListForLanguages
@abstract根据给定的语言首选项,返回字体
fallback派生自系统默认回退区域
的CTFontDescriptorRef的有序列表。给定的
的样式也匹配以及字体的重量和宽度不是系统UI字体之一,否则应用
UI字体回退。

@param font
字体参考。

@param languagePrefList
语言首选项列表 - 有序数组
ISO语言代码的CFStringRef。

@result后备字体的有序列表 -
CTFontDescriptors的有序数组。
* /
CFArrayRef CTFontCopyDefaultCascadeListForLanguages(
CTFontRef font,
CFArrayRef languagePrefList)CT_AVAILABLE(10_8,6_0);

在Mac OS X 10.10中,默认英文级联列表包含26种字体,涵盖了多种语言和符号。

您可以通过设置 CTFont 实例自定义级联列表/备用字体自定义 CTFontDescriptor 的属性c $ c> kCTFontCascadeListAttribute 给后备 CTFontDescriptor 对象。然后,用 CTFontCreateWithFontDescriptor()将它变成 CTFont 。如果您不在 CTFontDescriptor 上设置自定义级联列表,则默认情况下将使用全局列表。


I have a basic understanding of fonts and encoding, but recently I had to do something outside of my comfort zone: Turn the character ✖ (0x2716 "heavy multiplication x") into a CGPathRef.

I used Core Text's CTFontGetGlyphsForCharacters to accomplish this job. I understand that a CGGlyph is effectively an index into the set of glyphs supported by the given font. I soon discovered that this glyph is only supported by a very few fonts on iOS, including Zapf Dingbats, which is the one I chose.

Here's my question: I can use ✖ in any font, simply pasting it in. I did a little experiment in Pages where I pasted ✖ into a document, selected it, and then continually changed the font. I did a similar programmatic experiment. The glyph always displayed properly (and never changed). If very few of these fonts has ✖, how does this work? Is there some set of "fallback glyphs" (which I guess would be a "fallback font") if a given font doesn't have such a symbol? If so, is there any way to get programmatic access to these glyphs?

解决方案

A list of fallback fonts in Core Text is known as a "cascade list", which is an attribute of a CTFontDescriptor (the kCTFontCascadeListAttribute).

The system's default cascade list can be accessed using CTFontCopyDefaultCascadeListForLanguages(). For some reason this function isn't documented on Apple's website yet, but here's its documentation from the 'CTFont.h' header file:

/*!
    @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);

On Mac OS X 10.10, the default English cascade list contains 26 fonts that cover a wide set of languages and symbols.

You can customise the cascade list/fallback fonts for your own CTFont instances by setting the kCTFontCascadeListAttribute attribute of a custom CTFontDescriptor to an array of fallback CTFontDescriptor objects. Then, turn it into a CTFont with CTFontCreateWithFontDescriptor(). If you don't set a custom cascade list on your CTFontDescriptor, it will use the global list by default.

这篇关于苹果公司的文字渲染如何绘制字体所没有的字形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆