issus #2893:improve new label

This commit is contained in:
Dhilan007 2013-10-31 17:52:22 +08:00
parent 12db6a32ab
commit b8faaeba59
10 changed files with 52 additions and 67 deletions

View File

@ -14,15 +14,15 @@
NS_CC_BEGIN
FontAtlas::FontAtlas(Font &theFont) :
_font(theFont),
_font(&theFont),
_currentPageData(nullptr)
{
_font.retain();
_font->retain();
FontFreeType* fontTTf = dynamic_cast<FontFreeType*>(&_font);
FontFreeType* fontTTf = dynamic_cast<FontFreeType*>(_font);
if (fontTTf && fontTTf->isDynamicGlyphCollection())
{
_currentPageLineHeight = _font.getFontMaxHeight();
_currentPageLineHeight = _font->getFontMaxHeight();
_commonLineHeight = _currentPageLineHeight * 0.8f;
Texture2D * tex = new Texture2D;
_currentPage = 0;
@ -34,16 +34,16 @@ _currentPageData(nullptr)
_currentPageData = new unsigned char[_currentPageDataSize];
memset(_currentPageData, 0, _currentPageDataSize);
addTexture(*tex,0);
tex->release();
}
}
FontAtlas::~FontAtlas()
{
_font.release();
_font->release();
relaseTextures();
if(_currentPageData)
delete []_currentPageData;
delete []_currentPageData;
}
void FontAtlas::relaseTextures()
@ -79,7 +79,7 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String)
if(_currentPageData == nullptr)
return false;
FontFreeType* fontTTf = (FontFreeType*)&_font;
FontFreeType* fontTTf = (FontFreeType*)_font;
std::vector<FontLetterDefinition> fontDefs;
int length = cc_wcslen(utf16String);
@ -100,7 +100,6 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String)
if (!fontTTf->getBBOXFotChar(utf16String[i], tempRect))
{
//log("Warning: Cannot find definition for glyph: %c in font:%s", utf16String[i], _fontName.c_str());
tempDef.validDefinition = false;
tempDef.letteCharUTF16 = utf16String[i];
tempDef.commonLineHeight = 0;
@ -150,7 +149,9 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String)
return false;
memset(_currentPageData, 0, _currentPageDataSize);
_currentPage++;
addTexture(*(new Texture2D),_currentPage);
Texture2D* tex = new Texture2D;
addTexture(*tex,_currentPage);
tex->release();
}
}
renderCharAt(fontDefs[i].letteCharUTF16,_currentPageOrigX,_currentPageOrigY,_currentPageData,1024);
@ -182,7 +183,7 @@ bool FontAtlas::renderCharAt(unsigned short int charToRender, int posX, int posY
int sourceHeight = 0;
// get the glyph's bitmap
sourceBitmap = _font.getGlyphBitmap(charToRender, sourceWidth, sourceHeight);
sourceBitmap = _font->getGlyphBitmap(charToRender, sourceWidth, sourceHeight);
if (!sourceBitmap)
return false;
@ -234,7 +235,7 @@ void FontAtlas::setCommonLineHeight(float newHeight)
_commonLineHeight = newHeight;
}
Font & FontAtlas::getFont() const
const Font * FontAtlas::getFont() const
{
return _font;
}

View File

@ -70,7 +70,7 @@ public:
void setCommonLineHeight(float newHeight);
Texture2D & getTexture(int slot);
Font & getFont() const;
const Font * getFont() const;
private:
bool renderCharAt(unsigned short int charToRender, int posX, int posY, unsigned char *destMemory, int destSize);
@ -79,7 +79,7 @@ private:
std::map<int, Texture2D *> _atlasTextures;
std::map<unsigned short, FontLetterDefinition> _fontLetterDefinitions;
float _commonLineHeight;
Font & _font;
Font * _font;
// Dynamic GlyphCollection related stuff
int _currentPage;

View File

@ -35,7 +35,9 @@ FontAtlas * FontAtlasFactory::createAtlasFromFNT(const char* fntFilePath)
return atlas;
}
else
{
return nullptr;
}
}
NS_CC_END

View File

@ -49,6 +49,7 @@ FontDefinitionTTF* FontDefinitionTTF::create(Font *font, int textureSize)
if (ret->initDefinition(font, glyph, textureSize))
{
ret->autorelease();
return ret;
}
else

View File

@ -38,11 +38,7 @@ FontFNT * FontFNT::create(const char* fntFilePath)
FontFNT::~FontFNT()
{
//_configuration release when execute LabelBMFont::purgeCachedData();
/*if (_configuration)
{
_configuration->release();
}*/
}
Size * FontFNT::getAdvancesForTextUTF16(unsigned short *text, int &outNumLetters) const

View File

@ -88,8 +88,8 @@ FT_Library FontFreeType::getFTLibrary()
}
FontFreeType::FontFreeType(bool dynamicGlyphCollection)
: _letterPadding(5)
,_ttfData(nullptr),
: _letterPadding(5),
_ttfData(nullptr),
_dynamicGlyphCollection(dynamicGlyphCollection),
_fontRef(nullptr)
{
@ -130,8 +130,6 @@ bool FontFreeType::createFontObject(const std::string &fontName, int fontSize)
FontFreeType::~FontFreeType()
{
// release the font
// TO DO
if (_fontRef)
{
FT_Done_Face(_fontRef);
@ -160,8 +158,6 @@ FontAtlas * FontFreeType::createFontAtlas()
FontAtlas *atlas = def->createFontAtlas();
// release the font definition, we don't need it anymore
def->release();
return atlas;
}
}

View File

@ -39,7 +39,9 @@ class CC_DLL FontFreeType : public Font
public:
static FontFreeType * create(const std::string &fontName, int fontSize, GlyphCollection glyphs, const char *customGlyphs);
static void shutdownFreeType();
virtual FontAtlas * createFontAtlas() override;
virtual Size * getAdvancesForTextUTF16(unsigned short *text, int &outNumLetters) const override;
virtual GlyphDef * getGlyphDefintionsForText(const char *text, int &outNumGlyphs, bool UTF16text = false) const override;
@ -49,8 +51,7 @@ public:
bool getBBOXFotChar(unsigned short theChar, Rect &outRect) const;
inline bool isDynamicGlyphCollection() { return _dynamicGlyphCollection;}
static void shutdownFreeType();
inline bool isDynamicGlyphCollection() { return _dynamicGlyphCollection;}
protected:

View File

@ -112,12 +112,9 @@ Label::Label(FontAtlas *atlas, TextHAlignment alignment)
Label::~Label()
{
if (_currentUTF16String)
delete [] _currentUTF16String;
if (_originalUTF16String)
delete [] _originalUTF16String;
if (_advances)
delete [] _advances;
delete [] _currentUTF16String;
delete [] _originalUTF16String;
delete [] _advances;
if (_fontAtlas)
FontAtlasCache::releaseFontAtlas(_fontAtlas);
@ -254,8 +251,7 @@ void Label::alignText()
int strLen = cc_wcslen(_currentUTF16String);
if (_children && _children->count() != 0)
{
Object* child;
CCARRAY_FOREACH(_children, child)
for (auto child: *_children)
{
Node* pNode = static_cast<Node*>( child );
if (pNode)
@ -275,7 +271,7 @@ void Label::alignText()
{
if (_lettersInfo[ctr].def.validDefinition)
{
child = (Sprite*)this->getChildByTag(ctr);
child = static_cast<Sprite*>( this->getChildByTag(ctr) );
if (child)
{
uvRect.size.height = _lettersInfo[ctr].def.height;
@ -302,10 +298,8 @@ bool Label::computeAdvancesForString(unsigned short int *stringToRender)
_advances = 0;
}
Font &theFont = _fontAtlas->getFont();
int letterCount = 0;
_advances = theFont.getAdvancesForTextUTF16(stringToRender, letterCount);
_advances = _fontAtlas->getFont()->getAdvancesForTextUTF16(stringToRender, letterCount);
if(!_advances)
return false;
@ -519,7 +513,7 @@ int Label::getAdvanceForChar(unsigned short c, int hintPositionInString) const
Rect Label::getRectForChar(unsigned short c) const
{
return _fontAtlas->getFont().getRectForChar(c);
return _fontAtlas->getFont()->getRectForChar(c);
}
// string related stuff
@ -649,31 +643,27 @@ void Label::updateDisplayedOpacity(GLubyte parentOpacity)
{
_displayedOpacity = _realOpacity * parentOpacity/255.0;
Object* pObj;
CCARRAY_FOREACH(_children, pObj)
for (auto child: *_children)
{
Sprite *item = static_cast<Sprite*>( pObj );
Sprite *item = static_cast<Sprite*>( child );
item->updateDisplayedOpacity(_displayedOpacity);
}
//if (_cascadeOpacityEnabled)
V3F_C4B_T2F_Quad *quads = _textureAtlas->getQuads();
int count = _textureAtlas->getTotalQuads();
Color4B color4( _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity );
if (_isOpacityModifyRGB)
{
V3F_C4B_T2F_Quad *quads = _textureAtlas->getQuads();
int count = _textureAtlas->getTotalQuads();
Color4B color4( _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity );
if (_isOpacityModifyRGB)
{
color4.r *= _displayedOpacity/255.0f;
color4.g *= _displayedOpacity/255.0f;
color4.b *= _displayedOpacity/255.0f;
}
for (int index=0; index<count; ++index)
{
quads[index].bl.colors = color4;
quads[index].br.colors = color4;
quads[index].tl.colors = color4;
quads[index].tr.colors = color4;
_textureAtlas->updateQuad(&quads[index], index);
}
color4.r *= _displayedOpacity/255.0f;
color4.g *= _displayedOpacity/255.0f;
color4.b *= _displayedOpacity/255.0f;
}
for (int index = 0; index < count; ++index)
{
quads[index].bl.colors = color4;
quads[index].br.colors = color4;
quads[index].tl.colors = color4;
quads[index].tr.colors = color4;
_textureAtlas->updateQuad(&quads[index], index);
}
}

View File

@ -82,9 +82,9 @@ public:
virtual unsigned char getDisplayedOpacity() const override;
// CCLabelTextFormat protocol implementation
virtual std::vector<LetterInfo> *getLettersInfo(){ return &_lettersInfo; };
virtual bool recordLetterInfo(const cocos2d::Point& point,unsigned short int theChar, int spriteIndex);
virtual bool recordPlaceholderInfo(int spriteIndex);
virtual std::vector<LetterInfo> *getLettersInfo() override { return &_lettersInfo; };
virtual bool recordLetterInfo(const cocos2d::Point& point,unsigned short int theChar, int spriteIndex) override;
virtual bool recordPlaceholderInfo(int spriteIndex) override;
virtual float getLetterPosXLeft( int index ) const override;
virtual float getLetterPosXRight( int index ) const override;

View File

@ -71,8 +71,6 @@ bool LabelTextFormatter::multilineText(LabelTextFormatProtocol *theLabel)
}
skip += justSkipped;
tIndex = j + skip;
//if (!info->visible)
// continue;
if (i >= stringLength)
break;