Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get font name of .ttf file

Tags:

c#

.net

fonts

Lets say there is a .ttf (True Type Font) file. You can install it on windows with a click. The real name of the font is not the text that is before the .tff (lets say SuperFont.ttf => so the name is not "SuperFont" - it could be, but mostly not). I would like to read the .tff (somehow?) and get the name (without installing the font) of the font. Any ideas?

like image 247
sabisabi Avatar asked Aug 09 '12 14:08

sabisabi


People also ask

How to extract font names from TTF/otf files?

Extracting font names from TTF/OTF files using Python and fontTools Raw get_name.py This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

What is a TTF file?

A TTF file consists of several tables; each table represents some data, regarding of its type. Some tables are required; some are not. We actually need only one of them, called “name;” for example, the names table. This is the place where the font information is stored, such as font name, copyright, trademark, and more.

How to get the name of a font family?

You'll need to add the font to a private collection ( PrivateFontCollection ), then request the FontFamily instance and get its Name property. You'll need the namespaces:

How to retrieve font file name from display name?

The solution involves enumerating fonts listed in registry, after first checking Windows OS version. The demo FontFileTest project shows how to retrieve font file name from a font's display name (which is name you will see in MS Word font combobox, for example).


2 Answers

You'll need to add the font to a private collection (PrivateFontCollection), then request the FontFamily instance and get its Name property.

Like this:

PrivateFontCollection fontCol = new PrivateFontCollection();
fontCol.AddFontFile(@"PATH TO FONT");
Console.WriteLine(fontCol.Families[0].Name);

You'll need the namespaces:

using System.Drawing;
using System.Drawing.Text;

MSDN: PrivateFontCollection, FontFamily

like image 142
Matt Razza Avatar answered Nov 05 '22 04:11

Matt Razza


Here is the another code to extract fontname without using System.Drawing dll

foreach (FontFamily fontFamily in Fonts.GetFontFamilies("file:///D:/MyFonts/"))
{
    string name = fontFamily .ToString().Split('#')[fontFamily .ToString().Split('#').Count() - 1];
}
like image 34
Amrit Pal Singh Avatar answered Nov 05 '22 04:11

Amrit Pal Singh