-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDrawText.cpp
137 lines (107 loc) · 3.58 KB
/
DrawText.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
// CGDraw.cpp
//
// CGDraw Class
// Copyright (c) 2015 by Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
void CGDraw::Text (CG32bitImage &Dest, const CVector &vPos, const CString &sText, const CG16bitFont &Font, CG32bitPixel rgbColor, EBlendModes iMode, Metric rRotation, DWORD dwFlags)
// Text
//
// Draw a single line of text
{
// Create a bitmap containing the entire line of text (we need to do this
// because we're going to transform the result, and this is more efficient
// than trying to transform each character separately).
int cyText;
int cxText = Font.MeasureText(sText, &cyText, true);
CG8bitImage Line;
Line.Create(cxText, cyText, 0);
// Get the source font image. We expect it to have an alpha channel.
const CG16bitImage &FontImage = Font.GetCharacterImage();
BYTE *pFontAlpha = FontImage.GetAlphaRow(0);
if (pFontAlpha == NULL)
return;
int iFontAlphaRow = FontImage.GetAlphaRow(1) - FontImage.GetAlphaRow(0);
// Blt the characters to our line image
BYTE *pDestStart = Line.GetPixelPos(0, 0);
char *pPos = sText.GetASCIIZPointer();
char *pPosEnd = pPos + sText.GetLength();
int xPos = 0;
while (pPos < pPosEnd)
{
int xSrc, ySrc, cxSrc, cySrc, cxAdvance;
const CG16bitImage &Char = Font.GetCharacterImage(*pPos, &xSrc, &ySrc, &cxSrc, &cySrc, &cxAdvance);
// Blt
BYTE *pSrcRow = pFontAlpha + (ySrc * iFontAlphaRow) + xSrc;
BYTE *pDestRow = Line.GetPixelPos(xPos, 0);
BYTE *pDestRowEnd = Line.GetPixelPos(xPos, cyText);
while (pDestRow < pDestRowEnd)
{
BYTE *pSrc = pSrcRow;
BYTE *pDest = pDestRow;
BYTE *pDestEnd = pDest + cxSrc;
while (pDest < pDestEnd)
*pDest++ = *pSrc++;
// Next
pDestRow = Line.NextRow(pDestRow);
pSrcRow += iFontAlphaRow;
}
// Next
xPos += cxAdvance;
pPos++;
}
// Now compute the upper-left point where we should draw the text
CVector vText;
if (dwFlags & TEXT_ALIGN_CENTER)
vText = vPos + CVector::FromPolarInv(rRotation + PI, (0.5 * cxText));
else if (dwFlags & TEXT_ALIGN_RIGHT)
vText = vPos + CVector::FromPolarInv(rRotation + PI, cxText);
else
vText = vPos;
// Blt is from center of image.
vText = vText + CVector(0.5 * Line.GetWidth(), 0.5 * Line.GetHeight()).Rotate(-rRotation);
CVector vScale(1.0, 1.0);
// Blt the text based on the mode
switch (iMode)
{
case blendNormal:
{
if (rgbColor.GetAlpha() == 0xff)
{
TFillImageSolid<CGBlendCopy> Painter(rgbColor);
Painter.FillMaskTransformed(Dest, vText, vScale, rRotation, Line, 0, 0, Line.GetWidth(), Line.GetHeight());
}
else
{
TFillImageSolid<CGBlendBlend> Painter(rgbColor);
Painter.FillMaskTransformed(Dest, vText, vScale, rRotation, Line, 0, 0, Line.GetWidth(), Line.GetHeight());
}
break;
}
case blendHardLight:
{
TFillImageSolid<CGBlendHardLight> Painter(rgbColor);
Painter.FillMaskTransformed(Dest, vText, vScale, rRotation, Line, 0, 0, Line.GetWidth(), Line.GetHeight());
break;
}
case blendScreen:
{
TFillImageSolid<CGBlendScreen> Painter(rgbColor);
Painter.FillMaskTransformed(Dest, vText, vScale, rRotation, Line, 0, 0, Line.GetWidth(), Line.GetHeight());
break;
}
case blendCompositeNormal:
{
if (rgbColor.GetAlpha() == 0xff)
{
TFillImageSolid<CGBlendCompositeCopy> Painter(rgbColor);
Painter.FillMaskTransformed(Dest, vText, vScale, rRotation, Line, 0, 0, Line.GetWidth(), Line.GetHeight());
}
else
{
TFillImageSolid<CGBlendComposite> Painter(rgbColor);
Painter.FillMaskTransformed(Dest, vText, vScale, rRotation, Line, 0, 0, Line.GetWidth(), Line.GetHeight());
}
break;
}
}
}