// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Drawing; using System.Drawing.Text; using System.IO; using Microsoft.Win32; namespace Spludlow.Drawing { public class Fonts { public static string[] ListFonts() { List list = new List(); using (InstalledFontCollection fontCollection = new System.Drawing.Text.InstalledFontCollection()) { foreach (FontFamily fontFamily in fontCollection.Families) { list.Add(fontFamily.Name); fontFamily.Dispose(); } } return list.ToArray(); } public static DataSet ReportFonts() { DataTable installedFonts = InstalledFontsInfo(); DataTable fontTable = Spludlow.Data.TextTable.ReadText(new string[] { "Name RegistryName Filename Extention Size", "String String String String Int32", }); fontTable.TableName = "Fonts"; using (InstalledFontCollection fontCollection = new System.Drawing.Text.InstalledFontCollection()) { foreach (FontFamily fontFamily in fontCollection.Families) { string name = fontFamily.Name; DataRow[] installedRows = installedFonts.Select("Name = '" + name + "'"); if (installedRows.Length == 0) { fontTable.Rows.Add(name); } else { for (int index = 0; index < installedRows.Length; ++index) { if (index > 0) name = ""; DataRow installedRow = installedRows[index]; fontTable.Rows.Add(name, (string)installedRow["RegistryName"], (string)installedRow["Filename"], (string)installedRow["Extention"], (int)installedRow["Size"]); } } fontFamily.Dispose(); } } DataSet dataSet = new DataSet(); dataSet.Tables.Add(fontTable); dataSet.Tables.Add(installedFonts); Spludlow.Log.Report("Font Report", new object[] { dataSet }); return dataSet; } public static DataTable InstalledFontsInfo() { DataTable table = Spludlow.Data.TextTable.ReadText(new string[] { "Filename Extention Name RegistryName Size", "String String String String Int32", }); table.TableName = "InstalledFonts"; using (RegistryKey fontsRegKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion\Fonts", false)) { Dictionary fontFilenameLookup = new Dictionary(); if (fontsRegKey == null) { Spludlow.Log.Warning("Fonts RegistryKey not found"); } else { foreach (string name in fontsRegKey.GetValueNames()) { string value = (string)fontsRegKey.GetValue(name); if (value == null) continue; value = value.ToUpper(); if (fontFilenameLookup.ContainsKey(value) == true) { Spludlow.Log.Warning("Fonts RegistryKey multiple fonts with same filename: " + value); continue; } fontFilenameLookup.Add(value, name); } } foreach (string fontFilename in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Fonts))) { string extention = Path.GetExtension(fontFilename).ToUpper(); using (System.Drawing.Text.PrivateFontCollection fontCollection = new System.Drawing.Text.PrivateFontCollection()) { fontCollection.AddFontFile(fontFilename); foreach (FontFamily fontFamily in fontCollection.Families) { string registryName = ""; if (fontsRegKey != null) { string filename = Path.GetFileName(fontFilename).ToUpper(); if (fontFilenameLookup.ContainsKey(filename) == true) registryName = fontFilenameLookup[filename]; } table.Rows.Add(fontFilename, extention, fontFamily.Name, registryName, Spludlow.Io.Files.FileLength(fontFilename)); } } } } return table; } } }