-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTextMeshProExtensions.cs
More file actions
515 lines (473 loc) · 22.9 KB
/
Copy pathTextMeshProExtensions.cs
File metadata and controls
515 lines (473 loc) · 22.9 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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
using System;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
namespace TextMeshProMax.Runtime.Helper
{
public static class TextMeshProExtensions
{
/// <summary>
/// Attempts to retrieve the Rect information for a specific string within a TMP_Text object.
/// </summary>
/// <param name="text">The target TMP_Text object.</param>
/// <param name="targetString">The string to find within the text.</param>
/// <param name="findMode">
/// The mode to find the string (first occurrence, all occurrences, etc.):
/// <list type="bullet">
/// <item>
/// <description>TextFindMode.First: Returns a list containing the Rect values for the first occurrence of the specified string.</description>
/// </item>
/// <item>
/// <description>TextFindMode.All: Returns a list containing the Rect values for all occurrences of the specified string.</description>
/// </item>
/// </list>
/// </param>
/// <param name="results">The output list of TextRectInfo containing the Rect values for the specified string.</param>
/// <returns>True if the string was found and Rect information was retrieved; otherwise, false.</returns>
public static bool TryGetStringRects(this TMP_Text text, string targetString, TextFindMode findMode,
out List<TextRectInfo> results)
{
return TryGetStringRectsInternal(new TMPTextWrapper(text), targetString, findMode, out results);
}
/// <summary>
/// Attempts to retrieve the Rect information for a specific string within a TextMeshPro object.
/// </summary>
/// <param name="text">The target TextMeshPro object.</param>
/// <param name="targetString">The string to find within the text.</param>
/// <param name="findMode">
/// The mode to find the string (first occurrence, all occurrences, etc.):
/// <list type="bullet">
/// <item>
/// <description>TextFindMode.First: Returns a list containing the Rect values for the first occurrence of the specified string.</description>
/// </item>
/// <item>
/// <description>TextFindMode.All: Returns a list containing the Rect values for all occurrences of the specified string.</description>
/// </item>
/// </list>
/// </param>
/// <param name="results">The output list of TextRectInfo containing the Rect values for the specified string.</param>
/// <returns>True if the string was found and Rect information was retrieved; otherwise, false.</returns>
public static bool TryGetStringRects(this TextMeshPro text, string targetString, TextFindMode findMode,
out List<TextRectInfo> results)
{
return TryGetStringRects((TMP_Text)text, targetString, findMode, out results);
}
/// <summary>
/// Attempts to retrieve the Rect information for a specific string within a TextMeshProUGUI object.
/// </summary>
/// <param name="text">The target TextMeshProUGUI object.</param>
/// <param name="targetString">The string to find within the text.</param>
/// <param name="findMode">
/// The mode to find the string (first occurrence, all occurrences, etc.):
/// <list type="bullet">
/// <item>
/// <description>TextFindMode.First: Returns a list containing the Rect values for the first occurrence of the specified string.</description>
/// </item>
/// <item>
/// <description>TextFindMode.All: Returns a list containing the Rect values for all occurrences of the specified string.</description>
/// </item>
/// </list>
/// </param>
/// <param name="results">The output list of TextRectInfo containing the Rect values for the specified string.</param>
/// <returns>True if the string was found and Rect information was retrieved; otherwise, false.</returns>
public static bool TryGetStringRects(this TextMeshProUGUI text, string targetString, TextFindMode findMode,
out List<TextRectInfo> results)
{
return TryGetStringRects((TMP_Text)text, targetString, findMode, out results);
}
/// <summary>
/// Retrieves the Rect information for a specific string within a TMP_Text object.
/// The returned Rect values are in the local space of the text object's transform.
/// </summary>
/// <param name="text">The target TMP_Text object.</param>
/// <param name="targetString">The string to find within the text.</param>
/// <param name="findMode">
/// The mode to find the string (first occurrence, all occurrences, etc.):
/// <list type="bullet">
/// <item>
/// <description>TextFindMode.First: Returns a list containing the Rect values for the first occurrence of the specified string.</description>
/// </item>
/// <item>
/// <description>TextFindMode.All: Returns a list containing the Rect values for all occurrences of the specified string.</description>
/// </item>
/// </list>
/// </param>
/// <returns>A list of TextRectInfo containing the Rect values for the specified string.</returns>
/// <example>
/// <code>
/// TMP_Text text = GetComponent<TMP_Text>();
/// var rects = text.GetStringRects("Hello", TextFindMode.All);
///
/// foreach (var rectInfo in rects)
/// {
/// foreach (var rect in rectInfo.Rects)
/// {
/// Debug.Log("BottomLeft: " + rect.min + ", TopRight: " + rect.max + ", String: " + rectInfo.TargetString);
/// }
/// }
/// </code>
/// </example>
public static List<TextRectInfo> GetStringRects(
this TMP_Text text,
string targetString,
TextFindMode findMode = TextFindMode.First)
{
var wrapper = new TMPTextWrapper(text);
return TryGetStringRectsInternal(wrapper, targetString, findMode, out var results)
? results
: new List<TextRectInfo>();
}
/// <summary>
/// Retrieves the Rect information for a specific string within a TextMeshPro object.
/// The returned Rect values are in the text object's local space (relative to the text object's transform).
/// </summary>
/// <param name="text">The target TextMeshPro object.</param>
/// <param name="targetString">The string to find within the text.</param>
/// <param name="findMode">
/// The mode to find the string (first occurrence, all occurrences, etc.):
/// <list type="bullet">
/// <item>
/// <description>TextFindMode.First: Returns a list containing the Rect values for the first occurrence of the specified string.</description>
/// </item>
/// <item>
/// <description>TextFindMode.All: Returns a list containing the Rect values for all occurrences of the specified string.</description>
/// </item>
/// </list>
/// </param>
/// <returns>A list of TextRectInfo containing the Rect values for the specified string.</returns>
/// <example>
/// <code>
/// TextMeshPro text = GetComponent<TextMeshPro>();
/// var rects = text.GetStringRects("Hello", TextFindMode.All);
///
/// foreach (var rectInfo in rects)
/// {
/// foreach (var rect in rectInfo.Rects)
/// {
/// Debug.Log("BottomLeft: " + rect.min + ", TopRight: " + rect.max + ", String: " + rectInfo.TargetString);
/// }
/// }
/// </code>
/// </example>
public static List<TextRectInfo> GetStringRects(
this TextMeshPro text,
string targetString,
TextFindMode findMode = TextFindMode.First)
{
return GetStringRects((TMP_Text)text, targetString, findMode);
}
/// <summary>
/// Retrieves the Rect information for a specific string within a TextMeshProUGUI object.
/// The returned Rect values are in the canvas's local space (relative to the canvas transform).
/// </summary>
/// <param name="text">The target TextMeshProUGUI object.</param>
/// <param name="targetString">The string to find within the text.</param>
/// <param name="findMode">
/// The mode to find the string (first occurrence, all occurrences, etc.):
/// <list type="bullet">
/// <item>
/// <description>TextFindMode.First: Returns a list containing the Rect values for the first occurrence of the specified string.</description>
/// </item>
/// <item>
/// <description>TextFindMode.All: Returns a list containing the Rect values for all occurrences of the specified string.</description>
/// </item>
/// </list>
/// </param>
/// <returns>A list of TextRectInfo containing the Rect values for the specified string.</returns>
/// <example>
/// <code>
/// TextMeshProUGUI text = GetComponent<TextMeshProUGUI>();
/// var rects = text.GetStringRects("Hello", TextFindMode.All);
///
/// foreach (var rectInfo in rects)
/// {
/// foreach (var rect in rectInfo.Rects)
/// {
/// Debug.Log("BottomLeft: " + rect.min + ", TopRight: " + rect.max + ", String: " + rectInfo.TargetString);
/// }
/// }
/// </code>
/// </example>
public static List<TextRectInfo> GetStringRects(
this TextMeshProUGUI text,
string targetString,
TextFindMode findMode = TextFindMode.First)
{
return GetStringRects((TMP_Text)text, targetString, findMode);
}
#if RUBY_TEXT_SUPPORT
/// <summary>
/// Attempts to retrieve the Rect information for a specific RubyString within a RubyTextMeshPro object.
/// </summary>
/// <param name="text">The target RubyTextMeshPro object.</param>
/// <param name="rubyString">The RubyString to find within the text.</param>
/// <param name="findMode">
/// The mode to find the string (first occurrence, all occurrences, etc.):
/// <list type="bullet">
/// <item>
/// <description>TextFindMode.First: Returns a list containing the Rect values for the first occurrence of the specified string.</description>
/// </item>
/// <item>
/// <description>TextFindMode.All: Returns a list containing the Rect values for all occurrences of the specified string.</description>
/// </item>
/// </list>
/// </param>
/// <param name="results">The output list of TextRectInfo containing the Rect values for the specified string.</param>
/// <returns>True if the string was found and Rect information was retrieved; otherwise, false.</returns>
public static bool TryGetRubyStringRects(
this RubyTextMeshPro text,
RubyString rubyString,
TextFindMode findMode,
out List<TextRectInfo> results)
{
if (!rubyString.IsValid())
{
Debug.LogWarning("[RubyText] Invalid RubyString provided. Ensure all elements are valid.");
results = new List<TextRectInfo>();
return false;
}
return TryGetStringRectsInternal(new TMPTextWrapper(text), rubyString.ToPlainString(), findMode,
out results);
}
/// <summary>
/// Attempts to retrieve the Rect information for a specific RubyString within a RubyTextMeshProUGUI object.
/// </summary>
/// <param name="text">The target RubyTextMeshProUGUI object.</param>
/// <param name="rubyString">The RubyString to find within the text.</param>
/// <param name="findMode">
/// The mode to find the string (first occurrence, all occurrences, etc.):
/// <list type="bullet">
/// <item>
/// <description>TextFindMode.First: Returns a list containing the Rect values for the first occurrence of the specified string.</description>
/// </item>
/// <item>
/// <description>TextFindMode.All: Returns a list containing the Rect values for all occurrences of the specified string.</description>
/// </item>
/// </list>
/// </param>
/// <param name="results">The output list of TextRectInfo containing the Rect values for the specified string.</param>
/// <returns>True if the string was found and Rect information was retrieved; otherwise, false.</returns>
public static bool TryGetRubyStringRects(
this RubyTextMeshProUGUI text,
RubyString rubyString,
TextFindMode findMode,
out List<TextRectInfo> results)
{
if (!rubyString.IsValid())
{
Debug.LogWarning("[RubyText] Invalid RubyString provided. Ensure all elements are valid.");
results = new List<TextRectInfo>();
return false;
}
return TryGetStringRectsInternal(new TMPTextWrapper(text), rubyString.ToPlainString(), findMode,
out results);
}
/// <summary>
/// Retrieves the Rect information for a complex RubyString consisting of multiple RubyElement entries.
/// The returned Rect values are in the canvas's local space (relative to the canvas transform).
/// </summary>
/// <param name="rubyText">The target RubyTextMeshProUGUI object.</param>
/// <param name="rubyString">The RubyString to find within the text.</param>
/// <param name="findMode">
/// The mode to find the string (first occurrence, all occurrences, etc.):
/// <list type="bullet">
/// <item>
/// <description>TextFindMode.First: Returns a list containing the Rect values for the first occurrence of the specified string.</description>
/// </item>
/// <item>
/// <description>TextFindMode.All: Returns a list containing the Rect values for all occurrences of the specified string.</description>
/// </item>
/// </list>
/// </param>
/// <returns>A list of TextRectInfo containing the Rect values for the specified string.</returns>
/// <example>
/// <code>
/// RubyTextMeshProUGUI rubyText = GetComponent<RubyTextMeshProUGUI>();
/// RubyString rubyString = new RubyString(new List<RubyElement> { ... });
/// var rects = rubyText.GetRubyStringRects(rubyString, TextFindMode.All);
///
/// foreach (var rectInfo in rects)
/// {
/// foreach (var rect in rectInfo.Rects)
/// {
/// Debug.Log("BottomLeft: " + rect.min + ", TopRight: " + rect.max + ", String: " + rectInfo.TargetString);
/// }
/// }
/// </code>
/// </example>
public static List<TextRectInfo> GetRubyStringRects(
this RubyTextMeshProUGUI rubyText,
RubyString rubyString,
TextFindMode findMode = TextFindMode.First)
{
if (!rubyString.IsValid())
{
Debug.LogWarning("[RubyText] Invalid RubyString provided. Ensure all elements are valid.");
return new List<TextRectInfo>();
}
// Convert RubyString into a single plain search string
var searchString = rubyString.ToPlainString();
return GetStringRects((TMP_Text)rubyText, searchString, findMode);
}
/// <summary>
/// Retrieves the Rect information for a complex RubyString consisting of multiple RubyElement entries.
/// The returned Rect values are in the text object's local space (relative to the text object's transform).
/// </summary>
/// <param name="rubyText">The target RubyTextMeshPro object.</param>
/// <param name="rubyString">The RubyString to find within the text.</param>
/// <param name="findMode">
/// The mode to find the string (first occurrence, all occurrences, etc.):
/// <list type="bullet">
/// <item>
/// <description>TextFindMode.First: Returns a list containing the Rect values for the first occurrence of the specified string.</description>
/// </item>
/// <item>
/// <description>TextFindMode.All: Returns a list containing the Rect values for all occurrences of the specified string.</description>
/// </item>
/// </list>
/// </param>
/// <returns>A list of TextRectInfo containing the Rect values for the specified string.</returns>
/// <example>
/// <code>
/// RubyTextMeshPro rubyText = GetComponent<RubyTextMeshPro>();
/// RubyString rubyString = new RubyString(new List<RubyElement> { ... });
/// var rects = rubyText.GetRubyStringRects(rubyString, TextFindMode.All);
///
/// foreach (var rectInfo in rects)
/// {
/// foreach (var rect in rectInfo.Rects)
/// {
/// Debug.Log("BottomLeft: " + rect.min + ", TopRight: " + rect.max + ", String: " + rectInfo.TargetString);
/// }
/// }
/// </code>
/// </example>
public static List<TextRectInfo> GetRubyStringRects(
this RubyTextMeshPro rubyText,
RubyString rubyString,
TextFindMode findMode = TextFindMode.First)
{
if (!rubyString.IsValid())
{
Debug.LogWarning("[RubyText] Invalid RubyString provided. Ensure all elements are valid.");
return new List<TextRectInfo>();
}
// Convert RubyString into a single plain search string
var searchString = rubyString.ToPlainString();
return GetStringRects((TMP_Text)rubyText, searchString, findMode);
}
#endif
/// <summary>
/// Internal method that processes the logic for retrieving Rects for a target string, line by line.
/// </summary>
private static bool TryGetStringRectsInternal(
ITMPTextBase textBase,
string targetString,
TextFindMode findMode,
out List<TextRectInfo> results)
{
results = new List<TextRectInfo>();
if (textBase == null || string.IsNullOrEmpty(targetString))
return false;
// Force update to ensure the text mesh is updated
textBase.ForceMeshUpdate();
var textInfo = textBase.TextInfo;
var plainText = textBase.GetParsedText();
// Find occurrences of the target string in the plain text
var startIndexes = FindStringOccurrences(plainText, targetString, findMode);
if (startIndexes.Count == 0)
return false;
foreach (var startIndex in startIndexes)
{
// Calculate character indices for each occurrence
var charIndexes = GetCharacterIndexes(textInfo, startIndex, targetString.Length);
if (charIndexes.Count == 0) continue;
// Split into lines and calculate Rect for each line
var lineRects = CalculateLineBoundingRects(charIndexes, textInfo);
if (lineRects.Count > 0)
{
results.Add(new TextRectInfo
{
Rects = lineRects,
TargetString = targetString
});
}
}
return results.Count > 0;
}
/// <summary>
/// Calculates the bounding rectangles for the given character indices, grouped by lines.
/// </summary>
private static List<Rect> CalculateLineBoundingRects(
List<int> charIndexes,
TMP_TextInfo textInfo)
{
var rects = new List<Rect>();
var lineGroups = charIndexes
.GroupBy(index => textInfo.characterInfo[index].lineNumber)
.OrderBy(group => group.Key);
foreach (var lineGroup in lineGroups)
{
var lineCharIndexes = lineGroup.ToList();
var rect = CalculateBoundingRect(lineCharIndexes, textInfo);
if (rect.HasValue)
rects.Add(rect.Value);
}
return rects;
}
/// <summary>
/// Calculates the bounding rectangle for a list of character indices.
/// </summary>
private static Rect? CalculateBoundingRect(
List<int> charIndexes,
TMP_TextInfo textInfo)
{
if (charIndexes == null || charIndexes.Count == 0)
return null;
var firstCharInfo = textInfo.characterInfo[charIndexes[0]];
var bottomLeft = (Vector2)firstCharInfo.bottomLeft;
var topRight = (Vector2)firstCharInfo.topRight;
foreach (var index in charIndexes.Skip(1))
{
var charInfo = textInfo.characterInfo[index];
var charBottomLeft = (Vector2)charInfo.bottomLeft;
var charTopRight = (Vector2)charInfo.topRight;
bottomLeft = Vector2.Min(bottomLeft, charBottomLeft);
topRight = Vector2.Max(topRight, charTopRight);
}
return Rect.MinMaxRect(bottomLeft.x, bottomLeft.y, topRight.x, topRight.y);
}
private static List<int> FindStringOccurrences(string plainText, string targetString, TextFindMode findMode)
{
var startIndexes = new List<int>();
int currentIndex = 0;
while (currentIndex < plainText.Length)
{
var foundIndex = plainText.IndexOf(targetString, currentIndex, StringComparison.Ordinal);
if (foundIndex == -1) break;
startIndexes.Add(foundIndex);
if (findMode == TextFindMode.First) break;
currentIndex = foundIndex + targetString.Length;
}
return startIndexes;
}
private static List<int> GetCharacterIndexes(TMP_TextInfo textInfo, int startIndex, int length)
{
var indexes = new List<int>();
var plainIndex = 0;
var inRichText = false;
for (int i = 0; i < textInfo.characterCount; i++)
{
var charInfo = textInfo.characterInfo[i];
var character = charInfo.character;
if (character == '<') inRichText = true;
if (!inRichText && plainIndex >= startIndex && plainIndex < startIndex + length)
indexes.Add(i);
if (!inRichText) plainIndex++;
if (character == '>' && inRichText) inRichText = false;
}
return indexes;
}
}
}