-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGfxFont.cpp
335 lines (282 loc) · 7.71 KB
/
GfxFont.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
#include "GfxFont.h"
#include <atlbase.h>
#include <stdio.h>
__inline float _floor(float f)
{
static int _n;
_asm fld f
_asm fistp _n
return (float)_n;
}
// 65级灰度表
const unsigned char g_byAlphaLevel[65] =
{
0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48,
52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96,100,
104,108,112,116,120,124,128,132,136,140,144,148,152,
156,160,164,168,172,176,180,184,188,192,196,200,204,
208,212,216,220,224,228,232,236,240,244,248,252,255
};
GfxFont::GfxFont(const char* lpsFontName, int nFaceSize, BOOL bBold, BOOL bItalic, BOOL bAntialias)
{
m_pHGE = hgeCreate(HGE_VERSION);
// 创建GDI相关设备
HDC hDC = GetDC(m_pHGE->System_GetState(HGE_HWND));
m_hMemDC = CreateCompatibleDC(hDC);
if (NULL == m_hMemDC) return;
ReleaseDC(m_pHGE->System_GetState(HGE_HWND), hDC);
::SetMapMode(m_hMemDC, MM_TEXT);
::SetTextColor(m_hMemDC, RGB(255, 255, 255));
::SetBkColor(m_hMemDC, RGB(0, 0, 0));
m_hFont = CreateFont(
-nFaceSize,
0,
0,
0,
(bBold) ? FW_BOLD : FW_NORMAL,
bItalic,
FALSE,
FALSE,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
FF_DONTCARE | DEFAULT_PITCH,
lpsFontName);
if (NULL == (m_hFont)) return;
SelectObject(m_hMemDC, m_hFont);
memset(m_Glyphs, 0, sizeof(TENGINEFONTGLYPH)*font_count);
m_nAntialias = bAntialias ? GGO_GRAY8_BITMAP : GGO_BITMAP;
TEXTMETRIC tm;
::GetTextMetrics(m_hMemDC, &tm);
m_nAscent = tm.tmAscent;
m_nFontSize = static_cast<float>(nFaceSize);
m_nKerningWidth = 0;
m_nKerningHeight = 0;
m_pSprite = new hgeSprite(0, 0, 0, 0, 0);
m_pSprite->SetColor(ARGB(255, 255, 255, 255));
}
GfxFont::~GfxFont(void)
{
for (int nIdx = 0; nIdx < font_count; ++nIdx)
{ if (m_Glyphs[nIdx].t) m_pHGE->Texture_Free(m_Glyphs[nIdx].t); }
if ((m_hFont)) DeleteObject(m_hFont);
if ((m_hMemDC)) DeleteDC(m_hMemDC);
if(m_pSprite) delete m_pSprite;
if(m_pHGE) m_pHGE->Release();
}
// 渲染文本
void GfxFont::Print( float x, float y, float size,const char *format, ... )
{
char sBuffer[10240] = {0};
char *lpsArg=(char*)&format+sizeof(format);
vsprintf(sBuffer, format, lpsArg);
Render(x,y,CA2W(sBuffer),size);
}
void GfxFont::Render(float x, float y, const wchar_t* text,float size,bool shape)
{
float offsetX = x;
float offsetY = y;
while(*text)
{
if (*text == L'\n' || *text == L'\r')
{
offsetX = x;
offsetY += (m_nFontSize + m_nKerningHeight);
}
else
{
unsigned int idx = GetGlyphByCharacter(*text);
if (idx > 0)
{
//包含描边
DWORD color = m_pSprite->GetColor();
float alpha = GETA(color);
float r = GETR(color);
float g = GETG(color);
float b = GETB(color);
m_pSprite->SetTexture(m_Glyphs[idx].t);
m_pSprite->SetTextureRect(0, 0, m_Glyphs[idx].w, m_Glyphs[idx].h);
m_pSprite->SetHotSpot(m_pSprite->GetWidth()/2,m_pSprite->GetHeight()/2);
//255-((255-r)*((255-alpha)/255))=alpha+r-(alpha*r/255)????!!!!
m_pSprite->SetColor(ARGB(alpha, alpha + r - (alpha*r / 255), alpha + g - (alpha*g / 255), alpha + b - (alpha*b / 255)));
if (shape)
{
m_pSprite->RenderEx(offsetX - m_Glyphs[idx].x + 1, offsetY - m_Glyphs[idx].y, 0, size, size);
m_pSprite->RenderEx(offsetX - m_Glyphs[idx].x - 1, offsetY - m_Glyphs[idx].y, 0, size, size);
m_pSprite->RenderEx(offsetX - m_Glyphs[idx].x, offsetY - m_Glyphs[idx].y + 1, 0, size, size);
m_pSprite->RenderEx(offsetX - m_Glyphs[idx].x, offsetY - m_Glyphs[idx].y - 1, 0, size, size);
}
m_pSprite->SetColor(color);
m_pSprite->RenderEx(offsetX - m_Glyphs[idx].x, offsetY - m_Glyphs[idx].y, 0, size, size);
offsetX += (GetWidthFromCharacter(*text) + m_nKerningWidth);
}
else
{
offsetX += (GetWidthFromCharacter(*text) + m_nKerningWidth);
}
}
++text;
}
}
// 设置与获取颜色
void GfxFont::SetColor( DWORD dwColor, int i )
{
m_pSprite->SetColor(dwColor,i);
}
DWORD GfxFont::GetColor(int i)
{
return m_pSprite->GetColor(i);
}
// 获取文本宽高
SIZE GfxFont::GetTextSize( const wchar_t* text )
{
SIZE dim = {0, static_cast<LONG>(m_nFontSize)};
float nRowWidth = 0;
while(*text)
{
if (*text == L'\n' || *text == L'\r')
{
dim.cy += static_cast<LONG>(m_nFontSize + m_nKerningHeight);
if (dim.cx < static_cast<LONG>(nRowWidth))
dim.cx = static_cast<LONG>(nRowWidth);
nRowWidth = 0;
}
else
nRowWidth += (GetWidthFromCharacter(*text) + m_nKerningWidth);
++text;
}
if (dim.cx < static_cast<LONG>(nRowWidth))
dim.cx = static_cast<LONG>(nRowWidth);
return dim;
}
// 根据坐标获取字符
wchar_t GfxFont::GetCharacterFromPos( const wchar_t* text, float pixel_x, float pixel_y )
{
float x = 0;
float y = 0;
while (*text)
{
if (*text == L'\n' || *text == L'\r')
{
x = 0;
y += (m_nFontSize+m_nKerningHeight);
text++;
if (!(*text))
break;
}
float w = GetWidthFromCharacter(*text);
if (pixel_x > x && pixel_x <= x + w &&
pixel_y > y && pixel_y <= y + m_nFontSize)
return *text;
x += (w+m_nKerningWidth);
text++;
}
return L'\0';
}
// 设置字间距
void GfxFont::SetKerningWidth( float kerning )
{
m_nKerningWidth = kerning;
}
void GfxFont::SetKerningHeight( float kerning )
{
m_nKerningHeight = kerning;
}
// 获取字间距
float GfxFont::GetKerningWidth()
{
return m_nKerningWidth;
}
float GfxFont::GetKerningHeight()
{
return m_nKerningHeight;
}
// 字体大小
float GfxFont::GetFontSize()
{
return m_nFontSize;
}
// 根据字符获取轮廓
unsigned int GfxFont::GetGlyphByCharacter( wchar_t c )
{
unsigned int idx = (unsigned int)c;
if (NULL == (m_Glyphs[idx].t)) CacheCharacter(idx,c);
return idx;
}
inline float GfxFont::GetWidthFromCharacter( wchar_t c, bool original )
{
unsigned int idx = GetGlyphByCharacter(c);
if (original && idx > 0 && idx < font_count) return m_Glyphs[idx].c;
return (idx >= 0x2000) ? m_nFontSize : _floor(m_nFontSize / 2);
}
inline void GfxFont::CacheCharacter(unsigned int idx, wchar_t c)
{
if (idx < font_count && NULL == m_Glyphs[idx].t)
{
UINT nChar = (UINT)c;
MAT2 mat2 = {{0,1},{0,0},{0,0},{0,1}};
GLYPHMETRICS gm;
DWORD nLen = ::GetGlyphOutlineW(m_hMemDC,nChar,m_nAntialias,&gm,0,NULL,&mat2);
HTEXTURE hTex = m_pHGE->Texture_Create(gm.gmBlackBoxX,gm.gmBlackBoxY);
if (NULL == hTex) return;
if((signed)nLen > 0)
{
LPBYTE lpBuf = new BYTE[nLen];
if (nLen == ::GetGlyphOutlineW(m_hMemDC,nChar,m_nAntialias,&gm,nLen,lpBuf,&mat2))
{
BYTE* lpSrc = lpBuf;
DWORD* lpDst = m_pHGE->Texture_Lock(hTex,FALSE);
if (GGO_BITMAP == m_nAntialias)
{
LONG nSrcPitch = (gm.gmBlackBoxX / 32 + (gm.gmBlackBoxX % 32 == 0 ? 0 : 1)) * 4;
LONG nDstPitch = m_pHGE->Texture_GetWidth(hTex);
for (UINT y = 0; y < gm.gmBlackBoxY; ++y)
{
for (UINT x = 0; x < gm.gmBlackBoxX; ++x)
{
for(UINT k = 0; k < 8; ++k)
{
UINT i = 8 * x + k;
if (i >= gm.gmBlackBoxX)
{
x+=7;
break;
}
lpDst[i] = ((lpSrc[x] >> (7 - k)) & 1) ? 0xFFFFFFFF : 0x0;
}
}
lpSrc += nSrcPitch;
lpDst += nDstPitch;
}
}
else
{
LONG nSrcPitch = (gm.gmBlackBoxX / 4 + (gm.gmBlackBoxX % 4 == 0 ? 0 : 1)) * 4;
LONG nDstPitch = m_pHGE->Texture_GetWidth(hTex);
for (UINT y = 0; y < gm.gmBlackBoxY; ++y)
{
for (UINT x = 0; x < gm.gmBlackBoxX; ++x)
{
lpDst[x] = ARGB(g_byAlphaLevel[lpSrc[x]],0xFF,0xFF,0xFF);
}
lpSrc += nSrcPitch;
lpDst += nDstPitch;
}
}
m_pHGE->Texture_Unlock(hTex);
}
delete lpBuf;
}
else
{
// 非正常显示字符
}
m_Glyphs[idx].t = hTex;
m_Glyphs[idx].w = static_cast<float>(gm.gmBlackBoxX);
m_Glyphs[idx].h = static_cast<float>(gm.gmBlackBoxY);
m_Glyphs[idx].x = static_cast<float>(-gm.gmptGlyphOrigin.x);
m_Glyphs[idx].y = static_cast<float>(-m_nAscent + gm.gmptGlyphOrigin.y);
m_Glyphs[idx].c = static_cast<float>(gm.gmCellIncX);
}
}