Access OTF font

Michael Z picture Michael Z · May 14, 2012 · Viewed 7.4k times · Source

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

Answer

Martin Liversage picture Martin Liversage · May 14, 2012

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.)