为什么新的FontFamily(“无效字体”)不会抛出异常?

时间:2012-10-08 20:33:41

标签: c# wpf fonts font-family

为什么以下代码不会抛出异常?

FontFamily font = new FontFamily("bla bla bla");

我需要知道我当前的操作系统中是否存在特定的font(如FontFamily,FontStyle,FontWeight,...的组合)。我该怎么办?

4 个答案:

答案 0 :(得分:7)

这是设计的。程序经常要求机器上没有的字体,特别是在远离程序员住所的国家。字体映射器生成替代方案。字体替换通常很常见。如果你在Windows机器上,你现在正在看Arial。但是我可以将你好世界粘贴到这篇文章中,你会看到它准确呈现,即使Arial没有汉字字形。

因此,第一个提示实际上并不担心可用的字体。 Windows api具有枚举可用字体系列的EnumFontFamiliesEx()。但这并没有暴露在WPF中,与OpenType存在一些摩擦,这是一种与Windows集成较差的字体标准。当Adobe参与微软所做的任何事情时,另一个影子投射,似乎。

关于Winforms的FontFamily类的评论中有些混淆。在这种情况下实际可用,其GetFamilies()方法返回可用族的数组。但只有TrueType,而不是OpenType字体。

答案 1 :(得分:0)

您可以使用课程System.Drawing.Text.InstalledFontCollection

http://msdn.microsoft.com/en-us/library/system.drawing.text.installedfontcollection.aspx

WPF具有特定于框架的方法Fonts.SystemFontFamilies

http://msdn.microsoft.com/en-us/library/system.windows.media.fonts.systemfontfamilies.aspx

答案 2 :(得分:0)

为了解答为什么它没有抛出异常的问题,根据FontFamily Constructor on MSDN,在框架版本3.5之前没有添加异常。

我怀疑您的目标是3.0或更低版本。

干杯!

答案 3 :(得分:0)

您可以使用Fonts.SystemFontFamilies集合浏览系统上的可用字体 - 使用一些Linq匹配您需要的任何条件;

// true
bool exists = (from f in Fonts.SystemFontFamilies where f.Source.Equals("Arial") select f).Any();

// false
exists = (from f in Fonts.SystemFontFamilies where f.Source.Equals("blahblah") select f).Any();