-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCG16bitFont.cpp
1216 lines (915 loc) · 23.3 KB
/
CG16bitFont.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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// CG16bitFont.cpp
//
// Implementation of raw 16-bit image font object
#include <windows.h>
#include "Alchemy.h"
#include "DirectXUtil.h"
const int FONT_SAVE_VERSION = 1;
#define STR_ELLIPSIS CONSTLIT("...")
const int g_iStartChar = ' ';
const int g_iCharCount = 0xff - g_iStartChar + 1;
CG16bitFont CG16bitFont::m_DefaultFont;
void FormatLine (char *pPos, int iLen, bool *ioInSmartQuotes, TArray<CString> *retLines);
CG16bitFont::CG16bitFont (void)
// CG16bitFont constructor
{
}
CG16bitFont::CG16bitFont (const CG16bitFont &Src)
{
// Not Yet Implemented
ASSERT(false);
}
CG16bitFont &CG16bitFont:: operator= (const CG16bitFont &Src)
// CG16bitFont operator =
{
// Not Yet Implemented
ASSERT(false);
return *this;
}
int CG16bitFont::BreakText (const CString &sText, int cxWidth, TArray<CString> *retLines, DWORD dwFlags) const
// BreakText
//
// Splits the given string into multiple lines so that they fit
// in the given width
{
char *pPos = sText.GetASCIIZPointer();
char *pStartLine = pPos;
int iCharsInLine = 0;
char *pStartWord = pPos;
int iCharsInWord = 0;
int cxWordWidth = 0;
int cxRemainingWidth = cxWidth;
int iLines = 0;
if (retLines)
retLines->DeleteAll();
// Can't handle 0 widths
if (cxWidth <= 0)
return 0;
// Smart quotes
bool bInSmartQuotes = false;
bool *ioInSmartQuotes;
if (dwFlags & SmartQuotes)
ioInSmartQuotes = &bInSmartQuotes;
else
ioInSmartQuotes = NULL;
// If we need to truncate, then we need to adjust the width
if (dwFlags & TruncateLine)
{
cxWidth -= MeasureText(STR_ELLIPSIS);
if (cxWidth < 0)
return 0;
}
// Do it
bool bTruncate = false;
while (*pPos != '\0')
{
// If we've got a carriage return then we immediately end
// the line.
if (*pPos == '\n')
{
// Add the current word to the line
iCharsInLine += iCharsInWord;
// Add the line to the array
FormatLine(pStartLine, iCharsInLine, ioInSmartQuotes, retLines);
iLines++;
// Reset the line and word
pStartLine = pPos + 1;
pStartWord = pStartLine;
iCharsInLine = 0;
iCharsInWord = 0;
cxWordWidth = 0;
cxRemainingWidth = cxWidth;
// Reset smartquotes (sometimes we end a paragraph without closing
// a quote. In that case, we need to start with an open quote).
bInSmartQuotes = false;
// If we're truncating, we're out of here (we don't add an ellipsis)
if (dwFlags & TruncateLine)
break;
}
// Otherwise, continue by trying to add the character to the word
else
{
char chChar = *pPos;
if (chChar == '"' && ioInSmartQuotes) chChar = '“';
// Get the metrics for the character
int iIndex = (int)(BYTE)chChar - g_iStartChar;
iIndex = max(0, iIndex);
CharMetrics &Metrics = m_Metrics[iIndex];
// Does the character fit in the line?
bool bCharFits = ((cxRemainingWidth - cxWordWidth) >= Metrics.cxWidth);
// If the character doesn't fit, then we've reached the end
// of the line.
if (!bCharFits)
{
// If the character is a space then the entire word should
// fit on the line
if (*pPos == ' ')
{
iCharsInLine += iCharsInWord;
// Reset the word
pStartWord = pPos + 1;
iCharsInWord = 0;
cxWordWidth = 0;
}
// If this is the first word in the line then we need to break
// up the word across lines.
if (iCharsInLine == 0)
{
// Add what we've got to the array
FormatLine(pStartWord, iCharsInWord, ioInSmartQuotes, retLines);
iLines++;
// Reset the word
pStartWord = pPos;
pStartLine = pStartWord;
iCharsInWord = 1;
cxWordWidth = Metrics.cxAdvance;
cxRemainingWidth = cxWidth - cxWordWidth;
}
// Otherwise, add the line to the array
else
{
FormatLine(pStartLine, iCharsInLine, ioInSmartQuotes, retLines);
iLines++;
// Reset the line
pStartLine = pStartWord;
iCharsInLine = 0;
cxRemainingWidth = cxWidth;
// Add the character that didn't fit to the word
if (*pPos != ' ')
{
iCharsInWord++;
cxWordWidth += Metrics.cxAdvance;
}
}
// Done if we're truncating
if (dwFlags & TruncateLine)
{
iCharsInLine = 0;
iCharsInWord = 0;
bTruncate = true;
break;
}
}
// Otherwise, if it does fit, add it to the end of the word
else
{
iCharsInWord++;
cxWordWidth += Metrics.cxAdvance;
// If this character is a space or a hyphen, add it to the
// end of the line
if (*pPos == ' ' || *pPos == '-' || *pPos == '\x97')
{
iCharsInLine += iCharsInWord;
cxRemainingWidth -= cxWordWidth;
// Reset the word
pStartWord = pPos + 1;
iCharsInWord = 0;
cxWordWidth = 0;
}
}
}
// Next character
pPos++;
}
// Add the remainder
iCharsInLine += iCharsInWord;
if (iCharsInLine)
{
FormatLine(pStartLine, iCharsInLine, ioInSmartQuotes, retLines);
iLines++;
}
// Add ellipsis, if necessary
if (bTruncate && retLines)
retLines->GetAt(0).Append(STR_ELLIPSIS);
// Return the lines
return iLines;
}
ALERROR CG16bitFont::Create (const CString &sTypeface, int iSize, bool bBold, bool bItalic, bool bUnderline)
// Create
//
// Creates a basic font
{
ALERROR error;
DWORD dwQuality;
if (Absolute(iSize) <= 10)
dwQuality = ANTIALIASED_QUALITY;
else
dwQuality = PROOF_QUALITY;
DWORD dwCharSet;
if (strEquals(sTypeface, CONSTLIT("Wingdings")))
dwCharSet = SYMBOL_CHARSET;
else
dwCharSet = ANSI_CHARSET;
HFONT hFont = ::CreateFont(iSize,
0,
0,
0,
(bBold ? FW_BOLD : FW_NORMAL),
(bItalic ? TRUE : FALSE),
(bUnderline ? TRUE : FALSE),
FALSE,
dwCharSet,
OUT_TT_ONLY_PRECIS,
CLIP_DEFAULT_PRECIS,
dwQuality,
FF_DONTCARE,
sTypeface.GetASCIIZPointer());
error = CreateFromFont(hFont);
DeleteObject(hFont);
// HACK: For now we remember the styles from what was passed in
// (In the future, these should be set inside CreateFromFont from
// the actual font selected).
m_bBold = bBold;
m_bItalic = bItalic;
m_bUnderline = bUnderline;
return error;
}
ALERROR CG16bitFont::CreateFromFont (HFONT hFont)
// CreatFromFont
//
// Creates from a GDI font
{
ALERROR error;
HDC hDC = CreateCompatibleDC(NULL);
HFONT hOldFont = (HFONT)SelectObject(hDC, hFont);
HBITMAP hTempBmp = NULL;
HBITMAP hFontBmp = NULL;
HBITMAP hOldBitmap;
int i, y;
// Get some metrics. For some reason we need to recreate the DC after
// the GetTextMetrics or else the fonts won't be anti-aliased!
TEXTMETRIC tm;
GetTextMetrics(hDC, &tm);
SelectObject(hDC, hOldFont);
DeleteDC(hDC);
hDC = CreateCompatibleDC(NULL);
// Remember some for the whole font metrics
m_Metrics.DeleteAll();
m_cyHeight = tm.tmHeight;
m_cyAscent = tm.tmAscent;
m_cxAveWidth = tm.tmAveCharWidth;
// Create a bitmap that will contain all the font characters
if (error = dibCreate16bitDIB(tm.tmMaxCharWidth, tm.tmHeight * g_iCharCount, &hFontBmp, NULL))
goto Fail;
// Prepare the DC
hOldBitmap = (HBITMAP)SelectObject(hDC, hFontBmp);
hOldFont = (HFONT)SelectObject(hDC, hFont);
SetTextColor(hDC, RGB(255,255,255));
SetBkColor(hDC, RGB(0,0,0));
SetBkMode(hDC, TRANSPARENT);
// Get the name of the font that we selected
char szFontName[256];
::GetTextFace(hDC, sizeof(szFontName), szFontName);
m_sTypeface = CString(szFontName);
// Write each font character to the bitmap
y = 0;
m_Metrics.GrowToFit(g_iCharCount);
for (i = 0; i < g_iCharCount; i++)
{
char chChar = (char)(g_iStartChar + i);
// Blt
TextOut(hDC, 0, y, &chChar, 1);
// Remember some metrics
CharMetrics Metrics;
ABC abc;
GetCharABCWidths(hDC, (BYTE)chChar, (BYTE)chChar, &abc);
Metrics.cxWidth = abc.abcA + abc.abcB;
Metrics.cxAdvance = abc.abcA + abc.abcB + abc.abcC;
m_Metrics.Insert(Metrics);
#ifdef MOREDEBUG
kernelDebugLogPattern("char: %d width: %d advance: %d", (int)chChar, Metrics.cxWidth, Metrics.cxAdvance);
#endif
// Next
y += m_cyHeight;
}
SelectObject(hDC, hOldBitmap);
// Now apply the bitmap to our image
if (error = m_FontImage.CreateFromBitmap(NULL, hFontBmp, CG16bitImage::cfbDesaturateAlpha))
goto Fail;
// Done
DeleteObject(hFontBmp);
SelectObject(hDC, hOldFont);
DeleteDC(hDC);
return NOERROR;
Fail:
if (hFontBmp)
{
SelectObject(hDC, hOldBitmap);
DeleteObject(hFontBmp);
}
SelectObject(hDC, hOldFont);
DeleteDC(hDC);
return error;
}
ALERROR CG16bitFont::CreateFromResource (HINSTANCE hInst, char *pszRes)
// CreateFromResource
//
// Loads the font from a resource
// Use DirectXFont to save a font to a file
{
ALERROR error;
HRSRC hRes;
HGLOBAL hGlobalRes;
void *pImage;
int iSize;
// Load the resource
hRes = ::FindResource(hInst, pszRes, "DXFN");
if (hRes == NULL)
return ERR_FAIL;
iSize = ::SizeofResource(hInst, hRes);
if (iSize == 0)
return ERR_FAIL;
hGlobalRes = ::LoadResource(hInst, hRes);
if (hGlobalRes == NULL)
return ERR_FAIL;
pImage = ::LockResource(hGlobalRes);
if (pImage == NULL)
return ERR_FAIL;
// Load
CMemoryReadStream Stream((char *)pImage, iSize);
if (error = Stream.Open())
return error;
if (error = ReadFromStream(&Stream))
return error;
Stream.Close();
return NOERROR;
}
void CG16bitFont::DrawText (CG16bitImage &Dest,
int x,
int y,
WORD wColor,
DWORD byOpacity,
const CString &sText,
DWORD dwFlags,
int *retx) const
// DrawText
//
// Draws a line of text on the given image
{
char *pPos = sText.GetASCIIZPointer();
char *pEndPos = pPos + sText.GetLength();
int xPos = x;
int cxWidth = -1;
if (dwFlags & AlignCenter)
{
cxWidth = MeasureText(sText);
xPos -= cxWidth / 2;
}
else if (dwFlags & AlignRight)
{
cxWidth = MeasureText(sText);
xPos -= cxWidth;
}
else if (dwFlags & AdjustToFit)
cxWidth = MeasureText(sText);
// If necessary, adjust our position so we can fit inside the clip region
// of the destination.
if (dwFlags & AdjustToFit)
{
const RECT &rcClip = Dest.GetClipRect();
xPos = Min(Max((int)rcClip.left, xPos), (int)rcClip.right - cxWidth);
}
bool bInQuotes = false;
while (pPos < pEndPos)
{
// Get metrics
int iIndex = (int)(BYTE)(*pPos) - g_iStartChar;
iIndex = max(0, iIndex);
CharMetrics &Metrics = m_Metrics[iIndex];
// Paint
Dest.FillMask(0,
iIndex * m_cyHeight,
Metrics.cxWidth,
m_cyHeight,
m_FontImage,
wColor,
xPos,
y,
(BYTE)byOpacity);
pPos++;
xPos += Metrics.cxAdvance;
}
if (retx)
*retx = xPos;
}
void CG16bitFont::DrawText (CG16bitImage &Dest,
const RECT &rcRect,
WORD wColor,
DWORD byOpacity,
const CString &sText,
int iLineAdj,
DWORD dwFlags,
int *retcyHeight) const
// Draw
//
// Draws wrapped text
{
int i;
TArray<CString> Lines;
BreakText(sText, RectWidth(rcRect), &Lines, dwFlags);
int y = rcRect.top;
for (i = 0; i < Lines.GetCount(); i++)
{
int x = rcRect.left;
if (dwFlags & AlignCenter)
{
int cxWidth = MeasureText(Lines[i]);
x = rcRect.left + (RectWidth(rcRect) - cxWidth) / 2;
}
else if (dwFlags & AlignRight)
{
int cxWidth = MeasureText(Lines[i]);
x = rcRect.right - cxWidth;
}
if (!(dwFlags & MeasureOnly))
DrawText(Dest, x, y, wColor, byOpacity, Lines[i]);
y += m_cyHeight + iLineAdj;
}
if (retcyHeight)
*retcyHeight = y - rcRect.top;
}
void CG16bitFont::DrawText (CG32bitImage &Dest,
int x,
int y,
CG32bitPixel rgbColor,
const CString &sText,
DWORD dwFlags,
int *retx) const
// DrawText
//
// Draws a line of text on the given image
{
char *pPos = sText.GetASCIIZPointer();
char *pEndPos = pPos + sText.GetLength();
int xPos = x;
int cxWidth = -1;
if (dwFlags & AlignCenter)
{
cxWidth = MeasureText(sText);
xPos -= cxWidth / 2;
}
else if (dwFlags & AlignRight)
{
cxWidth = MeasureText(sText);
xPos -= cxWidth;
}
else if (dwFlags & AdjustToFit)
cxWidth = MeasureText(sText);
// If necessary, adjust our position so we can fit inside the clip region
// of the destination.
if (dwFlags & AdjustToFit)
{
const RECT &rcClip = Dest.GetClipRect();
xPos = Min(Max((int)rcClip.left, xPos), (int)rcClip.right - cxWidth);
}
bool bInQuotes = false;
while (pPos < pEndPos)
{
// Get metrics
int iIndex = (int)(BYTE)(*pPos) - g_iStartChar;
iIndex = max(0, iIndex);
CharMetrics &Metrics = m_Metrics[iIndex];
// Paint
Dest.FillMask(0,
iIndex * m_cyHeight,
Metrics.cxWidth,
m_cyHeight,
m_FontImage,
rgbColor,
xPos,
y);
pPos++;
xPos += Metrics.cxAdvance;
}
if (retx)
*retx = xPos;
}
void CG16bitFont::DrawText (CG32bitImage &Dest,
const RECT &rcRect,
CG32bitPixel rgbColor,
const CString &sText,
int iLineAdj,
DWORD dwFlags,
int *retcyHeight) const
// Draw
//
// Draws wrapped text
{
int i;
TArray<CString> Lines;
// Break into lines
BreakText(sText, RectWidth(rcRect), &Lines, dwFlags);
int iLines = Lines.GetCount();
int cyHeight = iLines * m_cyHeight + (iLineAdj * (iLines - 1));
// If we're truncating, see if we exceeded the vertical height.
int iEllipsisLine = -1;
if (dwFlags & TruncateBlock)
{
while (iLines > 1 && cyHeight > RectHeight(rcRect))
{
iLines--;
cyHeight -= (m_cyHeight + iLineAdj);
}
// If necessary, modify the last line to add an ellipsis
if (Lines.GetCount() != iLines)
{
int cxEllipsis = MeasureText(STR_ELLIPSIS);
CString &sLastLine = Lines[iLines - 1];
int cxLastLine = MeasureText(sLastLine) + cxEllipsis;
char *pPos = sLastLine.GetASCIIZPointer();
char *pPosEnd = pPos + sLastLine.GetLength();
while (pPosEnd > pPos && cxLastLine > RectWidth(rcRect))
{
pPosEnd--;
const CharMetrics &Metrics = GetCharMetrics(*pPosEnd);
cxLastLine -= Metrics.cxAdvance;
}
CString sNewLine = CString(pPos, (int)(pPosEnd - pPos));
sNewLine.Append(STR_ELLIPSIS);
Lines[iLines - 1] = sNewLine;
}
}
// Compute top alignment
int y = rcRect.top;
if (dwFlags & AlignMiddle)
{
y += (RectHeight(rcRect) - cyHeight) / 2;
}
// Paint each line
for (i = 0; i < iLines; i++)
{
int x = rcRect.left;
if (dwFlags & AlignCenter)
{
int cxWidth = MeasureText(Lines[i]);
x = rcRect.left + (RectWidth(rcRect) - cxWidth) / 2;
}
else if (dwFlags & AlignRight)
{
int cxWidth = MeasureText(Lines[i]);
x = rcRect.right - cxWidth;
}
if (!(dwFlags & MeasureOnly))
DrawText(Dest, x, y, rgbColor, Lines[i]);
y += m_cyHeight + iLineAdj;
}
// Done
if (retcyHeight)
*retcyHeight = y - rcRect.top;
}
void CG16bitFont::DrawText (CG32bitImage &Dest, int x, int y, CG32bitPixel rgbColor, const TArray<CString> &Lines, int iLineAdj, DWORD dwFlags, int *rety) const
// DrawText
//
// Draws an array of lines.
{
int i;
// Paint each line
for (i = 0; i < Lines.GetCount(); i++)
{
int xPos;
if (dwFlags & AlignCenter)
{
int cxWidth = MeasureText(Lines[i]);
xPos = x - (cxWidth / 2);
}
else if (dwFlags & AlignRight)
{
int cxWidth = MeasureText(Lines[i]);
xPos = x - cxWidth;
}
else
xPos = x;
if (!(dwFlags & MeasureOnly))
DrawText(Dest, x, y, rgbColor, Lines[i]);
y += m_cyHeight + iLineAdj;
}
// Done
if (rety)
*rety = y;
}
void CG16bitFont::DrawTextEffect (CG16bitImage &Dest,
int x,
int y,
WORD wColor,
const CString &sText,
int iEffectsCount,
const SEffectDesc *pEffects,
DWORD dwFlags,
int *retx) const
// DrawTextEffect
//
// Draw text with effect
{
int i;
// Paint background effects
for (i = 0; i < iEffectsCount; i++)
{
switch (pEffects[i].iType)
{
case effectShadow:
{
int xOffset = m_cyHeight / 16;
int yOffset = m_cyHeight / 16;
DrawText(Dest,
x + xOffset,
y + yOffset,
CG16bitImage::RGBValue(0, 0, 0),
sText,
dwFlags);
break;
}
}
}
// Paint
DrawText(Dest, x, y, wColor, sText, dwFlags, retx);
}
void CG16bitFont::DrawTextEffect (CG32bitImage &Dest,
int x,
int y,
CG32bitPixel rgbColor,
const CString &sText,
int iEffectsCount,
const SEffectDesc *pEffects,
DWORD dwFlags,
int *retx) const
// DrawTextEffect
//
// Draw text with effect
{
int i;
// Paint background effects
for (i = 0; i < iEffectsCount; i++)
{
switch (pEffects[i].iType)
{
case effectShadow:
{
int xOffset = m_cyHeight / 16;
int yOffset = m_cyHeight / 16;
DrawText(Dest,
x + xOffset,
y + yOffset,
CG32bitPixel(0, 0, 0),
sText,
dwFlags);
break;
}
}
}
// Paint
DrawText(Dest, x, y, rgbColor, sText, dwFlags, retx);
}
const CG16bitImage &CG16bitFont::GetCharacterImage (char chChar, int *retx, int *rety, int *retcxWidth, int *retcyHeight, int *retcxAdvance) const
// GetCharacterImage
//
// Returns the character image
{
// Get metrics
int iIndex = (int)(BYTE)(chChar) - g_iStartChar;
iIndex = Max(0, iIndex);
CharMetrics &Metrics = m_Metrics[iIndex];
// Paint
if (retx)
*retx = 0;
if (rety)
*rety = iIndex * m_cyHeight;
if (retcxWidth)
*retcxWidth = Metrics.cxWidth;
if (retcyHeight)
*retcyHeight = m_cyHeight;
if (retcxAdvance)
*retcxAdvance = Metrics.cxAdvance;
return m_FontImage;
}
const CG16bitFont::CharMetrics &CG16bitFont::GetCharMetrics (char chChar) const
// GetCharMetrics
//
// Return metrics for this character.
{
int iIndex = (int)(BYTE)chChar - g_iStartChar;
iIndex = Max(0, iIndex);
return m_Metrics[iIndex];
}
const CG16bitFont &CG16bitFont::GetDefault (void)
// GetDefault
//
// Returns a default font.
{
if (m_DefaultFont.IsEmpty())
{
m_DefaultFont.Create(CONSTLIT("Arial"), -16);
m_DefaultFont.m_sTypeface = NULL_STR;
}
return m_DefaultFont;
}
int CG16bitFont::MeasureText (const CString &sText, int *retcyHeight, bool bAlwaysAdvance) const
// MeasureText
//
// Returns the width of the text in pixels and optionally the height
{
int cxWidth = 0;
char *pPos = sText.GetASCIIZPointer();
char *pEndPos = pPos + sText.GetLength();
while (pPos != pEndPos)
{
int iIndex = (int)(BYTE)(*pPos) - g_iStartChar;
iIndex = max(0, iIndex);
CharMetrics &Metrics = m_Metrics[iIndex];
pPos++;
// The last character needs to have the whole width; all
// others just use the advance distance.
// (Except for spaces, which should always use the advance width
// otherwise, the cxWidth seems to be 0)
if (pPos == pEndPos && iIndex != 0 && !bAlwaysAdvance)
cxWidth += Metrics.cxWidth;
else
cxWidth += Metrics.cxAdvance;
}
// Done
if (retcyHeight)
*retcyHeight = m_cyHeight;
return cxWidth;
}
bool CG16bitFont::ParseFontDesc (const CString &sDesc, CString *retsTypeface, int *retiSize, bool *retbBold, bool *retbItalic)
// ParseFontDesc
//
// Parses a string of the form:
//
// {typeface} {font-size} [bold] [italic]
//
// Returns TRUE if valid
{
char *pPos = sDesc.GetASCIIZPointer();
CString sTypeface = CONSTLIT("Arial");
int iSize = 16;
bool bBold = false;
bool bItalic = false;
// Skip whitespace
while (*pPos == ' ' || *pPos == '\t')
pPos++;