Why doesn't the new FontFamily ("Invalid Font") throw an exception?

Why doesn't the following code throw an exception?

FontFamily font = new FontFamily("bla bla bla"); 

I need to know if a specific font exists in my current OS (like a combination of FontFamily, FontStyle, FontWeight, ...). How can I do it?

+4
source share
4 answers

This is by design. Programs often ask for fonts that are not available on the machine, especially in a country remote from the programmer's place of residence. A font macro creates an alternative. Typically, font replacement is very common. You are looking at Arial right now if you are on a Windows machine. But I can insert δ½ ε₯½ δΈ–η•Œ in this post, and you will see that it is displayed accurately, although Arial does not have glyphs for Chinese characters.

So, hint number one is not really worried about which fonts are available. The Windows api has EnumFontFamiliesEx () for listing the available font families. But this is not shown in WPF, some friction with OpenType is there, a font standard that is pretty poorly integrated with Windows. Another shadow when Adobe takes part in anything Microsoft does.

Some confusion in the comments on the FontFamily Winforms class. This method is actually useful; its GetFamilies () method returns an array of available families. But only TrueType, not OpenType fonts.

+7
source

To answer the question of why it does not throw an exception, according to the FontFamily Constructor on MSDN, an exception was not added until the structure is version 3.5.

I suspect you are targeting version 3.0 or lower.

Hooray!

0
source

You can view available fonts in the System using the Fonts.SystemFontFamilies collection - use Linq to meet any conditions that you need;

 // true bool exists = (from f in Fonts.SystemFontFamilies where f.Source.Equals("Arial") select f).Any(); // false exists = (from f in Fonts.SystemFontFamilies where f.Source.Equals("blahblah") select f).Any(); 
0
source

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


All Articles