我们的设计更喜欢使用Century Gothic(如果它已安装),但如果没有,则可以回到Arial或Lucidia.这在大多数情况下正常工作,除非机器没有安装世纪的哥特式的,但确实有世纪(这似乎是很多XP的机器没有Office)."有帮助"它代替Century,这对于Century Gothic来说是可怕的,并且完全忽略了我们的后备字体.
通过拥有Century而非Century Gothic的机器可以轻松重现,然后创建一个TextBlock,例如:
<TextBlock FontFamily="Century Gothic, Wingdings">Should be gibberish</TextBlock>
Run Code Online (Sandbox Code Playgroud)
它应该显示为胡言乱语的Wingdings,但它显示在丑陋但可读的世纪.
我试过用复合材料包装Century Gothic,但这有同样的问题:
<FontFamily
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/composite-font"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib">
<FontFamily.FamilyNames>
<System:String x:Key="en-US">CenturyGothic</System:String>
</FontFamily.FamilyNames>
<FontFamily.FamilyTypefaces>
<FamilyTypeface Weight="Normal" Stretch="Normal" Style="Normal" />
<FamilyTypeface Weight="Bold" Stretch="Normal" Style="Normal" />
</FontFamily.FamilyTypefaces>
<FontFamily.FamilyMaps>
<FontFamilyMap Target="Century Gothic" Scale="1" />
</FontFamily.FamilyMaps>
</FontFamily>
Run Code Online (Sandbox Code Playgroud)
嵌入字体不是一种选择(许可) - 有什么方法可以强制它忽略Century,还是我不得不改变它来使用不同的字体?
编辑:我刚刚尝试使用FontFace ="'Century Gothic',Wingdings",但即使安装了它也不会显示Century Gothic.
好吧,目前我有一个可行的解决方案,虽然感觉有点笨拙。我有一个静态帮助器类,如下所示:
namespace FontFallbackTest
{
using System.Linq;
using System.Windows.Markup;
using System.Windows.Media;
public static class FontHelper
{
private static readonly FontFamily testFont;
public static FontFamily TestFont
{
get
{
return testFont;
}
}
static FontHelper()
{
testFont = new FontFamily();
testFont.FamilyNames.Add(XmlLanguage.GetLanguage("en-US"), "TestFont");
if (Fonts.SystemFontFamilies.Any(f => f.FamilyNames.Any(kv => kv.Value == "Century Gothic")))
{
testFont.FamilyMaps.Add(new FontFamilyMap() { Target = "Century Gothic" });
}
testFont.FamilyMaps.Add(new FontFamilyMap() { Target = "Comic Sans MS", Scale = 0.5 });
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我在 XAML 中引用它,如下所示:
<Window x:Class="FontFallbackTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:FontFallbackTest="clr-namespace:FontFallbackTest"
Title="MainWindow" Height="550" Width="525">
<Grid>
<Label Margin="0,100,0,0" FontSize="48" FontFamily="{x:Static FontFallbackTest:FontHelper.TestFont}">This is some text</Label>
<Label Margin="0,150,0,0" FontSize="48" FontFamily="{x:Static FontFallbackTest:FontHelper.TestFont}">This is more text</Label>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
这似乎有效 - 显示世纪哥特式,忽略世纪,并允许我控制后备的字体缩放。