Download last name (not font) from a font in the file system

I download fonts from the file system.

overall, my code works very well, but since I load fonts through PrivateFontCollection, the name of the loaded FontFamily is the TypeFace name in the font (which I see under the Typeface name in the Windows window preview) and not the FontFamily name ...

Since I have some fonts that have the same Typeface name but a different font name, I would like to distinguish between them. Does anyone have an idea how to get the real name FontFamily?

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; } 
+4
source share
1 answer

If you want to get FontFamily as opposed to the value in the Fonts.Families [0] .Name file, you will find it in family.FamilyNames by culture:

 FontFamily family = new FontFamily(String.Format("file:///{0}#{1}", fi.FullName, fileFonts.Families[0].Name)); Console.WriteLine("\tFamilySource: {0}", family.Source); foreach (var x in family.FamilyNames) { Console.WriteLine("\tFamilyName: {0}", x); // <------ HERE } foreach (var y in family.FamilyTypefaces) { foreach (var z in y.AdjustedFaceNames) { Console.WriteLine("\tTypeface: {0}",z); } } 

Output Example:

 Arial FamilySource: file:///C:\Users\tim\Desktop\arial.ttf#Arial FamilyName: [en-us, Arial] Typeface: [en-us, Regular] Typeface: [en-us, Bold] Typeface: [en-us, Bold Oblique] Typeface: [en-us, Oblique] Arial FamilySource: file:///C:\Users\tim\Desktop\arialbd.ttf#Arial FamilyName: [en-us, Arial] Typeface: [en-us, Bold] Typeface: [en-us, Bold Oblique] Arial FamilySource: file:///C:\Users\tim\Desktop\arialbi.ttf#Arial FamilyName: [en-us, Arial] Typeface: [en-us, Bold Italic] Arial FamilySource: file:///C:\Users\tim\Desktop\ariali.ttf#Arial FamilyName: [en-us, Arial] Typeface: [en-us, Italic] Typeface: [en-us, Bold Italic] Arial Narrow FamilySource: file:///C:\Users\tim\Desktop\ARIALN.TTF#Arial Narrow FamilyName: [en-us, Arial] Typeface: [en-us, Condensed] Typeface: [en-us, Condensed Bold] Typeface: [en-us, Condensed Bold Oblique] Typeface: [en-us, Condensed Oblique] Arial Narrow FamilySource: file:///C:\Users\tim\Desktop\ARIALNB.TTF#Arial Narrow FamilyName: [en-us, Arial] Typeface: [en-us, Condensed Bold] Typeface: [en-us, Condensed Bold Oblique] Arial Narrow FamilySource: file:///C:\Users\tim\Desktop\ARIALNBI.TTF#Arial Narrow FamilyName: [en-us, Arial] Typeface: [en-us, Condensed Bold Italic] Arial Narrow FamilySource: file:///C:\Users\tim\Desktop\ARIALNI.TTF#Arial Narrow FamilyName: [en-us, Arial] Typeface: [en-us, Condensed Italic] Typeface: [en-us, Condensed Bold Italic] Arial Black FamilySource: file:///C:\Users\tim\Desktop\ariblk.ttf#Arial Black FamilyName: [en-us, Arial] Typeface: [en-us, Black] Typeface: [en-us, Black Oblique] 

However, your note that β€œI have some fonts that have the same Typeface name but a different font name and I would like to distinguish between them” suggests that FontFamily is not what you are really looking for .

+3
source

All Articles