为什么在请求 Myriad Pro 的粗体版本时 Core Text 返回 Myriad Pro Semibold
我安装了常见的 Adobe Myriad Pro 字体。其中包括 Myriad Pro Regular、Myriad Pro Bold 和 Myriad Pro Semibold。假设我有一个指向 Myriad Pro Regular 的 CTFontRef baseFont
,并且我想要的字体大小是 size
。我运行以下代码:
CTFontRef boldFont = CTFontCreateCopyWithSymbolicTraits(baseFont, size, NULL, kCTFontBoldTrait, kCTFontBoldTrait);
返回的字体是 Myriad Pro Semibold,而不是 Myriad Pro Bold。
除了请求命名样式“Bold”之外,是否有办法强制返回 Myriad Pro Bold?我想让这段代码完全通用,而不需要硬连接样式名称。
我已经尝试了各种排列,包括在最初创建字体时将粗体特征作为属性字典的一部分传递,避免了此处描述的两步过程,但它仍然优先于正常粗体返回半粗体字体。我还对字体本身进行了一些研究。全粗体字体在其
表中的粗细为 700,半粗体字体的粗细为 600。PANOSE 粗细与此相对应。但是,半粗体和粗体字体的 表中的
macStyle
字段都设置了粗体标志,因此大概这就是 Core Text 使用的内容。但有什么办法可以让它更具辨别力吗?
I have the common Adobe Myriad Pro fonts installed. These include Myriad Pro Regular, Myriad Pro Bold and Myriad Pro Semibold. Assume that I have a CTFontRef baseFont
that points to Myriad Pro Regular, and that the font size I desire is size
. I run the following code:
CTFontRef boldFont = CTFontCreateCopyWithSymbolicTraits(baseFont, size, NULL, kCTFontBoldTrait, kCTFontBoldTrait);
The returned font is Myriad Pro Semibold, not Myriad Pro Bold.
Is there a way of coercing this to return Myriad Pro Bold instead, other than requesting the named style 'Bold'? I wanted to keep this code entirely generic without hard-wiring style names.
I have tried this in various permutations, including passing the bold trait as part of an attribute dictionary when I initially create my font, avoiding the two-step process described here, but it still returns the semibold font in preference to the normal bold. I've also poked around the fonts themselves a little. The full bold font has a weight of 700 in its <OS/2>
table, and the semibold font has a weight of 600. The PANOSE weights correspond with this. However, the macStyle
fields in the <head>
table of the semibold and bold fonts both have the bold flag set, so presumably this is what Core Text is using. But is there any way to make it more discriminating?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据对文档的阅读,并以一般的字体处理知识(但不是具体的核心文本知识)为基础,我认为这可能是可能的,但并不简单。
CTFontCreateCopyWithSymbolicTraits()
文档指定symTraitValue
和symTraitMask
参数具有类型CTFontSymbolicTraits
。CTFontDescriptor()
文档 定义您使用的“粗体”值,所以这个显然是一个布尔特征。然而,正如您所看到的,字体粗细是一个范围,而不是一个布尔特征,尽管数十年来文字处理器用户界面中的“粗体”按钮已将其呈现为布尔特征。
CTFontCreateCopyWithSymbolicTraits()
不具备您所需的表达能力。另一种可能有效的方法是尝试调用
CTFontDescriptorCreateMatchingFontDescriptors()
。您向此函数传递一个CTFontDescriptorRef
到初始字体,以及一个带有必须存在的属性的CFSetRef
。该函数返回一个字体描述符数组,所有这些描述符都与您请求的属性相匹配。因此,您可以向其传递 Myriad Pro Regular 的
CTFontDescriptorRef
,也可能传递一个CFSetRef
表示您想要粗体,然后查看返回数组中的每个字体描述符以查找重量最重的一个。我还没有编写这段代码,而且我对核心文本的无知意味着我可能会遗漏一些东西,但这似乎是一种合理的方法。
Based on a reading of the documentation, backed up by some knowledge of font handling in general but not Core Text specifically, I'd say it may be possible, but it's not straightforward.
The
CTFontCreateCopyWithSymbolicTraits()
documentation specifies that thesymTraitValue
andsymTraitMask
parameters have typeCTFontSymbolicTraits
. TheCTFontDescriptor()
documentation defines that "Bold" value that you are using asSo this is clearly a boolean trait. However, as you've seen, font weight is a spectrum, not a boolean trait, even though decades of "bold" buttons in word processor UIs have presented it as a boolean trait.
CTFontCreateCopyWithSymbolicTraits()
doesn't have the expressive power you need.One other approach which might work is to try calling
CTFontDescriptorCreateMatchingFontDescriptors()
. You pass this function aCTFontDescriptorRef
to an initial font, and aCFSetRef
with attributes which must be present. This function returns an array of font descriptors, all of which match the attributes you requested.So, you could pass it a
CTFontDescriptorRef
for Myriad Pro Regular, and maybe aCFSetRef
saying you want bold, and then look through every font descriptor in the returned array to find the one with the heaviest weight.I haven't written this code, and my ignorance of Core Text means I may be missing something, but that seems like a plausible approach.
对于
CTFontDescriptor
,您可以指定一个属性kCTFontTraitsAttribute
,它应该是一个CFDictionaryRef
,您可以在其中指定kCTFontWeightTrait
,它采用一个CFNumberRef
表示 -1 和 1 之间的浮点数,为您提供一系列权重,1 是最粗体的变体,0 是最粗体的变体常规/中等。For the
CTFontDescriptor
you can specify an attributekCTFontTraitsAttribute
which should be anCFDictionaryRef
where you can specify thekCTFontWeightTrait
which takes aCFNumberRef
that represents floating point between -1 and 1, giving you a spectrum of weights, 1 being the most bold variant, and 0 being the regular/medium.