Merge pull request #292 from zilongshanren/add-systemfont-bold-support

Add systemfont bold support
This commit is contained in:
Wang Nan
2016-08-30 17:40:07 +08:00
committed by GitHub
6 changed files with 73 additions and 43 deletions
+28 -16
View File
@@ -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)
{
+1
View File
@@ -572,6 +572,7 @@ public:
bool _enableWrap;
/// enable shrink font size
int _overflow;
bool _enableBold;
};
/**
@@ -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;
}
@@ -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;
+22 -14
View File
@@ -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;
}
+11 -7
View File
@@ -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;
}