-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGLFontTextNodeList.cs
More file actions
238 lines (210 loc) · 8.11 KB
/
GLFontTextNodeList.cs
File metadata and controls
238 lines (210 loc) · 8.11 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
using System;
using System.Collections;
using System.Text;
namespace GLGUI
{
/// <summary>
/// A doubly linked list of text nodes
/// </summary>
class GLFontTextNodeList : IEnumerable
{
public GLFontTextNode Head;
public GLFontTextNode Tail;
/// <summary>
/// Builds a doubly linked list of text nodes from the given input string
/// </summary>
/// <param name="text"></param>
public GLFontTextNodeList(string text)
{
bool carriageReturn = false;
bool wordInProgress = false;
StringBuilder currentWord = new StringBuilder();
for (int i = 0; i < text.Length; i++)
{
if (text[i] == '\r' || text[i] == '\n' || text[i] == ' ' || text[i] == '\t')
{
if (text[i] == '\n' && carriageReturn) //text = text.Replace("\r\n", "\r");
continue;
carriageReturn = false;
if (text[i] == '\r')
carriageReturn = true;
if (wordInProgress)
{
Add(new GLFontTextNode(GLFontTextNodeType.Word, currentWord.ToString()));
wordInProgress = false;
}
if (text[i] == '\r' || text[i] == '\n')
Add(new GLFontTextNode(GLFontTextNodeType.LineBreak, null));
else if (text[i] == ' ')
Add(new GLFontTextNode(GLFontTextNodeType.Space, null));
else if (text[i] == '\t')
Add(new GLFontTextNode(GLFontTextNodeType.Tab, null));
}
else
{
carriageReturn = false;
if (!wordInProgress)
{
wordInProgress = true;
currentWord = new StringBuilder();
}
currentWord.Append(text[i]);
}
}
if (wordInProgress)
Add(new GLFontTextNode(GLFontTextNodeType.Word, currentWord.ToString()));
}
public void MeasureNodes(GLFontData fontData, GLFontRenderOptions options)
{
bool monospaced = fontData.IsMonospacingActive(options);
float monospaceWidth = fontData.GetMonoSpaceWidth(options);
foreach (GLFontTextNode node in this)
{
if (node.Length == 0f)
{
if (node.Type == GLFontTextNodeType.Space)
{
if (monospaced)
{
node.Length = monospaceWidth;
continue;
}
node.Length = (float)Math.Ceiling(fontData.meanGlyphWidth * options.WordSpacing);
continue;
}
if (node.Type == GLFontTextNodeType.Tab)
{
if (monospaced)
{
node.Length = monospaceWidth * 4;
continue;
}
node.Length = (float)Math.Ceiling(4 * fontData.meanGlyphWidth * options.WordSpacing);
continue;
}
if (node.Type == GLFontTextNodeType.Word)
{
for (int i = 0; i < node.Text.Length; i++)
{
char c = node.Text[i];
GLFontGlyph glyph;
if (fontData.CharSetMapping.TryGetValue(c, out glyph))
{
if (monospaced)
node.Length += monospaceWidth;
else
node.Length += (float)Math.Ceiling(glyph.Rect.Width + fontData.meanGlyphWidth * options.CharacterSpacing + fontData.GetKerningPairCorrection(i, node.Text, node));
}
}
}
}
}
}
/// <summary>
/// Splits a word into sub-words of size less than or equal to baseCaseSize
/// </summary>
/// <param name="node"></param>
/// <param name="baseCaseSize"></param>
public void Crumble(GLFontTextNode node, int baseCaseSize)
{
if(node.Text.Length <= baseCaseSize)
return;
var left = SplitNode(node);
var right = left.Next;
Crumble(left, baseCaseSize);
Crumble(right, baseCaseSize);
}
/// <summary>
/// Splits a word node in two, adding both new nodes to the list in sequence.
/// </summary>
/// <param name="node"></param>
/// <returns>The first new node</returns>
public GLFontTextNode SplitNode(GLFontTextNode node)
{
if (node.Type != GLFontTextNodeType.Word)
throw new Exception("Cannot split text node of type: " + node.Type);
int midPoint = node.Text.Length / 2;
string newFirstHalf = node.Text.Substring(0, midPoint);
string newSecondHalf = node.Text.Substring(midPoint, node.Text.Length - midPoint);
GLFontTextNode newFirst = new GLFontTextNode(GLFontTextNodeType.Word, newFirstHalf);
GLFontTextNode newSecond = new GLFontTextNode(GLFontTextNodeType.Word, newSecondHalf);
newFirst.Next = newSecond;
newSecond.Previous = newFirst;
//node is head
if (node.Previous == null)
Head = newFirst;
else
{
node.Previous.Next = newFirst;
newFirst.Previous = node.Previous;
}
//node is tail
if (node.Next == null)
Tail = newSecond;
else
{
node.Next.Previous = newSecond;
newSecond.Next = node.Next;
}
return newFirst;
}
public void Add(GLFontTextNode node)
{
if(Head == null)
{
Head = Tail = node;
}
else
{
Tail.Next = node;
node.Previous = Tail;
Tail = node;
}
}
public override string ToString()
{
StringBuilder builder = new StringBuilder();
foreach(GLFontTextNode node in this)
{
if (node.Type == GLFontTextNodeType.Space)
builder.Append(" ");
else if (node.Type == GLFontTextNodeType.Tab)
builder.Append(" ");
else if (node.Type == GLFontTextNodeType.LineBreak)
builder.Append(System.Environment.NewLine);
else if (node.Type == GLFontTextNodeType.Word)
builder.Append("" + node.Text + "");
}
return builder.ToString();
}
public IEnumerator GetEnumerator()
{
return new TextNodeListEnumerator(this);
}
private class TextNodeListEnumerator : IEnumerator
{
private GLFontTextNode currentNode = null;
private GLFontTextNodeList targetList;
public TextNodeListEnumerator(GLFontTextNodeList targetList)
{
this.targetList = targetList;
}
public object Current
{
get { return currentNode; }
}
public virtual bool MoveNext()
{
if (currentNode == null)
currentNode = targetList.Head;
else
currentNode = currentNode.Next;
return currentNode != null;
}
public void Reset()
{
currentNode = null;
}
}
}
}