CCDevice-win32.cpp
17.4 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
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "platform/CCDevice.h"
#include "platform/CCFileUtils.h"
#include "platform/CCStdC.h"
NS_CC_BEGIN
int Device::getDPI()
{
static int dpi = -1;
if (dpi == -1)
{
HDC hScreenDC = GetDC(nullptr);
int PixelsX = GetDeviceCaps(hScreenDC, HORZRES);
int MMX = GetDeviceCaps(hScreenDC, HORZSIZE);
ReleaseDC(nullptr, hScreenDC);
dpi = (int)(254.0f*PixelsX / MMX / 10);
}
return dpi;
}
void Device::setAccelerometerEnabled(bool isEnabled)
{}
void Device::setAccelerometerInterval(float interval)
{}
class BitmapDC
{
public:
BitmapDC(HWND hWnd = nullptr)
: _DC(nullptr)
, _bmp(nullptr)
, _font((HFONT)GetStockObject(DEFAULT_GUI_FONT))
, _wnd(nullptr)
{
_wnd = hWnd;
HDC hdc = GetDC(hWnd);
_DC = CreateCompatibleDC(hdc);
ReleaseDC(hWnd, hdc);
}
~BitmapDC()
{
prepareBitmap(0, 0);
if (_DC)
{
DeleteDC(_DC);
}
removeCustomFont();
}
wchar_t * utf8ToUtf16(const std::string& str)
{
wchar_t * pwszBuffer = nullptr;
do
{
if (str.empty())
{
break;
}
// utf-8 to utf-16
int nLen = str.size();
int nBufLen = nLen + 1;
pwszBuffer = new wchar_t[nBufLen];
CC_BREAK_IF(!pwszBuffer);
memset(pwszBuffer, 0, nBufLen);
nLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), nLen, pwszBuffer, nBufLen);
pwszBuffer[nLen] = '\0';
} while (0);
return pwszBuffer;
}
bool setFont(const char * pFontName = nullptr, int nSize = 0, bool enableBold = false)
{
bool bRet = false;
do
{
std::string fontName = pFontName;
std::string fontPath;
HFONT hDefFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
LOGFONTA tNewFont = { 0 };
LOGFONTA tOldFont = { 0 };
GetObjectA(hDefFont, sizeof(tNewFont), &tNewFont);
if (!fontName.empty())
{
// create font from ttf file
if (FileUtils::getInstance()->getFileExtension(fontName) == ".ttf")
{
fontPath = FileUtils::getInstance()->fullPathForFilename(fontName.c_str());
int nFindPos = fontName.rfind("/");
fontName = &fontName[nFindPos + 1];
nFindPos = fontName.rfind(".");
fontName = fontName.substr(0, nFindPos);
}
else
{
auto nFindPos = fontName.rfind("/");
if (nFindPos != fontName.npos)
{
if (fontName.length() == nFindPos + 1)
{
fontName = "";
}
else
{
fontName = &fontName[nFindPos + 1];
}
}
}
tNewFont.lfCharSet = DEFAULT_CHARSET;
strcpy_s(tNewFont.lfFaceName, LF_FACESIZE, fontName.c_str());
}
if (nSize)
{
tNewFont.lfHeight = -nSize;
}
if (enableBold)
{
tNewFont.lfWeight = FW_BOLD;
}
else
{
tNewFont.lfWeight = FW_NORMAL;
}
GetObjectA(_font, sizeof(tOldFont), &tOldFont);
if (tOldFont.lfHeight == tNewFont.lfHeight
&& tOldFont.lfWeight == tNewFont.lfWeight
&& 0 == strcmp(tOldFont.lfFaceName, tNewFont.lfFaceName))
{
bRet = true;
break;
}
// delete old font
removeCustomFont();
if (fontPath.size() > 0)
{
_curFontPath = fontPath;
wchar_t * pwszBuffer = utf8ToUtf16(_curFontPath);
if (pwszBuffer)
{
if (AddFontResource(pwszBuffer))
{
PostMessage(_wnd, WM_FONTCHANGE, 0, 0);
}
delete[] pwszBuffer;
pwszBuffer = nullptr;
}
}
_font = nullptr;
// disable Cleartype
tNewFont.lfQuality = ANTIALIASED_QUALITY;
// create new font
_font = CreateFontIndirectA(&tNewFont);
if (!_font)
{
// create failed, use default font
_font = hDefFont;
break;
}
bRet = true;
} while (0);
return bRet;
}
SIZE sizeWithText(const wchar_t * pszText,
int nLen,
DWORD dwFmt,
const char* fontName,
int textSize,
LONG nWidthLimit,
LONG nHeightLimit,
bool enableWrap,
int overflow)
{
SIZE tRet = { 0 };
do
{
CC_BREAK_IF(!pszText || nLen <= 0);
RECT rc = { 0, 0, 0, 0 };
DWORD dwCalcFmt = DT_CALCRECT;
if (!enableWrap)
{
dwCalcFmt |= DT_SINGLELINE;
}
if (nWidthLimit > 0)
{
rc.right = nWidthLimit;
dwCalcFmt |= DT_WORDBREAK
| (dwFmt & DT_CENTER)
| (dwFmt & DT_RIGHT);
}
if (overflow == 2)
{
LONG actualWidth = nWidthLimit + 1;
LONG actualHeight = nHeightLimit + 1;
int newFontSize = textSize + 1;
while (actualWidth > nWidthLimit || actualHeight > nHeightLimit)
{
if (newFontSize <= 0)
{
break;
}
this->setFont(fontName, newFontSize);
// use current font to measure text extent
HGDIOBJ hOld = SelectObject(_DC, _font);
rc.right = nWidthLimit;
// measure text size
DrawTextW(_DC, pszText, nLen, &rc, dwCalcFmt);
SelectObject(_DC, hOld);
actualWidth = rc.right;
actualHeight = rc.bottom;
newFontSize = newFontSize - 1;
}
}
else
{
// use current font to measure text extent
HGDIOBJ hOld = SelectObject(_DC, _font);
// measure text size
DrawTextW(_DC, pszText, nLen, &rc, dwCalcFmt);
SelectObject(_DC, hOld);
}
tRet.cx = rc.right;
tRet.cy = rc.bottom;
} while (0);
return tRet;
}
bool prepareBitmap(int nWidth, int nHeight)
{
// release bitmap
if (_bmp)
{
DeleteObject(_bmp);
_bmp = nullptr;
}
if (nWidth > 0 && nHeight > 0)
{
_bmp = CreateBitmap(nWidth, nHeight, 1, 32, nullptr);
if (!_bmp)
{
return false;
}
}
return true;
}
int drawText(const char * pszText, SIZE& tSize, Device::TextAlign eAlign, const char * fontName, int textSize,
bool enableWrap, int overflow)
{
int nRet = 0;
wchar_t * pwszBuffer = nullptr;
wchar_t* fixedText = nullptr;
do
{
CC_BREAK_IF(!pszText);
DWORD dwFmt = DT_WORDBREAK;
if (!enableWrap) {
dwFmt |= DT_SINGLELINE;
}
DWORD dwHoriFlag = (int)eAlign & 0x0f;
DWORD dwVertFlag = ((int)eAlign & 0xf0) >> 4;
switch (dwHoriFlag)
{
case 1: // left
dwFmt |= DT_LEFT;
break;
case 2: // right
dwFmt |= DT_RIGHT;
break;
case 3: // center
dwFmt |= DT_CENTER;
break;
}
int nLen = strlen(pszText);
// utf-8 to utf-16
int nBufLen = nLen + 1;
pwszBuffer = new wchar_t[nBufLen];
CC_BREAK_IF(!pwszBuffer);
memset(pwszBuffer, 0, sizeof(wchar_t)*nBufLen);
nLen = MultiByteToWideChar(CP_UTF8, 0, pszText, nLen, pwszBuffer, nBufLen);
if (strchr(pszText, '&'))
{
fixedText = new wchar_t[nLen * 2 + 1];
int fixedIndex = 0;
for (int index = 0; index < nLen; ++index)
{
if (pwszBuffer[index] == '&')
{
fixedText[fixedIndex] = '&';
fixedText[fixedIndex + 1] = '&';
fixedIndex += 2;
}
else
{
fixedText[fixedIndex] = pwszBuffer[index];
fixedIndex += 1;
}
}
fixedText[fixedIndex] = '\0';
nLen = fixedIndex;
}
SIZE newSize;
if (fixedText)
{
newSize = sizeWithText(fixedText, nLen, dwFmt, fontName, textSize, tSize.cx, tSize.cy, enableWrap, overflow);
}
else
{
newSize = sizeWithText(pwszBuffer, nLen, dwFmt, fontName, textSize, tSize.cx, tSize.cy, enableWrap, overflow);
}
RECT rcText = { 0 };
// if content width is 0, use text size as content size
if (tSize.cx <= 0)
{
tSize = newSize;
rcText.right = newSize.cx;
rcText.bottom = newSize.cy;
}
else
{
LONG offsetX = 0;
LONG offsetY = 0;
rcText.right = newSize.cx; // store the text width to rectangle
// calculate text horizontal offset
if (1 != dwHoriFlag // and text isn't align to left
&& newSize.cx < tSize.cx) // and text's width less then content width,
{ // then need adjust offset of X.
offsetX = (2 == dwHoriFlag) ? tSize.cx - newSize.cx // align to right
: (tSize.cx - newSize.cx) / 2; // align to center
}
// if content height is 0, use text height as content height
// else if content height less than text height, use content height to draw text
if (tSize.cy <= 0)
{
tSize.cy = newSize.cy;
dwFmt |= DT_NOCLIP;
rcText.bottom = newSize.cy; // store the text height to rectangle
}
else if (tSize.cy < newSize.cy)
{
// content height larger than text height need, clip text to rect
rcText.bottom = tSize.cy;
}
else
{
rcText.bottom = newSize.cy; // store the text height to rectangle
// content larger than text, need adjust vertical position
dwFmt |= DT_NOCLIP;
// calculate text vertical offset
offsetY = (2 == dwVertFlag) ? tSize.cy - newSize.cy // align to bottom
: (3 == dwVertFlag) ? (tSize.cy - newSize.cy) / 2 // align to middle
: 0; // align to top
}
if (offsetX || offsetY)
{
OffsetRect(&rcText, offsetX, offsetY);
}
}
CC_BREAK_IF(!prepareBitmap(tSize.cx, tSize.cy));
// draw text
HGDIOBJ hOldFont = SelectObject(_DC, _font);
HGDIOBJ hOldBmp = SelectObject(_DC, _bmp);
SetBkMode(_DC, TRANSPARENT);
SetTextColor(_DC, RGB(255, 255, 255)); // white color
// draw text
if (fixedText)
{
nRet = DrawTextW(_DC, fixedText, nLen, &rcText, dwFmt);
}
else
{
nRet = DrawTextW(_DC, pwszBuffer, nLen, &rcText, dwFmt);
}
SelectObject(_DC, hOldBmp);
SelectObject(_DC, hOldFont);
} while (0);
CC_SAFE_DELETE_ARRAY(pwszBuffer);
delete[] fixedText;
return nRet;
}
CC_SYNTHESIZE_READONLY(HDC, _DC, DC);
CC_SYNTHESIZE_READONLY(HBITMAP, _bmp, Bitmap);
private:
friend class Image;
HFONT _font;
HWND _wnd;
std::string _curFontPath;
void removeCustomFont()
{
HFONT hDefFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
if (hDefFont != _font)
{
DeleteObject(_font);
_font = hDefFont;
}
// release temp font resource
if (_curFontPath.size() > 0)
{
wchar_t * pwszBuffer = utf8ToUtf16(_curFontPath);
if (pwszBuffer)
{
RemoveFontResource(pwszBuffer);
PostMessage(_wnd, WM_FONTCHANGE, 0, 0);
delete[] pwszBuffer;
pwszBuffer = nullptr;
}
_curFontPath.clear();
}
}
};
static BitmapDC& sharedBitmapDC()
{
static BitmapDC s_BmpDC;
return s_BmpDC;
}
Data Device::getTextureDataForText(const char * text, const FontDefinition& textDefinition, TextAlign align, int &width, int &height, bool& hasPremultipliedAlpha)
{
Data ret;
do
{
BitmapDC& dc = sharedBitmapDC();
if (!dc.setFont(textDefinition._fontName.c_str(), (int)textDefinition._fontSize,false))
{
log("Can't found font(%s), use system default", textDefinition._fontName.c_str());
}
// draw text
// does changing to SIZE here affects the font size by rounding from float?
SIZE size = { (LONG)textDefinition._dimensions.width,(LONG)textDefinition._dimensions.height };
CC_BREAK_IF(!dc.drawText(text, size, align, textDefinition._fontName.c_str(), (int)textDefinition._fontSize, textDefinition._enableWrap, textDefinition._overflow));
int dataLen = size.cx * size.cy * 4;
unsigned char* dataBuf = (unsigned char*)malloc(sizeof(unsigned char) * dataLen);
CC_BREAK_IF(!dataBuf);
struct
{
BITMAPINFOHEADER bmiHeader;
int mask[4];
} bi = { 0 };
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
CC_BREAK_IF(!GetDIBits(dc.getDC(), dc.getBitmap(), 0, 0,
nullptr, (LPBITMAPINFO)&bi, DIB_RGB_COLORS));
width = (short)size.cx;
height = (short)size.cy;
// copy pixel data
bi.bmiHeader.biHeight = (bi.bmiHeader.biHeight > 0)
? -bi.bmiHeader.biHeight : bi.bmiHeader.biHeight;
GetDIBits(dc.getDC(), dc.getBitmap(), 0, height, dataBuf,
(LPBITMAPINFO)&bi, DIB_RGB_COLORS);
COLORREF textColor = (textDefinition._fontFillColor.b << 16 | textDefinition._fontFillColor.g << 8 | textDefinition._fontFillColor.r) & 0x00ffffff;
float alpha = textDefinition._fontAlpha / 255.0f;
COLORREF * pPixel = nullptr;
for (int y = 0; y < height; ++y)
{
pPixel = (COLORREF *)dataBuf + y * width;
for (int x = 0; x < width; ++x)
{
COLORREF& clr = *pPixel;
clr = ((BYTE)(GetRValue(clr) * alpha) << 24) | textColor;
++pPixel;
}
}
ret.fastSet(dataBuf, dataLen);
hasPremultipliedAlpha = false;
} while (0);
return ret;
}
void Device::setKeepScreenOn(bool value)
{
CC_UNUSED_PARAM(value);
}
void Device::vibrate(float duration)
{
CC_UNUSED_PARAM(duration);
}
NS_CC_END