When using fonts for my application I get no warning/error/exception when a FontFamily I set in a style is not found.
Normally in WPF you get a warning message in your output window telling you you’re binding doesn’t exist. Or you get an exception when your ResourceDictionary can’t be loaded because it’s file can’t be found. However there is absolutely no warning at all when trying to load a FontFamily without a proper name or from a faulty assembly, is there a way to see what font is actually loaded/shown and if it succeeds?
*EDIT:
I used the answer below and changed the TestFont method to the following:
[Conditional("DEBUG")]
private void CheckFontFamily()
{
string fontName = FontFamily.Source.Substring(FontFamily.Source.IndexOf('#') + 1);
if (!FontFamily.FamilyNames.Any(x => x.Value == fontName))
{
bool fontFamilyFound = false;
string baseFontName = fontName;
while (!fontFamilyFound && fontName.Contains(' '))
{
fontName = fontName.Substring(0, fontName.LastIndexOf(' '));
if (FontFamily.FamilyNames.Any(x => x.Value == fontName))
{
fontFamilyFound = true;
string fontFace = baseFontName.Substring(fontName.Length + 1);
if (!FontFamily.FamilyTypefaces.Any(x => x.AdjustedFaceNames.Any(y => y.Value == fontFace)))
{
Console.WriteLine("WARNING: Font '{0}' with TypeFace '{1}' not found at '{2}'.", fontName, fontFace, FontFamily.Source);
}
break;
}
}
if (!fontFamilyFound)
{
Console.WriteLine("WARNING: Font '{0}' not found at '{1}'.", fontName, FontFamily.Source);
}
}
This will show if loading a font like this will work: (and thus check if the path worked)
<TextBlock Text="Lorem Ipsum" FontFamily="{Fonts:FontFamily '/Project;component/Fonts/#ITC Quay Book'}"/>
You could use a markup extension, unfortunately that would require you to use it in every place where you set the
FontFamily.e.g.
The warning would then be displayed in the output window when running in debug (which should satisfy the conditional).
You could get more info from the service provider, e.g. on what kind of object the FontFamily was set and output that as well to aid the debugging.