From d727e2657f100613413845d40b59c2cdf12b13a1 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 15 May 2017 17:28:28 +0800 Subject: [PATCH 1/2] fix windows system label overflow --- .../cocos/platform/win32/CCDevice-win32.cpp | 72 +++++++++++++++---- 1 file changed, 60 insertions(+), 12 deletions(-) diff --git a/native/cocos/platform/win32/CCDevice-win32.cpp b/native/cocos/platform/win32/CCDevice-win32.cpp index 3389c77813..8b9c9c2576 100644 --- a/native/cocos/platform/win32/CCDevice-win32.cpp +++ b/native/cocos/platform/win32/CCDevice-win32.cpp @@ -201,7 +201,14 @@ public: return bRet; } - SIZE sizeWithText(const wchar_t * pszText, int nLen, DWORD dwFmt, LONG nWidthLimit) + SIZE sizeWithText(const wchar_t * pszText, + int nLen, + DWORD dwFmt, + int textSize, + LONG nWidthLimit, + LONG nHeightLimit, + bool enableWrap, + int overflow) { SIZE tRet = {0}; do @@ -210,6 +217,10 @@ public: RECT rc = {0, 0, 0, 0}; DWORD dwCalcFmt = DT_CALCRECT; + if (!enableWrap) + { + dwCalcFmt |= DT_SINGLELINE; + } if (nWidthLimit > 0) { @@ -218,15 +229,51 @@ public: | (dwFmt & DT_CENTER) | (dwFmt & DT_RIGHT); } - // use current font to measure text extent - HGDIOBJ hOld = SelectObject(_DC, _font); + if (overflow == 2) + { + LONG actualWidth = nWidthLimit + 1; + LONG actualHeight = nHeightLimit + 1; + int newFontSize = textSize + 1; + LOGFONT lf; + int ret = GetObject(_font, sizeof(lf), &lf); - // measure text size - DrawTextW(_DC, pszText, nLen, &rc, dwCalcFmt); - SelectObject(_DC, hOld); + char* fontName = new char[wcslen(lf.lfFaceName) + 1]; + + wcstombs(fontName, lf.lfFaceName, wcslen(lf.lfFaceName)); + + while (actualWidth > nWidthLimit || actualHeight > nHeightLimit) + { + if (newFontSize <= 0) + { + break; + } + this->setFont("Arial", newFontSize); + // use current font to measure text extent + HGDIOBJ hOld = SelectObject(_DC, _font); + rc.right = nWidthLimit; + // measure text size + DrawTextW(_DC, pszText, nLen, &rc, dwCalcFmt); + SelectObject(_DC, hOld); + + actualWidth = rc.right; + actualHeight = rc.bottom; + newFontSize = newFontSize - 1; + } + delete[] fontName; + } + 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); + } tRet.cx = rc.right; tRet.cy = rc.bottom; + } while (0); return tRet; @@ -251,7 +298,7 @@ public: return true; } - int drawText(const char * pszText, SIZE& tSize, Device::TextAlign eAlign) + int drawText(const char * pszText, SIZE& tSize, Device::TextAlign eAlign, int textSize, bool enableWrap, int overflow) { int nRet = 0; wchar_t * pwszBuffer = nullptr; @@ -261,6 +308,9 @@ public: CC_BREAK_IF(! pszText); DWORD dwFmt = DT_WORDBREAK; + if (!enableWrap) { + dwFmt |= DT_SINGLELINE; + } DWORD dwHoriFlag = (int)eAlign & 0x0f; DWORD dwVertFlag = ((int)eAlign & 0xf0) >> 4; @@ -311,11 +361,11 @@ public: SIZE newSize; if (fixedText) { - newSize = sizeWithText(fixedText, nLen, dwFmt, tSize.cx); + newSize = sizeWithText(fixedText, nLen, dwFmt, textSize, tSize.cx, tSize.cy, enableWrap, overflow); } else { - newSize = sizeWithText(pwszBuffer, nLen, dwFmt, tSize.cx); + newSize = sizeWithText(pwszBuffer, nLen, dwFmt, textSize, tSize.cx, tSize.cy, enableWrap, overflow); } RECT rcText = {0}; @@ -392,8 +442,6 @@ public: nRet = DrawTextW(_DC, pwszBuffer, nLen, &rcText, dwFmt); } - //DrawTextA(_DC, pszText, nLen, &rcText, dwFmt); - SelectObject(_DC, hOldBmp); SelectObject(_DC, hOldFont); } while (0); @@ -457,7 +505,7 @@ Data Device::getTextureDataForText(const char * text, const FontDefinition& text // draw text // does changing to SIZE here affects the font size by rounding from float? SIZE size = {(LONG) textDefinition._dimensions.width,(LONG) textDefinition._dimensions.height}; - CC_BREAK_IF(! dc.drawText(text, size, align)); + CC_BREAK_IF(! dc.drawText(text, size, align, textDefinition._fontSize, textDefinition._enableWrap, textDefinition._overflow)); int dataLen = size.cx * size.cy * 4; unsigned char* dataBuf = (unsigned char*)malloc(sizeof(unsigned char) * dataLen); From dba7eb0d8d8d548a9aa89b6b5cfadd37a3b393c1 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 15 May 2017 17:36:45 +0800 Subject: [PATCH 2/2] refine code --- .../cocos/platform/win32/CCDevice-win32.cpp | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/native/cocos/platform/win32/CCDevice-win32.cpp b/native/cocos/platform/win32/CCDevice-win32.cpp index 8b9c9c2576..eb1bbb119a 100644 --- a/native/cocos/platform/win32/CCDevice-win32.cpp +++ b/native/cocos/platform/win32/CCDevice-win32.cpp @@ -204,6 +204,7 @@ public: SIZE sizeWithText(const wchar_t * pszText, int nLen, DWORD dwFmt, + const char* fontName, int textSize, LONG nWidthLimit, LONG nHeightLimit, @@ -234,12 +235,6 @@ public: LONG actualWidth = nWidthLimit + 1; LONG actualHeight = nHeightLimit + 1; int newFontSize = textSize + 1; - LOGFONT lf; - int ret = GetObject(_font, sizeof(lf), &lf); - - char* fontName = new char[wcslen(lf.lfFaceName) + 1]; - - wcstombs(fontName, lf.lfFaceName, wcslen(lf.lfFaceName)); while (actualWidth > nWidthLimit || actualHeight > nHeightLimit) { @@ -247,7 +242,7 @@ public: { break; } - this->setFont("Arial", newFontSize); + this->setFont(fontName, newFontSize); // use current font to measure text extent HGDIOBJ hOld = SelectObject(_DC, _font); rc.right = nWidthLimit; @@ -259,7 +254,6 @@ public: actualHeight = rc.bottom; newFontSize = newFontSize - 1; } - delete[] fontName; } else { @@ -298,7 +292,8 @@ public: return true; } - int drawText(const char * pszText, SIZE& tSize, Device::TextAlign eAlign, int textSize, bool enableWrap, int overflow) + int drawText(const char * pszText, SIZE& tSize, Device::TextAlign eAlign, const char * fontName, int textSize, + bool enableWrap, int overflow) { int nRet = 0; wchar_t * pwszBuffer = nullptr; @@ -361,11 +356,11 @@ public: SIZE newSize; if (fixedText) { - newSize = sizeWithText(fixedText, nLen, dwFmt, textSize, tSize.cx, tSize.cy, enableWrap, overflow); + newSize = sizeWithText(fixedText, nLen, dwFmt, fontName, textSize, tSize.cx, tSize.cy, enableWrap, overflow); } else { - newSize = sizeWithText(pwszBuffer, nLen, dwFmt, textSize, tSize.cx, tSize.cy, enableWrap, overflow); + newSize = sizeWithText(pwszBuffer, nLen, dwFmt, fontName, textSize, tSize.cx, tSize.cy, enableWrap, overflow); } RECT rcText = {0}; @@ -505,7 +500,7 @@ Data Device::getTextureDataForText(const char * text, const FontDefinition& text // draw text // does changing to SIZE here affects the font size by rounding from float? SIZE size = {(LONG) textDefinition._dimensions.width,(LONG) textDefinition._dimensions.height}; - CC_BREAK_IF(! dc.drawText(text, size, align, textDefinition._fontSize, textDefinition._enableWrap, textDefinition._overflow)); + CC_BREAK_IF(! dc.drawText(text, size, align, textDefinition._fontName.c_str(), textDefinition._fontSize, textDefinition._enableWrap, textDefinition._overflow)); int dataLen = size.cx * size.cy * 4; unsigned char* dataBuf = (unsigned char*)malloc(sizeof(unsigned char) * dataLen);