-
Notifications
You must be signed in to change notification settings - Fork 3
/
MgTextBounder.cs
446 lines (406 loc) · 18.7 KB
/
MgTextBounder.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Microsoft.Xna.Framework
{
/// <summary>
/// Wraps a stringbuilder to a bounding box.
/// This class of course works for a no garbage MgStringBuilder as well.
/// </summary>
public static class MgTextBounder
{
private static SpriteFont tsf;
private static Dictionary<char, SpriteFont.Glyph> _glyphs;
private static SpriteFont.Glyph defaultGlyph;
private static char defaultfontchar = ' ';
private static string newline = Environment.NewLine;
/// <summary>
/// Set the spritefont this needs to be done before the class is used.
/// </summary>
public static void SetSpriteFont(SpriteFont s)
{
tsf = s;
_glyphs = tsf.GetGlyphs();
defaultGlyph = new SpriteFont.Glyph();
if (tsf.DefaultCharacter.HasValue)
{
defaultfontchar = (char)(tsf.DefaultCharacter.Value);
defaultGlyph = _glyphs[defaultfontchar];
}
}
/// <summary>
/// New i just added this here for now.
/// Alter a characters width in this spritefont.
/// requires ... using System.Collections.Generic;
/// </summary>
public static SpriteFont AlterSpriteFontWidth(SpriteFont sf, char chartoalter, float width_amount_to_add)
{
Dictionary<char, SpriteFont.Glyph> dgyphs;
SpriteFont.Glyph defaultglyph;
char defaultchar = ' ';
// Alter one of my methods a bit here for this purpose.
// just drop all the alterd values into a new spritefont
dgyphs = sf.GetGlyphs();
defaultglyph = new SpriteFont.Glyph();
if (sf.DefaultCharacter.HasValue)
{
defaultchar = (char)(sf.DefaultCharacter.Value);
defaultglyph = dgyphs[defaultchar];
}
else
{
// we could create a default value from like a pixel in the sprite font and add the glyph.
}
var altered = dgyphs[chartoalter];
altered.Width = altered.Width + width_amount_to_add; // ect
dgyphs.Remove(chartoalter);
dgyphs.Add(chartoalter, altered);
//sf.Glyphs = _glyphs; // cant do it as its readonly private that sucks hard we would of been done
List<Rectangle> glyphBounds = new List<Rectangle>();
List<Rectangle> cropping = new List<Rectangle>();
List<char> characters = new List<char>();
List<Vector3> kerning = new List<Vector3>();
foreach (var item in dgyphs)
{
glyphBounds.Add(item.Value.BoundsInTexture);
cropping.Add(item.Value.Cropping);
characters.Add(item.Value.Character);
kerning.Add(new Vector3(item.Value.LeftSideBearing, item.Value.Width, item.Value.RightSideBearing));
}
List<Rectangle> b = new List<Rectangle>();
sf = new SpriteFont(sf.Texture, glyphBounds, cropping, characters, sf.LineSpacing, sf.Spacing, kerning, defaultchar);
return sf;
}
/// <summary>
/// This is the primary working method the others only will generate garbage during runtime.
/// That may or may not be a problem depending on if you are just pre-calculating.
/// </summary>
public static void WordWrapTextWithinBounds(ref MgStringBuilder text, Vector2 scale, Rectangle boundRect)
{
var Spacing = tsf.Spacing;
var lineHeight = tsf.LineSpacing * scale.Y;
Vector2 offset = Vector2.Zero;
float yextent = offset.Y + lineHeight;
Vector2 redrawOffset = Vector2.Zero;
Rectangle dest = new Rectangle();
var currentGlyph = SpriteFont.Glyph.Empty;
var firstGlyphOfLine = true;
int lastWordBreakCharPos = 0;
bool firstwordonline = true;
Vector2 rewindOffset = Vector2.Zero;
for (int i = 0; i < text.Length; i++)
{
char c = text[i];
if (c == '\r')
continue;
if (c == '\n')
{
offset.X = 0;
offset.Y += lineHeight;
yextent = offset.Y + lineHeight;
firstGlyphOfLine = true;
firstwordonline = true;
continue;
}
if (_glyphs.ContainsKey(c))
currentGlyph = _glyphs[c];
else
if (!tsf.DefaultCharacter.HasValue)
throw new ArgumentException("Text Contains a Unresolvable Character");
else
currentGlyph = defaultGlyph;
// Solves the problem- the first character on a line with a negative left side bearing.
if (firstGlyphOfLine)
{
offset.X = Math.Max(currentGlyph.LeftSideBearing, 0);
firstGlyphOfLine = false;
}
else
offset.X += Spacing + currentGlyph.LeftSideBearing;
// matrix calculations unrolled rotation is excluded for this version
var m = offset;
m.X += currentGlyph.Cropping.X;
m.Y += currentGlyph.Cropping.Y;
dest = new Rectangle(
(int)(m.X * scale.X),
(int)(m.Y * scale.Y),
(int)(currentGlyph.BoundsInTexture.Width * scale.X),
(int)(currentGlyph.BoundsInTexture.Height * scale.Y)
);
// Begin word wrapping operations.
// if char is a word break character e.g. white space
if (c == ' ')
{
lastWordBreakCharPos = i;
rewindOffset = offset;
if (firstwordonline)
firstwordonline = false;
}
// word wrapping calculations.
if (yextent >= boundRect.Height)
{
// this essentially is function termination due to height boundry
text.Length = i;
i = text.Length;
}
else
{
if (dest.Right > boundRect.Width)
{
if (text.Length > (i + 1))
{
if (firstwordonline == false)
{
if (text[lastWordBreakCharPos + 1] != '\n')
{
// i also pulled the +1 here which is i think inserting a improperly positioned like break using append at.
text.AppendAt(lastWordBreakCharPos + 1, '\n'); // !!! insert here is a gcollection problem... fixed
if (text[lastWordBreakCharPos + 1] != ' ')
{
offset = rewindOffset;
i = lastWordBreakCharPos;
}
}
}
else // first word on the line true
{
if (text[i + 1] != '\n') // if its not already a new line char
{
text.AppendAt(i + 1, '\n'); // !!! insert here is a gcollection problem... fixed
}
}
}
}
}
offset.X += currentGlyph.Width + currentGlyph.RightSideBearing;
}
}
/// <summary>
/// This changes the ref stringbuilder by word wrapping it to a bounding box.
/// </summary>
public static void WordWrapTextWithinBounds(ref StringBuilder text, Vector2 scale, Rectangle boundRect)
{
var Spacing = tsf.Spacing;
var lineHeight = tsf.LineSpacing * scale.Y;
Vector2 offset = Vector2.Zero;
float yextent = offset.Y + lineHeight;
Vector2 redrawOffset = Vector2.Zero;
Rectangle dest = new Rectangle();
var currentGlyph = SpriteFont.Glyph.Empty;
var firstGlyphOfLine = true;
int lastWordBreakCharPos = 0;
bool firstwordonline = true;
Vector2 rewind = Vector2.Zero;
//int i = -1;
//Console.WriteLine(" text.StringBuilder.Length " + text.StringBuilder.Length.ToString() + " ");
//while (i < text.Length)
//{ i++;
for (int i = 0; i < text.Length; i++)
{
char c = text[i];
//Console.Write(" text[" + i + "]");
//Console.Write(" = " + c + " ");
if (c == '\r')
continue;
if (c == '\n')
{
offset.X = 0;
offset.Y += lineHeight;
yextent = offset.Y + lineHeight;
//Console.Write(" >> [" + i.ToString() + "] newline is set. lineHeight:" + lineHeight.ToString() + " y offset:" + offset.Y.ToString() + " the y extent is " + yextent.ToString());
firstGlyphOfLine = true;
firstwordonline = true;
continue;
}
if (_glyphs.ContainsKey(c))
currentGlyph = _glyphs[c];
else
if (!tsf.DefaultCharacter.HasValue)
throw new ArgumentException("Text Contains a Unresolvable Character");
else
currentGlyph = defaultGlyph;
// Solves the problem- the first character on a line with a negative left side bearing.
if (firstGlyphOfLine)
{
offset.X = Math.Max(currentGlyph.LeftSideBearing, 0);
firstGlyphOfLine = false;
}
else
offset.X += Spacing + currentGlyph.LeftSideBearing;
// matrix calculations unrolled varys max 8 mults and up to 10 add subs.
var m = offset;
m.X += currentGlyph.Cropping.X;
m.Y += currentGlyph.Cropping.Y;
dest = new Rectangle(
(int)(m.X * scale.X),
(int)(m.Y * scale.Y),
(int)(currentGlyph.BoundsInTexture.Width * scale.X),
(int)(currentGlyph.BoundsInTexture.Height * scale.Y)
);
//Console.WriteLine(" >>> dest.Height " + dest.Height + " , dest.Bottom " + dest.Bottom);
// if char == a word break character white space
if (c == ' ')
{
lastWordBreakCharPos = i;
rewind = offset;
if (firstwordonline)
firstwordonline = false;
}
// Begin word wrapping calculations.
if (yextent >= boundRect.Height)
{
//Console.WriteLine(" >> dest.Bottom " + dest.Bottom + " >= boundRect.Height " + boundRect.Height);
//Console.WriteLine(" >> text.Length = i + 1; i = text.Length;");
text.Length = i;
i = text.Length;
}
else
{
if (dest.Right > boundRect.Width)
{
if (text.Length > (i + 1))
{
if (firstwordonline == false)
{
if (text[lastWordBreakCharPos + 1] != '\n')
{
//Console.WriteLine(" >> (" + dest.Right + " > " + boundRect.Width + "), text.Insert(lastWordBreakCharPos " + lastWordBreakCharPos + " + 1, newline); offset = rewind; i = lastWordBreakCharPos; ");
text.Insert(lastWordBreakCharPos + 1, '\n');
if (text[lastWordBreakCharPos + 1] != ' ')
{
offset = rewind;
i = lastWordBreakCharPos;
}
}
}
else // first word on the line true
{
if (text[i + 1] != '\n')
{
//Console.WriteLine(" >> text.Insert(i + 1, newline);'");
text.Insert(i + 1, '\n');
}
}
}
}
}
offset.X += currentGlyph.Width + currentGlyph.RightSideBearing;
}
//return lastLineBreakCharPos;
}
/// <summary>
/// This vesion copys to a new stringbuilder and then wraps it before returning it.
/// </summary>
public static StringBuilder WordWrapTextWithinBounds(StringBuilder text, Vector2 scale, Rectangle boundRect)
{
var Spacing = tsf.Spacing;
var lineHeight = tsf.LineSpacing * scale.Y;
Vector2 offset = Vector2.Zero;
float yextent = offset.Y + lineHeight;
Vector2 redrawOffset = Vector2.Zero;
Rectangle dest = new Rectangle();
var currentGlyph = SpriteFont.Glyph.Empty;
var firstGlyphOfLine = true;
int lastWordBreakCharPos = 0;
bool firstwordonline = true;
Vector2 rewind = Vector2.Zero;
//Console.WriteLine(" text.StringBuilder.Length " + text.StringBuilder.Length.ToString() + " ");
StringBuilder tmp = new StringBuilder();
tmp.Append(text);
for (int i = 0; i < tmp.Length; i++)
{
char c = tmp[i];
//Console.Write(" text[" + i + "]");
//Console.Write(" = " + c + " ");
if (c == '\r')
continue;
if (c == '\n')
{
offset.X = 0;
offset.Y += lineHeight;
yextent = offset.Y + lineHeight;
//Console.Write(" >> [" + i.ToString() + "] newline is set. lineHeight:" + lineHeight.ToString() + " y offset:" + offset.Y.ToString() + " the y extent is " + yextent.ToString());
firstGlyphOfLine = true;
firstwordonline = true;
continue;
}
if (_glyphs.ContainsKey(c))
currentGlyph = _glyphs[c];
else
if (!tsf.DefaultCharacter.HasValue)
throw new ArgumentException("Text Contains a Unresolvable Character");
else
currentGlyph = defaultGlyph;
// Solves the problem- the first character on a line with a negative left side bearing.
if (firstGlyphOfLine)
{
offset.X = Math.Max(currentGlyph.LeftSideBearing, 0);
firstGlyphOfLine = false;
}
else
offset.X += Spacing + currentGlyph.LeftSideBearing;
// matrix calculations unrolled varys max 8 mults and up to 10 add subs.
var m = offset;
m.X += currentGlyph.Cropping.X;
m.Y += currentGlyph.Cropping.Y;
dest = new Rectangle(
(int)(m.X * scale.X),
(int)(m.Y * scale.Y),
(int)(currentGlyph.BoundsInTexture.Width * scale.X),
(int)(currentGlyph.BoundsInTexture.Height * scale.Y)
);
//Console.WriteLine(" >>> dest.Height " + dest.Height + " , dest.Bottom " + dest.Bottom);
// if char == a word break character white space
if (c == ' ')
{
lastWordBreakCharPos = i;
rewind = offset;
if (firstwordonline)
firstwordonline = false;
}
// Begin word wrapping calculations.
if (yextent >= boundRect.Height)
{
//Console.WriteLine(" >> dest.Bottom " + dest.Bottom + " >= boundRect.Height " + boundRect.Height);
//Console.WriteLine(" >> text.Length = i + 1; i = text.Length;");
tmp.Length = i;
i = tmp.Length;
}
else
{
if (dest.Right > boundRect.Width)
{
if (tmp.Length > (i + 1))
{
if (firstwordonline == false)
{
if (tmp[lastWordBreakCharPos + 1] != '\n')
{
//Console.WriteLine(" >> (" + dest.Right + " > " + boundRect.Width + "), text.Insert(lastWordBreakCharPos " + lastWordBreakCharPos + " + 1, newline); offset = rewind; i = lastWordBreakCharPos; ");
tmp.Insert(lastWordBreakCharPos + 1, '\n');
if (tmp[lastWordBreakCharPos + 1] != ' ')
{
offset = rewind;
i = lastWordBreakCharPos;
}
}
}
else // first word on the line true
{
if (tmp[i + 1] != '\n')
{
//Console.WriteLine(" >> text.Insert(i + 1, newline);'");
tmp.Insert(i + 1, '\n');
}
}
}
}
}
offset.X += currentGlyph.Width + currentGlyph.RightSideBearing;
}
return tmp;
}
}
}