コード例 #1
0
bool CCTexture2D_richlabel::initWithRichString(const char *text, ccRichFontDefinition *textDefinition) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    
#if CC_ENABLE_CACHE_TEXTURE_DATA
	// cache the texture data
    VolatileTexture_richlabel::addRichStringTexture(this, text, *textDefinition);
#endif
	
	bool bRet = false;
	CCImage::ETextAlign eAlign;
	
	if (kCCVerticalTextAlignmentTop == textDefinition->m_vertAlignment)
	{
		eAlign = (kCCTextAlignmentCenter == textDefinition->m_alignment) ? CCImage::kAlignTop
		: (kCCTextAlignmentLeft == textDefinition->m_alignment) ? CCImage::kAlignTopLeft : CCImage::kAlignTopRight;
	}
	else if (kCCVerticalTextAlignmentCenter == textDefinition->m_vertAlignment)
	{
		eAlign = (kCCTextAlignmentCenter == textDefinition->m_alignment) ? CCImage::kAlignCenter
		: (kCCTextAlignmentLeft == textDefinition->m_alignment) ? CCImage::kAlignLeft : CCImage::kAlignRight;
	}
	else if (kCCVerticalTextAlignmentBottom == textDefinition->m_vertAlignment)
	{
		eAlign = (kCCTextAlignmentCenter == textDefinition->m_alignment) ? CCImage::kAlignBottom
		: (kCCTextAlignmentLeft == textDefinition->m_alignment) ? CCImage::kAlignBottomLeft : CCImage::kAlignBottomRight;
	}
	else
	{
		CCAssert(false, "Not supported alignment format!");
		return false;
	}
	
	// handle shadow parameters
	bool  shadowEnabled =  false;
	float shadowDX      = 0.0;
	float shadowDY      = 0.0;
	float shadowBlur    = 0.0;
    int shadowColor     = 0;
	
	if ( textDefinition->m_shadow.m_shadowEnabled )
	{
		shadowEnabled =  true;
		shadowDX      = textDefinition->m_shadow.m_shadowOffset.width;
		shadowDY      = textDefinition->m_shadow.m_shadowOffset.height;
		shadowBlur    = textDefinition->m_shadow.m_shadowBlur;
        shadowColor   = textDefinition->m_shadowColor;
	}
	
	// handle stroke parameters
	bool strokeEnabled = false;
	float strokeColorR = 0.0;
	float strokeColorG = 0.0;
	float strokeColorB = 0.0;
	float strokeSize   = 0.0;
	
	if ( textDefinition->m_stroke.m_strokeEnabled )
	{
		strokeEnabled = true;
		strokeColorR = textDefinition->m_stroke.m_strokeColor.r / 255.0f;
		strokeColorG = textDefinition->m_stroke.m_strokeColor.g / 255.0f;
		strokeColorB = textDefinition->m_stroke.m_strokeColor.b / 255.0f;
		strokeSize   = textDefinition->m_stroke.m_strokeSize;
	}
	
	CCImage_richlabel* pImage = new CCImage_richlabel();
	do
	{
		CC_BREAK_IF(NULL == pImage);
		
		bRet = pImage->initWithRichStringShadowStroke(text,
												  (int)textDefinition->m_dimensions.width,
												  (int)textDefinition->m_dimensions.height,
												  eAlign,
												  textDefinition->m_fontName.c_str(),
												  textDefinition->m_fontSize,
												  textDefinition->m_fontFillColor.r / 255.0f,
												  textDefinition->m_fontFillColor.g / 255.0f,
												  textDefinition->m_fontFillColor.b / 255.0f,
												  shadowEnabled,
												  shadowDX,
												  shadowDY,
                                                  shadowColor,
												  shadowBlur,
												  strokeEnabled,
												  strokeColorR,
												  strokeColorG,
												  strokeColorB,
												  strokeSize,
                                                  textDefinition->m_globalImageScaleFactor,
												  textDefinition->decryptFunc);
		
		
		CC_BREAK_IF(!bRet);
		bRet = initWithImage(pImage);
		
		// save info needed by rich label
		m_shadowStrokePadding = pImage->getShadowStrokePadding();
		m_linkMetas = pImage->getLinkMetas();
		m_imageRects = pImage->getImageRects();
	} while (0);
	
	CC_SAFE_RELEASE(pImage);
	
	return bRet;
    
    
#else
    
	CCAssert(false, "Currently only supported on iOS and Android!");
	return false;
    
#endif
}
コード例 #2
0
bool CCTexture2D_richlabel::initWithRichString(const char *text, ccRichFontDefinition *textDefinition) {
 #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    // std::vector <unsigned short> utf16result;
    // utf8::unchecked::utf8to16((char*)text, (char*)text + strlen(text), back_inserter(utf16result));
    // unsigned short utf16string[] = utf16result.;
    // std::vector<unsigned char> utf8result;
    // utf8::unchecked::utf16to8(utf16result.begin(), utf16result.end(), back_inserter(utf8result));
    /* char* pMem = new char [strlen(text) +6];
     memcpy(pMem, text, strlen(text));
     pMem[strlen(text)] = 0;
     //CCLOG("beginrender:%s",text);*/
    int errIndex = 0;
    if( !correctUtfBytes( text ,errIndex) )
    {
         CCLOG("initWithRichString error:%s,%d",text, errIndex);
        return false;
    }
 #endif
    
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    
#if CC_ENABLE_CACHE_TEXTURE_DATA
	// cache the texture data
    VolatileTexture_richlabel::addRichStringTexture(this, text, *textDefinition);
#endif
	
	bool bRet = false;
    Device::TextAlign eAlign;
	
	if (kCCVerticalTextAlignmentTop == textDefinition->_vertAlignment)
	{
        eAlign = (kCCTextAlignmentCenter == textDefinition->_alignment) ? Device::TextAlign::TOP
		: (kCCTextAlignmentLeft == textDefinition->_alignment) ? Device::TextAlign::TOP_LEFT : Device::TextAlign::TOP_RIGHT;
	}
	else if (kCCVerticalTextAlignmentCenter == textDefinition->_vertAlignment)
	{
        eAlign = (kCCTextAlignmentCenter == textDefinition->_alignment) ? Device::TextAlign::CENTER
		: (kCCTextAlignmentLeft == textDefinition->_alignment) ? Device::TextAlign::LEFT : Device::TextAlign::RIGHT;
	}
	else if (kCCVerticalTextAlignmentBottom == textDefinition->_vertAlignment)
	{
		eAlign = (kCCTextAlignmentCenter == textDefinition->_alignment) ? Device::TextAlign::BOTTOM
		: (kCCTextAlignmentLeft == textDefinition->_alignment) ? Device::TextAlign::BOTTOM_LEFT : Device::TextAlign::BOTTOM_RIGHT;
	}
	else
	{
		CCAssert(false, "Not supported alignment format!");
		return false;
	}
	
	// handle shadow parameters
	bool  shadowEnabled =  false;
	float shadowDX      = 0.0;
	float shadowDY      = 0.0;
	float shadowBlur    = 0.0;
    int shadowColor     = 0;
	
	if ( textDefinition->_shadow._shadowEnabled )
	{
		shadowEnabled =  true;
		shadowDX      = textDefinition->_shadow._shadowOffset.width;
		shadowDY      = textDefinition->_shadow._shadowOffset.height;
		shadowBlur    = textDefinition->_shadow._shadowBlur;
        shadowColor   = textDefinition->m_shadowColor;
	}
	
	// handle stroke parameters
	bool strokeEnabled = false;
	float strokeColorR = 0.0;
	float strokeColorG = 0.0;
	float strokeColorB = 0.0;
	float strokeSize   = 0.0;
	
	if ( textDefinition->_stroke._strokeEnabled )
	{
		strokeEnabled = true;
		strokeColorR = textDefinition->_stroke._strokeColor.r / 255.0f;
		strokeColorG = textDefinition->_stroke._strokeColor.g / 255.0f;
		strokeColorB = textDefinition->_stroke._strokeColor.b / 255.0f;
		strokeSize   = textDefinition->_stroke._strokeSize;
	}
	
	CCImage_richlabel* pImage = new CCImage_richlabel();
	do
	{
		CC_BREAK_IF(NULL == pImage);
		
		bRet = pImage->initWithRichStringShadowStroke(text,
												  (int)textDefinition->_dimensions.width,
												  (int)textDefinition->_dimensions.height,
												  eAlign,
												  textDefinition->_fontName.c_str(),
												  textDefinition->_fontSize,
												  textDefinition->_fontFillColor.r / 255.0f,
												  textDefinition->_fontFillColor.g / 255.0f,
												  textDefinition->_fontFillColor.b / 255.0f,
												  shadowEnabled,
												  shadowDX,
												  shadowDY,
                                                  shadowColor,
												  shadowBlur,
												  strokeEnabled,
												  strokeColorR,
												  strokeColorG,
												  strokeColorB,
												  strokeSize);
		
		
		CC_BREAK_IF(!bRet);
		bRet = initWithImage(pImage);
		
		// save info needed by rich label
		m_shadowStrokePadding = pImage->getShadowStrokePadding();
		m_linkMetas = pImage->getLinkMetas();
	} while (0);
	
	CC_SAFE_RELEASE(pImage);
	
	return bRet;
    
    
#else
    
	CCAssert(false, "Currently only supported on iOS and Android!");
	return false;
    
#endif
}