When using CCLabelTTF with utf-8 encoded string text, width calculation appears to be wrong.
All strings in the picture should have same x
position.
CCLabelTTF* item = CCLabelTTF::labelWithString(utf8_convert(L"Привет привет привет").c_str(), DEFAULT_FONT, 20);
item->setAnchorPoint(ccp(0, 0.5));
item->setPosition(ccp(300, 500));
CCLabelTTF*item2 = CCLabelTTF::labelWithString(utf8_convert(L"|").c_str(), DEFAULT_FONT, 20);
item2->setAnchorPoint(ccp(0, 0.5));
item2->setPosition(ccp(300, 470));
In CCImage_win32.cpp BitmapDC::drawText
size of text is calculated as SIZE newSize = sizeWithText(pszText, nLen, dwFmt, tSize.cx);
from char* representation with DrawTextA
.
Then text is converted to wchar_t*
buffer and drawn with DrawTextW
.
I think it should be
SIZE sizeWithText(const wchar_t * pszText, int nLen, DWORD dwFmt, LONG nWidthLimit)
{
...
// measure text size
//! DrawTextA(m_hDC, pszText, nLen, &rc, dwCalcFmt);
DrawTextW(m_hDC, pszText, nLen, &rc, dwCalcFmt);
}
and
int drawText(const char * pszText, SIZE& tSize, CCImage::ETextAlign eAlign)
{
...
do
{
...
switch (dwHoriFlag)
{
...
}
int nLen = strlen(pszText);
//! wchar_t buffer moved here
// utf-8 to utf-16
int nBufLen = nLen + 1;
pwszBuffer = new wchar_t[nBufLen];
CC_BREAK_IF(! pwszBuffer);
nLen = MultiByteToWideChar(CP_UTF8, 0, pszText, nLen, pwszBuffer, nBufLen);
//! SIZE newSize = sizeWithText(pszText, nLen, dwFmt, tSize.cx);
SIZE newSize = sizeWithText(pwszBuffer, nLen, dwFmt, tSize.cx);
...
//! wchar_t buffer moved up
// draw text
//! nRet = DrawTextA(m_hDC, pszText, nLen, &rcText, dwFmt);
nRet = DrawTextW(m_hDC, pwszBuffer, nLen, &rcText, dwFmt);
...
}
...
}
[cocos2d-1.0.1-x-0.13.0-beta]
testfailed.png (75.9 KB)