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] 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) {