-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathText.cpp
More file actions
665 lines (513 loc) · 16.3 KB
/
Text.cpp
File metadata and controls
665 lines (513 loc) · 16.3 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
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
/*******************************************************************
// Copyright (c) 2000, Robert Umbehant
// mailto:rumbehant@wheresjames.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307 USA
//
*******************************************************************/
// Text.cpp: implementation of the CText class.
//
//////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include <Math.h>
#ifdef DEBUG_NEW
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CText::CText()
{
m_hFont = NULL;
// Setup for default fonts
Default();
}
CText::~CText()
{
Destroy();
}
BOOL CText::SetFont(DWORD dwSize, LPCTSTR pFont )
{
HFONT hFont;
// Zero size or NULL pFont means use the default
if ( dwSize == 0 || pFont == NULL )
{
Destroy();
return TRUE;
} // end if
// Attempt to create a new font
hFont = CreateFont( dwSize, 0,
m_nEscapement, m_nEscapement,
m_nWeight,
m_bItalic, m_bUnderline, m_bStrikeOut,
ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH, pFont );
// Did we get the font
if ( hFont == NULL ) return FALSE;
// Lose the old font
Destroy();
// Save font information
m_logfont.lfHeight = dwSize;
m_logfont.lfWidth = 0;
m_logfont.lfEscapement = m_nEscapement;
m_logfont.lfOrientation = 0;
m_logfont.lfWeight = m_nWeight;
m_logfont.lfItalic = m_bItalic;
m_logfont.lfUnderline = m_bUnderline;
m_logfont.lfStrikeOut = m_bStrikeOut;
m_logfont.lfCharSet = ANSI_CHARSET;
m_logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
m_logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
m_logfont.lfQuality = DEFAULT_QUALITY;
m_logfont.lfPitchAndFamily = DEFAULT_PITCH;
strcpy( m_logfont.lfFaceName, pFont );
// Save our new font
m_hFont = hFont;
return TRUE;
}
void CText::Destroy()
{
if ( m_hFont != NULL )
{
DeleteObject( m_hFont );
m_hFont = NULL;
} // end if
ZeroMemory( &m_logfont, sizeof( m_logfont ) );
}
BOOL CText::DrawText( HDC hDC, LPCTSTR pText, LPRECT pRect )
{
RECT rect;
HFONT oldFont = NULL;
COLORREF oldTextColor;
// Sanity check
if ( hDC == NULL || pText == NULL || pRect == NULL )
return FALSE;
if ( m_rgbColor != MAXDWORD )
{
oldTextColor = ::GetTextColor( hDC );
::SetTextColor( hDC, m_rgbColor );
} // end if
// Have we been given a font to use?
if ( m_hFont != NULL ) oldFont = (HFONT)::SelectObject( hDC, m_hFont );
// Transparent text
int mode = ::GetBkMode( hDC );
::SetBkMode( hDC, TRANSPARENT );
// Adjust for margins
CopyRect( &rect, pRect );
InflateRect( &rect, -m_xMargin, -m_yMargin );
DWORD flags = ( m_dwFlags & EDT_NOEXTRA );
if ( ( m_dwFlags & EDT_MULTICENTER ) != 0 )
{
flags &= ~( DT_TOP | DT_VCENTER | DT_BOTTOM );
CalcRect( hDC, pText, &rect );
long tw = rect.right - rect.left + 2;
long th = rect.bottom - rect.top + 2;
long rw = pRect->right - pRect->left;
long rh = pRect->bottom - pRect->top;
long xoff = 0, yoff = 0;
if ( ( m_dwFlags & EDT_ESCAPEMENTCENTER ) != 0 &&
m_logfont.lfEscapement > 0 )
{
long deg = m_logfont.lfEscapement % 3600;
float rad = float( deg ) / ( float( 57.29582791 ) * float( 10 ) );
xoff = -long( float( ( tw >> 1 ) ) * cos( rad ) ) + ( tw >> 1 );
xoff -= long( float( ( th >> 1 ) ) * sin( rad ) );
yoff = -long( float( ( th >> 1 ) ) * cos( rad ) ) + ( th >> 1 );
yoff += long( float( ( tw >> 1 ) ) * sin( rad ) );
} // end if
rect.left = pRect->left + ( ( rw - tw ) >> 1 ) + xoff;
rect.right = rect.left + tw;
rect.top = pRect->top + ( ( rh - th ) >> 1 ) + yoff;
rect.bottom = rect.top + th;
} // end if
// Draw the text
BOOL ret = ( ::DrawTextEx( hDC, (char*)pText, strlen( pText ), &rect, flags, NULL ) != 0 );
// Restore bk mode
::SetBkMode( hDC, mode );
// Restore the text color
if ( m_rgbColor != MAXDWORD ) ::SetTextColor( hDC, oldTextColor );
// Restore the old font
if ( m_hFont != NULL ) ::SelectObject( hDC, oldFont );
return ret;
}
void CText::Default()
{
// Default font
Destroy();
// Default margins
m_xMargin = 1;
m_yMargin = 1;
// Default font stuff
m_nWeight = FW_NORMAL;
m_nEscapement = 0;
m_bItalic = FALSE;
m_bUnderline = FALSE;
m_bStrikeOut = FALSE;
SetFlags();
SetColor();
m_txtheight = 0;
m_txtwidth = 0;
SetRect( &m_txtbox, 0, 0, 0, 0 );
m_monotab = 4;
ZeroMemory( &m_monowidth, sizeof( m_monowidth ) );
}
BOOL CText::SetFont(LPLOGFONT pFont)
{
HFONT hFont;
// Zero size or NULL pFont means use the default
if ( pFont == NULL || *pFont->lfFaceName == NULL )
{
Destroy();
return TRUE;
} // end if
// Attempt to create a new font
hFont = CreateFontIndirect( pFont );
// Did we get the font
if ( hFont == NULL ) return FALSE;
// Lose the old font
Destroy();
// Save font information
memcpy( &m_logfont, pFont, sizeof( LOGFONT ) );
// Save our new font
m_hFont = hFont;
return TRUE;
}
BOOL CText::ChooseFont(LPLOGFONT pFont, HWND hWnd, BOOL bInit, COLORREF *pRGB )
{
if ( pFont == NULL ) return FALSE;
CHOOSEFONT cf;
cf.lStructSize = sizeof( cf );
cf.hwndOwner = hWnd;
cf.hDC = NULL;
cf.lpLogFont = pFont;
cf.iPointSize = 0;
cf.Flags = CF_EFFECTS | CF_FORCEFONTEXIST | CF_SCREENFONTS;
if ( bInit ) cf.Flags |= CF_INITTOLOGFONTSTRUCT;
if ( pRGB != NULL ) cf.rgbColors = *pRGB;
else cf.rgbColors = RGB( 0, 0, 0 );
cf.lCustData = NULL;
cf.lpfnHook = NULL;
cf.lpTemplateName = NULL;
cf.hInstance = NULL;
cf.lpszStyle = NULL;
cf.nFontType = 0;
cf.nSizeMin = 0;
cf.nSizeMax = 0;
BOOL ret = ::ChooseFont( &cf );
if ( !ret ) return FALSE;
if ( pRGB != NULL ) *pRGB = cf.rgbColors;
return TRUE;
}
BOOL CText::GetFont(LPLOGFONT pFont)
{
if ( pFont == NULL || m_hFont == NULL ) return FALSE;
memcpy( pFont, &m_logfont, sizeof( LOGFONT ) );
return TRUE;
}
BOOL CText::CalcRect(HDC hDC, LPCTSTR pText, LPRECT pRect)
{
HFONT oldFont = NULL;
// Sanity check
if ( hDC == NULL || pText == NULL || pRect == NULL )
return FALSE;
// Have we been given a font to use?
if ( m_hFont != NULL ) oldFont = (HFONT)::SelectObject( hDC, m_hFont );
// Adjust for margins
InflateRect( pRect, -m_xMargin, -m_yMargin );
// Calculate the bounding rectangle
DWORD flags = ( m_dwFlags & EDT_NOEXTRA ) | DT_CALCRECT;
if ( ( m_dwFlags & EDT_MULTICENTER ) != 0 )
flags &= ~( DT_TOP | DT_VCENTER | DT_BOTTOM );
BOOL ret = ( ::DrawTextEx( hDC, (char*)pText, strlen( pText ), pRect, flags, NULL ) != 0 );
// Restore the old font
if ( m_hFont != NULL ) ::SelectObject( hDC, oldFont );
return ret;
}
long CText::CalcTextHeight( HDC hDC, long *pHeight )
{
DWORD h = 0;
RECT rect;
// Zero width array if needed
if ( pHeight != NULL ) ZeroMemory( pHeight, sizeof( long ) * 256 );
if ( pHeight == NULL )
{
SetRect( &rect, 0, 0, 10000, 10 );
// Get the height
if ( !CalcRect( hDC, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890~!@#$%^&*(),./;'[]", &rect ) ) return 0;
// Save height
h = RH( rect );
} // end if
else
{
DWORD t;
char c[ 2 ] = { 0, 0 };
for ( c[ 0 ] = ' '; c[ 0 ] <= '~'; c[ 0 ]++ )
{
// Get character width
SetRect( &rect, 0, 0, 10000, 10 );
if ( CalcRect( hDC, c, &rect ) )
{
t = rect.right - rect.left;
// Save in array if any
if ( pHeight != NULL ) pHeight[ c[ 0 ] ] = t;
// Track largest width
if ( t > h ) h = t;
} // end if
} // end for
} // end else
// Return the new height
return h;
}
long CText::CalcTextWidth(HDC hDC, long *pWidth)
{
RECT rect;
// Zero width array if needed
if ( pWidth != NULL ) ZeroMemory( pWidth, sizeof( long ) * 256 );
// Get the width of the largest character
DWORD w = 0, t;
char c[ 2 ] = { 0, 0 };
for ( c[ 0 ] = ' '; c[ 0 ] <= '~'; c[ 0 ]++ )
{
// Get character width
SetRect( &rect, 0, 0, 10000, 10 );
if ( CalcRect( hDC, c, &rect ) )
{
t = rect.right - rect.left;
// Save in array if any
if ( pWidth != NULL ) pWidth[ c[ 0 ] ] = t;
// Track largest width
if ( t > w ) w = t;
} // end if
} // end for
// Return the largest width
return w;
}
BOOL CText::DrawMonoSpacedText(HDC hDC, LPCTSTR pText, LPRECT pRect)
{
RECT txt;
char t[ 2 ] = { 0, 0 };
HFONT oldFont = NULL;
COLORREF oldTextColor;
// Do we need to setup?
if ( m_txtwidth == 0 || m_txtheight == 0 )
{ SetMonoSpacedText( hDC );
if ( m_txtwidth == 0 || m_txtheight == 0 ) return FALSE;
} // end if
// Have we been given a font to use?
if ( m_hFont != NULL ) oldFont = (HFONT)::SelectObject( hDC, m_hFont );
if ( m_rgbColor != MAXDWORD )
{
oldTextColor = ::GetTextColor( hDC );
::SetTextColor( hDC, m_rgbColor );
} // end if
// Transparent text
int mode = ::GetBkMode( hDC );
::SetBkMode( hDC, TRANSPARENT );
// Draw the text mono-spaced
DWORD i = 0;
DWORD size = strlen( pText );
char last = 0;
long x = pRect->left;
long y = pRect->top;
while ( i < size )
{
// No need to keep going if past the bottom of rect
if ( y >= pRect->bottom ) return TRUE;
// Create one character string
t[ 0 ] = pText[ i++ ];
if ( t[ 0 ] >= ' ' )
{
// Where will the text go?
txt.left = x; txt.right = x + m_txtwidth;
txt.top = y; txt.bottom = y + m_txtheight;
// Center character
txt.left += ( m_txtwidth - m_monowidth[ t[ 0 ] ] ) >> 1;
// Draw text if anyone could see it
if ( txt.left < pRect->right && txt.top < pRect->bottom )
DrawTextEx( hDC, t, 1, &txt, DT_NOCLIP, NULL );
// Move over one character width
x += m_txtwidth;
} // end if
// Carriage return
else if ( t[ 0 ] == '\r' ) y += m_txtheight;
// Line feed
else if ( t[ 0 ] == '\n' )
{
// Auto detect CRLF or new line
if ( last != '\r' ) y += m_txtheight;
x = pRect->left;
} // end else if
// Tab character
else if ( t[ 0 ] == '\t' ) x += ( m_txtwidth * m_monotab );
// Track last character
last = t[ 0 ];
} // end for
// Restore bk mode
::SetBkMode( hDC, mode );
// Restore the text color
if ( m_rgbColor != MAXDWORD ) ::SetTextColor( hDC, oldTextColor );
// Restore the old font
if ( m_hFont != NULL ) ::SelectObject( hDC, oldFont );
return TRUE;
}
BOOL CText::ChooseFont(HWND hWnd)
{
if ( !ChooseFont( &m_logfont, hWnd, TRUE, &m_rgbColor ) )
return FALSE;
SetFont( &m_logfont );
SetColor( m_rgbColor );
return TRUE;
}
DWORD CText::HtmlText(LPSTR out, DWORD odw, LPCTSTR buf, DWORD size, BOOL bHyperLinks)
{
if ( out == NULL || odw == NULL || buf == NULL || size == 0 )
return FALSE;
DWORD opos = 0;
DWORD i = 0;
DWORD ptr = 0;
char buffer[ 4096 ];
DWORD max = sizeof( buffer ) >> 2;
DWORD maxcopy = max + ( max >> 1 );
while ( buf[ i ] != 0x0 && i < size )
{
// Check for hyper links
if ( bHyperLinks &&
(
!strnicmp( &buf[ i ], "https://", 8 ) ||
!strnicmp( &buf[ i ], "http://", 7 ) ||
!strnicmp( &buf[ i ], "ftp://", 6 ) ||
!strnicmp( &buf[ i ], "mailto:", 7 ) ||
!strnicmp( &buf[ i ], "news:", 7 ) ||
!strnicmp( &buf[ i ], "www.", 4 ) ||
!strnicmp( &buf[ i ], "ftp.", 4 ) )
)
{
// Start the link
strcpy( &buffer[ ptr ], "<a target=\"_Launch\" href=\"" ); ptr += 26;
// Must copy the protocol if not specified
if ( !strnicmp( &buf[ i ], "mailto:", 7 ) )
{/* strcpy( &buffer[ ptr ], "http://" ); ptr += 7; */}
else if ( !strnicmp( &buf[ i ], "news:", 5 ) )
{ strcpy( &buffer[ ptr ], "http://" ); ptr += 7; }
else if ( !strnicmp( &buf[ i ], "www.", 4 ) )
{ strcpy( &buffer[ ptr ], "http://" ); ptr += 7; }
else if ( !strnicmp( &buf[ i ], "ftp.", 4 ) )
{ strcpy( &buffer[ ptr ], "ftp://" ); ptr += 6; }
// Find start and end to link
DWORD start = i, end = i;
while( end < size && buf[ end ] > ' ' && buf[ end ] < '~' ) end++;
// Back up over punctuation
while( end > start &&
( buf[ end ] < '0' || buf[ end ] > '9' ) &&
( buf[ end ] < 'a' || buf[ end ] > 'z' ) &&
( buf[ end ] < 'A' || buf[ end ] > 'Z' ) &&
buf[ end ] != '_' && buf[ end ] != '/' ) end--;
// Copy the link
while( ptr < maxcopy && i <= end ) buffer[ ptr++ ] = buf[ i++ ];
// close tag
strcpy( &buffer[ ptr ], "\">" ); ptr += 2;
// Go ahead and flush
opos = WriteBuffer( out, opos, odw, buffer, ptr );
ptr = 0;
// Now copy the link as text
if ( odw > opos )
opos += HtmlText( &out[ opos ], odw - opos, &buf[ start ], ( end + 1 ) - start, FALSE );
// end tag
strcpy( &buffer[ ptr ], "</a>" ); ptr += 4;
} // end if
// Check for e-mail addresses
if ( bHyperLinks &&
(
buf[ i ] > ' ' && buf[ i ] < '~' &&
(
( buf[ i ] >= '0' && buf[ i ] <= '9' ) ||
( buf[ i ] >= 'a' && buf[ i ] <= 'z' ) ||
( buf[ i ] >= 'A' && buf[ i ] <= 'Z' ) ||
buf[ i ] == '_'
)
)
)
{
DWORD c = i;
BOOL bEmail = FALSE;
while( !bEmail && buf[ c ] > ' ' && buf[ c ] < '~' )
if ( buf[ c++ ] == '@' ) bEmail = TRUE;
if ( bEmail )
{
// Start the link
strcpy( &buffer[ ptr ], "<a target=\"_Launch\" href=\"mailto:" ); ptr += 33;
// Find start and end to link
DWORD start = i, end = i;
while( end < size && buf[ end ] > ' ' && buf[ end ] < '~' ) end++;
// Back up over punctuation
while( end > start &&
( buf[ end ] < '0' || buf[ end ] > '9' ) &&
( buf[ end ] < 'a' || buf[ end ] > 'z' ) &&
( buf[ end ] < 'A' || buf[ end ] > 'Z' ) &&
buf[ end ] != '_' && buf[ end ] != '/' ) end--;
// Copy the link
while( ptr < maxcopy && i <= end ) buffer[ ptr++ ] = buf[ i++ ];
// close tag
strcpy( &buffer[ ptr ], "\">" ); ptr += 2;
// Go ahead and flush
opos = WriteBuffer( out, opos, odw, buffer, ptr );
ptr = 0;
// Now copy the link as text
if ( odw > opos )
opos += HtmlText( &out[ opos ], odw - opos, &buf[ start ], ( end + 1 ) - start, FALSE );
// end tag
strcpy( &buffer[ ptr ], "</a>" ); ptr += 4;
} // end if
} // end if
if ( buf[ i ] == '<' )
{ strcpy( &buffer[ ptr ], "<" ); ptr += 4; }
else if ( buf[ i ] == '&' )
{ strcpy( &buffer[ ptr ], "&" ); ptr += 5; }
else if ( buf[ i ] == '>' )
{ strcpy( &buffer[ ptr ], ">" ); ptr += 4; }
else if ( buf[ i ] == '\"' )
{ strcpy( &buffer[ ptr ], """ ); ptr += 6; }
else if ( buf[ i ] == ' ' && i > 0 && buf[ i - 1 ] == ' ' )
{ strcpy( &buffer[ ptr ], " " ); ptr += 6; }
else if ( buf[ i ] >= ' ' && buf[ i ] <= '~' )
buffer[ ptr++ ] = buf[ i ];
else if ( buf[ i ] == '\n' )
{ strcpy( &buffer[ ptr ], "<br>\r\n" ); ptr += 6; }
// Flush to disk
if ( ptr > max )
{ opos = WriteBuffer( out, opos, odw, buffer, ptr ); ptr = 0; }
i++;
} // end while
// Write out last bit of data if any
if ( ptr !=0 ) opos = WriteBuffer( out, opos, odw, buffer, ptr );
return opos;
}
DWORD CText::WriteBuffer(LPSTR out, DWORD op, DWORD om, LPCTSTR buf, DWORD size)
{
// Sanity check
if ( out == NULL || buf == NULL || size == 0 || op >= om ) return 0;
if ( size > ( om - op ) ) size = om - op;
// Copy the data
memcpy( &out[ op ], buf, size );
// Return new pointer
return ( op + size );
}