-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
FontCollection.cs
150 lines (128 loc) · 4.97 KB
/
FontCollection.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
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
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using SixLabors.Fonts.Exceptions;
namespace SixLabors.Fonts
{
/// <summary>
/// Provides a collection of fonts.
/// </summary>
public sealed class FontCollection : IFontCollection
{
private Dictionary<string, List<IFontInstance>> instances = new Dictionary<string, List<IFontInstance>>(StringComparer.OrdinalIgnoreCase);
private Dictionary<string, FontFamily> families = new Dictionary<string, FontFamily>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Initializes a new instance of the <see cref="FontCollection"/> class.
/// </summary>
public FontCollection()
{
}
/// <summary>
/// Gets the collection of <see cref="FontFamily"/> objects associated with this <see cref="FontCollection"/>.
/// </summary>
/// <value>
/// The families.
/// </value>
public IEnumerable<FontFamily> Families => this.families.Values.ToImmutableArray();
#if FILESYSTEM
/// <summary>
/// Installs a font from the specified path.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>the description of the font just loaded.</returns>
public FontFamily Install(string path)
{
using (FileStream fs = File.OpenRead(path))
{
return this.Install(fs);
}
}
#endif
/// <summary>
/// Installs the specified font stream.
/// </summary>
/// <param name="fontStream">The font stream.</param>
/// <returns>the description of the font just loaded.</returns>
public FontFamily Install(Stream fontStream)
{
FontInstance instance = FontInstance.LoadFont(fontStream);
return this.Install(instance);
}
/// <summary>
/// Finds the specified font family.
/// </summary>
/// <param name="fontFamily">The font family.</param>
/// <returns>The family if installed otherwise throws <see cref="FontFamilyNotFoundException"/></returns>
public FontFamily Find(string fontFamily)
{
if (this.TryFind(fontFamily, out FontFamily result))
{
return result;
}
throw new FontFamilyNotFoundException(fontFamily);
}
/// <summary>
/// Finds the specified font family.
/// </summary>
/// <param name="fontFamily">The font family to find.</param>
/// <param name="family">The found family.</param>
/// <returns>true if a font of that family has been installed into the font collection.</returns>
public bool TryFind(string fontFamily, out FontFamily family)
{
if (this.families.ContainsKey(fontFamily))
{
family = this.families[fontFamily];
return true;
}
family = null;
return false;
}
internal IEnumerable<FontStyle> AvailibleStyles(string fontFamily)
{
return this.FindAll(fontFamily).Select(x => x.Description.Style).ToImmutableArray();
}
internal FontFamily Install(IFontInstance instance)
{
if (instance != null && instance.Description != null)
{
lock (this.instances)
{
if (!this.instances.ContainsKey(instance.Description.FontFamily))
{
this.instances.Add(instance.Description.FontFamily, new List<IFontInstance>(4));
}
if (!this.families.ContainsKey(instance.Description.FontFamily))
{
this.families.Add(instance.Description.FontFamily, new FontFamily(instance.Description.FontFamily, this));
}
this.instances[instance.Description.FontFamily].Add(instance);
}
return this.families[instance.Description.FontFamily];
}
return null;
}
internal IFontInstance Find(string fontFamily, FontStyle style)
{
if (!this.instances.ContainsKey(fontFamily))
{
return null;
}
// once we have to support verient fonts then we
List<IFontInstance> inFamily = this.instances[fontFamily];
return inFamily.FirstOrDefault(x => x.Description.Style == style);
}
internal IEnumerable<IFontInstance> FindAll(string name)
{
if (!this.instances.ContainsKey(name))
{
return Enumerable.Empty<IFontInstance>();
}
// once we have to support verient fonts then we
return this.instances[name];
}
}
}