From 871476343c872cab736b932b185f53b679d2f2bf Mon Sep 17 00:00:00 2001 From: Knox Date: Mon, 13 May 2019 16:53:42 +0800 Subject: [PATCH 1/2] improve convert draw y point calculate for 2d-tasks/issues/1378 (#1739) * improve convert draw y point calculate for 2d-tasks/issues/1378 * update comment * improve convert draw y point calculate for android * improve convert draw y point calculate for win32 --- .../cocos2dx/lib/CanvasRenderingContext2DImpl.java | 8 ++------ .../apple/CCCanvasRenderingContext2D-apple.mm | 14 ++++---------- .../win32/CCCanvasRenderingContext2D-win32.cpp | 13 ++++++------- 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/native/cocos/platform/android/java/src/org/cocos2dx/lib/CanvasRenderingContext2DImpl.java b/native/cocos/platform/android/java/src/org/cocos2dx/lib/CanvasRenderingContext2DImpl.java index 18df00effc..cb9f701ac4 100644 --- a/native/cocos/platform/android/java/src/org/cocos2dx/lib/CanvasRenderingContext2DImpl.java +++ b/native/cocos/platform/android/java/src/org/cocos2dx/lib/CanvasRenderingContext2DImpl.java @@ -370,9 +370,7 @@ public class CanvasRenderingContext2DImpl { mTextPaint.setStyle(Paint.Style.FILL); scaleX(mTextPaint, text, maxWidth); Point pt = convertDrawPoint(new Point(x, y), text); - // Convert to baseline Y - float baselineY = pt.y - mTextPaint.getFontMetrics().descent; - mCanvas.drawText(text, pt.x, baselineY, mTextPaint); + mCanvas.drawText(text, pt.x, pt.y, mTextPaint); } private void strokeText(String text, float x, float y, float maxWidth) { @@ -383,9 +381,7 @@ public class CanvasRenderingContext2DImpl { mTextPaint.setStrokeWidth(mLineWidth); scaleX(mTextPaint, text, maxWidth); Point pt = convertDrawPoint(new Point(x, y), text); - // Convert to baseline Y - float baselineY = pt.y - mTextPaint.getFontMetrics().descent; - mCanvas.drawText(text, pt.x, baselineY, mTextPaint); + mCanvas.drawText(text, pt.x, pt.y, mTextPaint); } private float measureText(String text) { diff --git a/native/cocos/platform/apple/CCCanvasRenderingContext2D-apple.mm b/native/cocos/platform/apple/CCCanvasRenderingContext2D-apple.mm index af015f245e..dd11711d60 100644 --- a/native/cocos/platform/apple/CCCanvasRenderingContext2D-apple.mm +++ b/native/cocos/platform/apple/CCCanvasRenderingContext2D-apple.mm @@ -301,21 +301,15 @@ enum class CanvasTextBaseline { 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 #if CC_TARGET_PLATFORM == CC_PLATFORM_MAC - // We use font size to calculate text height, but 'drawPointAt' method on macOS is based on - // the real font height and in bottom-left position, add the adjust value to make the text inside text rectangle. - point.y += (textSize.height - _fontSize) / 2.0f; + point.y -= _font.descender; // The origin on macOS is bottom-left by default, so we need to convert y from top-left origin to bottom-left origin. point.y = _height - point.y; #else - // The origin of drawing text on iOS is from top-left, but now we get bottom-left, - // So, we need to substract the font size to convert 'point' to top-left. - point.y -= _fontSize; - - // We use font size to calculate text height, but 'drawPointAt' method on iOS is based on - // the real font height and in top-left position, substract the adjust value to make text inside text rectangle. - point.y -= (textSize.height - _fontSize) / 2.0f; + point.y -= _font.ascender; #endif return point; } diff --git a/native/cocos/platform/win32/CCCanvasRenderingContext2D-win32.cpp b/native/cocos/platform/win32/CCCanvasRenderingContext2D-win32.cpp index 7800c2770a..4e9c1400e5 100644 --- a/native/cocos/platform/win32/CCCanvasRenderingContext2D-win32.cpp +++ b/native/cocos/platform/win32/CCCanvasRenderingContext2D-win32.cpp @@ -370,6 +370,8 @@ private: cocos2d::Color4F _fillStyle; cocos2d::Color4F _strokeStyle; + TEXTMETRIC _tm; + // change utf-8 string to utf-16, pRetLen is the string length after changing wchar_t * _utf8ToUtf16(const std::string& str, int * pRetLen = nullptr) { @@ -587,14 +589,11 @@ private: { point.y += _fontSize / 2.0f; } - // The origin of drawing text on win32 is from top-left, but now we get bottom-left, - // So, we need to substract the font size to convert 'point' to top-left. - point.y -= _fontSize; - // We use font size to calculate text height, but draw text on win32 is based on - // the real font height and in top-left position, substract the adjust value to make text inside text rectangle. - // check - // point.y -= (textSize.height - _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 + GetTextMetrics(_DC, &_tm); + point.y -= _tm.tmAscent; return point; } From 4d1fa2734642b22b97a8ef41fe599e4e136a873a Mon Sep 17 00:00:00 2001 From: cary <39078800+caryliu1999@users.noreply.github.com> Date: Wed, 15 May 2019 15:23:35 +0800 Subject: [PATCH 2/2] Fix the black border of the label on the Win32 platform. (#1741) * Fix the black border of the label on the Win32 platform. * fix indent * fix indent * Optimize data copy. --- .../CCCanvasRenderingContext2D-win32.cpp | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/native/cocos/platform/win32/CCCanvasRenderingContext2D-win32.cpp b/native/cocos/platform/win32/CCCanvasRenderingContext2D-win32.cpp index 7800c2770a..303e87d7d2 100644 --- a/native/cocos/platform/win32/CCCanvasRenderingContext2D-win32.cpp +++ b/native/cocos/platform/win32/CCCanvasRenderingContext2D-win32.cpp @@ -120,7 +120,7 @@ public: DeleteObject(_hpen); if (_bufferWidth < 1.0f || _bufferHeight < 1.0f) return; - _imageData = _getTextureData(); + _fillTextureData(); } void saveContext() @@ -156,10 +156,11 @@ public: uint8_t* buffer = _imageData.getBytes(); if (buffer) { - uint8_t r = _fillStyle.r * 255.0f; - uint8_t g = _fillStyle.g * 255.0f; - uint8_t b = _fillStyle.b * 255.0f; - uint8_t a = _fillStyle.a; + float alpha = _fillStyle.a; + uint8_t r = _fillStyle.r * 255.0f * alpha; + uint8_t g = _fillStyle.g * 255.0f * alpha; + uint8_t b = _fillStyle.b * 255.0f * alpha; + uint8_t a = _fillStyle.a * 255.0f; fillRectWithColor(buffer, (uint32_t)_bufferWidth, (uint32_t)_bufferHeight, (uint32_t)x, (uint32_t)y, (uint32_t)w, (uint32_t)h, r, g, b, a); } } @@ -173,7 +174,7 @@ public: Point offsetPoint = _convertDrawPoint(Point(x, y), text); _drawText(text, (int)offsetPoint.x, (int)offsetPoint.y); - _imageData = _getTextureData(); + _fillTextureData(); } @@ -514,14 +515,15 @@ private: return true; } - Data _getTextureData() + void _fillTextureData() { - Data ret; do { int dataLen = _bufferWidth * _bufferHeight * 4; unsigned char* dataBuf = (unsigned char*)malloc(sizeof(unsigned char) * dataLen); CC_BREAK_IF(!dataBuf); + unsigned char* imageBuf = _imageData.getBytes(); + CC_BREAK_IF(!imageBuf); struct { @@ -541,12 +543,15 @@ private: uint8_t r, g, b; float alpha = _fillStyle.a; COLORREF * pPixel = nullptr; + COLORREF * pImage = nullptr; for (int y = 0; y < _bufferHeight; ++y) { pPixel = (COLORREF *)dataBuf + y * (int)_bufferWidth; + pImage = (COLORREF *)imageBuf + y * (int)_bufferWidth; for (int x = 0; x < _bufferWidth; ++x) { COLORREF& clr = *pPixel; + COLORREF& val = *pImage; uint8_t dirtyValue = GetRValue(clr); // "dirtyValue > 0" means pixel was covered when drawing text if (dirtyValue > 0) @@ -556,16 +561,13 @@ private: g = _fillStyle.g * dirtyValue * alpha; b = _fillStyle.b * dirtyValue * alpha; COLORREF textColor = (b << 16 | g << 8 | r) & 0x00ffffff; - clr = ((BYTE)(dirtyValue * alpha) << 24) | textColor; + val = ((BYTE)(dirtyValue * alpha) << 24) | textColor; } ++pPixel; + ++pImage; } } - - ret.fastSet(dataBuf, dataLen); } while (0); - - return ret; } Point _convertDrawPoint(Point point, std::string text) {