I’m loading fonts from the filesystem .
generally my code works very well, but as i load the fonts through a PrivateFontCollection the name of the loaded FontFamily is the name of the TypeFace in the font (what i can see under Typeface name in the Font previewer of windows) and not the FontFamily name…
As i have some fonts which have the same Typeface name but another font name i would like to be able to distinguish between them. Anyone has a idea how to get the real FontFamily name?
public Dictionary<string, FontFamily> LoadFontsFromDirectory(string path)
{
Dictionary<string, FontFamily> foundFonts = new Dictionary<string, FontFamily>();
if (!Directory.Exists(path)) throw new Exception("directory doesnt exist");
foreach(FileInfo fi in new DirectoryInfo(path).GetFiles("*.ttf"))
{
PrivateFontCollection fileFonts = new PrivateFontCollection();
fileFonts.AddFontFile(fi.FullName);
if (!foundFonts.ContainsKey(fileFonts.Families[0].Name))
{
//add the font only if this fontfamily doesnt exist yet
FontFamily family = new FontFamily(String.Format("file:///{0}#{1}", fi.FullName, fileFonts.Families[0].Name));
foundFonts.Add(family.Name, family);
}
}
return foundFonts;
}
If you want to get the FontFamily as opposed to the value in fileFonts.Families[0].Name, you’ll find it in family.FamilyNames by culture:
Sample output:
That said, your note that “i have some fonts which have the same Typeface name but another font name and i would like to be able to distinguish between them” suggests that FontFamily is not what you are actually looking for.