From 96d066204db49a8c0e7f66c33924c87092861b3b Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 30 Aug 2016 14:41:59 +0800 Subject: [PATCH 1/3] 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 2/3] 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 3/3] 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;