-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFontLibrary.cs
More file actions
56 lines (49 loc) · 1.94 KB
/
FontLibrary.cs
File metadata and controls
56 lines (49 loc) · 1.94 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
using Microsoft.Xna.Framework.Graphics;
using SharpFont;
using System;
using System.Collections.Generic;
using System.IO;
namespace FNT
{
/// <summary>Responsible for creating Font instances from a given file.</summary>
public sealed partial class FontLibrary : IDisposable
{
/// <summary>
/// Constructor.
/// </summary>
/// <param name="fontStream">A stream to the TTF/OTF file that will be used. The stream will be copied internally but will NOT be disposed.</param>
/// <param name="graphicsDevice">XNA GraphicsDevice which will be used to create textures.</param>
public FontLibrary(Stream fontStream, GraphicsDevice graphicsDevice)
{
GraphicsDevice = graphicsDevice;
using (var ms = new MemoryStream())
{
fontStream.CopyTo(ms);
fontBytes = ms.ToArray();
}
}
/// <summary>
/// Create an instance of a Font with the given size.
/// If called multiple times with the same size, the same Font instance will always be returned.
/// </summary>
/// <param name="size">Font size</param>
/// <returns>The created Font</returns>
public IFont CreateFont(int size)
{
if (!fonts.TryGetValue(size, out var font))
font = new Font(size, fontBytes, GraphicsDevice);
return font;
}
/// <summary>Disposes all Fonts tracked by this FontLibrary.</summary>
public void Dispose()
{
foreach (var font in fonts.Values)
font.Dispose();
_lib.Dispose();
}
private readonly Dictionary<int, IFont> fonts = new Dictionary<int, IFont>();
private static Library _lib = new Library();
private readonly GraphicsDevice GraphicsDevice;
private readonly byte[] fontBytes;
}
}