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
|
using System;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Plex.Engine.GraphicsSubsystem;
using Microsoft.Xna.Framework.Graphics;
namespace Plex.Engine
{
public enum WrapMode
{
None,
Words,
Letters
}
/// <summary>
/// A class for rendering text.
/// </summary>
public static class TextRenderer
{
private static string WrapLine(SpriteFont font, string text, float maxLineWidth)
{
if (string.IsNullOrEmpty(text))
return text;
if (font.MeasureString(text).X <= maxLineWidth)
return text;
text = text.Trim();
var sb = new StringBuilder();
return sb.ToString().TrimEnd();
}
/// <summary>
/// Perform text wrapping on a string of text using the specified <see cref="SpriteFont"/>.
/// </summary>
/// <param name="font">The <see cref="SpriteFont"/> representing the font to measure the text in.</param>
/// <param name="text">The text to wrap.</param>
/// <param name="maxLineWidth">The maximum width (in pixels) that a line of text can be.</param>
/// <param name="mode">The type of text wrapping to apply.</param>
/// <returns>The resulting wrapped text.</returns>
public static string WrapText(SpriteFont font, string text, float maxLineWidth, WrapMode mode)
{
if (string.IsNullOrEmpty(text))
return text;
if (font.MeasureString(text).X <= maxLineWidth)
return text;
if (mode == WrapMode.Words)
{
float spacewidth = font.MeasureString(" ").X;
if (maxLineWidth < spacewidth)
return text;
text = text.Trim().Replace("\r", "");
string[] lines = text.Split('\n');
float lineWidth = 0;
var sb = new StringBuilder();
foreach (var line in lines)
{
lineWidth = 0;
if (sb.Length>0)
sb.Append("\n");
var words = line.Split(' ').ToList();
for (int i = 0; i < words.Count; i++)
{
string word = words[i];
Vector2 size = font.MeasureString(word);
if (lineWidth + size.X <= maxLineWidth)
{
sb.Append(word + " ");
lineWidth += size.X + spacewidth;
}
else
{
if (size.X >= maxLineWidth)
{
if (sb.Length>0)
sb.Append("\n");
int half = word.Length / 2;
string first = word.Substring(0, half);
string second = word.Substring(half);
words[i] = first;
words.Insert(i + 1, second);
i--;
continue;
}
else
{
sb.Append("\n" + word + " ");
lineWidth = size.X + spacewidth;
}
}
}
}
return sb.ToString().TrimEnd();
}
else
{
float lineWidth = 0;
string newstr = "";
foreach (char c in text)
{
var measure = font.MeasureString(c.ToString());
if(lineWidth + measure.X > maxLineWidth)
{
newstr += "\n";
lineWidth = 0;
}
else
{
if (c == '\n')
{
lineWidth = 0;
}
else
lineWidth += measure.X;
}
newstr += c;
}
return newstr;
}
}
/// <summary>
/// Measure a string of text to get its width and height in pixels.
/// </summary>
/// <param name="text">The text to measure</param>
/// <param name="font">The font to measure with</param>
/// <param name="maxwidth">The maximum width text can be before it is wrapped</param>
/// <param name="wrapMode">The wrap mode to use</param>
/// <returns>The size in pixels of the text.</returns>
public static Vector2 MeasureText(string text, SpriteFont font, int maxwidth, WrapMode wrapMode)
{
if (string.IsNullOrEmpty(text))
return Vector2.Zero;
switch (wrapMode)
{
case WrapMode.None:
return font.MeasureString(text);
default:
return font.MeasureString(WrapText(font, text, maxwidth, wrapMode));
}
}
/// <summary>
/// Render a string of text.
/// </summary>
/// <param name="gfx">The graphics context to render the text to.</param>
/// <param name="text">The text to render.</param>
/// <param name="x">The X coordinate of the text.</param>
/// <param name="y">The Y coordinate of the text.</param>
/// <param name="font">The font to render the text in.</param>
/// <param name="maxwidth">The maximum width text can be before it is wrapped.</param>
/// <param name="alignment">The alignment of the text.</param>
/// <param name="wrapMode">The type of text wrapping to use.</param>
/// <param name="color">The color of the text to render</param>
public static void DrawText(GraphicsContext gfx, int x, int y, string text, SpriteFont font, Color color, int maxwidth, TextAlignment alignment, WrapMode wrapMode)
{
if (string.IsNullOrEmpty(text))
return;
string measured = (wrapMode == WrapMode.None) ? text : WrapText(font, text, maxwidth, wrapMode);
string[] lines = measured.Split('\n');
for (int i = 0; i < lines.Length; i++)
{
var line = lines[i];
var measure = font.MeasureString(line);
switch (alignment)
{
case TextAlignment.Left:
gfx.Batch.DrawString(font, line, new Vector2(x, y + (measure.Y * i)), color);
break;
case TextAlignment.Center:
gfx.Batch.DrawString(font, line, new Vector2(x + ((maxwidth-measure.X)/2), y + (measure.Y * i)), color);
break;
case TextAlignment.Right:
gfx.Batch.DrawString(font, line, new Vector2(x + (maxwidth - measure.X), y + (measure.Y * i)), color);
break;
}
}
}
}
/// <summary>
/// Describes how text should be aligned when rendered.
/// </summary>
public enum TextAlignment
{
/// <summary>
/// Text should be aligned to the centre of the render bounds.
/// </summary>
Center = 0,
/// <summary>
/// Text should be aligned to the left.
/// </summary>
Left = 1,
/// <summary>
/// Text should be rendered to the right.
/// </summary>
Right = 2,
}
/// <summary>
/// Indicates that this <see cref="ATextRenderer"/> should be the default renderer for the game.
/// </summary>
[Obsolete("GDI fonts no longer supported.")]
public class DefaultRenderer : Attribute
{
}
/// <summary>
/// Indicates that this <see cref="ATextRenderer"/> should be the fallback renderer for the game.
/// </summary>
[Obsolete("GDI fonts no longer supported.")]
public class FallbackRenderer : Attribute
{
}
}
|