-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathFontUtil.cs
More file actions
64 lines (58 loc) · 2.5 KB
/
FontUtil.cs
File metadata and controls
64 lines (58 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using PaintDotNet.DirectWrite;
using System;
using System.Collections.Generic;
using System.Linq;
namespace PdnCodeLab
{
internal static class FontUtil
{
internal static IReadOnlyCollection<string> FontList { get; } = BuildFontList();
private static IReadOnlyCollection<string> BuildFontList()
{
List<string> installedMonoFonts = new List<string>();
using IDirectWriteFactory dwFactory = DirectWriteFactory.CreateRef();
using IFontCollection fontCollection = dwFactory.GetSystemFontCollection(false);
for (int familyIndex = 0; familyIndex < fontCollection.FontFamilyCount; familyIndex++)
{
using IFontFamily fontFamily = fontCollection.GetFontFamily(familyIndex);
for (int fontIndex = 0; fontIndex < fontFamily.FontCount; fontIndex++)
{
using IFont font = fontFamily.GetFont(fontIndex);
if (font.IsMonospacedFont)
{
using ILocalizedStrings langTags = font.TryGetInformationalStrings(InformationalStringID.DesignScriptLanguageTag);
if (langTags != null)
{
for (int i = 0; i < langTags.Count; i++)
{
if (langTags.GetString(i) == "Latn")
{
installedMonoFonts.Add(fontFamily.GetFamilyName());
break;
}
}
}
else
{
installedMonoFonts.Add(fontFamily.GetFamilyName());
}
break;
}
}
}
string[] recommendedFonts = ["Cascadia Code", "Consolas", "Courier New", "Envy Code R", "Fira Code", "Hack", "JetBrains Mono", "Fake Font"];
return recommendedFonts
.Except(installedMonoFonts, StringComparer.Ordinal)
.Select(x => x + '*')
.Concat(installedMonoFonts)
.Append("Verdana")
.Order(StringComparer.OrdinalIgnoreCase)
.ToList()
.AsReadOnly();
}
internal static bool IsFontInstalled(string fontName)
{
return FontList.Contains(fontName, StringComparer.Ordinal);
}
}
}