diff --git a/native/cocos/2d/CCLabel.cpp b/native/cocos/2d/CCLabel.cpp index d5f9d2adc0..5f56d30987 100644 --- a/native/cocos/2d/CCLabel.cpp +++ b/native/cocos/2d/CCLabel.cpp @@ -1130,14 +1130,14 @@ void Label::enableItalics() void Label::enableBold() { - if (!_boldEnabled) + if (!_boldEnabled && _currentLabelType != LabelType::STRING_TEXTURE) { // bold is implemented with outline enableShadow(Color4B::WHITE, Size(0.9f, 0), 0); // add one to kerning setAdditionalKerning(_additionalKerning+1); - _boldEnabled = true; } + _boldEnabled = true; } void Label::enableUnderline() @@ -1371,8 +1371,29 @@ void Label::updateContent() if (_underlineNode) { _underlineNode->clear(); - - if (_numberOfLines) + + if(_currentLabelType == Label::LabelType::STRING_TEXTURE) { + // system font + const auto spriteSize = _textSprite->getContentSize(); + + this->computeStringNumLines(); + float startY = spriteSize.height / 2 - _lineHeight * (_numberOfLines - 1) / 2 - _systemFontSize / 2; + if (_numberOfLines > 0) { + // atlas font + _underlineNode->setLineWidth(_systemFontSize / 8); + for (int i=0; i<_numberOfLines; ++i) + { + if (_strikethroughEnabled) + startY += _systemFontSize / 2; + // FIXME: Might not work with different vertical alignments + _underlineNode->drawLine(Vec2(0, startY - 1), + Vec2(spriteSize.width, startY - 1), _textColorF); + } + } + + } + + else if(_numberOfLines) { const float charheight = (_textDesiredHeight / _numberOfLines); _underlineNode->setLineWidth(charheight/6); @@ -1388,19 +1409,6 @@ void Label::updateContent() _underlineNode->drawLine(Vec2(_linesOffsetX[i],y), Vec2(_linesWidth[i] + _linesOffsetX[i],y), _textColorF); } } - else if (_textSprite) - { - // system font - float y = 0; - const auto spriteSize = _textSprite->getContentSize(); - _underlineNode->setLineWidth(spriteSize.height/6); - - if (_strikethroughEnabled) - // FIXME: system fonts don't report the height of the font correctly. only the size of the texture, which is POT - y += spriteSize.height / 2; - // FIXME: Might not work with different vertical alignments - _underlineNode->drawLine(Vec2(0,y), Vec2(spriteSize.width,y), Color4F(_textSprite->getDisplayedColor())); - } } if(updateFinished){ @@ -2016,6 +2024,7 @@ FontDefinition Label::_getFontDefinition() const systemFontDef._shadow._shadowEnabled = false; systemFontDef._enableWrap = _enableWrap; systemFontDef._overflow = (int)_overflow; + systemFontDef._enableBold = _boldEnabled; if (_currLabelEffect == LabelEffect::OUTLINE && _outlineSize > 0.f) { diff --git a/native/cocos/2d/CCTMXXMLParser.cpp b/native/cocos/2d/CCTMXXMLParser.cpp index 7e9ecb4e1d..bd28548956 100644 --- a/native/cocos/2d/CCTMXXMLParser.cpp +++ b/native/cocos/2d/CCTMXXMLParser.cpp @@ -418,7 +418,12 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts) if (startPos != string::npos) { colorStr.replace(startPos, 1, ""); } - int num = stoi(colorStr.c_str(), 0, 16); + // Android NDK 10 doesn't support std::stoi a/ std::stoul +#if CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID + int num = std::stoi(colorStr); +#else + int num = atoi(colorStr.c_str()); +#endif int r = num / 0x10000; int g = (num / 0x100) % 0x100; int b = num % 0x100; diff --git a/native/cocos/base/ccTypes.h b/native/cocos/base/ccTypes.h index ae6fb672d5..48af68c280 100644 --- a/native/cocos/base/ccTypes.h +++ b/native/cocos/base/ccTypes.h @@ -572,6 +572,7 @@ public: bool _enableWrap; /// enable shrink font size int _overflow; + bool _enableBold; }; /** diff --git a/native/cocos/platform/android/CCDevice-android.cpp b/native/cocos/platform/android/CCDevice-android.cpp index b40636e186..a1517a0c8d 100644 --- a/native/cocos/platform/android/CCDevice-android.cpp +++ b/native/cocos/platform/android/CCDevice-android.cpp @@ -86,56 +86,57 @@ public: Device::TextAlign eAlignMask, const FontDefinition& textDefinition ) { - JniMethodInfo methodInfo; - if (! JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/lib/Cocos2dxBitmap", "createTextBitmapShadowStroke", - "([BLjava/lang/String;IIIIIIIIZFFFFZIIIIFZI)Z")) - { - CCLOG("%s %d: error to get methodInfo", __FILE__, __LINE__); - return false; - } + JniMethodInfo methodInfo; + if (! JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/lib/Cocos2dxBitmap", "createTextBitmapShadowStroke", + "(Ljava/lang/String;Ljava/lang/String;IIIIIIIIZIIIIFZIZ)Z")) + { + CCLOG("%s %d: error to get methodInfo", __FILE__, __LINE__); + return false; + } - // Do a full lookup for the font path using FileUtils in case the given font name is a relative path to a font file asset, - // or the path has been mapped to a different location in the app package: - std::string fullPathOrFontName = textDefinition._fontName; - if(FileUtils::getInstance()->isFileExist(fullPathOrFontName)) { - fullPathOrFontName = FileUtils::getInstance()->fullPathForFilename(textDefinition._fontName); - // If the path name returned includes the 'assets' dir then that needs to be removed, because the android.content.Context - // requires this portion of the path to be omitted for assets inside the app package. - if (fullPathOrFontName.find("assets/") == 0) - { - fullPathOrFontName = fullPathOrFontName.substr(strlen("assets/")); // Chop out the 'assets/' portion of the path. - } - } + // Do a full lookup for the font path using FileUtils in case the given font name is a relative path to a font file asset, + // or the path has been mapped to a different location in the app package: + std::string fullPathOrFontName = textDefinition._fontName; + if(FileUtils::getInstance()->isFileExist(fullPathOrFontName)) { + fullPathOrFontName = FileUtils::getInstance()->fullPathForFilename(textDefinition._fontName); + // If the path name returned includes the 'assets' dir then that needs to be removed, because the android.content.Context + // requires this portion of the path to be omitted for assets inside the app package. + if (fullPathOrFontName.find("assets/") == 0) + { + fullPathOrFontName = fullPathOrFontName.substr(strlen("assets/")); // Chop out the 'assets/' portion of the path. + } + } - /**create bitmap - * this method call Cococs2dx.createBitmap()(java code) to create the bitmap, the java code - * will call Java_org_cocos2dx_lib_Cocos2dxBitmap_nativeInitBitmapDC() to init the width, height - * and data. - * use this approach to decrease the jni call number - */ - int count = strlen(text); - jbyteArray strArray = methodInfo.env->NewByteArray(count); - methodInfo.env->SetByteArrayRegion(strArray, 0, count, reinterpret_cast(text)); - jstring jstrFont = methodInfo.env->NewStringUTF(fullPathOrFontName.c_str()); + /**create bitmap + * this method call Cococs2dx.createBitmap()(java code) to create the bitmap, the java code + * will call Java_org_cocos2dx_lib_Cocos2dxBitmap_nativeInitBitmapDC() to init the width, height + * and data. + * use this approach to decrease the jni call number + */ + int count = strlen(text); + jbyteArray strArray = methodInfo.env->NewByteArray(count); + methodInfo.env->SetByteArrayRegion(strArray, 0, count, reinterpret_cast(text)); + jstring jstrFont = methodInfo.env->NewStringUTF(fullPathOrFontName.c_str()); + bool ret = true; + if(!methodInfo.env->CallStaticBooleanMethod(methodInfo.classID, methodInfo.methodID, jstrText, + jstrFont, textDefinition._fontSize, + textDefinition._fontFillColor.r, textDefinition._fontFillColor.g, + textDefinition._fontFillColor.b, textDefinition._fontAlpha, + eAlignMask, nWidth, nHeight, + textDefinition._stroke._strokeEnabled, + textDefinition._stroke._strokeColor.r, textDefinition._stroke._strokeColor.g, + textDefinition._stroke._strokeColor.b, textDefinition._stroke._strokeAlpha, + textDefinition._stroke._strokeSize, + textDefinition._enableWrap, textDefinition._overflow, textDefinition._enableBold)) + { + return false; + } - if(!methodInfo.env->CallStaticBooleanMethod(methodInfo.classID, methodInfo.methodID, strArray, - jstrFont, textDefinition._fontSize, textDefinition._fontFillColor.r, textDefinition._fontFillColor.g, - textDefinition._fontFillColor.b, textDefinition._fontAlpha, - eAlignMask, nWidth, nHeight, - textDefinition._shadow._shadowEnabled, textDefinition._shadow._shadowOffset.width, -textDefinition._shadow._shadowOffset.height, - textDefinition._shadow._shadowBlur, textDefinition._shadow._shadowOpacity, - textDefinition._stroke._strokeEnabled, textDefinition._stroke._strokeColor.r, textDefinition._stroke._strokeColor.g, - textDefinition._stroke._strokeColor.b, textDefinition._stroke._strokeAlpha, textDefinition._stroke._strokeSize, - textDefinition._enableWrap, textDefinition._overflow)) - { - return false; - } + methodInfo.env->DeleteLocalRef(strArray); + methodInfo.env->DeleteLocalRef(jstrFont); + methodInfo.env->DeleteLocalRef(methodInfo.classID); - methodInfo.env->DeleteLocalRef(strArray); - methodInfo.env->DeleteLocalRef(jstrFont); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - - return true; + return true; } public: diff --git a/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxBitmap.java b/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxBitmap.java index e9f40a4ccd..1258f7f70f 100644 --- a/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxBitmap.java +++ b/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxBitmap.java @@ -145,14 +145,8 @@ public final class Cocos2dxBitmap { public static boolean createTextBitmapShadowStroke(byte[] bytes, final String fontName, int fontSize, int fontTintR, int fontTintG, int fontTintB, int fontTintA, int alignment, int width, int height, - boolean shadow, float shadowDX, float shadowDY, float shadowBlur, float shadowOpacity, - boolean stroke, int strokeR, int strokeG, int strokeB, int strokeA, float strokeSize, boolean enableWrap, int overflow) { - String string; - if (bytes == null || bytes.length == 0) { - return false; - } else { - string = new String(bytes); - } + boolean stroke, int strokeR, int strokeG, int strokeB, int strokeA, + float strokeSize, boolean enableWrap, int overflow, boolean enableBold) { Layout.Alignment hAlignment = Layout.Alignment.ALIGN_NORMAL; int horizontalAlignment = alignment & 0x0F; @@ -169,7 +163,7 @@ public final class Cocos2dxBitmap { break; } - TextPaint paint = Cocos2dxBitmap.newPaint(fontName, fontSize); + TextPaint paint = Cocos2dxBitmap.newPaint(fontName, fontSize, enableBold); if (stroke) { paint.setStyle(TextPaint.Style.STROKE); @@ -252,7 +246,7 @@ public final class Cocos2dxBitmap { return true; } - private static TextPaint newPaint(final String fontName, final int fontSize) { + private static TextPaint newPaint(final String fontName, final int fontSize, final boolean enableBold) { final TextPaint paint = new TextPaint(); paint.setTextSize(fontSize); paint.setAntiAlias(true); @@ -271,7 +265,11 @@ public final class Cocos2dxBitmap { paint.setTypeface(Typeface.create(fontName, Typeface.NORMAL)); } } else { - paint.setTypeface(Typeface.create(fontName, Typeface.NORMAL)); + if(enableBold) { + paint.setTypeface(Typeface.create(fontName, Typeface.BOLD)); + } else { + paint.setTypeface(Typeface.create(fontName, Typeface.NORMAL)); + } } return paint; diff --git a/native/cocos/platform/ios/CCDevice-ios.mm b/native/cocos/platform/ios/CCDevice-ios.mm index 95fe6b2a28..9d62bd3b07 100644 --- a/native/cocos/platform/ios/CCDevice-ios.mm +++ b/native/cocos/platform/ios/CCDevice-ios.mm @@ -354,27 +354,35 @@ static CGSize _calculateStringSize(NSAttributedString *str, id font, CGSize *con return dim; } -static id _createSystemFont( const char * fontName, int size) +static id _createSystemFont( const char * fontName, int size, bool enableBold) { NSString * fntName = [NSString stringWithUTF8String:fontName]; - // On iOS custom fonts must be listed beforehand in the App info.plist (in order to be usable) and referenced only the by the font family name itself when - // calling [UIFont fontWithName]. Therefore even if the developer adds 'SomeFont.ttf' or 'fonts/SomeFont.ttf' to the App .plist, the font must - // be referenced as 'SomeFont' when calling [UIFont fontWithName]. Hence we strip out the folder path components and the extension here in order to get just - // the font family name itself. This stripping step is required especially for references to user fonts stored in CCB files; CCB files appear to store - // the '.ttf' extensions when referring to custom fonts. - fntName = [[fntName lastPathComponent] stringByDeletingPathExtension]; - - // create the font - id font = [UIFont fontWithName:fntName size:size]; + NSString* pathExtension = [fntName pathExtension]; + id font = NULL; + if ([pathExtension length] > 0) { + // On iOS custom fonts must be listed beforehand in the App info.plist (in order to be usable) and referenced only the by the font family name itself when + // calling [UIFont fontWithName]. Therefore even if the developer adds 'SomeFont.ttf' or 'fonts/SomeFont.ttf' to the App .plist, the font must + // be referenced as 'SomeFont' when calling [UIFont fontWithName]. Hence we strip out the folder path components and the extension here in order to get just + // the font family name itself. This stripping step is required especially for references to user fonts stored in CCB files; CCB files appear to store + // the '.ttf' extensions when referring to custom fonts. + fntName = [[fntName lastPathComponent] stringByDeletingPathExtension]; + + // create the font + font = [UIFont fontWithName:fntName size:size]; + } if (!font) { - font = [UIFont systemFontOfSize:size]; + if (enableBold) { + font = [UIFont boldSystemFontOfSize:size]; + } else { + font = [UIFont systemFontOfSize:size]; + } } return font; } -static bool _initWithString(const char * text, cocos2d::Device::TextAlign align, const char * fontName, int size, tImageInfo* info, bool enableWrap, int overflow) +static bool _initWithString(const char * text, cocos2d::Device::TextAlign align, const char * fontName, int size, tImageInfo* info, bool enableWrap, int overflow, bool enableBold) { bool bRet = false; @@ -382,7 +390,7 @@ static bool _initWithString(const char * text, cocos2d::Device::TextAlign align, { CC_BREAK_IF(! text || ! info); - id font = _createSystemFont(fontName, size); + id font = _createSystemFont(fontName, size, enableBold); CC_BREAK_IF(! font); @@ -562,7 +570,7 @@ Data Device::getTextureDataForText(const char * text, const FontDefinition& text info.tintColorB = textDefinition._fontFillColor.b / 255.0f; info.tintColorA = textDefinition._fontAlpha / 255.0f; - if (! _initWithString(text, align, textDefinition._fontName.c_str(), textDefinition._fontSize, &info, textDefinition._enableWrap, textDefinition._overflow)) + if (! _initWithString(text.c_str(), align, textDefinition._fontName.c_str(), textDefinition._fontSize, &info, textDefinition._enableWrap, textDefinition._overflow, textDefinition._enableBold)) { break; } diff --git a/native/cocos/platform/mac/CCDevice-mac.mm b/native/cocos/platform/mac/CCDevice-mac.mm index c57ffe7e0b..09554e6e08 100644 --- a/native/cocos/platform/mac/CCDevice-mac.mm +++ b/native/cocos/platform/mac/CCDevice-mac.mm @@ -129,7 +129,10 @@ static NSSize _calculateRealSizeForString(NSAttributedString **str, id font, NSS while (actualSize.size.width > constrainSize.width || actualSize.size.height > constrainSize.height) { fontSize = fontSize - 1; - + if (fontSize < 0) { + actualSize = CGRectMake(0, 0, 0, 0); + break; + } NSMutableAttributedString *mutableString = [[*str mutableCopy] autorelease]; *str = __attributedStringWithFontSize(mutableString, fontSize); @@ -162,7 +165,10 @@ static NSSize _calculateRealSizeForString(NSAttributedString **str, id font, NSS while (actualSize.size.height > constrainSize.height ||actualSize.size.width > constrainSize.width) { fontSize = fontSize - 1; - + if (fontSize < 0) { + actualSize = CGRectMake(0, 0, 0, 0); + break; + } NSMutableAttributedString *mutableString = [[*str mutableCopy] autorelease]; *str = __attributedStringWithFontSize(mutableString, fontSize); @@ -194,22 +200,25 @@ static NSSize _calculateRealSizeForString(NSAttributedString **str, id font, NSS return CGSizeMake(actualSize.size.width, actualSize.size.height); } -static NSFont* _createSystemFont(const char* fontName, int size) +static NSFont* _createSystemFont(const char* fontName, int size, bool enableBold) { NSString * fntName = [NSString stringWithUTF8String:fontName]; fntName = [[fntName lastPathComponent] stringByDeletingPathExtension]; - + NSFontTraitMask mask = NSUnboldFontMask | NSUnitalicFontMask; + if (enableBold) { + mask = NSBoldFontMask | NSUnitalicFontMask; + } // font NSFont *font = [[NSFontManager sharedFontManager] fontWithFamily:fntName - traits:NSUnboldFontMask | NSUnitalicFontMask + traits:mask weight:0 size:size]; if (font == nil) { font = [[NSFontManager sharedFontManager] fontWithFamily:@"Arial" - traits:NSUnboldFontMask | NSUnitalicFontMask + traits: mask weight:0 size:size]; } @@ -235,7 +244,7 @@ static CGFloat _calculateTextDrawStartHeight(cocos2d::Device::TextAlign align, C return startH; } -static bool _initWithString(const char * text, Device::TextAlign align, const char * fontName, int size, tImageInfo* info, const Color3B* fontColor, int fontAlpha, bool enableWrap, int overflow) +static bool _initWithString(const char * text, Device::TextAlign align, const char * fontName, int size, tImageInfo* info, const Color3B* fontColor, int fontAlpha, bool enableWrap, int overflow, bool enableBold) { bool ret = false; @@ -246,7 +255,7 @@ static bool _initWithString(const char * text, Device::TextAlign align, const ch NSString * string = [NSString stringWithUTF8String:text]; CC_BREAK_IF(!string); - id font = _createSystemFont(fontName, size); + id font = _createSystemFont(fontName, size, enableBold); CC_BREAK_IF(!font); // color @@ -345,8 +354,11 @@ Data Device::getTextureDataForText(const char * text, const FontDefinition& text tImageInfo info = {0}; info.width = textDefinition._dimensions.width; info.height = textDefinition._dimensions.height; - - if (! _initWithString(text, align, textDefinition._fontName.c_str(), textDefinition._fontSize, &info, &textDefinition._fontFillColor, textDefinition._fontAlpha, textDefinition._enableWrap, textDefinition._overflow)) + + if (! _initWithString(text.c_str(), align, textDefinition._fontName.c_str(), + textDefinition._fontSize, &info, &textDefinition._fontFillColor, + textDefinition._fontAlpha, textDefinition._enableWrap, textDefinition._overflow, + textDefinition._enableBold)) { break; } diff --git a/native/cocos/platform/win32/CCDevice-win32.cpp b/native/cocos/platform/win32/CCDevice-win32.cpp index 9ce9d5ffb7..3389c77813 100644 --- a/native/cocos/platform/win32/CCDevice-win32.cpp +++ b/native/cocos/platform/win32/CCDevice-win32.cpp @@ -99,7 +99,7 @@ public: } - bool setFont(const char * pFontName = "", int nSize = 0) + bool setFont(const char * pFontName = nullptr, int nSize = 0, bool enableBold = false) { bool bRet = false; do @@ -139,16 +139,27 @@ public: tNewFont.lfCharSet = DEFAULT_CHARSET; strcpy_s(tNewFont.lfFaceName, LF_FACESIZE, fontName.c_str()); } + if (nSize) { tNewFont.lfHeight = -nSize; } - GetObjectA(_font, sizeof(tOldFont), &tOldFont); + + if (enableBold) + { + tNewFont.lfWeight = FW_BOLD; + } + else + { + tNewFont.lfWeight = FW_NORMAL; + } + + GetObjectA(_font, sizeof(tOldFont), &tOldFont); if (tOldFont.lfHeight == tNewFont.lfHeight + && tOldFont.lfWeight == tNewFont.lfWeight && 0 == strcmp(tOldFont.lfFaceName, tNewFont.lfFaceName)) { - // already has the font bRet = true; break; } @@ -175,7 +186,7 @@ public: // disable Cleartype tNewFont.lfQuality = ANTIALIASED_QUALITY; - + // create new font _font = CreateFontIndirectA(&tNewFont); if (! _font) @@ -438,7 +449,7 @@ Data Device::getTextureDataForText(const char * text, const FontDefinition& text { BitmapDC& dc = sharedBitmapDC(); - if (! dc.setFont(textDefinition._fontName.c_str(), textDefinition._fontSize)) + if (! dc.setFont(textDefinition._fontName.c_str(), textDefinition._fontSize, textDefinition._enableBold)) { log("Can't found font(%s), use system default", textDefinition._fontName.c_str()); }