From a6c71559226281cb389797f94a7fb26a900bfc86 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 29 Aug 2016 11:12:01 +0800 Subject: [PATCH 1/7] fix label shrink crash issue --- native/cocos/platform/mac/CCDevice-mac.mm | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/native/cocos/platform/mac/CCDevice-mac.mm b/native/cocos/platform/mac/CCDevice-mac.mm index 6ac18241eb..e5a9bb286c 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); @@ -156,7 +159,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); From 6c92d8daaaa7305e328f90d00d64de3a5d11ebb4 Mon Sep 17 00:00:00 2001 From: zhangbin Date: Tue, 30 Aug 2016 14:08:27 +0800 Subject: [PATCH 2/7] Solve the error when compile project with NDK r10. --- native/cocos/2d/CCTMXXMLParser.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/native/cocos/2d/CCTMXXMLParser.cpp b/native/cocos/2d/CCTMXXMLParser.cpp index d875a6c8ad..ba4743eee3 100644 --- a/native/cocos/2d/CCTMXXMLParser.cpp +++ b/native/cocos/2d/CCTMXXMLParser.cpp @@ -412,7 +412,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; From 96d066204db49a8c0e7f66c33924c87092861b3b Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 30 Aug 2016 14:41:59 +0800 Subject: [PATCH 3/7] add bold support for mac --- native/cocos/2d/CCLabel.cpp | 44 ++++++++++++++--------- native/cocos/base/ccTypes.h | 1 + native/cocos/platform/mac/CCDevice-mac.mm | 18 ++++++---- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/native/cocos/2d/CCLabel.cpp b/native/cocos/2d/CCLabel.cpp index 817738c325..206a586875 100644 --- a/native/cocos/2d/CCLabel.cpp +++ b/native/cocos/2d/CCLabel.cpp @@ -1107,14 +1107,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() @@ -1348,8 +1348,32 @@ void Label::updateContent() if (_underlineNode) { _underlineNode->clear(); + + if(_currentLabelType == Label::LabelType::STRING_TEXTURE) { + // system font + float y = 0; + const auto spriteSize = _textSprite->getContentSize(); - if (_numberOfLines) + this->computeStringNumLines(); + + if (_numberOfLines > 0) { + // atlas font + const float charheight = (spriteSize.height / _numberOfLines); + _underlineNode->setLineWidth(charheight/10); + for (int i=0; i<_numberOfLines; ++i) + { + float offsety = 0; + if (_strikethroughEnabled) + offsety += charheight / 2; + // FIXME: Might not work with different vertical alignments + y = (_numberOfLines - i - 1) * charheight + offsety; + _underlineNode->drawLine(Vec2(0, y), Vec2(spriteSize.width,y), _textColorF); + } + } + + } + + else if(_numberOfLines) { const float charheight = (_textDesiredHeight / _numberOfLines); _underlineNode->setLineWidth(charheight/6); @@ -1365,19 +1389,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){ @@ -1985,6 +1996,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/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/mac/CCDevice-mac.mm b/native/cocos/platform/mac/CCDevice-mac.mm index e5a9bb286c..dbfd3f2a63 100644 --- a/native/cocos/platform/mac/CCDevice-mac.mm +++ b/native/cocos/platform/mac/CCDevice-mac.mm @@ -188,22 +188,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]; } @@ -230,7 +233,7 @@ static CGFloat _calculateTextDrawStartHeight(cocos2d::Device::TextAlign align, C } -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; @@ -240,7 +243,7 @@ static bool _initWithString(const char * text, Device::TextAlign align, const ch do { NSString * string = [NSString stringWithUTF8String:text]; - id font = _createSystemFont(fontName, size); + id font = _createSystemFont(fontName, size, enableBold); CC_BREAK_IF(!font); // color @@ -342,7 +345,8 @@ Data Device::getTextureDataForText(const std::string& text, const FontDefinition if (! _initWithString(text.c_str(), align, textDefinition._fontName.c_str(), textDefinition._fontSize, &info, &textDefinition._fontFillColor, - textDefinition._fontAlpha, textDefinition._enableWrap, textDefinition._overflow)) + textDefinition._fontAlpha, textDefinition._enableWrap, textDefinition._overflow, + textDefinition._enableBold)) { break; } From 5134ca185149086ade79fedb8d22e743e2f8cf0f Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 30 Aug 2016 15:00:20 +0800 Subject: [PATCH 4/7] add bold support for iOS --- native/cocos/platform/ios/CCDevice-ios.mm | 36 ++++++++++++++--------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/native/cocos/platform/ios/CCDevice-ios.mm b/native/cocos/platform/ios/CCDevice-ios.mm index 8fcc76512c..c70da05761 100644 --- a/native/cocos/platform/ios/CCDevice-ios.mm +++ b/native/cocos/platform/ios/CCDevice-ios.mm @@ -348,27 +348,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; @@ -376,7 +384,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); @@ -554,7 +562,7 @@ Data Device::getTextureDataForText(const std::string& text, const FontDefinition info.tintColorB = textDefinition._fontFillColor.b / 255.0f; info.tintColorA = textDefinition._fontAlpha / 255.0f; - if (! _initWithString(text.c_str(), 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; } From 81393c3ef44633d075f3c0b9975736992cd2dbfb Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 30 Aug 2016 15:16:10 +0800 Subject: [PATCH 5/7] add android bold support --- native/cocos/platform/android/CCDevice-android.cpp | 4 ++-- .../java/src/org/cocos2dx/lib/Cocos2dxBitmap.java | 13 +++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/native/cocos/platform/android/CCDevice-android.cpp b/native/cocos/platform/android/CCDevice-android.cpp index c19a92f4b6..7a98991840 100644 --- a/native/cocos/platform/android/CCDevice-android.cpp +++ b/native/cocos/platform/android/CCDevice-android.cpp @@ -90,7 +90,7 @@ public: { JniMethodInfo methodInfo; if (! JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/lib/Cocos2dxBitmap", "createTextBitmapShadowStroke", - "(Ljava/lang/String;Ljava/lang/String;IIIIIIIIZIIIIFZI)Z")) + "(Ljava/lang/String;Ljava/lang/String;IIIIIIIIZIIIIFZIZ)Z")) { CCLOG("%s %d: error to get methodInfo", __FILE__, __LINE__); return false; @@ -129,7 +129,7 @@ public: textDefinition._stroke._strokeColor.r, textDefinition._stroke._strokeColor.g, textDefinition._stroke._strokeColor.b, textDefinition._stroke._strokeAlpha, textDefinition._stroke._strokeSize, - textDefinition._enableWrap, textDefinition._overflow)) + textDefinition._enableWrap, textDefinition._overflow, textDefinition._enableBold)) { ret = false; } 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 bf43de4c6d..a4ca820a52 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 @@ -124,7 +124,8 @@ public final class Cocos2dxBitmap { public static boolean createTextBitmapShadowStroke(final String string, final String fontName, int fontSize, int fontTintR, int fontTintG, int fontTintB, int fontTintA, int alignment, int width, int height, - boolean stroke, int strokeR, int strokeG, int strokeB, int strokeA, float strokeSize, boolean enableWrap, int overflow) { + 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; @@ -141,7 +142,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); @@ -224,7 +225,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); @@ -243,7 +244,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; From ae35fc46f53362c52bd371853d973a898dd4cb9f Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 31 Aug 2016 10:46:14 +0800 Subject: [PATCH 6/7] improve system font underline --- native/cocos/2d/CCLabel.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/native/cocos/2d/CCLabel.cpp b/native/cocos/2d/CCLabel.cpp index 206a586875..94c2923262 100644 --- a/native/cocos/2d/CCLabel.cpp +++ b/native/cocos/2d/CCLabel.cpp @@ -1351,23 +1351,20 @@ void Label::updateContent() if(_currentLabelType == Label::LabelType::STRING_TEXTURE) { // system font - float y = 0; const auto spriteSize = _textSprite->getContentSize(); - - this->computeStringNumLines(); + this->computeStringNumLines(); + float startY = spriteSize.height / 2 - _lineHeight * (_numberOfLines - 1) / 2 - _systemFontSize / 2; if (_numberOfLines > 0) { // atlas font - const float charheight = (spriteSize.height / _numberOfLines); - _underlineNode->setLineWidth(charheight/10); + _underlineNode->setLineWidth(_systemFontSize / 8); for (int i=0; i<_numberOfLines; ++i) { - float offsety = 0; if (_strikethroughEnabled) - offsety += charheight / 2; + startY += _systemFontSize / 2; // FIXME: Might not work with different vertical alignments - y = (_numberOfLines - i - 1) * charheight + offsety; - _underlineNode->drawLine(Vec2(0, y), Vec2(spriteSize.width,y), _textColorF); + _underlineNode->drawLine(Vec2(0, startY - 1), + Vec2(spriteSize.width, startY - 1), _textColorF); } } From 299280cbe069acc335ba2f2d553f840ec4881913 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 31 Aug 2016 11:35:45 -0700 Subject: [PATCH 7/7] fix windows systemfont bold --- .../cocos/platform/win32/CCDevice-win32.cpp | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/native/cocos/platform/win32/CCDevice-win32.cpp b/native/cocos/platform/win32/CCDevice-win32.cpp index 6b938365f3..6ca37016ec 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 = nullptr, 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 std::string& text, const FontDefinition { 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()); }