Skip to content

Commit

Permalink
fix misusage of SelectObject and DeleteObject (#1766)
Browse files Browse the repository at this point in the history
* fix misusage of SelectObject and DeleteObject

* revert coordination convertion

* add comment back
  • Loading branch information
minggo authored and jareguo committed Jul 5, 2019
1 parent ad52fe3 commit 4e19492
Showing 1 changed file with 27 additions and 66 deletions.
93 changes: 27 additions & 66 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

0 comments on commit 4e19492

Please sign in to comment.