using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using ReFuel.FreeType; using Dashboard.Media.Defaults.Fallback; using Dashboard.Media.Defaults.Linux; using Dashboard.Media.Fonts; using Dashboard.PAL; namespace Dashboard.Media.Defaults { public static class FontDataBaseProvider { public static IFontDataBase Instance { get; } static FontDataBaseProvider() { try { // TODO: add as other operating systems are supported. if (OperatingSystem.IsLinux()) { Instance = new FontConfigFontDatabase(); } else { Instance = new FallbackFontDatabase(); } } catch (Exception ex) { throw new NotSupportedException("Could not load a suitable font database implementation.", ex); } } public static (FontFace, FileInfo) ResolveSystemFont(string envVar, string defaults, IFontDataBase db) { StringBuilder builder = new StringBuilder(); string user = Environment.GetEnvironmentVariable(envVar); if (user != null) { builder.Append(user); builder.Append(':'); } builder.Append(defaults); string[] list = builder.ToString().Split(':'); foreach (string item in list) { if (File.Exists(item)) { // Process file. if (FT.NewFace(FTProvider.Ft, item, 0, out FTFace ftface) != FTError.None) { continue; } FontFace face = FontFace.Parse(ftface.FamilyName, ftface.StyleName); FT.DoneFace(ftface); return (face, new FileInfo(item)); } else { IEnumerable faces = db.Search( new FontFace(item, FontSlant.Normal, FontWeight.Normal, FontStretch.Normal), FontMatchCriteria.Family); if (faces.Any()) { FontFace face = faces.First(); return (face, db.FontFileInfo(face)); } } } try { FontFace face = db.GetSystemFontFace(SystemFontFamily.Sans); return (face, db.FontFileInfo(face)); } catch (Exception ex) { throw new NotImplementedException("No fallback font yet.", ex); } } } }