8
votes

I try to use OTF font installed on my system but I failed:

FontFamily family = new FontFamily(name_for_my_font);

I have tried to list all my fonts in system and indeed I haven't seen required font:

foreach (FontFamily font in System.Drawing.FontFamily.Families)
{
Console.WriteLine(font);
}

But I can see my font in Fonts folder. It has OTF extension. Maybe that is the problem? I can see only TTF fonts. But why? How can I access OTF font from my program?

UPDATE: Sorry the problem is with OTF access but not TTF! I have made corrections in my answer

2
It is the other way around, System.Drawing can handle only TrueType fonts, not OpenType fonts.Hans Passant
wow! I haven't no idea about that. So how can I access my OTF fonts?Michael Z
There's an easy and a hard solution for that. The easy one is stop trying to use OpenType fonts because they are never going to work in a Winforms app. The hard one is to throw away everything you have and switch to WPF.Hans Passant

2 Answers

9
votes

The problem is that OpenType fonts come in several flavors, one based on TrueType outlines and another based on PostScript Type 1 outlines. The Windows GDI+ graphics API does not support PostScript type 1 outlines. This is kind of ironic as the OpenType font format was created by Microsoft as a result of not being able to license some font technology from Apple.

So if a Windows application is based on GDI+ it will not be able to render OpenType fonts having PostScript type 1 outlines or PostScript type 1 fonts for that matter. Unfortunately System.Drawing is implemented using GDI+. Your only option is to stick to fonts using TrueType outlines.

(Or, if you are really desparate, you can P/Invoke to "classic" GDI and render the font into a bitmap.)