Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DynamicSpriteFont.cs
Go to the documentation of this file.
1using System;
7using ReLogic.Text;
8
9namespace ReLogic.Graphics;
10
12{
13 private struct SpriteCharacterData
14 {
15 public readonly Texture2D Texture;
16
17 public readonly Rectangle Glyph;
18
19 public readonly Rectangle Padding;
20
21 public readonly Vector3 Kerning;
22
23 public SpriteCharacterData(Texture2D texture, Rectangle glyph, Rectangle padding, Vector3 kerning)
24 {
25 //IL_0008: Unknown result type (might be due to invalid IL or missing references)
26 //IL_0009: Unknown result type (might be due to invalid IL or missing references)
27 //IL_000f: Unknown result type (might be due to invalid IL or missing references)
28 //IL_0010: Unknown result type (might be due to invalid IL or missing references)
29 //IL_0016: Unknown result type (might be due to invalid IL or missing references)
30 //IL_0018: Unknown result type (might be due to invalid IL or missing references)
31 Texture = texture;
32 Glyph = glyph;
33 Padding = padding;
34 Kerning = kerning;
35 }
36
41 }
42
44
46
47 public readonly char DefaultCharacter;
48
49 private readonly float _characterSpacing;
50
51 private readonly int _lineSpacing;
52
54
56
57 public DynamicSpriteFont(float spacing, int lineSpacing, char defaultCharacter)
58 {
59 _characterSpacing = spacing;
60 _lineSpacing = lineSpacing;
61 DefaultCharacter = defaultCharacter;
62 }
63
65 {
66 if (character != '\n' && character != '\r')
67 {
69 }
70 return true;
71 }
72
74 {
75 foreach (char character in characters)
76 {
78 {
79 return false;
80 }
81 }
82 return true;
83 }
84
85 internal void SetPages(FontPage[] pages)
86 {
87 //IL_006a: Unknown result type (might be due to invalid IL or missing references)
88 //IL_0078: Unknown result type (might be due to invalid IL or missing references)
89 //IL_0086: Unknown result type (might be due to invalid IL or missing references)
90 int num = 0;
92 foreach (FontPage fontPage in array)
93 {
94 num += fontPage.Characters.Count;
95 }
97 array = pages;
98 foreach (FontPage fontPage2 in array)
99 {
100 for (int i = 0; i < fontPage2.Characters.Count; i++)
101 {
102 _spriteCharacters.Add(fontPage2.Characters[i], new SpriteCharacterData(fontPage2.Texture, fontPage2.Glyphs[i], fontPage2.Padding[i], fontPage2.Kerning[i]));
103 if (fontPage2.Characters[i] == DefaultCharacter)
104 {
106 }
107 }
108 }
109 }
110
111 internal void InternalDraw(string text, SpriteBatch spriteBatch, Vector2 startPosition, Color color, float rotation, Vector2 origin, ref Vector2 scale, SpriteEffects spriteEffects, float depth)
112 {
113 //IL_0005: Unknown result type (might be due to invalid IL or missing references)
114 //IL_001a: Unknown result type (might be due to invalid IL or missing references)
115 //IL_002f: Unknown result type (might be due to invalid IL or missing references)
116 //IL_0036: Unknown result type (might be due to invalid IL or missing references)
117 //IL_003b: Unknown result type (might be due to invalid IL or missing references)
118 //IL_0040: Unknown result type (might be due to invalid IL or missing references)
119 //IL_0041: Unknown result type (might be due to invalid IL or missing references)
120 //IL_0046: Unknown result type (might be due to invalid IL or missing references)
121 //IL_0047: Unknown result type (might be due to invalid IL or missing references)
122 //IL_004c: Unknown result type (might be due to invalid IL or missing references)
123 //IL_0056: Unknown result type (might be due to invalid IL or missing references)
124 //IL_005c: Unknown result type (might be due to invalid IL or missing references)
125 //IL_0061: Unknown result type (might be due to invalid IL or missing references)
126 //IL_0063: Unknown result type (might be due to invalid IL or missing references)
127 //IL_0094: Unknown result type (might be due to invalid IL or missing references)
128 //IL_0077: Unknown result type (might be due to invalid IL or missing references)
129 //IL_00aa: Unknown result type (might be due to invalid IL or missing references)
130 //IL_0123: Unknown result type (might be due to invalid IL or missing references)
131 //IL_013f: Unknown result type (might be due to invalid IL or missing references)
132 //IL_0144: Unknown result type (might be due to invalid IL or missing references)
133 //IL_0148: Unknown result type (might be due to invalid IL or missing references)
134 //IL_014d: Unknown result type (might be due to invalid IL or missing references)
135 //IL_014f: Unknown result type (might be due to invalid IL or missing references)
136 //IL_0175: Unknown result type (might be due to invalid IL or missing references)
137 //IL_016c: Unknown result type (might be due to invalid IL or missing references)
138 //IL_0193: Unknown result type (might be due to invalid IL or missing references)
139 //IL_019e: Unknown result type (might be due to invalid IL or missing references)
140 //IL_01df: Unknown result type (might be due to invalid IL or missing references)
141 //IL_01b0: Unknown result type (might be due to invalid IL or missing references)
142 //IL_01f1: Unknown result type (might be due to invalid IL or missing references)
143 //IL_0200: Unknown result type (might be due to invalid IL or missing references)
144 //IL_0209: Unknown result type (might be due to invalid IL or missing references)
145 //IL_020a: Unknown result type (might be due to invalid IL or missing references)
146 //IL_0215: Unknown result type (might be due to invalid IL or missing references)
147 //IL_0230: Unknown result type (might be due to invalid IL or missing references)
148 //IL_024d: Unknown result type (might be due to invalid IL or missing references)
149 //IL_024f: Unknown result type (might be due to invalid IL or missing references)
150 //IL_0250: Unknown result type (might be due to invalid IL or missing references)
151 //IL_0255: Unknown result type (might be due to invalid IL or missing references)
152 //IL_025f: Unknown result type (might be due to invalid IL or missing references)
153 //IL_0263: Unknown result type (might be due to invalid IL or missing references)
154 //IL_026d: Unknown result type (might be due to invalid IL or missing references)
155 //IL_0271: Unknown result type (might be due to invalid IL or missing references)
156 //IL_0278: Unknown result type (might be due to invalid IL or missing references)
157 //IL_027d: Unknown result type (might be due to invalid IL or missing references)
158 //IL_028f: Unknown result type (might be due to invalid IL or missing references)
159 //IL_0296: Unknown result type (might be due to invalid IL or missing references)
160 //IL_02a6: Unknown result type (might be due to invalid IL or missing references)
161 Matrix matrix = Matrix.CreateTranslation((0f - origin.X) * scale.X, (0f - origin.Y) * scale.Y, 0f) * Matrix.CreateRotationZ(rotation);
164 bool flag = true;
165 float x = 0f;
166 if ((int)spriteEffects != 0)
167 {
169 if (((Enum)spriteEffects).HasFlag((Enum)(object)(SpriteEffects)1))
170 {
171 x = vector.X * scale.X;
172 one.X = -1f;
173 }
174 if (((Enum)spriteEffects).HasFlag((Enum)(object)(SpriteEffects)2))
175 {
176 zero.Y = (vector.Y - (float)LineSpacing) * scale.Y;
177 one.Y = -1f;
178 }
179 }
180 zero.X = x;
181 foreach (char c in text)
182 {
183 switch (c)
184 {
185 case '\n':
186 zero.X = x;
187 zero.Y += (float)LineSpacing * scale.Y * one.Y;
188 flag = true;
189 continue;
190 case '\r':
191 continue;
192 }
194 Vector3 kerning = characterData.Kerning;
195 Rectangle padding = characterData.Padding;
196 if (((Enum)spriteEffects).HasFlag((Enum)(object)(SpriteEffects)1))
197 {
198 padding.X -= padding.Width;
199 }
200 if (((Enum)spriteEffects).HasFlag((Enum)(object)(SpriteEffects)2))
201 {
202 padding.Y = LineSpacing - characterData.Glyph.Height - padding.Y;
203 }
204 if (flag)
205 {
206 kerning.X = Math.Max(kerning.X, 0f);
207 }
208 else
209 {
210 zero.X += CharacterSpacing * scale.X * one.X;
211 }
212 zero.X += kerning.X * scale.X * one.X;
213 Vector2 position = zero;
214 position.X += (float)padding.X * scale.X;
215 position.Y += (float)padding.Y * scale.Y;
216 Vector2.Transform(ref position, ref matrix, ref position);
217 position += startPosition;
218 spriteBatch.Draw(characterData.Texture, position, (Rectangle?)characterData.Glyph, color, rotation, Vector2.Zero, scale, spriteEffects, depth);
219 zero.X += (kerning.Y + kerning.Z) * scale.X * one.X;
220 flag = false;
221 }
222 }
223
225 {
227 {
229 }
230 return value;
231 }
232
234 {
235 //IL_000e: Unknown result type (might be due to invalid IL or missing references)
236 //IL_0013: Unknown result type (might be due to invalid IL or missing references)
237 //IL_0008: Unknown result type (might be due to invalid IL or missing references)
238 //IL_0161: Unknown result type (might be due to invalid IL or missing references)
239 //IL_0172: Unknown result type (might be due to invalid IL or missing references)
240 //IL_005a: Unknown result type (might be due to invalid IL or missing references)
241 //IL_0079: Unknown result type (might be due to invalid IL or missing references)
242 //IL_007e: Unknown result type (might be due to invalid IL or missing references)
243 //IL_00a5: Unknown result type (might be due to invalid IL or missing references)
244 //IL_00aa: Unknown result type (might be due to invalid IL or missing references)
245 //IL_00b2: Unknown result type (might be due to invalid IL or missing references)
246 //IL_00e6: Unknown result type (might be due to invalid IL or missing references)
247 //IL_00ed: Unknown result type (might be due to invalid IL or missing references)
248 //IL_00f7: Unknown result type (might be due to invalid IL or missing references)
249 //IL_0101: Unknown result type (might be due to invalid IL or missing references)
250 //IL_0109: Unknown result type (might be due to invalid IL or missing references)
251 if (text.Length == 0)
252 {
253 return Vector2.Zero;
254 }
256 zero.Y = LineSpacing;
257 float val = 0f;
258 int num = 0;
259 float num2 = 0f;
260 bool flag = true;
261 foreach (char c in text)
262 {
263 switch (c)
264 {
265 case '\n':
266 val = Math.Max(zero.X + Math.Max(num2, 0f), val);
267 num2 = 0f;
268 zero = Vector2.Zero;
269 zero.Y = LineSpacing;
270 flag = true;
271 num++;
272 continue;
273 case '\r':
274 continue;
275 }
277 Vector3 kerning = characterData.Kerning;
278 if (flag)
279 {
280 kerning.X = Math.Max(kerning.X, 0f);
281 }
282 else
283 {
284 zero.X += CharacterSpacing + num2;
285 }
286 zero.X += kerning.X + kerning.Y;
287 num2 = kerning.Z;
288 zero.Y = Math.Max(zero.Y, characterData.Padding.Height);
289 flag = false;
290 }
291 zero.X += Math.Max(num2, 0f);
292 zero.Y += num * LineSpacing;
293 zero.X = Math.Max(zero.X, val);
294 return zero;
295 }
296
297 public string CreateWrappedText(string text, float maxWidth)
298 {
299 return CreateWrappedText(text, maxWidth, Thread.CurrentThread.CurrentCulture);
300 }
301
302 public string CreateWrappedText(string text, float maxWidth, CultureInfo culture)
303 {
305 wrappedTextBuilder.Append(text);
306 return wrappedTextBuilder.ToString();
307 }
308
309 public string CreateCroppedText(string text, float maxWidth)
310 {
311 //IL_0002: Unknown result type (might be due to invalid IL or missing references)
312 //IL_0007: Unknown result type (might be due to invalid IL or missing references)
313 //IL_000e: Unknown result type (might be due to invalid IL or missing references)
314 //IL_0013: Unknown result type (might be due to invalid IL or missing references)
315 //IL_0015: Unknown result type (might be due to invalid IL or missing references)
316 //IL_001f: Unknown result type (might be due to invalid IL or missing references)
317 //IL_002d: Unknown result type (might be due to invalid IL or missing references)
318 //IL_0071: Unknown result type (might be due to invalid IL or missing references)
319 //IL_006b: Unknown result type (might be due to invalid IL or missing references)
320 //IL_0070: Unknown result type (might be due to invalid IL or missing references)
323 maxWidth -= vector2.X;
324 if (maxWidth <= vector2.X)
325 {
326 return "…";
327 }
328 if (vector.X > maxWidth)
329 {
330 int num = 200;
331 while (vector.X > maxWidth && text.Length > 1)
332 {
333 num--;
334 if (num <= 0)
335 {
336 break;
337 }
338 text = text.Substring(0, text.Length - 1);
339 if (text.Length == 1)
340 {
341 text = "";
342 break;
343 }
345 }
346 text += "…";
347 }
348 return text;
349 }
350
355}
DynamicSpriteFont(float spacing, int lineSpacing, char defaultCharacter)
string CreateWrappedText(string text, float maxWidth, CultureInfo culture)
string CreateCroppedText(string text, float maxWidth)
GlyphMetrics GetCharacterMetrics(char character)
bool AreCharactersSupported(IEnumerable< char > characters)
string CreateWrappedText(string text, float maxWidth)
SpriteCharacterData GetCharacterData(char character)
Dictionary< char, SpriteCharacterData > _spriteCharacters
bool IsCharacterSupported(char character)
void InternalDraw(string text, SpriteBatch spriteBatch, Vector2 startPosition, Color color, float rotation, Vector2 origin, ref Vector2 scale, SpriteEffects spriteEffects, float depth)
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
void Add(TKey key, TValue value)
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static Thread CurrentThread
Definition Thread.cs:312
static Matrix CreateTranslation(Vector3 position)
Definition Matrix.cs:438
static Matrix CreateRotationZ(float radians)
Definition Matrix.cs:746
static Vector2 Transform(Vector2 position, Matrix matrix)
Definition Vector2.cs:317
SpriteCharacterData(Texture2D texture, Rectangle glyph, Rectangle padding, Vector3 kerning)
static GlyphMetrics FromKerningData(float leftPadding, float characterWidth, float rightPadding)