System.Drawing.FontFamily.GenericSansSerif - What affects the main font that it selects?

I am looking at image generation code that uses FontFamily.GenericSansSerif from the system.drawing namespace. Usually I see the code at the tail of xhtml / css font selection commands as a fallback / in extreme cases, if a more desirable (specific) font is not found. In the .net infrastructure, what are the environmental parameters that will affect which font that it actually selects? Is there some kind of diagram where it is indicated how the .net structure selects the font when you specify GenericSansSerif?

+4
source share
2 answers

In terms of GenericSansSerif he will try to return a family of the following fonts named "Microsoft San Serif", "Arial", "Tahoma" in that order. If none of these fonts are installed, it seems that it selects the family of the first installed font, sorted by name.

In terms of GenericSerif he is trying to bring the family back from the Times New Roman font. If this is not installed, the same rules are used as GenericSanSerif . those. if you remove the Times New Roman font, it will be the same as calling GenericSanSerif .

+3
source

After a bit of reflection (reflection?), I think I can explain how it works.

Both FontFamily.GenericSansSerif and FontFamily.GenericSerif use an internal constructor that scans the default font in the system by the value of IntPtr . In both cases, it passes IntPtr.Zero , which effectively allows GDI + to make a choice (I decided not to go down this particular rabbit hole).

In principle, the FontFamily class is also sealed with pointers, so I would not try to override these properties. Instead, you can write your own method that mimics the fallback behavior that 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()); } 
0
source

Source: https://habr.com/ru/post/1384764/


All Articles