Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
WrappedTextBuilder.cs
Go to the documentation of this file.
2using System.IO;
3using System.Text;
4
5namespace ReLogic.Text;
6
8{
9 private struct NonBreakingText
10 {
11 public readonly string Text;
12
13 public readonly float Width;
14
15 public readonly float WidthOnNewLine;
16
17 public readonly bool IsWhitespace;
18
20
21 public NonBreakingText(IFontMetrics font, string text)
22 {
23 Text = text;
24 IsWhitespace = true;
25 float num = 0f;
26 float num2 = 0f;
27 _font = font;
28 for (int i = 0; i < text.Length; i++)
29 {
30 GlyphMetrics characterMetrics = font.GetCharacterMetrics(text[i]);
31 if (i == 0)
32 {
33 num2 = characterMetrics.KernedWidthOnNewLine - characterMetrics.KernedWidth;
34 }
35 else
36 {
37 num += font.CharacterSpacing;
38 }
39 num += characterMetrics.KernedWidth;
40 if (text[i] != ' ')
41 {
42 IsWhitespace = false;
43 }
44 }
45 Width = num;
46 WidthOnNewLine = num + num2;
47 }
48
49 public string GetAsWrappedText(float maxWidth)
50 {
51 float num = 0f;
52 StringBuilder stringBuilder = new StringBuilder(Text.Length);
53 for (int i = 0; i < Text.Length; i++)
54 {
55 GlyphMetrics characterMetrics = _font.GetCharacterMetrics(Text[i]);
56 num = ((i != 0) ? (num + (_font.CharacterSpacing + characterMetrics.KernedWidth)) : (num + characterMetrics.KernedWidthOnNewLine));
57 if (num > maxWidth)
58 {
59 num = characterMetrics.KernedWidthOnNewLine;
60 stringBuilder.Append('\n');
61 }
62 stringBuilder.Append(Text[i]);
63 }
64 return stringBuilder.ToString();
65 }
66 }
67
68 private readonly IFontMetrics _font;
69
70 private readonly CultureInfo _culture;
71
72 private readonly float _maxWidth;
73
74 private readonly StringBuilder _completedText = new StringBuilder();
75
76 private readonly StringBuilder _workingLine = new StringBuilder();
77
78 private float _workingLineWidth;
79
80 public WrappedTextBuilder(IFontMetrics font, float maxWidth, CultureInfo culture)
81 {
82 _font = font;
83 _maxWidth = maxWidth;
84 _culture = culture;
86 }
87
88 public void CommitWorkingLine()
89 {
90 if (!_completedText.IsEmpty())
91 {
93 }
97 }
98
99 private void Append(NonBreakingText textToken)
100 {
101 float num = ((!_workingLine.IsEmpty()) ? (_workingLineWidth + _font.CharacterSpacing + textToken.Width) : textToken.WidthOnNewLine);
102 if (textToken.WidthOnNewLine > _maxWidth)
103 {
104 if (!_workingLine.IsEmpty())
105 {
107 }
108 if (textToken.Text.Length == 1)
109 {
110 _workingLineWidth = num;
111 _workingLine.Append(textToken.Text);
112 }
113 else
114 {
116 }
117 }
118 else if (num <= _maxWidth)
119 {
120 _workingLineWidth = num;
121 _workingLine.Append(textToken.Text);
122 }
123 else if (_workingLine.IsEmpty())
124 {
125 _completedText.Append(textToken.Text);
128 }
129 else
130 {
132 if (!textToken.IsWhitespace)
133 {
134 _workingLine.Append(textToken.Text);
136 }
137 }
138 }
139
140 public void Append(string text)
141 {
142 StringReader stringReader = new StringReader(text);
144 while (stringReader.Peek() > 0)
145 {
146 if ((ushort)stringReader.Peek() == 10)
147 {
148 stringReader.Read();
150 }
151 else
152 {
153 string text2 = stringReader.ReadUntilBreakable(_culture);
154 Append(new NonBreakingText(_font, text2));
155 }
156 }
157 }
158
159 public override string ToString()
160 {
161 if (_completedText.IsEmpty())
162 {
163 return _workingLine.ToString();
164 }
165 return _completedText.ToString() + "\n" + _workingLine;
166 }
167}
readonly StringBuilder _workingLine
void Append(NonBreakingText textToken)
WrappedTextBuilder(IFontMetrics font, float maxWidth, CultureInfo culture)
readonly StringBuilder _completedText
override int Read()
override int Peek()
int EnsureCapacity(int capacity)
override string ToString()
StringBuilder Append(char value, int repeatCount)
GlyphMetrics GetCharacterMetrics(char character)
NonBreakingText(IFontMetrics font, string text)