Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix misusage of SelectObject and DeleteObject #1766

Merged
merged 3 commits into from Jul 5, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
97 changes: 28 additions & 69 deletions cocos/platform/win32/CCCanvasRenderingContext2D-win32.cpp
Expand Up @@ -62,12 +62,10 @@ class CanvasRenderingContext2DImpl

~CanvasRenderingContext2DImpl()
{
_prepareBitmap(0, 0);
_deleteBitmap();
_removeCustomFont();
if (_DC)
{
DeleteDC(_DC);
}
_removeCustomFont();
}

void recreateBuffer(float w, float h)
Expand All @@ -76,7 +74,7 @@ class CanvasRenderingContext2DImpl
_bufferHeight = h;
if (_bufferWidth < 1.0f || _bufferHeight < 1.0f)
{
_prepareBitmap(0, 0);
_deleteBitmap();
return;
}

Expand Down Expand Up @@ -199,16 +197,12 @@ class CanvasRenderingContext2DImpl

void updateFont(const std::string& fontName, float fontSize, bool bold = false)
{
bool bRet = false;
do
{
_fontName = fontName;
_fontSize = fontSize;
std::string fontPath;
HFONT hDefFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
LOGFONTA tNewFont = { 0 };
LOGFONTA tOldFont = { 0 };
GetObjectA(hDefFont, sizeof(tNewFont), &tNewFont);
LOGFONTA tFont = { 0 };
if (!_fontName.empty())
{
// firstly, try to create font from ttf file
Expand Down Expand Up @@ -240,33 +234,20 @@ class CanvasRenderingContext2DImpl
}
}
}
tNewFont.lfCharSet = DEFAULT_CHARSET;
strcpy_s(tNewFont.lfFaceName, LF_FACESIZE, _fontName.c_str());
tFont.lfCharSet = DEFAULT_CHARSET;
strcpy_s(tFont.lfFaceName, LF_FACESIZE, _fontName.c_str());
}

if (_fontSize)
{
tNewFont.lfHeight = -_fontSize;
}
tFont.lfHeight = -_fontSize;

if (bold)
{
tNewFont.lfWeight = FW_BOLD;
}
tFont.lfWeight = FW_BOLD;
else
{
tNewFont.lfWeight = FW_NORMAL;
}

GetObjectA(_font, sizeof(tOldFont), &tOldFont);
tFont.lfWeight = FW_NORMAL;

if (tOldFont.lfHeight == tNewFont.lfHeight
&& tOldFont.lfWeight == tNewFont.lfWeight
&& 0 == strcmp(tOldFont.lfFaceName, tNewFont.lfFaceName))
{
bRet = true;
break;
}
// disable Cleartype
tFont.lfQuality = ANTIALIASED_QUALITY;

// delete old font
_removeCustomFont();
Expand All @@ -286,29 +267,21 @@ class CanvasRenderingContext2DImpl
}
}

_font = nullptr;

// disable Cleartype
tNewFont.lfQuality = ANTIALIASED_QUALITY;

// create new font
_font = CreateFontIndirectA(&tNewFont);
_font = CreateFontIndirectA(&tFont);
if (!_font)
{
// create failed, use default font
_font = hDefFont;
SE_LOGE("Failed to create custom font(font name: %s, font size: %f), use default font.\n",
_fontName.c_str(), fontSize);
break;
}
else
{
SelectObject(_DC, _font);
SendMessage(_wnd, WM_FONTCHANGE, 0, 0);
}

bRet = true;

} while (0);

}

void setTextAlign(CanvasTextAlign align)
Expand Down Expand Up @@ -403,8 +376,7 @@ class CanvasRenderingContext2DImpl
HFONT hDefFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
if (hDefFont != _font)
{
DeleteObject(_font);
_font = hDefFont;
DeleteObject(SelectObject(_DC, hDefFont));
}
// release temp font resource
if (_curFontPath.size() > 0)
Expand Down Expand Up @@ -453,18 +425,11 @@ class CanvasRenderingContext2DImpl

// SE_LOGE("_drawText text,%s size: (%d, %d) offset after convert: (%d, %d) \n", text.c_str(), newSize.cx, newSize.cy, offsetX, offsetY);

// 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
nRet = DrawTextW(_DC, pwszBuffer, bufferLen, &rcText, dwFmt);

DeleteObject(hOldBmp);
DeleteObject(hOldFont);
} while (0);
CC_SAFE_DELETE_ARRAY(pwszBuffer);

Expand All @@ -481,39 +446,35 @@ class CanvasRenderingContext2DImpl
RECT rc = { 0, 0, 0, 0 };
DWORD dwCalcFmt = DT_CALCRECT;

// use current font to measure text extent
HGDIOBJ hOld = SelectObject(_DC, _font);

// measure text size
DrawTextW(_DC, pszText, nLen, &rc, dwCalcFmt);

tRet.cx = rc.right;
tRet.cy = rc.bottom;

DeleteObject(hOld);

} while (0);

return tRet;
}

bool _prepareBitmap(int nWidth, int nHeight)
void _prepareBitmap(int nWidth, int nHeight)
{
// release bitmap
_deleteBitmap();

if (nWidth > 0 && nHeight > 0)
{
_bmp = CreateBitmap(nWidth, nHeight, 1, 32, nullptr);
SelectObject(_DC, _bmp);
}
}

void _deleteBitmap()
{
if (_bmp)
{
DeleteObject(_bmp);
_bmp = nullptr;
}
if (nWidth > 0 && nHeight > 0)
{
_bmp = CreateBitmap(nWidth, nHeight, 1, 32, nullptr);
if (!_bmp)
{
return false;
}
}
return true;
}

void _fillTextureData()
Expand Down Expand Up @@ -588,11 +549,9 @@ class CanvasRenderingContext2DImpl
}
else if (_textBaseLine == CanvasTextBaseline::MIDDLE)
{
point.y += _fontSize / 2.0f;
point.y += _fontSize / 2.0f;
}

// Since the web platform cannot get the baseline of the font, an additive offset is performed for all platforms.
// That's why we should add baseline back again on other platforms
jareguo marked this conversation as resolved.
Show resolved Hide resolved
GetTextMetrics(_DC, &_tm);
point.y -= _tm.tmAscent;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里似乎撤销了 knox 前段时间的改动,需要 @knoxHuang 确认下

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块逻辑还原了。


Expand Down