public static void SetupFontsFiles(string[] sSupportedFonts) { List <FontFileInfo> tempFontInfoList = new List <FontFileInfo>(); foreach (string fontPathFile in sSupportedFonts) { try { FontFileInfo fontInfo = FontFileInfo.Load(fontPathFile); Debug.WriteLine(fontPathFile); tempFontInfoList.Add(fontInfo); } catch (System.Exception e) { System.Console.Error.WriteLine(e); } } // Deserialize all font families foreach (IGrouping <string, FontFileInfo> familyGroup in tempFontInfoList.GroupBy(info => info.FamilyName)) { try { string familyName = familyGroup.Key; FontFamilyModel family = DeserializeFontFamily(familyName, familyGroup); InstalledFonts.Add(familyName.ToLower(), family); } catch (System.Exception e) { System.Console.Error.WriteLine(e); } } }
private static FontFamilyModel DeserializeFontFamily(string fontFamilyName, IEnumerable <FontFileInfo> fontList) { FontFamilyModel font = new FontFamilyModel { Name = fontFamilyName }; // there is only one font if (fontList.Count() == 1) { font.FontFiles.Add(XFontStyle.Regular, fontList.First().Path); } else { foreach (FontFileInfo info in fontList) { XFontStyle style = info.GuessFontStyle(); if (!font.FontFiles.ContainsKey(style)) { font.FontFiles.Add(style, info.Path); } } } return(font); }
private static FontFamilyModel DeserializeFontFamily(KeyValuePair <string, List <string> > fontFamily) { var font = new FontFamilyModel { Name = fontFamily.Key }; var fontList = fontFamily.Value; // there is only one font if (fontFamily.Value.Count == 1) { font.FontFiles.Add(XFontStyle.Regular, fontFamily.Value[0]); return(font); } // if element filenames have diff. lengths -> shortest name is regular // skip this check if Regular font has 'regular' sufix, because 'fontName-bold' shorter that 'fontName-regular' if (fontList.Any(e => e.Length != fontList[0].Length) && fontList.All(fontFileName => !Path.GetFileNameWithoutExtension(fontFileName)?.ToLower().Contains("regular") ?? true)) { var orderedList = fontList.OrderBy(o => o.Length); font.FontFiles.Add(XFontStyle.Regular, orderedList.First()); foreach (var elem in orderedList.Skip(1)) { var pair = DeserializeFontName(elem); if (!font.FontFiles.ContainsKey(pair.Key)) { font.FontFiles.Add(pair.Key, pair.Value); } } return(font); } // else foreach (var elem in fontList) { var pair = DeserializeFontName(elem); if (!font.FontFiles.ContainsKey(pair.Key)) { font.FontFiles.Add(pair.Key, pair.Value); } } return(font); }
private static FontFamilyModel DeserializeFontFamily(KeyValuePair <string, List <string> > fontFamily) { var font = new FontFamilyModel { Name = fontFamily.Key }; var fontList = fontFamily.Value; // there is only one font if (fontFamily.Value.Count == 1) { font.FontFiles.Add(XFontStyle.Regular, fontFamily.Value[0]); return(font); } // if element filenames have diff. lengths -> shortest name is regular if (fontList.Any(e => e.Length != fontList[0].Length)) { var orderedList = fontList.OrderBy(o => o.Length); font.FontFiles.Add(XFontStyle.Regular, orderedList.First()); foreach (var elem in orderedList.Skip(1)) { var pair = DeserializeFontName(elem); if (!font.FontFiles.ContainsKey(pair.Key)) { font.FontFiles.Add(pair.Key, pair.Value); } } return(font); } // else foreach (var elem in fontList) { var pair = DeserializeFontName(elem); if (!font.FontFiles.ContainsKey(pair.Key)) { font.FontFiles.Add(pair.Key, pair.Value); } } return(font); }