This repository was archived by the owner on Sep 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSpriteFont.cs
More file actions
252 lines (203 loc) · 6.62 KB
/
Copy pathSpriteFont.cs
File metadata and controls
252 lines (203 loc) · 6.62 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
namespace Foster.Framework;
/// <summary>
/// Sprite Font is a Font rendered to a Texture at a given size, which is useful for drawing Text with sprite batchers
/// </summary>
public class SpriteFont
{
/// <summary>
/// A single Sprite Font Character
/// </summary>
public class Character
{
/// <summary>
/// The Unicode Value
/// </summary>
public char Unicode;
/// <summary>
/// The rendered Character Image
/// </summary>
public Subtexture Image;
/// <summary>
/// The Offset to draw the Character at
/// </summary>
public Vector2 Offset;
/// <summary>
/// The Amount to Advance the rendering by, horizontally
/// </summary>
public float Advance;
/// <summary>
/// The Kerning value for following Characters
/// </summary>
public Dictionary<char, float> Kerning = new Dictionary<char, float>();
public Character(char unicode, Subtexture image, Vector2 offset, float advance)
{
Unicode = unicode;
Image = image;
Offset = offset;
Advance = advance;
}
}
/// <summary>
/// A list of all the Characters in the Sprite Font
/// </summary>
public readonly Dictionary<char, Character> Charset = new Dictionary<char, Character>();
/// <summary>
/// The Font Family Name
/// </summary>
public string FamilyName;
/// <summary>
/// The Font Style Name
/// </summary>
public string StyleName;
/// <summary>
/// The Size of the Sprite Font
/// </summary>
public int Size;
/// <summary>
/// The Font Ascent
/// </summary>
public float Ascent;
/// <summary>
/// The Font Descent
/// </summary>
public float Descent;
/// <summary>
/// The Line Gap of the Font. This is the vertical space between lines
/// </summary>
public float LineGap;
/// <summary>
/// The Height of the Font (Ascent - Descent)
/// </summary>
public float Height;
/// <summary>
/// The Line Height of the Font (Height + LineGap). This is the total height of a single line, including the line gap
/// </summary>
public float LineHeight;
public SpriteFont(string? familyName = null, string? styleName = null)
{
FamilyName = familyName ?? "Unknown";
StyleName = styleName ?? "Unknown";
}
public SpriteFont(string fontFile, int size, string charset, TextureFilter filter = TextureFilter.Linear)
: this(new FontSize(new Font(fontFile), size, charset), filter)
{
}
public SpriteFont(Stream stream, int size, string charset, TextureFilter filter = TextureFilter.Linear)
: this(new FontSize(new Font(stream), size, charset), filter)
{
}
public SpriteFont(Font font, int size, string charset, TextureFilter filter = TextureFilter.Linear)
: this(new FontSize(font, size, charset), filter)
{
}
public SpriteFont(FontSize fontSize, TextureFilter filter = TextureFilter.Linear)
{
FamilyName = fontSize.Font.FamilyName;
StyleName = fontSize.Font.StyleName;
Size = fontSize.Size;
Ascent = fontSize.Ascent;
Descent = fontSize.Descent;
LineGap = fontSize.LineGap;
Height = fontSize.Height;
LineHeight = fontSize.LineHeight;
var packer = new Packer();
{
var bufferSize = (fontSize.Size * 2) * (fontSize.Size * 2);
var buffer = (bufferSize <= 16384 ? stackalloc Color[bufferSize] : new Color[bufferSize]);
foreach (var ch in fontSize.Charset.Values)
{
var name = ch.Unicode.ToString();
// pack bmp
if (fontSize.Render(ch.Unicode, buffer, out int w, out int h))
packer.AddPixels(name, w, h, buffer);
// create character
var sprChar = new Character(ch.Unicode, new Subtexture(), new Vector2(ch.OffsetX, ch.OffsetY), ch.Advance);
Charset.Add(ch.Unicode, sprChar);
// get all kerning
foreach (var ch2 in fontSize.Charset.Values)
{
var kerning = fontSize.GetKerning(ch.Unicode, ch2.Unicode);
if (Math.Abs(kerning) > 0.000001f)
sprChar.Kerning[ch2.Unicode] = kerning;
}
}
packer.Pack();
}
// link textures
var output = packer.Pack();
if (output != null)
{
for (int i = 0; i < output.Pages.Count; i++)
{
var texture = new Texture(output.Pages[i]);
texture.Filter = filter;
foreach (var entry in output.Entries.Values)
{
if (entry.Page != i)
continue;
if (Charset.TryGetValue(entry.Name[0], out var character))
character.Image.Reset(texture, entry.Source, entry.Frame);
}
}
}
}
/// <summary>
/// Measures the Width of the given text
/// </summary>
public float WidthOf(string text)
{
return WidthOf(text.AsSpan());
}
/// <summary>
/// Measures the Width of the given text
/// </summary>
public float WidthOf(ReadOnlySpan<char> text)
{
var width = 0f;
var line = 0f;
for (int i = 0; i < text.Length; i++)
{
if (text[i] == '\n')
{
if (line > width)
width = line;
line = 0;
continue;
}
if (!Charset.TryGetValue(text[i], out var ch))
continue;
line += ch.Advance;
}
return Math.Max(width, line);
}
public float HeightOf(string text)
{
if (string.IsNullOrEmpty(text))
return 0;
return HeightOf(text.AsSpan());
}
public float HeightOf(ReadOnlySpan<char> text)
{
if (text.Length <= 0)
return 0;
var height = Height;
for (int i = 0; i < text.Length; i++)
{
if (text[i] == '\n')
height += LineHeight;
}
return height;
}
public Vector2 SizeOf(string text)
{
return new Vector2(WidthOf(text.AsSpan()), HeightOf(text.AsSpan()));
}
public Vector2 SizeOf(ReadOnlySpan<char> text)
{
return new Vector2(WidthOf(text), HeightOf(text));
}
}