-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMFontLoader.cs
178 lines (148 loc) · 3.72 KB
/
BMFontLoader.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
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
using System;
using System.Collections.Generic;
using Cyotek.Drawing.BitmapFont;
using System.Linq;
using System.Reflection;
#if !XENKO
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#else
using Xenko.Core.Mathematics;
using Xenko.Graphics;
using Xenko.Graphics.Font;
using Texture2D = Xenko.Graphics.Texture;
#endif
namespace SpriteFontPlus
{
#if BF2SF_INTERNAL
internal
#else
public
#endif
class TextureWithOffset
{
public Texture2D Texture { get; set; }
public Point Offset { get; set; }
public TextureWithOffset(Texture2D texture)
{
if (texture == null)
{
throw new ArgumentNullException("texture");
}
Texture = texture;
}
public TextureWithOffset(Texture2D texture, Point offset): this(texture)
{
Offset = offset;
}
}
#if BF2SF_INTERNAL
internal
#else
public
#endif
static class BMFontLoader
{
#if XENKO
private static FontSystem _fontSystem;
public static FontSystem FontSystem
{
get
{
if (_fontSystem != null)
{
return _fontSystem;
}
_fontSystem = new FontSystem();
FontSystem.Load(GraphicsDevice, null);
return _fontSystem;
}
}
public static GraphicsDevice GraphicsDevice;
#endif
private static SpriteFont Load(BitmapFont data, Func<string, TextureWithOffset> textureGetter)
{
if (data.Pages.Length > 1)
{
throw new NotSupportedException("For now only BMFonts with single texture are supported");
}
var texture = textureGetter(data.Pages[0].FileName);
#if !XENKO
var glyphBounds = new List<Rectangle>();
var cropping = new List<Rectangle>();
var chars = new List<char>();
var kerning = new List<Vector3>();
var characters = data.Characters.Values.OrderBy(c => c.Char);
foreach (var character in characters)
{
var bounds = character.Bounds;
bounds.Offset(texture.Offset);
glyphBounds.Add(bounds);
cropping.Add(new Rectangle(character.Offset.X, character.Offset.Y, bounds.Width, bounds.Height));
chars.Add(character.Char);
kerning.Add(new Vector3(0, character.Bounds.Width, character.XAdvance - character.Bounds.Width));
}
var constructorInfo = typeof(SpriteFont).GetTypeInfo().DeclaredConstructors.First();
var result = (SpriteFont)constructorInfo.Invoke(new object[]
{
texture.Texture, glyphBounds, cropping,
chars, data.LineHeight, 0, kerning, ' '
});
return result;
#else
var textureRegion = textureGetter(data.Pages[0].FileName);
var glyphs = new List<Glyph>();
foreach (var pair in data.Characters)
{
var character = pair.Value;
var bounds = character.Bounds;
bounds.X += textureRegion.Offset.X;
bounds.Y += textureRegion.Offset.Y;
var glyph = new Glyph
{
Character = character.Char,
BitmapIndex = 0,
Offset = new Vector2(character.Offset.X, character.Offset.Y),
Subrect = bounds,
XAdvance = character.XAdvance
};
glyphs.Add(glyph);
}
var textures = new List<Texture2D>
{
textureRegion.Texture
};
return FontSystem.NewStatic(data.LineHeight, glyphs, textures, 0, data.LineHeight);
#endif
}
public static SpriteFont LoadXml(string xml,
Func<string, TextureWithOffset> textureGetter)
{
var data = new BitmapFont();
data.LoadXml(xml);
return Load(data, textureGetter);
}
public static SpriteFont LoadText(string xml,
Func<string, TextureWithOffset> textureGetter)
{
var data = new BitmapFont();
data.LoadText(xml);
return Load(data, textureGetter);
}
public static SpriteFont Load(string str,
Func<string, TextureWithOffset> textureGetter)
{
var data = new BitmapFont();
if (str.StartsWith("<"))
{
// xml
data.LoadXml(str);
}
else
{
data.LoadText(str);
}
return Load(data, textureGetter);
}
}
}