2014-01-07 11:25:07 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-07-15 23:03:43 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io/
|
2014-01-07 11:25:07 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCDevice.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
2014-09-10 08:53:08 +08:00
|
|
|
#include "platform/CCStdC.h"
|
2013-02-17 11:34:55 +08:00
|
|
|
|
2021-11-26 17:19:50 +08:00
|
|
|
#include "ntcvt/ntcvt.hpp"
|
2021-05-18 23:50:23 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2013-02-17 11:34:55 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int Device::getDPI()
|
2013-02-17 11:34:55 +08:00
|
|
|
{
|
2013-02-17 17:13:06 +08:00
|
|
|
static int dpi = -1;
|
|
|
|
if (dpi == -1)
|
|
|
|
{
|
2017-05-17 16:20:21 +08:00
|
|
|
HDC hScreenDC = GetDC(nullptr);
|
2021-12-25 10:04:45 +08:00
|
|
|
int PixelsX = GetDeviceCaps(hScreenDC, HORZRES);
|
|
|
|
int MMX = GetDeviceCaps(hScreenDC, HORZSIZE);
|
2017-05-17 16:20:21 +08:00
|
|
|
ReleaseDC(nullptr, hScreenDC);
|
2021-12-25 10:04:45 +08:00
|
|
|
dpi = (int)(254.0f * PixelsX / MMX / 10);
|
2013-02-17 17:13:06 +08:00
|
|
|
}
|
|
|
|
return dpi;
|
2013-02-17 11:34:55 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Device::setAccelerometerEnabled(bool isEnabled) {}
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Device::setAccelerometerInterval(float interval) {}
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2014-02-13 21:08:43 +08:00
|
|
|
class BitmapDC
|
|
|
|
{
|
|
|
|
public:
|
2014-07-10 00:45:27 +08:00
|
|
|
BitmapDC(HWND hWnd = nullptr)
|
2021-12-25 10:04:45 +08:00
|
|
|
: _DC(nullptr), _bmp(nullptr), _font((HFONT)GetStockObject(DEFAULT_GUI_FONT)), _wnd(hWnd)
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
|
|
|
HDC hdc = GetDC(hWnd);
|
2021-12-25 10:04:45 +08:00
|
|
|
_DC = CreateCompatibleDC(hdc);
|
2014-02-13 21:08:43 +08:00
|
|
|
ReleaseDC(hWnd, hdc);
|
|
|
|
}
|
|
|
|
|
|
|
|
~BitmapDC()
|
|
|
|
{
|
|
|
|
prepareBitmap(0, 0);
|
|
|
|
if (_DC)
|
|
|
|
{
|
|
|
|
DeleteDC(_DC);
|
|
|
|
}
|
|
|
|
removeCustomFont();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool setFont(const char* pFontName, int nSize = 0, bool enableBold = false)
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
2015-06-25 17:36:57 +08:00
|
|
|
do
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
2021-05-18 23:50:23 +08:00
|
|
|
std::wstring fontName = ntcvt::from_chars(pFontName);
|
2021-12-25 10:04:45 +08:00
|
|
|
HFONT hDefFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
|
|
|
|
LOGFONTW tNewFont = {0};
|
|
|
|
LOGFONTW tOldFont = {0};
|
2021-05-18 23:50:23 +08:00
|
|
|
GetObjectW(hDefFont, sizeof(tNewFont), &tNewFont);
|
2016-05-20 23:10:28 +08:00
|
|
|
if (!fontName.empty())
|
2015-06-25 17:36:57 +08:00
|
|
|
{
|
2014-02-13 21:08:43 +08:00
|
|
|
tNewFont.lfCharSet = DEFAULT_CHARSET;
|
2021-05-18 23:50:23 +08:00
|
|
|
wcscpy_s(tNewFont.lfFaceName, LF_FACESIZE, fontName.c_str());
|
2014-02-13 21:08:43 +08:00
|
|
|
}
|
2017-05-17 16:20:21 +08:00
|
|
|
|
2014-02-13 21:08:43 +08:00
|
|
|
if (nSize)
|
|
|
|
{
|
|
|
|
tNewFont.lfHeight = -nSize;
|
|
|
|
}
|
2017-05-17 16:20:21 +08:00
|
|
|
|
|
|
|
if (enableBold)
|
|
|
|
{
|
|
|
|
tNewFont.lfWeight = FW_BOLD;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tNewFont.lfWeight = FW_NORMAL;
|
|
|
|
}
|
|
|
|
|
2021-05-18 23:50:23 +08:00
|
|
|
GetObjectW(_font, sizeof(tOldFont), &tOldFont);
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tOldFont.lfHeight == tNewFont.lfHeight && tOldFont.lfWeight == tNewFont.lfWeight &&
|
|
|
|
0 == wcscmp(tOldFont.lfFaceName, tNewFont.lfFaceName))
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
|
|
|
bRet = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// disable Cleartype
|
|
|
|
tNewFont.lfQuality = ANTIALIASED_QUALITY;
|
|
|
|
|
2021-05-18 23:52:25 +08:00
|
|
|
// delete old font before create new
|
|
|
|
removeCustomFont();
|
|
|
|
|
2014-02-13 21:08:43 +08:00
|
|
|
// create new font
|
2021-05-18 23:50:23 +08:00
|
|
|
_font = CreateFontIndirectW(&tNewFont);
|
2017-05-17 16:20:21 +08:00
|
|
|
if (!_font)
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
|
|
|
// create failed, use default font
|
|
|
|
_font = hDefFont;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SIZE sizeWithText(const wchar_t* pszText,
|
|
|
|
int nLen,
|
|
|
|
DWORD dwFmt,
|
|
|
|
const char* fontName,
|
|
|
|
int textSize,
|
|
|
|
LONG nWidthLimit,
|
|
|
|
LONG nHeightLimit,
|
|
|
|
bool enableWrap,
|
|
|
|
int overflow)
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
SIZE tRet = {0};
|
2015-06-25 17:36:57 +08:00
|
|
|
do
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
2017-05-17 16:20:21 +08:00
|
|
|
CC_BREAK_IF(!pszText || nLen <= 0);
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
RECT rc = {0, 0, 0, 0};
|
2014-02-13 21:08:43 +08:00
|
|
|
DWORD dwCalcFmt = DT_CALCRECT;
|
2017-05-17 16:20:21 +08:00
|
|
|
if (!enableWrap)
|
|
|
|
{
|
|
|
|
dwCalcFmt |= DT_SINGLELINE;
|
|
|
|
}
|
2014-02-13 21:08:43 +08:00
|
|
|
|
|
|
|
if (nWidthLimit > 0)
|
|
|
|
{
|
|
|
|
rc.right = nWidthLimit;
|
2021-12-25 10:04:45 +08:00
|
|
|
dwCalcFmt |= DT_WORDBREAK | (dwFmt & DT_CENTER) | (dwFmt & DT_RIGHT);
|
2014-02-13 21:08:43 +08:00
|
|
|
}
|
2017-05-17 16:20:21 +08:00
|
|
|
if (overflow == 2)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
LONG actualWidth = nWidthLimit + 1;
|
2017-05-17 16:20:21 +08:00
|
|
|
LONG actualHeight = nHeightLimit + 1;
|
2021-12-25 10:04:45 +08:00
|
|
|
int newFontSize = textSize + 1;
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2017-05-17 16:20:21 +08:00
|
|
|
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);
|
2021-12-25 10:04:45 +08:00
|
|
|
rc.right = nWidthLimit;
|
2017-05-17 16:20:21 +08:00
|
|
|
// measure text size
|
|
|
|
DrawTextW(_DC, pszText, nLen, &rc, dwCalcFmt);
|
|
|
|
SelectObject(_DC, hOld);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
actualWidth = rc.right;
|
2017-05-17 16:20:21 +08:00
|
|
|
actualHeight = rc.bottom;
|
2021-12-25 10:04:45 +08:00
|
|
|
newFontSize = newFontSize - 1;
|
2017-05-17 16:20:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2014-02-13 21:08:43 +08:00
|
|
|
|
|
|
|
tRet.cx = rc.right;
|
|
|
|
tRet.cy = rc.bottom;
|
2017-05-17 16:20:21 +08:00
|
|
|
|
2014-02-13 21:08:43 +08:00
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return tRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool prepareBitmap(int nWidth, int nHeight)
|
|
|
|
{
|
|
|
|
// release bitmap
|
|
|
|
if (_bmp)
|
|
|
|
{
|
|
|
|
DeleteObject(_bmp);
|
2014-07-10 00:45:27 +08:00
|
|
|
_bmp = nullptr;
|
2014-02-13 21:08:43 +08:00
|
|
|
}
|
|
|
|
if (nWidth > 0 && nHeight > 0)
|
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
_bmp = CreateBitmap(nWidth, nHeight, 1, 32, nullptr);
|
2017-05-17 16:20:21 +08:00
|
|
|
if (!_bmp)
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int drawText(const char* pszText,
|
|
|
|
SIZE& tSize,
|
|
|
|
Device::TextAlign eAlign,
|
|
|
|
const char* fontName,
|
|
|
|
int textSize,
|
|
|
|
bool enableWrap,
|
|
|
|
int overflow)
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
int nRet = 0;
|
|
|
|
wchar_t* pwszBuffer = nullptr;
|
|
|
|
wchar_t* fixedText = nullptr;
|
2015-06-25 17:36:57 +08:00
|
|
|
do
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
2017-05-17 16:20:21 +08:00
|
|
|
CC_BREAK_IF(!pszText);
|
2014-02-13 21:08:43 +08:00
|
|
|
|
|
|
|
DWORD dwFmt = DT_WORDBREAK;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!enableWrap)
|
|
|
|
{
|
2017-05-17 16:20:21 +08:00
|
|
|
dwFmt |= DT_SINGLELINE;
|
|
|
|
}
|
2014-02-13 21:08:43 +08:00
|
|
|
DWORD dwHoriFlag = (int)eAlign & 0x0f;
|
|
|
|
DWORD dwVertFlag = ((int)eAlign & 0xf0) >> 4;
|
|
|
|
|
|
|
|
switch (dwHoriFlag)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case 1: // left
|
2014-02-13 21:08:43 +08:00
|
|
|
dwFmt |= DT_LEFT;
|
|
|
|
break;
|
2021-12-25 10:04:45 +08:00
|
|
|
case 2: // right
|
2014-02-13 21:08:43 +08:00
|
|
|
dwFmt |= DT_RIGHT;
|
|
|
|
break;
|
2021-12-25 10:04:45 +08:00
|
|
|
case 3: // center
|
2014-02-13 21:08:43 +08:00
|
|
|
dwFmt |= DT_CENTER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nLen = strlen(pszText);
|
|
|
|
// utf-8 to utf-16
|
2017-05-17 16:20:21 +08:00
|
|
|
int nBufLen = nLen + 1;
|
2021-12-25 10:04:45 +08:00
|
|
|
pwszBuffer = new wchar_t[nBufLen];
|
2017-05-17 16:20:21 +08:00
|
|
|
CC_BREAK_IF(!pwszBuffer);
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
memset(pwszBuffer, 0, sizeof(wchar_t) * nBufLen);
|
2014-02-13 21:08:43 +08:00
|
|
|
nLen = MultiByteToWideChar(CP_UTF8, 0, pszText, nLen, pwszBuffer, nBufLen);
|
|
|
|
|
2015-01-29 11:55:49 +08:00
|
|
|
if (strchr(pszText, '&'))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
fixedText = new wchar_t[nLen * 2 + 1];
|
2015-01-29 11:55:49 +08:00
|
|
|
int fixedIndex = 0;
|
|
|
|
for (int index = 0; index < nLen; ++index)
|
|
|
|
{
|
|
|
|
if (pwszBuffer[index] == '&')
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
fixedText[fixedIndex] = '&';
|
2015-01-29 11:55:49 +08:00
|
|
|
fixedText[fixedIndex + 1] = '&';
|
|
|
|
fixedIndex += 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fixedText[fixedIndex] = pwszBuffer[index];
|
|
|
|
fixedIndex += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fixedText[fixedIndex] = '\0';
|
2021-12-25 10:04:45 +08:00
|
|
|
nLen = fixedIndex;
|
2015-01-29 11:55:49 +08:00
|
|
|
}
|
2015-06-25 17:36:57 +08:00
|
|
|
|
2015-01-29 11:55:49 +08:00
|
|
|
SIZE newSize;
|
|
|
|
if (fixedText)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
newSize =
|
|
|
|
sizeWithText(fixedText, nLen, dwFmt, fontName, textSize, tSize.cx, tSize.cy, enableWrap, overflow);
|
2015-06-25 17:36:57 +08:00
|
|
|
}
|
2015-01-29 11:55:49 +08:00
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
newSize =
|
|
|
|
sizeWithText(pwszBuffer, nLen, dwFmt, fontName, textSize, tSize.cx, tSize.cy, enableWrap, overflow);
|
2015-01-29 11:55:49 +08:00
|
|
|
}
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
RECT rcText = {0};
|
2014-02-13 21:08:43 +08:00
|
|
|
// if content width is 0, use text size as content size
|
|
|
|
if (tSize.cx <= 0)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
tSize = newSize;
|
|
|
|
rcText.right = newSize.cx;
|
2014-02-13 21:08:43 +08:00
|
|
|
rcText.bottom = newSize.cy;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
LONG offsetX = 0;
|
|
|
|
LONG offsetY = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
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
|
2014-02-13 21:08:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2017-05-17 16:20:21 +08:00
|
|
|
dwFmt |= DT_NOCLIP;
|
2021-12-25 10:04:45 +08:00
|
|
|
rcText.bottom = newSize.cy; // store the text height to rectangle
|
2014-02-13 21:08:43 +08:00
|
|
|
}
|
|
|
|
else if (tSize.cy < newSize.cy)
|
|
|
|
{
|
|
|
|
// content height larger than text height need, clip text to rect
|
|
|
|
rcText.bottom = tSize.cy;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
rcText.bottom = newSize.cy; // store the text height to rectangle
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// content larger than text, need adjust vertical position
|
2014-02-13 21:08:43 +08:00
|
|
|
dwFmt |= DT_NOCLIP;
|
|
|
|
|
|
|
|
// calculate text vertical offset
|
2021-12-25 10:04:45 +08:00
|
|
|
offsetY = (2 == dwVertFlag) ? tSize.cy - newSize.cy // align to bottom
|
|
|
|
: (3 == dwVertFlag) ? (tSize.cy - newSize.cy) / 2 // align to middle
|
|
|
|
: 0; // align to top
|
2014-02-13 21:08:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (offsetX || offsetY)
|
|
|
|
{
|
|
|
|
OffsetRect(&rcText, offsetX, offsetY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 16:20:21 +08:00
|
|
|
CC_BREAK_IF(!prepareBitmap(tSize.cx, tSize.cy));
|
2014-02-13 21:08:43 +08:00
|
|
|
|
|
|
|
// draw text
|
|
|
|
HGDIOBJ hOldFont = SelectObject(_DC, _font);
|
2021-12-25 10:04:45 +08:00
|
|
|
HGDIOBJ hOldBmp = SelectObject(_DC, _bmp);
|
2014-02-13 21:08:43 +08:00
|
|
|
|
|
|
|
SetBkMode(_DC, TRANSPARENT);
|
2021-12-25 10:04:45 +08:00
|
|
|
SetTextColor(_DC, RGB(255, 255, 255)); // white color
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// draw text
|
2015-01-29 11:55:49 +08:00
|
|
|
if (fixedText)
|
|
|
|
{
|
|
|
|
nRet = DrawTextW(_DC, fixedText, nLen, &rcText, dwFmt);
|
2015-06-25 17:36:57 +08:00
|
|
|
}
|
2015-01-29 11:55:49 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
nRet = DrawTextW(_DC, pwszBuffer, nLen, &rcText, dwFmt);
|
|
|
|
}
|
2015-06-25 17:36:57 +08:00
|
|
|
|
2014-02-13 21:08:43 +08:00
|
|
|
SelectObject(_DC, hOldBmp);
|
|
|
|
SelectObject(_DC, hOldFont);
|
|
|
|
} while (0);
|
|
|
|
CC_SAFE_DELETE_ARRAY(pwszBuffer);
|
2015-01-29 11:55:49 +08:00
|
|
|
delete[] fixedText;
|
|
|
|
|
2014-02-13 21:08:43 +08:00
|
|
|
return nRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SYNTHESIZE_READONLY(HDC, _DC, DC);
|
|
|
|
CC_SYNTHESIZE_READONLY(HBITMAP, _bmp, Bitmap);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
private:
|
2014-02-13 21:08:43 +08:00
|
|
|
friend class Image;
|
2021-12-25 10:04:45 +08:00
|
|
|
HFONT _font;
|
|
|
|
HWND _wnd;
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2015-06-25 17:36:57 +08:00
|
|
|
void removeCustomFont()
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
|
|
|
HFONT hDefFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
|
|
|
|
if (hDefFont != _font)
|
|
|
|
{
|
|
|
|
DeleteObject(_font);
|
|
|
|
_font = hDefFont;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static BitmapDC& sharedBitmapDC()
|
|
|
|
{
|
|
|
|
static BitmapDC s_BmpDC;
|
|
|
|
return s_BmpDC;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Data Device::getTextureDataForText(const char* text,
|
|
|
|
const FontDefinition& textDefinition,
|
|
|
|
TextAlign align,
|
|
|
|
int& width,
|
|
|
|
int& height,
|
|
|
|
bool& hasPremultipliedAlpha)
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
|
|
|
Data ret;
|
2015-06-25 17:36:57 +08:00
|
|
|
do
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
|
|
|
BitmapDC& dc = sharedBitmapDC();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!dc.setFont(textDefinition._fontName.c_str(), (int)textDefinition._fontSize, false))
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
|
|
|
log("Can't found font(%s), use system default", textDefinition._fontName.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw text
|
2014-08-06 22:43:19 +08:00
|
|
|
// does changing to SIZE here affects the font size by rounding from float?
|
2021-12-25 10:04:45 +08:00
|
|
|
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));
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int dataLen = size.cx * size.cy * 4;
|
2014-02-18 10:11:04 +08:00
|
|
|
unsigned char* dataBuf = (unsigned char*)malloc(sizeof(unsigned char) * dataLen);
|
2017-05-17 16:20:21 +08:00
|
|
|
CC_BREAK_IF(!dataBuf);
|
2014-02-13 21:08:43 +08:00
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
BITMAPINFOHEADER bmiHeader;
|
|
|
|
int mask[4];
|
2021-12-25 10:04:45 +08:00
|
|
|
} bi = {0};
|
2014-02-13 21:08:43 +08:00
|
|
|
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_BREAK_IF(!GetDIBits(dc.getDC(), dc.getBitmap(), 0, 0, nullptr, (LPBITMAPINFO)&bi, DIB_RGB_COLORS));
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
width = (short)size.cx;
|
2017-05-17 16:20:21 +08:00
|
|
|
height = (short)size.cy;
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2016-03-29 09:52:17 +08:00
|
|
|
// copy pixel data
|
2021-12-25 10:04:45 +08:00
|
|
|
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;
|
2014-02-13 21:08:43 +08:00
|
|
|
for (int y = 0; y < height; ++y)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
pPixel = (COLORREF*)dataBuf + y * width;
|
2014-02-13 21:08:43 +08:00
|
|
|
for (int x = 0; x < width; ++x)
|
|
|
|
{
|
|
|
|
COLORREF& clr = *pPixel;
|
2021-12-25 10:04:45 +08:00
|
|
|
clr = ((BYTE)(GetRValue(clr) * alpha) << 24) | textColor;
|
2014-02-13 21:08:43 +08:00
|
|
|
++pPixel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 16:20:21 +08:00
|
|
|
ret.fastSet(dataBuf, dataLen);
|
2014-05-06 10:36:55 +08:00
|
|
|
hasPremultipliedAlpha = false;
|
2014-02-13 21:08:43 +08:00
|
|
|
} while (0);
|
2014-05-06 10:36:55 +08:00
|
|
|
|
2014-02-13 21:08:43 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-17 16:20:21 +08:00
|
|
|
void Device::setKeepScreenOn(bool value)
|
2014-08-18 20:41:12 +08:00
|
|
|
{
|
2017-05-17 16:20:21 +08:00
|
|
|
CC_UNUSED_PARAM(value);
|
2015-06-25 17:36:57 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 16:20:21 +08:00
|
|
|
void Device::vibrate(float duration)
|
2015-06-25 17:36:57 +08:00
|
|
|
{
|
2017-05-17 16:20:21 +08:00
|
|
|
CC_UNUSED_PARAM(duration);
|
2014-08-18 20:41:12 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|