4

I have a set of font files with unpredictable filenames, so I can't deduce the real "Font Family" name from the file name. I need to therefore read the font metadata to extract the real "Font Family" name, in order to render this font file. I'm in C#.NET 4.0 WinForms.

I've seen the function GetFontInformation but I can't seem to find the P/Invoke headers for the same. All I have is the C++ version which is honestly hard to figure out. Any ideas?

The reason I cannot use the PrivateFontCollection class to parse through a font file for me, is that these are OTF fonts and .NET/GDI+ only supports TTF fonts!

Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
  • If you have a `Font` object you can access the `FontFamily` property: http://msdn.microsoft.com/en-us/library/system.drawing.font.aspx Will this not work? – Yuck Mar 07 '13 at 18:51
  • You could try this as well, loading a font file using a file-based Uri: http://msdn.microsoft.com/en-us/library/ms602324.aspx – Yuck Mar 07 '13 at 18:52

2 Answers2

5

You need to add font to the (PrivateFontCollection) and then request for the FontFamily and get its Name property.

private static string GetFontNameFromFile(string filename)
{
    PrivateFontCollection fontCollection = new PrivateFontCollection();
    fontCollection.AddFontFile("path_to_font");
    return fontCollection.Families[0].Name;
}

Needed namespace :

using System.Drawing;
using System.Drawing.Text;
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
  • 4
    This method works for **TTF** fonts, but not for **OTF** fonts. – Robin Rodricks Mar 07 '13 at 18:58
  • 2
    @Geotarget That's not true. It works for OTF fonts too. – SepehrM Aug 06 '14 at 09:07
  • 1
    It does'nt work if you try it. That's impossible because .NET does not support OTF, since GDI+ only supports TTF, or something. http://www.codeproject.com/Tips/557423/Rendering-Text-with-OpenType-Fonts-Using-GDI http://stackoverflow.com/questions/6766670/textrenderer-drawtext-using-gdi-to-render-otf-fonts .. search google : https://www.google.co.in/webhp?sourceid=chrome-instant&rlz=1C1CHMO_en-GBIN580IN580&ion=1&espv=2&ie=UTF-8#q=.net+gdi+otf+support&spell=1 – Robin Rodricks Aug 07 '14 at 13:05
  • 2
    I've worked extensively with fonts and I can confirm .NET will not support OTF fonts (or rather, OpenType formatted fonts, sometimes OTF fonts actually have TTF data within, which works) – Robin Rodricks Sep 08 '14 at 12:41
  • Works fine with "FontAwesome.otf", which appears to be an OpenType font. Specifically, Windows font preview says it contains "PostScript Outlines". Other fonts have different combinations, for example "GLYPHICONS Halflings" contains "OpenType Layout, TrueType Outlines". – Triynko Dec 17 '15 at 20:43
2

Just open (Double Click) on one of the font files and you will see something like this:-

enter image description here

M. H.
  • 960
  • 11
  • 13