Posterous theme by Cory Watilo

Silverlight 4 font woes

This may or may not be a specific RichTextBox problem so I'm going to treat it like it is not.

The situation is that I'm displaying a document with text that can specify any font that it desires.  Of course, you cannot display text in a font that isn't installed.  Silverlight 4 does not give any kind of font list by font name.

Wait! Fonts.SystemFontFaces will give you a list of all fonts installed on the local system.  It will give you a list of font FILE NAMES.  Not a list of actual font names.  There does not seem to be anyway to translate a font file name to a font name.  So that's a big fat dead end.

Supposedly, Silverlight should just fall back on the system default which is "Portable User Interface" which is a best effort on Silverlight's part to display text.  That ought to be fine for me when I'm trying to display a font that isn't installed.

However, that isn't what happens for the RichTextBox.  What does happen is that the RichTextBox decides to have a CATASTROPHIC FAILURE in it's MeasureOverride method when trying to size up the box.  What seemingly happens is that the fallback mechanism just doesn't work.  I've specified a font that doesn't exist and Silverlight blows up.  Actually, it loops the exception for a while then throws up an unhandled crash box thing.  If I try to manually handle the error and give a reasonable size back, then it's an infinite loop :)

The solution is to manually specify a fallback font when I'm setting the FontFamily through code:

new FontFamily(formatting.FontName + ", " + TextFormatting.DefaultFontName);

DefaultFontName currently spits out "Arial" which is no good for non-latin languages.  So I'll have to solve that later.  I had hoped I could simply put "Portable User Interface" to be the default but that doesn't work at all.

If anyone knows of anything better, please comment.