检测字体是否支持变体(如上标和下标) [英] Detect whether a font supports variants (like superscript and subscript)

查看:193
本文介绍了检测字体是否支持变体(如上标和下标)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF有 Typography.Variants 附加的属性,让你做上标和下标。但是,它只适用于某些字体。对于其他字体,完全忽略该变体,文本显示正常。 (代码示例和截图此处

既然它悄无声息地回落到一个空洞的地方,我不知道什么地方出了问题。但我的用户会看到糟糕的行为。

有没有什么办法可以以编程方式检测给定的字体是否支持Typography.Variants?如果是这样,如果用户为需要上标/下标的东西选择了不支持变体的字体,那么我可以提供更有意义的行为。 我看了字形字体,因为它是一个用来查询字体是否可以被嵌入,但我没有看到有关变体的任何东西。我也没有在 FontFamily a>,而我唯一能找到的就是排版是Variants附加属性本身(及其getter和setter)。

解决方案

据我所知, WPF没有提供关于可用的GSUB表(告诉你这个信息)的信息。一切都隐藏在PresentationCore的私有类中。

一种方法是使用WPF的高级文本服务创建一个 TextFormatter ,然后检索 GlyphRun s通过一段文字创建的变体,一个与变体关闭,然后比较使用的字形索引。



另一种方法是通过 GlyphTypeFace.GetFontStream()来物理检查字体的数据。 TrueType字体格式不是很复杂,所以你可能会在网上找到一些关于如何解析二进制字体数据来查找GSUB表信息的信息。



请注意,简单的要求支持一个变体也是有点含糊不清。字体可以说它支持一个变体,但没有任何要求它提供任何有意义的替换。大多数的Adobe字体只提供上下标(甚至不包括整个拉丁字母表)的字母小写字母。这是相当无用的,恕我直言,因为你不能要求WPF伪造下标或像Word和其他文字处理器这样的上标。



不过,这将是很好,如果你可以直接询问 TypeFace.GetSupportedOpenTypeFeatures()


WPF has the Typography.Variants attached property that lets you do superscript and subscript. However, it only works for some fonts. For other fonts, the variant is utterly ignored, and the text is shown normally. (Code sample and screenshot here)

Since it silently falls back to a no-op, I have no idea that anything went wrong; but my user will see lousy behavior.

Is there any way that I can programmatically detect whether a given font supports Typography.Variants? If so, I could provide more meaningful behavior if the user selected a non-variant-supporting font for something that needs superscripts/subscripts.

I looked at GlyphTypeface, since it's the one you use to query whether a font can be embedded, but I didn't see anything there about variants. I also didn't see anything obvious on FontFamily, and the only thing I could find on Typography was the Variants attached property itself (and its getters and setters).

解决方案

As far as I can tell, WPF provides no information about the available GSUB tables (which tell you this information). Everything is hidden deep within private classes of PresentationCore.

One way would be to use the advanced text services of WPF to create a TextFormatter, and then retrieve the GlyphRuns created by a piece of text with the variants on, and one with the variants off, and then compare the glyph indexes used.

Another way would be to physically examine a font's data through GlyphTypeFace.GetFontStream(). The TrueType font format is not very complicated, so you'll probably find some information on the net on how to parse the binary font data to find information on the GSUB tables.

Note that simply asking wither a variant is supported is also a little ambiguous. A font can say it supports a variant, but nothing requires it to actually provide any meaningful substitutions. Most Adobe fonts provide only a few alphabetical lowercase characters for things like superscript and subscript (not even the entire Latin alphabet, mind you). Which is pretty useless, IMHO, since you can't ask WPF to fake subscripts or superscripts like Word and other word processors do.

Still, it would have been nice if you could simply ask TypeFace.GetSupportedOpenTypeFeatures().

这篇关于检测字体是否支持变体(如上标和下标)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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