diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/CanvasRenderingContext2DImpl.java b/cocos/platform/android/java/src/org/cocos2dx/lib/CanvasRenderingContext2DImpl.java index 18df00effc6..cb9f701ac4b 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/CanvasRenderingContext2DImpl.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/CanvasRenderingContext2DImpl.java @@ -370,9 +370,7 @@ private void fillText(String text, float x, float y, float maxWidth) { 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 @@ private void strokeText(String text, float x, float y, float maxWidth) { 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/cocos/platform/apple/CCCanvasRenderingContext2D-apple.mm b/cocos/platform/apple/CCCanvasRenderingContext2D-apple.mm index 4b57dcadf86..618290c0a7b 100644 --- a/cocos/platform/apple/CCCanvasRenderingContext2D-apple.mm +++ b/cocos/platform/apple/CCCanvasRenderingContext2D-apple.mm @@ -301,21 +301,15 @@ -(NSPoint) convertDrawPoint:(NSPoint) point text:(NSString*) text { 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/cocos/platform/win32/CCCanvasRenderingContext2D-win32.cpp b/cocos/platform/win32/CCCanvasRenderingContext2D-win32.cpp index 303e87d7d24..6e3712883da 100644 --- a/cocos/platform/win32/CCCanvasRenderingContext2D-win32.cpp +++ b/cocos/platform/win32/CCCanvasRenderingContext2D-win32.cpp @@ -371,6 +371,8 @@ class CanvasRenderingContext2DImpl 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) { @@ -589,14 +591,11 @@ class CanvasRenderingContext2DImpl { 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; }