views:

60

answers:

3

I've run into problems on numerous occasions of users not having one of the Windows Core fonts installed on their machine (Courier New, Comic Sans MS, Arial for example). Whenever I try to construct one of these it throws an error rather than falling back onto a related, or really, any other font.

What is the best way to construct a Font if I'd like it to be Courier, but I'm not particularly wedded to it?

+2  A: 

Well, you could use FontFamily.GenericMonospace to get a Monospace font, instead of specifically Courier:

Font f = new Font(FontFamily.GenericMonospace, 12, FontStyle.Regular);

There are a few more such generic font families as well.

You can also use this as a last resort after trying your preferred font and running into an exception.

Joey
Thanks Johannes, I'll need to research this a bit more to see if I can apply it.
David Rutten
+4  A: 
tommieb75
Tommie, thanks for the beautiful response. I'm afraid it won't do me much good though. We're not allowed to distribute all of these fonts, my app is supposed to be bit-depth agnostic and it's even supposed to run on the Mac via Mono. I sincerely hope I won't have to resort to a solution quite this hard-core...
David Rutten
@David: I am not sure if the Mono under the Mac would have PrivateFontCollection....as that's Windows specific, either way, I'd imagine it would be useless....unless there's a more generic way of doing it...in order for pure cross platform to work, no pinvokes and no Win32 specific API's....
tommieb75
+2  A: 

This is very unusual. The Windows font mapper will always provide a substitute font when your program requests a font that isn't available on the target machine. That substitute font will usually be Microsoft Sans Serif on XP, Segoe UI on later versions. Neither of those should ever cause a problem.

Then again, if the machine is so messed up that it doesn't have the core fonts available, it might have no TrueType font available at all. Yes, that will bomb your program. Frankly, this isn't something you should have to deal with, unless this is a really valuable customer. It is otherwise trivially solved by your customer's support team.

Hans Passant
Hi nobugz, it's happened a number of times already, but it seems to be limited to people with 'weird' localizations (Turkish, Chinese, Korean etc.). I was indeed under the impression that the Windows font-fallback logic is supposed to prevent this from crashing, but it clearly isn't. Thanks for the response though, it may just be the best approach to ignore this for now...
David Rutten
Ugh, are they even using a licensed copy of Windows? Something like Wine, maybe. Not your problem.
Hans Passant
nobugz, I have no idea. And if I ask them they'd probably lie about it if they were. Point is, whether or not they stole Windows, they *are* legit customers of ours. The fix isn't all that difficult of course ("Just install Font X"), but it did spark my curiosity about why the code is crashing at all.
David Rutten