Fix problem with CCLabelTTF + shadow and text alignment; also fontDefinition property names now are lowercase

This commit is contained in:
carlo morgantini 2013-05-10 14:15:02 -07:00
parent 36b88039a1
commit 3b50ffe5ba
4 changed files with 125 additions and 59 deletions

View File

@ -289,7 +289,7 @@ bool CCLabelTTF::updateTexture()
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
ccFontDefinition texDef = _prepareTextDefinition();
ccFontDefinition texDef = _prepareTextDefinition(true);
tex->initWithStringShadowStroke( m_string.c_str(), &texDef );
#else
@ -331,7 +331,9 @@ void CCLabelTTF::enableShadow(CCSize &shadowOffset, float shadowOpacity, float s
if ( (m_shadowOffset.width != shadowOffset.width) || (m_shadowOffset.height!=shadowOffset.height) )
{
m_shadowOffset = shadowOffset;
m_shadowOffset.width = shadowOffset.width;
m_shadowOffset.height = shadowOffset.height;
valueChanged = true;
}
@ -456,7 +458,7 @@ void CCLabelTTF::setTextDefinition(ccFontDefinition *theDefinition)
ccFontDefinition *CCLabelTTF::getTextDefinition()
{
ccFontDefinition *tempDefinition = new ccFontDefinition;
*tempDefinition = _prepareTextDefinition();
*tempDefinition = _prepareTextDefinition(false);
return tempDefinition;
}
@ -489,33 +491,56 @@ void CCLabelTTF::_updateWithTextDefinition(ccFontDefinition & textDefinition, bo
updateTexture();
}
ccFontDefinition CCLabelTTF::_prepareTextDefinition()
ccFontDefinition CCLabelTTF::_prepareTextDefinition(bool adjustForResolution)
{
ccFontDefinition texDef;
texDef.m_fontSize = m_fFontSize * CC_CONTENT_SCALE_FACTOR();
if (adjustForResolution)
texDef.m_fontSize = m_fFontSize * CC_CONTENT_SCALE_FACTOR();
else
texDef.m_fontSize = m_fFontSize;
texDef.m_fontName = *m_pFontName;
texDef.m_alignment = m_hAlignment;
texDef.m_vertAlignment = m_vAlignment;
texDef.m_dimensions = CC_SIZE_POINTS_TO_PIXELS(m_tDimensions);
if (adjustForResolution)
texDef.m_dimensions = CC_SIZE_POINTS_TO_PIXELS(m_tDimensions);
else
texDef.m_dimensions = m_tDimensions;
// stroke
if ( m_strokeEnabled )
{
texDef.m_stroke.m_strokeEnabled = true;
texDef.m_stroke.m_strokeSize = m_strokeSize;
texDef.m_stroke.m_strokeColor = m_strokeColor;
if (adjustForResolution)
texDef.m_stroke.m_strokeSize = m_strokeSize * CC_CONTENT_SCALE_FACTOR();
else
texDef.m_stroke.m_strokeSize = m_strokeSize;
}
else
{
texDef.m_stroke.m_strokeEnabled = false;
}
// shadow
if ( m_shadowEnabled )
{
texDef.m_shadow.m_shadowEnabled = true;
texDef.m_shadow.m_shadowOffset = m_shadowOffset;
texDef.m_shadow.m_shadowBlur = m_shadowBlur;
texDef.m_shadow.m_shadowOpacity = m_shadowOpacity;
texDef.m_shadow.m_shadowEnabled = true;
texDef.m_shadow.m_shadowBlur = m_shadowBlur;
texDef.m_shadow.m_shadowOpacity = m_shadowOpacity;
if (adjustForResolution)
texDef.m_shadow.m_shadowOffset = CC_SIZE_POINTS_TO_PIXELS(m_shadowOffset);
else
texDef.m_shadow.m_shadowOffset = m_shadowOffset;
}
else
{

View File

@ -144,7 +144,7 @@ protected:
/** set the text definition for this label */
void _updateWithTextDefinition(ccFontDefinition & textDefinition, bool mustUpdateTexture = true);
ccFontDefinition _prepareTextDefinition();
ccFontDefinition _prepareTextDefinition(bool adjustForResolution = false);
/** Dimensions of the label in Points */
CCSize m_tDimensions;

View File

@ -270,35 +270,43 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl
}
// compute the padding needed by shadow and stroke
float shadowStrokePaddingX = 0.0f;
float shadowStrokePaddingY = 0.0f;
if ( pInfo->hasStroke )
{
shadowStrokePaddingX = pInfo->strokeSize * 4.0;
shadowStrokePaddingY = pInfo->strokeSize * 4.0;
shadowStrokePaddingX = pInfo->strokeSize;
shadowStrokePaddingY = pInfo->strokeSize;
}
if ( pInfo->hasShadow )
{
shadowStrokePaddingX = std::max(shadowStrokePaddingX, ((float)abs(pInfo->shadowOffset.width) * 4));
shadowStrokePaddingY = std::max(shadowStrokePaddingY, ((float)abs(pInfo->shadowOffset.height) * 4));
shadowStrokePaddingX = std::max(shadowStrokePaddingX, (float)abs(pInfo->shadowOffset.width));
shadowStrokePaddingY = std::max(shadowStrokePaddingY, (float)abs(pInfo->shadowOffset.height));
}
// add the padding if needed
//dim.width += shadowStrokePaddingX;
// add the padding (this could be 0 if no shadow and no stroke)
dim.width += shadowStrokePaddingX;
dim.height += shadowStrokePaddingY;
unsigned char* data = new unsigned char[(int)(dim.width * dim.height * 4)];
memset(data, 0, (int)(dim.width * dim.height * 4));
// draw text
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(data, dim.width, dim.height, 8, dim.width * 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(data,
dim.width,
dim.height,
8,
dim.width * 4,
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
if ( ! context )
if (!context)
{
delete[] data;
break;
@ -306,9 +314,11 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl
// text color
CGContextSetRGBFillColor(context, pInfo->tintColorR, pInfo->tintColorG, pInfo->tintColorB, 1);
CGContextTranslateCTM(context, 0.0f, dim.height);
// move Y rendering to the top of the image
CGContextTranslateCTM(context, 0.0f, (dim.height - shadowStrokePaddingY) );
CGContextScaleCTM(context, 1.0f, -1.0f); //NOTE: NSString draws in UIKit referential i.e. renders upside-down compared to CGBitmapContext referential
// store the current context
UIGraphicsPushContext(context);
// measure text size with specified font and determine the rectangle to draw text in
@ -318,7 +328,6 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl
: UITextAlignmentLeft);
// take care of stroke if needed
if ( pInfo->hasStroke )
{
@ -336,8 +345,9 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl
CGContextSetShadow(context, offset, pInfo->shadowBlur);
}
// normal fonts
//if( [font isKindOfClass:[UIFont class] ] )
//{
// [str drawInRect:CGRectMake(0, startH, dim.width, dim.height) withFont:font lineBreakMode:(UILineBreakMode)UILineBreakModeWordWrap alignment:align];
@ -348,8 +358,39 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl
////alignment:align];
//}
// draw the text
[str drawInRect:CGRectMake(0, startH + (shadowStrokePaddingY), dim.width, dim.height) withFont:font lineBreakMode:(UILineBreakMode)UILineBreakModeWordWrap alignment:align];
// compute the rect used for rendering the text
// based on wether shadows or stroke are enabled
float textOriginX = 0.0;
float textOrigingY = 0.0;
float textWidth = dim.width - shadowStrokePaddingX;
float textHeight = dim.height - shadowStrokePaddingY;
if ( pInfo->shadowOffset.width < 0 )
{
textOriginX = shadowStrokePaddingX;
}
else
{
textOriginX = 0.0;
}
if (pInfo->shadowOffset.height > 0)
{
textOrigingY = startH;
}
else
{
textOrigingY = startH - shadowStrokePaddingY;
}
// actually draw the text in the context
[str drawInRect:CGRectMake(textOriginX, textOrigingY, textWidth, textHeight) withFont:font lineBreakMode:(UILineBreakMode)UILineBreakModeWordWrap alignment:align];
// pop the context
UIGraphicsPopContext();
@ -358,13 +399,13 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl
CGContextRelease(context);
// output params
pInfo->data = data;
pInfo->hasAlpha = true;
pInfo->data = data;
pInfo->hasAlpha = true;
pInfo->isPremultipliedAlpha = true;
pInfo->bitsPerComponent = 8;
pInfo->width = dim.width;
pInfo->height = dim.height;
bRet = true;
pInfo->bitsPerComponent = 8;
pInfo->width = dim.width;
pInfo->height = dim.height;
bRet = true;
} while (0);

View File

@ -2072,7 +2072,7 @@ JSBool jsval_to_ccfontdefinition( JSContext *cx, jsval vp, ccFontDefinition *out
// font name
jsval jsr;
JS_GetProperty(cx, jsobj, "FontName", &jsr);
JS_GetProperty(cx, jsobj, "fontName", &jsr);
JS_ValueToString(cx, jsr);
JSStringWrapper wrapper(jsr);
if ( wrapper )
@ -2086,10 +2086,10 @@ JSBool jsval_to_ccfontdefinition( JSContext *cx, jsval vp, ccFontDefinition *out
// font size
JSBool hasProperty;
JS_HasProperty(cx, jsobj, "FontSize", &hasProperty);
JS_HasProperty(cx, jsobj, "fontSize", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "FontSize", &jsr);
JS_GetProperty(cx, jsobj, "fontSize", &jsr);
double fontSize = 0.0;
JS_ValueToNumber(cx, jsr, &fontSize);
out->m_fontSize = fontSize;
@ -2100,10 +2100,10 @@ JSBool jsval_to_ccfontdefinition( JSContext *cx, jsval vp, ccFontDefinition *out
}
// font alignment horizontal
JS_HasProperty(cx, jsobj, "FontAlignmentH", &hasProperty);
JS_HasProperty(cx, jsobj, "fontAlignmentH", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "FontAlignmentH", &jsr);
JS_GetProperty(cx, jsobj, "fontAlignmentH", &jsr);
double fontAlign = 0.0;
JS_ValueToNumber(cx, jsr, &fontAlign);
out->m_alignment = (CCTextAlignment)fontAlign;
@ -2114,10 +2114,10 @@ JSBool jsval_to_ccfontdefinition( JSContext *cx, jsval vp, ccFontDefinition *out
}
// font alignment vertical
JS_HasProperty(cx, jsobj, "FontAlignmentV", &hasProperty);
JS_HasProperty(cx, jsobj, "fontAlignmentV", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "FontAlignmentV", &jsr);
JS_GetProperty(cx, jsobj, "fontAlignmentV", &jsr);
double fontAlign = 0.0;
JS_ValueToNumber(cx, jsr, &fontAlign);
out->m_vertAlignment = (CCVerticalTextAlignment)fontAlign;
@ -2128,10 +2128,10 @@ JSBool jsval_to_ccfontdefinition( JSContext *cx, jsval vp, ccFontDefinition *out
}
// font fill color
JS_HasProperty(cx, jsobj, "FontFillColor", &hasProperty);
JS_HasProperty(cx, jsobj, "fontFillColor", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "FontFillColor", &jsr);
JS_GetProperty(cx, jsobj, "fontFillColor", &jsr);
JSObject *jsobjColor;
if( ! JS_ValueToObject( cx, jsr, &jsobjColor ) )
@ -2141,10 +2141,10 @@ JSBool jsval_to_ccfontdefinition( JSContext *cx, jsval vp, ccFontDefinition *out
}
// font rendering box dimensions
JS_HasProperty(cx, jsobj, "FontDimensions", &hasProperty);
JS_HasProperty(cx, jsobj, "fontDimensions", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "FontDimensions", &jsr);
JS_GetProperty(cx, jsobj, "fontDimensions", &jsr);
JSObject *jsobjSize;
if( ! JS_ValueToObject( cx, jsr, &jsobjSize ) )
@ -2154,10 +2154,10 @@ JSBool jsval_to_ccfontdefinition( JSContext *cx, jsval vp, ccFontDefinition *out
}
// shadow
JS_HasProperty(cx, jsobj, "ShadowEnabled", &hasProperty);
JS_HasProperty(cx, jsobj, "shadowEnabled", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "ShadowEnabled", &jsr);
JS_GetProperty(cx, jsobj, "shadowEnabled", &jsr);
out->m_shadow.m_shadowEnabled = ToBoolean(jsr);
if( out->m_shadow.m_shadowEnabled )
@ -2168,10 +2168,10 @@ JSBool jsval_to_ccfontdefinition( JSContext *cx, jsval vp, ccFontDefinition *out
out->m_shadow.m_shadowOpacity = 1;
// shado offset
JS_HasProperty(cx, jsobj, "ShadowOffset", &hasProperty);
JS_HasProperty(cx, jsobj, "shadowOffset", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "ShadowOffset", &jsr);
JS_GetProperty(cx, jsobj, "shadowOffset", &jsr);
JSObject *jsobjShadowOffset;
if( ! JS_ValueToObject( cx, jsr, &jsobjShadowOffset ) )
@ -2180,20 +2180,20 @@ JSBool jsval_to_ccfontdefinition( JSContext *cx, jsval vp, ccFontDefinition *out
}
// shadow blur
JS_HasProperty(cx, jsobj, "ShadowBlur", &hasProperty);
JS_HasProperty(cx, jsobj, "shadowBlur", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "ShadowBlur", &jsr);
JS_GetProperty(cx, jsobj, "shadowBlur", &jsr);
double shadowBlur = 0.0;
JS_ValueToNumber(cx, jsr, &shadowBlur);
out->m_shadow.m_shadowBlur = shadowBlur;
}
// shadow intensity
JS_HasProperty(cx, jsobj, "ShadowOpacity", &hasProperty);
JS_HasProperty(cx, jsobj, "shadowOpacity", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "ShadowOpacity", &jsr);
JS_GetProperty(cx, jsobj, "shadowOpacity", &jsr);
double shadowOpacity = 0.0;
JS_ValueToNumber(cx, jsr, &shadowOpacity);
out->m_shadow.m_shadowOpacity = shadowOpacity;
@ -2202,10 +2202,10 @@ JSBool jsval_to_ccfontdefinition( JSContext *cx, jsval vp, ccFontDefinition *out
}
// stroke
JS_HasProperty(cx, jsobj, "StrokeEnabled", &hasProperty);
JS_HasProperty(cx, jsobj, "strokeEnabled", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "StrokeEnabled", &jsr);
JS_GetProperty(cx, jsobj, "strokeEnabled", &jsr);
out->m_stroke.m_strokeEnabled = ToBoolean(jsr);
if( out->m_stroke.m_strokeEnabled )
@ -2215,10 +2215,10 @@ JSBool jsval_to_ccfontdefinition( JSContext *cx, jsval vp, ccFontDefinition *out
out->m_stroke.m_strokeColor = ccBLUE;
// stroke color
JS_HasProperty(cx, jsobj, "StrokeColor", &hasProperty);
JS_HasProperty(cx, jsobj, "strokeColor", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "StrokeColor", &jsr);
JS_GetProperty(cx, jsobj, "strokeColor", &jsr);
JSObject *jsobjStrokeColor;
if( ! JS_ValueToObject( cx, jsr, &jsobjStrokeColor ) )
@ -2227,10 +2227,10 @@ JSBool jsval_to_ccfontdefinition( JSContext *cx, jsval vp, ccFontDefinition *out
}
// stroke size
JS_HasProperty(cx, jsobj, "StrokeSize", &hasProperty);
JS_HasProperty(cx, jsobj, "strokeSize", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "StrokeSize", &jsr);
JS_GetProperty(cx, jsobj, "strokeSize", &jsr);
double strokeSize = 0.0;
JS_ValueToNumber(cx, jsr, &strokeSize);
out->m_stroke.m_strokeSize = strokeSize;