using C1.Util.DX;
using C1.Util.DX.DirectWrite;
using System.Collections.Generic;
using System.Globalization;

namespace RichTextBoxInstalledFonts
{
    class InstalledFont
    {
        // Code taken straight from SharpDX\Samples\DirectWrite\FontEnumeration\Program.cs
        public static List<string> GetFonts()
        {
            var fontList = new List<string>();
            var factory = Factory.Create(FactoryType.Shared);
            var fontCollection = factory.GetSystemFontCollection(false);
            var familyCount = fontCollection.GetFontFamilyCount();

            for (int i = 0; i < familyCount; i++)
            {
                var fontFamily = fontCollection.GetFontFamily(i);
                var familyNames = fontFamily.GetFamilyNames();
                int index;

                if (!familyNames.FindLocaleName(CultureInfo.CurrentCulture.Name, out index))
                    familyNames.FindLocaleName("en-us", out index);

                string name = familyNames.GetString(index);
                fontList.Add(name);
                DXUtil.Dispose(ref fontFamily);
                DXUtil.Dispose(ref familyNames);
            }
            DXUtil.Dispose(ref factory);
            DXUtil.Dispose(ref fontCollection);

            return fontList;
        }
    }
}
