forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextImage.cpp
985 lines (861 loc) · 23 KB
/
TextImage.cpp
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Implementation of vgui::TextImage control
//
// $NoKeywords: $
//=============================================================================//
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <malloc.h>
#include <vgui/IPanel.h>
#include <vgui/ISurface.h>
#include <vgui/IScheme.h>
#include <vgui/IInput.h>
#include <vgui/ILocalize.h>
#include <KeyValues.h>
#include <vgui_controls/TextImage.h>
#include <vgui_controls/Controls.h>
#include "tier0/dbg.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
// enable this define if you want unlocalized strings logged to files unfound.txt and unlocalized.txt
// #define LOG_UNLOCALIZED_STRINGS
using namespace vgui;
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
TextImage::TextImage(const char *text) : Image()
{
_utext = NULL;
_textBufferLen = 0;
_font = INVALID_FONT;
_fallbackFont = INVALID_FONT;
_unlocalizedTextSymbol = INVALID_LOCALIZE_STRING_INDEX;
_drawWidth = 0;
_textBufferLen = 0;
_textLen = 0;
m_bWrap = false;
m_bWrapCenter = false;
m_LineBreaks.RemoveAll();
m_LineXIndent.RemoveAll();
m_pwszEllipsesPosition = NULL;
m_bUseFallbackFont = false;
m_bRenderUsingFallbackFont = false;
m_bAllCaps = false;
SetText(text); // set the text.
}
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
TextImage::TextImage(const wchar_t *wszText) : Image()
{
_utext = NULL;
_textBufferLen = 0;
_font = INVALID_FONT;
_fallbackFont = INVALID_FONT;
_unlocalizedTextSymbol = INVALID_LOCALIZE_STRING_INDEX;
_drawWidth = 0;
_textBufferLen = 0;
_textLen = 0;
m_bWrap = false;
m_bWrapCenter = false;
m_LineBreaks.RemoveAll();
m_LineXIndent.RemoveAll();
m_bUseFallbackFont = false;
m_bRenderUsingFallbackFont = false;
m_bAllCaps = false;
SetText(wszText); // set the text.
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
TextImage::~TextImage()
{
delete [] _utext;
}
//-----------------------------------------------------------------------------
// Purpose: takes the string and looks it up in the localization file to convert it to unicode
//-----------------------------------------------------------------------------
void TextImage::SetText(const char *text)
{
if (!text)
{
text = "";
}
// check for localization
if (*text == '#')
{
// try lookup in localization tables
_unlocalizedTextSymbol = g_pVGuiLocalize->FindIndex(text + 1);
if (_unlocalizedTextSymbol != INVALID_LOCALIZE_STRING_INDEX)
{
wchar_t *unicode = g_pVGuiLocalize->GetValueByIndex(_unlocalizedTextSymbol);
SetText(unicode);
return;
}
else
{
// could not find string
// debug code for logging unlocalized strings
#if defined(LOG_UNLOCALIZED_STRINGS)
if (*text)
{
// write out error to unfound.txt log file
static bool first = true;
FILE *f;
if (first)
{
first = false;
f = fopen("unfound.txt", "wt");
}
else
{
f = fopen("unfound.txt", "at");
}
if (f)
{
fprintf(f, "\"%s\"\n", text);
fclose(f);
}
}
#endif // LOG_UNLOCALIZED_STRINGS
}
}
else
{
// debug code for logging unlocalized strings
#if defined(LOG_UNLOCALIZED_STRINGS)
if (text[0])
{
// setting a label to be ANSI text, write out error to unlocalized.txt log file
static bool first = true;
FILE *f;
if (first)
{
first = false;
f = fopen("unlocalized.txt", "wt");
}
else
{
f = fopen("unlocalized.txt", "at");
}
if (f)
{
fprintf(f, "\"%s\"\n", text);
fclose(f);
}
}
#endif // LOG_UNLOCALIZED_STRINGS
}
// convert the ansi string to unicode and use that
wchar_t unicode[1024];
g_pVGuiLocalize->ConvertANSIToUnicode(text, unicode, sizeof(unicode));
SetText(unicode);
}
//-----------------------------------------------------------------------------
// Purpose: Sets the width that the text can be.
//-----------------------------------------------------------------------------
void TextImage::SetDrawWidth(int width)
{
_drawWidth = width;
m_bRecalculateTruncation = true;
}
//-----------------------------------------------------------------------------
// Purpose: Gets the width that the text can be.
//-----------------------------------------------------------------------------
void TextImage::GetDrawWidth(int &width)
{
width = _drawWidth;
}
//-----------------------------------------------------------------------------
// Purpose: sets unicode text directly
//-----------------------------------------------------------------------------
void TextImage::SetText(const wchar_t *unicode, bool bClearUnlocalizedSymbol)
{
if ( bClearUnlocalizedSymbol )
{
// Clear out unlocalized text symbol so that changing dialog variables
// doesn't stomp over the custom unicode string we're being set to.
_unlocalizedTextSymbol = INVALID_LOCALIZE_STRING_INDEX;
}
if (!unicode)
{
unicode = L"";
}
// reallocate the buffer if necessary
_textLen = (short)wcslen(unicode);
if (_textLen >= _textBufferLen)
{
delete [] _utext;
_textBufferLen = (short)(_textLen + 1);
_utext = new wchar_t[_textBufferLen];
}
m_LineBreaks.RemoveAll();
m_LineXIndent.RemoveAll();
// store the text as unicode
wcscpy(_utext, unicode);
m_bRecalculateTruncation = true;
}
//-----------------------------------------------------------------------------
// Purpose: Gets the text in the textImage
//-----------------------------------------------------------------------------
void TextImage::GetText(char *buffer, int bufferSize)
{
g_pVGuiLocalize->ConvertUnicodeToANSI(_utext, buffer, bufferSize);
if ( m_bAllCaps )
{
// Uppercase all the letters
for ( int i = Q_strlen( buffer ); i >= 0; --i )
{
buffer[ i ] = toupper( buffer[ i ] );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Gets the text in the textImage
//-----------------------------------------------------------------------------
void TextImage::GetText(wchar_t *buffer, int bufLenInBytes)
{
wcsncpy(buffer, _utext, bufLenInBytes / sizeof(wchar_t));
if ( m_bAllCaps )
{
// Uppercase all the letters
for ( int i = Q_wcslen( buffer ) - 1; i >= 0; --i )
{
buffer[ i ] = towupper( buffer[ i ] );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
void TextImage::GetUnlocalizedText(char *buffer, int bufferSize)
{
if (_unlocalizedTextSymbol != INVALID_LOCALIZE_STRING_INDEX)
{
const char *text = g_pVGuiLocalize->GetNameByIndex(_unlocalizedTextSymbol);
buffer[0] = '#';
Q_strncpy(buffer + 1, text, bufferSize - 1);
buffer[bufferSize-1] = 0;
}
else
{
GetText(buffer, bufferSize);
}
}
//-----------------------------------------------------------------------------
// Purpose: unlocalized text symbol
//-----------------------------------------------------------------------------
StringIndex_t TextImage::GetUnlocalizedTextSymbol()
{
return _unlocalizedTextSymbol;
}
//-----------------------------------------------------------------------------
// Purpose: Changes the current font
//-----------------------------------------------------------------------------
void TextImage::SetFont(HFont font)
{
_font = font;
m_bRecalculateTruncation = true;
}
//-----------------------------------------------------------------------------
// Purpose: Gets the font of the text.
//-----------------------------------------------------------------------------
HFont TextImage::GetFont()
{
if ( m_bRenderUsingFallbackFont )
{
return _fallbackFont;
}
return _font;
}
//-----------------------------------------------------------------------------
// Purpose: Sets the size of the TextImage. This is directly tied to drawWidth
//-----------------------------------------------------------------------------
void TextImage::SetSize(int wide, int tall)
{
Image::SetSize(wide, tall);
_drawWidth = wide;
m_bRecalculateTruncation = true;
}
//-----------------------------------------------------------------------------
// Purpose: Draw the Image on screen.
//-----------------------------------------------------------------------------
void TextImage::Paint()
{
int wide, tall;
GetSize(wide, tall);
if (!_utext || GetFont() == INVALID_FONT )
return;
if (m_bRecalculateTruncation)
{
if ( m_bWrap || m_bWrapCenter )
{
RecalculateNewLinePositions();
}
RecalculateEllipsesPosition();
}
DrawSetTextColor(GetColor());
HFont font = GetFont();
DrawSetTextFont(font);
int lineHeight = surface()->GetFontTall(font);
float x = 0.0f;
int y = 0;
int iIndent = 0;
int iNextColorChange = 0;
int px, py;
GetPos(px, py);
int currentLineBreak = 0;
if ( m_bWrapCenter && m_LineXIndent.Count() )
{
x = m_LineXIndent[0];
}
for (wchar_t *wsz = _utext; *wsz != 0; wsz++)
{
wchar_t ch = wsz[0];
if ( m_bAllCaps )
{
ch = towupper( ch );
}
if ( m_ColorChangeStream.Count() > iNextColorChange )
{
if ( m_ColorChangeStream[iNextColorChange].textStreamIndex == (wsz - _utext) )
{
DrawSetTextColor( m_ColorChangeStream[iNextColorChange].color );
iNextColorChange++;
}
}
// check for special characters
if ( ch == '\r' || ch <= 8 )
{
// ignore, just use \n for newlines
continue;
}
else if (ch == '\n')
{
// newline
iIndent++;
if ( m_bWrapCenter && iIndent < m_LineXIndent.Count() )
{
x = m_LineXIndent[iIndent];
}
else
{
x = 0;
}
y += lineHeight;
continue;
}
else if (ch == '&')
{
// "&&" means draw a single ampersand, single one is a shortcut character
if (wsz[1] == '&')
{
// just move on and draw the second ampersand
wsz++;
}
else
{
// draw the underline, then continue to the next character without moving forward
#ifdef VGUI_DRAW_HOTKEYS_ENABLED
surface()->DrawSetTextPos(x + px, y + py);
surface()->DrawUnicodeChar('_');
#endif
continue;
}
}
// see if we've hit the truncated portion of the string
if (wsz == m_pwszEllipsesPosition)
{
// string is truncated, draw ellipses
for (int i = 0; i < 3; i++)
{
surface()->DrawSetTextPos(x + px, y + py);
surface()->DrawUnicodeChar('.');
x += surface()->GetCharacterWidth(font, '.');
}
break;
}
if (currentLineBreak != m_LineBreaks.Count())
{
if (wsz == m_LineBreaks[currentLineBreak])
{
// newline
iIndent++;
if ( m_bWrapCenter && iIndent < m_LineXIndent.Count() )
{
x = m_LineXIndent[iIndent];
}
else
{
x = 0;
}
y += lineHeight;
currentLineBreak++;
}
}
// Underlined text wants to draw the spaces anyway
#if USE_GETKERNEDCHARWIDTH
wchar_t chBefore = 0;
wchar_t chAfter = 0;
if ( wsz > _utext )
chBefore = wsz[-1];
chAfter = wsz[1];
float flWide = 0.0f, flabcA = 0.0f;
surface()->GetKernedCharWidth( font, ch, chBefore, chAfter, flWide, flabcA );
if ( ch == L' ' )
x = ceil( x );
surface()->DrawSetTextPos( x + px, y + py);
surface()->DrawUnicodeChar(ch);
x += floor(flWide + 0.6);
#else
surface()->DrawSetTextPos( x + px, y + py);
surface()->DrawUnicodeChar(ch);
x += surface()->GetCharacterWidth(font, ch);
#endif
}
// Useful debugging
/*
DrawSetColor(GetColor());
DrawOutlinedRect( 0,0, _drawWidth, tall );
*/
}
//-----------------------------------------------------------------------------
// Purpose: Get the size of a text string in pixels
//-----------------------------------------------------------------------------
void TextImage::GetTextSize(int &wide, int &tall)
{
wide = 0;
tall = 0;
int maxWide = 0;
const wchar_t *text = _utext;
HFont font = _font;
if ( font == INVALID_FONT )
return;
if ( m_bWrap || m_bWrapCenter )
{
RecalculateNewLinePositions();
}
// For height, use the remapped font
int fontHeight = surface()->GetFontTall(GetFont());
tall = fontHeight;
int textLen = wcslen(text);
for (int i = 0; i < textLen; i++)
{
wchar_t ch = text[i];
// handle stupid special characters, these should be removed
if ( ch == '&' )
{
continue;
}
if ( m_bAllCaps )
{
ch = towupper( ch );
}
#if USE_GETKERNEDCHARWIDTH
wchar_t chBefore = 0;
wchar_t chAfter = 0;
if ( i > 0 )
chBefore = text[i-1];
chAfter = text[i+1];
float flWide = 0.0f, flabcA;
surface()->GetKernedCharWidth( font, text[i], chBefore, chAfter, flWide, flabcA );
if ( text[i] == L' ' )
flWide = ceil( flWide );
wide += floor( flWide + 0.6);
#else
int a, b, c;
surface()->GetCharABCwide(font, ch, a, b, c);
wide += (a + b + c);
#endif
if (ch == '\n')
{
tall += fontHeight;
if(wide>maxWide)
{
maxWide=wide;
}
wide=0; // new line, wide is reset...
}
if ( m_bWrap || m_bWrapCenter )
{
for(int j=0; j<m_LineBreaks.Count(); j++)
{
if ( &text[i] == m_LineBreaks[j] )
{
tall += fontHeight;
if(wide>maxWide)
{
maxWide=wide;
}
wide=0; // new line, wide is reset...
}
}
}
}
#ifdef OSX
wide += 2;
if ( textLen < 3 )
wide += 3;
#endif
if (wide < maxWide)
{
// maxWide only gets set if a newline is in the label
wide = maxWide;
}
}
//-----------------------------------------------------------------------------
// Purpose: Get the size of the text in the image
//-----------------------------------------------------------------------------
void TextImage::GetContentSize(int &wide, int &tall)
{
GetTextSize(wide, tall);
}
//-----------------------------------------------------------------------------
// Purpose: Resize the text image to the content size
//-----------------------------------------------------------------------------
void TextImage::ResizeImageToContent()
{
int wide, tall;
GetContentSize(wide, tall);
SetSize(wide, tall);
}
//-----------------------------------------------------------------------------
// Purpose: Recalculates line breaks
//-----------------------------------------------------------------------------
void TextImage::RecalculateNewLinePositions()
{
HFont font = GetFont();
int charWidth;
int x = 0;
//int wordStartIndex = 0;
wchar_t *wordStartIndex = _utext;
int wordLength = 0;
bool hasWord = false;
bool justStartedNewLine = true;
bool wordStartedOnNewLine = true;
int startChar = 0;
// clear the line breaks list
m_LineBreaks.RemoveAll();
m_LineXIndent.RemoveAll();
// handle the case where this char is a new line, in that case
// we have already taken its break index into account above so skip it.
if (_utext[startChar] == '\r' || _utext[startChar] == '\n')
{
startChar++;
}
// loop through all the characters
for (wchar_t *wsz = &_utext[startChar]; *wsz != 0; wsz++)
{
// handle stupid special characters, these should be removed
// 0x01, 0x02 and 0x03 are color escape characters and should be ignored
if ( ( wsz[0] == '&' || wsz[0] == 0x01 || wsz[0] == 0x02 || wsz[0] == 0x03 ) && wsz[1] != 0 )
{
wsz++;
}
wchar_t ch = wsz[0];
if ( m_bAllCaps )
{
ch = towupper( ch );
}
// line break only on whitespace characters
if (!iswspace(ch))
{
if ( !hasWord )
{
// Start a new word
wordStartIndex = wsz;
hasWord = true;
wordStartedOnNewLine = justStartedNewLine;
wordLength = 0;
}
//else append to the current word
}
else
{
// whitespace/punctuation character
// end the word
hasWord = false;
}
// get the width
#if USE_GETKERNEDCHARWIDTH
wchar_t chBefore = 0;
wchar_t chAfter = 0;
if ( wsz > _utext )
chBefore = wsz[-1];
chAfter = wsz[1];
float flWide = 0.0f, flabcA = 0.0f;
surface()->GetKernedCharWidth( font, ch, chBefore, chAfter, flWide, flabcA );
charWidth = floor( flWide + 0.6 );
#else
charWidth = surface()->GetCharacterWidth(font, ch);
#endif
if (!iswcntrl(ch))
{
justStartedNewLine = false;
}
// check to see if the word is past the end of the line [wordStartIndex, i)
if ((x + charWidth) > _drawWidth || ch == '\r' || ch == '\n')
{
justStartedNewLine = true;
hasWord = false;
if (ch == '\r' || ch == '\n')
{
// set the break at the current character
//don't do this, paint will manually wrap on newline chars
// m_LineBreaks.AddToTail(i);
}
else if (wordStartedOnNewLine)
{
// word is longer than a line, so set the break at the current cursor
m_LineBreaks.AddToTail(wsz);
}
else
{
// set it at the last word Start
m_LineBreaks.AddToTail(wordStartIndex);
// just back to reparse the next line of text
// ywb 8/1/07: Back off one extra char since the 'continue' will increment wsz for us by one in the for loop
wsz = wordStartIndex - 1;
}
// reset word length
wordLength = 0;
x = 0;
continue;
}
// add to the size
x += charWidth;
wordLength += charWidth;
}
RecalculateCenterWrapIndents();
}
//-----------------------------------------------------------------------------
// Purpose: Calculates where the text should be truncated
//-----------------------------------------------------------------------------
void TextImage::RecalculateEllipsesPosition()
{
m_bRecalculateTruncation = false;
m_pwszEllipsesPosition = NULL;
//don't insert ellipses on wrapped strings
if ( m_bWrap || m_bWrapCenter )
return;
// don't truncate strings with newlines
if (wcschr(_utext, '\n') != NULL)
return;
if ( _drawWidth == 0 )
{
int h;
GetSize( _drawWidth, h );
}
for ( int check = 0; check < (m_bUseFallbackFont ? 2 : 1); ++check )
{
HFont font = GetFont();
if ( check == 1 && _fallbackFont != INVALID_FONT )
{
m_pwszEllipsesPosition = NULL;
font = _fallbackFont;
m_bRenderUsingFallbackFont = true;
}
int ellipsesWidth = 3 * surface()->GetCharacterWidth(font, '.');
int x = 0;
for (wchar_t *wsz = _utext; *wsz != 0; wsz++)
{
wchar_t ch = wsz[0];
if ( m_bAllCaps )
{
ch = towupper( ch );
}
// check for special characters
if (ch == '\r')
{
// ignore, just use \n for newlines
continue;
}
else if (ch == '&')
{
// "&&" means draw a single ampersand, single one is a shortcut character
if (wsz[1] == '&')
{
// just move on and draw the second ampersand
wsz++;
}
else
{
continue;
}
}
#if USE_GETKERNEDCHARWIDTH
wchar_t chBefore = 0;
wchar_t chAfter = 0;
if ( wsz > _utext )
chBefore = wsz[-1];
chAfter = wsz[1];
float flWide = 0.0f, flabcA = 0.0f;
surface()->GetKernedCharWidth( font, ch, chBefore, chAfter, flWide, flabcA );
int len = floor( flWide + 0.6 );
#else
int len = surface()->GetCharacterWidth(font, ch);
#endif
// don't truncate the first character
if (wsz == _utext)
{
x += len;
continue;
}
if (x + len + ellipsesWidth > _drawWidth)
{
// potential have an ellipses, see if the remaining characters will fit
int remainingLength = len;
for (const wchar_t *rwsz = wsz + 1; *rwsz != 0; rwsz++)
{
#if USE_GETKERNEDCHARWIDTH
wchar_t chBefore = 0;
wchar_t chAfter = 0;
if ( rwsz > _utext )
chBefore = rwsz[-1];
chAfter = rwsz[1];
float flWide = 0.0f, flabcA = 0.0f;
surface()->GetKernedCharWidth( font, *rwsz, chBefore, chAfter, flWide, flabcA );
int len = floor( flWide + 0.6 );
remainingLength += floor( flWide + 0.6 );
#else
remainingLength += surface()->GetCharacterWidth(font, *rwsz);
#endif
}
if (x + remainingLength > _drawWidth)
{
// looks like we've got an ellipses situation
m_pwszEllipsesPosition = wsz;
break;
}
}
x += len;
}
// Didn't need ellipses...
if ( !m_pwszEllipsesPosition )
break;
}
}
void TextImage::SetWrap( bool bWrap )
{
m_bWrap = bWrap;
}
void TextImage::SetCenterWrap( bool bWrap )
{
m_bWrapCenter = bWrap;
}
void TextImage::SetUseFallbackFont( bool bState, HFont hFallback )
{
m_bUseFallbackFont = bState;
_fallbackFont = hFallback;
}
void TextImage::SetAllCaps( bool bAllCaps )
{
m_bAllCaps = bAllCaps;
}
void TextImage::ResizeImageToContentMaxWidth( int nMaxWidth )
{
_drawWidth = nMaxWidth;
// Since we might have to use the "fallback" font, go ahead and recalc the ellipses state first to see if that's the case
// NOTE: I think there may be a race condition lurking here, but this seems to work.
if ( m_bRecalculateTruncation )
{
if ( m_bWrap || m_bWrapCenter )
{
RecalculateNewLinePositions();
}
RecalculateEllipsesPosition();
}
ResizeImageToContent();
}
//-----------------------------------------------------------------------------
// Purpose: For center wrapping of multi-line text images, determines the indent each line needs to be centered
//-----------------------------------------------------------------------------
void TextImage::RecalculateCenterWrapIndents()
{
m_LineXIndent.RemoveAll();
if ( !m_bWrapCenter )
return;
if (!_utext || GetFont() == INVALID_FONT )
return;
HFont font = GetFont();
int px, py;
GetPos(px, py);
int currentLineBreak = 0;
int iCurLineW = 0;
for (wchar_t *wsz = _utext; *wsz != 0; wsz++)
{
wchar_t ch = wsz[0];
if ( m_bAllCaps )
{
ch = towupper( ch );
}
// check for special characters
if (ch == '\r')
{
// ignore, just use \n for newlines
continue;
}
else if (ch == '\n')
{
int iIdx = m_LineXIndent.AddToTail();
m_LineXIndent[iIdx] = (_drawWidth - iCurLineW) * 0.5;
iCurLineW = 0;
continue;
}
else if (ch == '&')
{
// "&&" means draw a single ampersand, single one is a shortcut character
if (wsz[1] == '&')
{
// just move on and draw the second ampersand
wsz++;
}
else
{
// draw the underline, then continue to the next character without moving forward
continue;
}
}
// Don't need to check ellipses, they're not used when wrapping
if (currentLineBreak != m_LineBreaks.Count())
{
if (wsz == m_LineBreaks[currentLineBreak])
{
int iIdx = m_LineXIndent.AddToTail();
m_LineXIndent[iIdx] = (_drawWidth - iCurLineW) * 0.5;
iCurLineW = 0;
currentLineBreak++;
}
}
#if USE_GETKERNEDCHARWIDTH
wchar_t chBefore = 0;
wchar_t chAfter = 0;
if ( wsz > _utext )
chBefore = wsz[-1];
chAfter = wsz[1];
float flWide = 0.0f, flabcA = 0.0f;
surface()->GetKernedCharWidth( font, ch, chBefore, chAfter, flWide, flabcA );
iCurLineW += floor( flWide + 0.6 );
#else
iCurLineW += surface()->GetCharacterWidth(font, ch);
#endif
}
// Add the final line
int iIdx = m_LineXIndent.AddToTail();
m_LineXIndent[iIdx] = (_drawWidth - iCurLineW) * 0.5;
}
void TextImage::AddColorChange( Color col, int iTextStreamIndex )
{
label_colorchange_t tmpChange;
tmpChange.color = col;
tmpChange.textStreamIndex = iTextStreamIndex;
m_ColorChangeStream.Insert( tmpChange );
}
void TextImage::SetColorChangeStream( CUtlSortVector<label_colorchange_t,CColorChangeListLess> *pUtlVecStream )
{
ClearColorChangeStream();
m_ColorChangeStream = *pUtlVecStream;
}