Repository navigation
Expand file tree
/
Copy pathFontResolver.cs
More file actions
152 lines (125 loc) · 5.22 KB
/
Copy pathFontResolver.cs
File metadata and controls
152 lines (125 loc) · 5.22 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using PdfSharp.Fonts;
using TheArtOfDev.HtmlRenderer.PdfSharp.FontResolution.Discovery;
using TheArtOfDev.HtmlRenderer.PdfSharp.FontResolution.Parsing;
namespace TheArtOfDev.HtmlRenderer.PdfSharp.FontResolution
{
public class FontResolver : IFontResolver
{
private readonly FontDiscoveryService _fontDiscoveryService = new FontDiscoveryService();
private readonly Dictionary<string, FontMetadata> _fontCache = new Dictionary<string, FontMetadata>();
private List<FontMetadata> _fontMetadataCache = new List<FontMetadata>();
public static string FallbackFont => "Tuffy";
public FontResolveStrategy ResolveStrategy { get; set; } = FontResolveStrategy.Strict;
public FontResolverInfo ResolveTypeface(string familyName, bool bold, bool italic)
{
if (_fontMetadataCache.Count == 0)
{
DiscoverFonts();
}
var fontAttributes = new FontAttributes
{
Style = italic ? FontStyle.Italic : FontStyle.Normal,
Weight = bold ? FontWeight.Bold : FontWeight.Normal
};
var fontNames = FontNameResolver.ResolveFontNames(familyName, fontAttributes, ResolveStrategy);
foreach (var name in fontNames)
{
foreach (var metadata in _fontMetadataCache)
{
var stylizedPreferredFamilyName = FontNameResolver.StylizeFontNamePreferredFamily(metadata);
var stylizedFamilyName = FontNameResolver.StylizeFontNameFamily(metadata);
if (stylizedPreferredFamilyName == name || stylizedFamilyName == name)
{
if (!_fontCache.ContainsKey(name))
{
_fontCache[name] = metadata;
}
return new FontResolverInfo(name);
}
}
}
// Return fallback if no matching font is found
var stylizedFallbackFontName = FontNameResolver.StylizeFontNameStrict(FallbackFont, fontAttributes);
return new FontResolverInfo(stylizedFallbackFontName);
}
public byte[] GetFont(string faceName)
{
if (_fontCache.TryGetValue(faceName, out var font))
{
if (File.Exists(font.FilePath))
{
return File.ReadAllBytes(font.FilePath);
}
}
if (!faceName.StartsWith(FallbackFont))
{
return null;
}
// Try to load embedded fallback font
var assembly = Assembly.GetExecutingAssembly();
var resourceName = assembly.GetManifestResourceNames()
.FirstOrDefault(r => r.EndsWith($"{faceName}.ttf", StringComparison.OrdinalIgnoreCase));
if (resourceName == null)
{
return null;
}
using (var stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream == null)
{
return null;
}
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
return memoryStream.ToArray();
}
}
}
public void RegisterCustomFontDirectory(string fontDirectory)
{
_fontDiscoveryService.RegisterCustomFontDirectory(fontDirectory);
// Invalidate the cache to trigger discovery
_fontMetadataCache.Clear();
}
public List<string> DiscoverFontFamilies()
{
if (_fontMetadataCache.Count == 0)
{
DiscoverFonts();
}
var fontFamilies = new List<string>();
foreach (var metadata in _fontMetadataCache)
{
var stylizedPreferredFamilyName = FontNameResolver.StylizeFontNamePreferredFamily(metadata);
var stylizedFamilyName = FontNameResolver.StylizeFontNameFamily(metadata);
if (!string.IsNullOrEmpty(stylizedPreferredFamilyName))
{
fontFamilies.Add(stylizedFamilyName);
}
fontFamilies.Add(stylizedFamilyName);
}
return fontFamilies;
}
public static FontResolver Register()
{
var fontResolver = new FontResolver();
GlobalFontSettings.FontResolver = fontResolver;
return fontResolver;
}
private void DiscoverFonts()
{
var fontInfos = _fontDiscoveryService.DiscoverFontInfos();
_fontMetadataCache = fontInfos.Select(fontInfo => fontInfo.FilePath)
.Distinct()
.Select(FontParser.ExtractFontMetadata)
.Where(fontMetadata => fontMetadata != null)
.ToList();
}
}
}