Skip to content

Commit be1163e

Browse files
summerinsectshuangwei1024
authored andcommitted
refine CCDevice-win32 BitmapDC (cocos2d#19144)
in `BitmapDC::sizeWithText` and `BitmapDC::drawText`, use DT_HIDEPREFIX flag for ignoring the ampersand (&) prefix character in the text. we don't need to handle ampersands ourselves refer [MSDN](https://docs.microsoft.com/zh-cn/windows/desktop/api/winuser/nf-winuser-drawtextexw)
1 parent 550f077 commit be1163e

File tree

1 file changed

+5
-44
lines changed

1 file changed

+5
-44
lines changed

cocos/platform/win32/CCDevice-win32.cpp

+5-44
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class BitmapDC
218218
CC_BREAK_IF(!pszText || nLen <= 0);
219219

220220
RECT rc = { 0, 0, 0, 0 };
221-
DWORD dwCalcFmt = DT_CALCRECT;
221+
DWORD dwCalcFmt = DT_CALCRECT | DT_NOPREFIX;
222222
if (!enableWrap)
223223
{
224224
dwCalcFmt |= DT_SINGLELINE;
@@ -298,12 +298,11 @@ class BitmapDC
298298
{
299299
int nRet = 0;
300300
wchar_t * pwszBuffer = nullptr;
301-
wchar_t* fixedText = nullptr;
302301
do
303302
{
304303
CC_BREAK_IF(!pszText);
305304

306-
DWORD dwFmt = DT_WORDBREAK;
305+
DWORD dwFmt = DT_WORDBREAK | DT_NOPREFIX;
307306
if (!enableWrap) {
308307
dwFmt |= DT_SINGLELINE;
309308
}
@@ -332,37 +331,7 @@ class BitmapDC
332331
memset(pwszBuffer, 0, sizeof(wchar_t)*nBufLen);
333332
nLen = MultiByteToWideChar(CP_UTF8, 0, pszText, nLen, pwszBuffer, nBufLen);
334333

335-
if (strchr(pszText, '&'))
336-
{
337-
fixedText = new wchar_t[nLen * 2 + 1];
338-
int fixedIndex = 0;
339-
for (int index = 0; index < nLen; ++index)
340-
{
341-
if (pwszBuffer[index] == '&')
342-
{
343-
fixedText[fixedIndex] = '&';
344-
fixedText[fixedIndex + 1] = '&';
345-
fixedIndex += 2;
346-
}
347-
else
348-
{
349-
fixedText[fixedIndex] = pwszBuffer[index];
350-
fixedIndex += 1;
351-
}
352-
}
353-
fixedText[fixedIndex] = '\0';
354-
nLen = fixedIndex;
355-
}
356-
357-
SIZE newSize;
358-
if (fixedText)
359-
{
360-
newSize = sizeWithText(fixedText, nLen, dwFmt, fontName, textSize, tSize.cx, tSize.cy, enableWrap, overflow);
361-
}
362-
else
363-
{
364-
newSize = sizeWithText(pwszBuffer, nLen, dwFmt, fontName, textSize, tSize.cx, tSize.cy, enableWrap, overflow);
365-
}
334+
SIZE newSize = sizeWithText(pwszBuffer, nLen, dwFmt, fontName, textSize, tSize.cx, tSize.cy, enableWrap, overflow);
366335

367336
RECT rcText = { 0 };
368337
// if content width is 0, use text size as content size
@@ -429,20 +398,12 @@ class BitmapDC
429398
SetTextColor(_DC, RGB(255, 255, 255)); // white color
430399

431400
// draw text
432-
if (fixedText)
433-
{
434-
nRet = DrawTextW(_DC, fixedText, nLen, &rcText, dwFmt);
435-
}
436-
else
437-
{
438-
nRet = DrawTextW(_DC, pwszBuffer, nLen, &rcText, dwFmt);
439-
}
401+
nRet = DrawTextW(_DC, pwszBuffer, nLen, &rcText, dwFmt);
440402

441403
SelectObject(_DC, hOldBmp);
442404
SelectObject(_DC, hOldFont);
443405
} while (0);
444406
CC_SAFE_DELETE_ARRAY(pwszBuffer);
445-
delete[] fixedText;
446407

447408
return nRet;
448409
}
@@ -558,4 +519,4 @@ void Device::vibrate(float duration)
558519

559520
NS_CC_END
560521

561-
#endif // CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
522+
#endif // CC_TARGET_PLATFORM == CC_PLATFORM_WIN32

0 commit comments

Comments
 (0)