Repository navigation
Expand file tree
/
Copy pathLinuxFontDiscovery.cs
More file actions
129 lines (112 loc) · 3.86 KB
/
Copy pathLinuxFontDiscovery.cs
File metadata and controls
129 lines (112 loc) · 3.86 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace TheArtOfDev.HtmlRenderer.PdfSharp.FontResolution.Discovery
{
internal static class LinuxFontDiscovery
{
public static List<FontInfo> DiscoverFontFiles()
{
var fontInfos = new List<FontInfo>();
if (!IsFontConfigAvailable())
{
return fontInfos;
}
try
{
var startInfo = new ProcessStartInfo
{
FileName = "fc-list",
Arguments = ":",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (var process = Process.Start(startInfo))
{
if (process == null)
{
return fontInfos;
}
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
if (process.ExitCode != 0 || string.IsNullOrWhiteSpace(output))
{
return fontInfos;
}
var lines = output.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var fontFamilies = DiscoverFontFile(line);
if (fontFamilies.Count > 0)
{
fontInfos.AddRange(fontFamilies);
}
}
}
}
catch
{
// Silently continue on errors
}
return fontInfos;
}
private static bool IsFontConfigAvailable()
{
try
{
var startInfo = new ProcessStartInfo
{
FileName = "which",
Arguments = "fc-list",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (var process = Process.Start(startInfo))
{
if (process == null)
{
return false;
}
process.WaitForExit();
return process.ExitCode == 0;
}
}
catch
{
return false;
}
}
private static List<FontInfo> DiscoverFontFile(string line)
{
var fonts = new List<FontInfo>();
// Parse fc-list output: "path: family1,family2:style=style"
var parts = line.Split(new[] { ':' }, 2, StringSplitOptions.None);
if (parts.Length < 2)
{
return fonts;
}
var fontFilePath = parts[0].Trim();
var families = parts[1].Trim().Split(',');
if (!File.Exists(fontFilePath) ||
!FontDiscoveryService.SupportedFontExtensions.Any(e => fontFilePath.EndsWith(e, StringComparison.InvariantCultureIgnoreCase)))
{
return fonts;
}
foreach (var family in families)
{
var familyName = family.Trim();
if (!string.IsNullOrEmpty(familyName) && File.Exists(fontFilePath))
{
fonts.Add(new FontInfo { Name = familyName, FilePath = fontFilePath });
}
}
return fonts;
}
}
}