/
FontCollectionExtensions.cs
59 lines (52 loc) · 2.3 KB
/
FontCollectionExtensions.cs
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
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System;
namespace SixLabors.Fonts
{
/// <summary>
/// Extension methods for <see cref="IFontCollection"/>.
/// </summary>
public static class FontCollectionExtensions
{
/// <summary>
/// Adds the fonts from the <see cref="SystemFonts"/> collection to this <see cref="FontCollection"/>.
/// </summary>
/// <param name="collection">The font collection.</param>
/// <returns>The <see cref="FontCollection"/> containing the system fonts.</returns>
public static FontCollection AddSystemFonts(this FontCollection collection)
{
// This cast is safe because our underlying SystemFontCollection implements
// both interfaces separately.
foreach (FontMetrics metric in (IReadOnlyFontMetricsCollection)SystemFonts.Collection)
{
((IFontMetricsCollection)collection).AddMetrics(metric);
}
collection.AddSearchDirectories(SystemFonts.Collection.SearchDirectories);
return collection;
}
/// <summary>
/// Adds the fonts from the <see cref="SystemFonts"/> collection to this <see cref="FontCollection"/>.
/// </summary>
/// <param name="collection">The font collection.</param>
/// <param name="match">The <see cref="Predicate{T}"/> delegate that defines the conditions of <see cref="FontMetrics"/> to add into the font collection.</param>
/// <returns>The <see cref="FontCollection"/> containing the system fonts.</returns>
public static FontCollection AddSystemFonts(this FontCollection collection, Predicate<FontMetrics> match)
{
bool isMatch = false;
foreach (FontMetrics metric in (IReadOnlyFontMetricsCollection)SystemFonts.Collection)
{
bool currentMatch = match(metric);
isMatch |= currentMatch;
if (currentMatch)
{
((IFontMetricsCollection)collection).AddMetrics(metric);
}
}
if (isMatch)
{
collection.AddSearchDirectories(SystemFonts.Collection.SearchDirectories);
}
return collection;
}
}
}