Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve convert draw y point calculate for 2d-tasks/issues/1378 (#1739) #1764

Merged
merged 1 commit into from Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
14 changes: 4 additions & 10 deletions cocos/platform/apple/CCCanvasRenderingContext2D-apple.mm
Expand Up @@ -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;
}
Expand Down
15 changes: 7 additions & 8 deletions cocos/platform/win32/CCCanvasRenderingContext2D-win32.cpp
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand Down