Reputation: 157
I am reviewing image generation code that uses FontFamily.GenericSansSerif from the system.drawing namespace. I typically see code like that at the tail end of xhtml/css font selection statements as a fall back / last resort if a more desireable (specific) font is not discovered. In the .net framework, what are the environmental parameters that will affect which font this actually selects? Is there some sort of chart out there that states how the .net framework selects the font when you specify GenericSansSerif?
Upvotes: 5
Views: 3519
Reputation: 13141
After a bit of reflecting (reflection?), I think I can explain how this works.
Both FontFamily.GenericSansSerif
and FontFamily.GenericSerif
use an internal constructor that looks up the default font on the system by it's IntPtr
value. In both cases, it passes IntPtr.Zero
which effectively lets GDI+ do the selection (I decided not to go down that particular rabbit hole).
Basically, the FontFamily class is sealed and using pointers, so I wouldn't bother with trying to override those properties. Instead, you could write your own method that mimics the fallback behavior you see in CSS:
public FontFamily DefaultFont(params string[] fonts)
{
// Try to return the first matching font
foreach (var font in fonts)
{
try { return new FontFamily(font); }
catch (ArgumentException) { }
}
// Resort to system default
return new FontFamily(new GenericFontFamilies());
}
Upvotes: 0
Reputation: 35881
In terms of GenericSansSerif
, it will try to return the family from the following fonts by name "Microsoft San Serif", "Arial", "Tahoma" in that order. If none of those fonts are installed, it appears it picks the family of the first installed font, ordered by name.
In terms of GenericSerif
, it tries to return the family from the font named "Times New Roman". If that isn't installed the same rules as GenericSanSerif
are used. i.e. if you remove the "Times New Roman" font, it's the same as calling GenericSanSerif
.
Upvotes: 4