Merge pull request #3588 from minggo/label-best-practice

fixed #2793:best practice of New Label
This commit is contained in:
minggo 2013-09-12 22:29:56 -07:00
commit fd92708d16
18 changed files with 342 additions and 356 deletions

View File

@ -39,7 +39,7 @@ Font::Font() : _usedGlyphs(GlyphCollection::ASCII), _customGlyphs(nullptr)
{
}
const char * Font::getGlyphCollection(GlyphCollection glyphs)
const char * Font::getGlyphCollection(GlyphCollection glyphs) const
{
switch (glyphs)
{
@ -85,7 +85,7 @@ void Font::setCurrentGlyphCollection(GlyphCollection glyphs, const char *customG
}
}
const char * Font::getCurrentGlyphCollection()
const char * Font::getCurrentGlyphCollection() const
{
if (_customGlyphs)
{
@ -107,9 +107,9 @@ Font* Font::createWithFNT(const char* fntFilePath)
return FontFNT::create(fntFilePath);
}
unsigned short int * Font::getUTF16Text(const char *pText, int &outNumLetters)
unsigned short int * Font::getUTF16Text(const char *text, int &outNumLetters) const
{
unsigned short* utf16String = cc_utf8_to_utf16(pText);
unsigned short* utf16String = cc_utf8_to_utf16(text);
if(!utf16String)
return 0;
@ -118,28 +118,28 @@ unsigned short int * Font::getUTF16Text(const char *pText, int &outNumLetters)
return utf16String;
}
int Font::getUTF16TextLenght(unsigned short int *pText)
int Font::getUTF16TextLenght(unsigned short int *text) const
{
return cc_wcslen(pText);
return cc_wcslen(text);
}
unsigned short int * Font::trimUTF16Text(unsigned short int *pText, int newBegin, int newEnd)
unsigned short int * Font::trimUTF16Text(unsigned short int *text, int newBegin, int newEnd) const
{
if ( newBegin<0 || newEnd<=0 )
if ( newBegin < 0 || newEnd <= 0 )
return 0;
if ( newBegin>=newEnd )
if ( newBegin >= newEnd )
return 0;
if (newEnd >= cc_wcslen(pText))
if (newEnd >= cc_wcslen(text))
return 0;
int newLenght = newEnd - newBegin + 2;
unsigned short* trimmedString = new unsigned short[newLenght];
for(int c = 0; c < (newLenght-1); ++c)
for(int c = 0; c < (newLenght - 1); ++c)
{
trimmedString[c] = pText[newBegin + c];
trimmedString[c] = text[newBegin + c];
}
// last char
@ -149,15 +149,9 @@ unsigned short int * Font::trimUTF16Text(unsigned short int *pText, int newBegi
return trimmedString;
}
Rect Font::getRectForChar(unsigned short theChar)
Rect Font::getRectForChar(unsigned short theChar) const
{
Rect temp;
temp.size.width = 0;
temp.size.height = 0;
temp.origin.x = 0;
temp.origin.y = 0;
return temp;
return Rect::ZERO;
}
NS_CC_END

View File

@ -47,25 +47,25 @@ public:
virtual FontAtlas *createFontAtlas() = 0;
virtual Size * getAdvancesForTextUTF16(unsigned short *pText, int &outNumLetters) = 0;
virtual const char * getCurrentGlyphCollection();
virtual Size * getAdvancesForTextUTF16(unsigned short *text, int &outNumLetters) const = 0;
virtual const char * getCurrentGlyphCollection() const;
virtual int getLetterPadding() { return 0; }
virtual unsigned char * getGlyphBitmap(unsigned short theChar, int &outWidth, int &outHeight) { return 0; }
virtual GlyphDef * getGlyphDefintionsForText(const char *pText, int &outNumGlyphs, bool UTF16text = false) { return 0; }
virtual int getFontMaxHeight() { return 0; }
virtual Rect getRectForChar(unsigned short theChar);
virtual int getLetterPadding() const { return 0; }
virtual unsigned char * getGlyphBitmap(unsigned short theChar, int &outWidth, int &outHeight) const { return 0; }
virtual GlyphDef * getGlyphDefintionsForText(const char *text, int &outNumGlyphs, bool UTF16text = false) const { return 0; }
virtual int getFontMaxHeight() const { return 0; }
virtual Rect getRectForChar(unsigned short theChar) const;
virtual int getUTF16TextLenght(unsigned short int *pText);
virtual unsigned short int * getUTF16Text(const char *pText, int &outNumLetters);
virtual unsigned short int * trimUTF16Text(unsigned short int *pText, int newBegin, int newEnd);
virtual int getUTF16TextLenght(unsigned short int *text) const;
virtual unsigned short int * getUTF16Text(const char *text, int &outNumLetters) const;
virtual unsigned short int * trimUTF16Text(unsigned short int *text, int newBegin, int newEnd) const;
protected:
Font();
virtual ~Font() {}
void setCurrentGlyphCollection(GlyphCollection glyphs, const char *customGlyphs = 0);
const char * getGlyphCollection(GlyphCollection glyphs);
const char * getGlyphCollection(GlyphCollection glyphs) const;
private:

View File

@ -62,7 +62,7 @@ Texture2D & FontAtlas::getTexture(int slot)
return *(_atlasTextures[slot]);
}
float FontAtlas::getCommonLineHeight()
float FontAtlas::getCommonLineHeight() const
{
return _commonLineHeight;
}
@ -72,7 +72,7 @@ void FontAtlas::setCommonLineHeight(float newHeight)
_commonLineHeight = newHeight;
}
Font & FontAtlas::getFont()
Font & FontAtlas::getFont() const
{
return _font;
}

View File

@ -58,11 +58,11 @@ public:
bool getLetterDefinitionForChar(unsigned short letteCharUTF16, FontLetterDefinition &outDefinition);
void addTexture(Texture2D &texture, int slot);
float getCommonLineHeight();
float getCommonLineHeight() const;
void setCommonLineHeight(float newHeight);
Texture2D & getTexture(int slot);
Font & getFont();
Texture2D & getTexture(int slot);
Font & getFont() const;
private:

View File

@ -31,10 +31,10 @@ FontAtlas * FontAtlasFactory::createAtlasFromTTF(const char* fntFilePath, int fo
FontAtlas * FontAtlasFactory::createAtlasFromFNT(const char* fntFilePath)
{
Font *pFont = Font::createWithFNT(fntFilePath);
Font *font = Font::createWithFNT(fntFilePath);
if(pFont)
return pFont->createFontAtlas();
if(font)
return font->createFontAtlas();
else
return nullptr;
}

View File

@ -27,7 +27,7 @@
NS_CC_BEGIN
const int FontDefinitionTTF::_DEFAUL_ATALS_TEXTURE_SIZE = 1024;
const int FontDefinitionTTF::DEFAUL_ATALS_TEXTURE_SIZE = 1024;
FontDefinitionTTF::FontDefinitionTTF():_textImages(0), _commonLineHeight(0)
{
@ -36,18 +36,18 @@ FontDefinitionTTF::FontDefinitionTTF():_textImages(0), _commonLineHeight(0)
FontDefinitionTTF* FontDefinitionTTF::create(Font *font, int textureSize)
{
if (textureSize == 0)
textureSize = _DEFAUL_ATALS_TEXTURE_SIZE;
textureSize = DEFAUL_ATALS_TEXTURE_SIZE;
FontDefinitionTTF *ret = new FontDefinitionTTF;
if(!ret)
if (!ret)
return 0;
const char *pGlyph = font->getCurrentGlyphCollection();
if (!pGlyph)
const char *glyph = font->getCurrentGlyphCollection();
if (!glyph)
return nullptr;
if ( ret->initDefinition(font, pGlyph, textureSize ) )
if (ret->initDefinition(font, glyph, textureSize))
{
return ret;
}
@ -69,34 +69,33 @@ FontDefinitionTTF::~FontDefinitionTTF()
bool FontDefinitionTTF::prepareLetterDefinitions(TextFontPagesDef *pageDefs)
{
// get all the pages
TextFontPagesDef *pPages = pageDefs;
if (!pPages)
TextFontPagesDef *pages = pageDefs;
if (!pages)
return (false);
float maxLineHeight = -1;
// loops all the pages
for (int cPages = 0; cPages<pPages->getNumPages(); ++cPages)
for (int cPages = 0; cPages < pages->getNumPages(); ++cPages)
{
// loops all the lines in this page
for (int cLines = 0; cLines<pPages->getPageAt(cPages)->getNumLines(); ++cLines)
for (int cLines = 0; cLines<pages->getPageAt(cPages)->getNumLines(); ++cLines)
{
float posXUV = 0.0;
float posYUV = pPages->getPageAt(cPages)->getLineAt(cLines)->getY();
float posYUV = pages->getPageAt(cPages)->getLineAt(cLines)->getY();
int charsCounter = 0;
for (int c = 0; c < pPages->getPageAt(cPages)->getLineAt(cLines)->getNumGlyph(); ++c)
for (int c = 0; c < pages->getPageAt(cPages)->getLineAt(cLines)->getNumGlyph(); ++c)
{
// the current glyph
GlyphDef currentGlyph = pPages->getPageAt(cPages)->getLineAt(cLines)->getGlyphAt(c);
GlyphDef currentGlyph = pages->getPageAt(cPages)->getLineAt(cLines)->getGlyphAt(c);
// letter width
float letterWidth = currentGlyph.getRect().size.width;
// letter height
float letterHeight = pPages->getPageAt(cPages)->getLineAt(cLines)->getHeight();
float letterHeight = pages->getPageAt(cPages)->getLineAt(cLines)->getHeight();
// add this letter definition
FontLetterDefinition tempDef;
@ -180,7 +179,7 @@ bool FontDefinitionTTF::initDefinition(cocos2d::Font *font, const char *letters,
return prepareLetterDefinitions(_textImages->getPages());
}
void FontDefinitionTTF::addLetterDefinition(FontLetterDefinition &defToAdd)
void FontDefinitionTTF::addLetterDefinition(const FontLetterDefinition &defToAdd)
{
if (_fontLettersDefinitionUTF16.find(defToAdd.letteCharUTF16) == _fontLettersDefinitionUTF16.end())
{
@ -191,10 +190,10 @@ void FontDefinitionTTF::addLetterDefinition(FontLetterDefinition &defToAdd)
FontAtlas * FontDefinitionTTF::createFontAtlas()
{
int numTextures = 0;
TextFontPagesDef *pPages = _textImages->getPages();
TextFontPagesDef *pages = _textImages->getPages();
if (pPages)
numTextures = pPages->getNumPages();
if (pages)
numTextures = pages->getNumPages();
else
return nullptr;

View File

@ -47,12 +47,12 @@ private:
bool initDefinition(Font *font, const char *letters, int textureSize);
bool prepareLetterDefinitions(TextFontPagesDef *pageDefs);
void addLetterDefinition(FontLetterDefinition &defToAdd);
void addLetterDefinition(const FontLetterDefinition &defToAdd);
TextImage * _textImages;
std::map<unsigned short, FontLetterDefinition> _fontLettersDefinitionUTF16;
float _commonLineHeight;
static const int _DEFAUL_ATALS_TEXTURE_SIZE;
static const int DEFAUL_ATALS_TEXTURE_SIZE;
};

View File

@ -19,7 +19,7 @@ FontFNT * FontFNT::create(const char* fntFilePath)
// add the texture
Texture2D *tempTexture = TextureCache::getInstance()->addImage(newConf->getAtlasName());
if ( !tempTexture )
if (!tempTexture)
{
delete newConf;
return nullptr;
@ -42,39 +42,39 @@ FontFNT::~FontFNT()
_configuration->release();
}
Size * FontFNT::getAdvancesForTextUTF16(unsigned short *pText, int &outNumLetters)
Size * FontFNT::getAdvancesForTextUTF16(unsigned short *text, int &outNumLetters) const
{
if (!pText)
if (!text)
return 0;
outNumLetters = cc_wcslen(pText);
outNumLetters = cc_wcslen(text);
if (!outNumLetters)
return 0;
Size *pSizes = new Size[outNumLetters];
if (!pSizes)
Size *sizes = new Size[outNumLetters];
if (!sizes)
return 0;
for (int c = 0; c<outNumLetters; ++c)
for (int c = 0; c < outNumLetters; ++c)
{
int advance = 0;
int kerning = 0;
advance = getAdvanceForChar(pText[c]);
advance = getAdvanceForChar(text[c]);
if ( c < (outNumLetters-1) )
kerning = getHorizontalKerningForChars(pText[c], pText[c+1]);
if (c < (outNumLetters-1))
kerning = getHorizontalKerningForChars(text[c], text[c+1]);
pSizes[c].width = (advance + kerning);
sizes[c].width = (advance + kerning);
}
return pSizes;
return sizes;
}
int FontFNT::getAdvanceForChar(unsigned short theChar)
int FontFNT::getAdvanceForChar(unsigned short theChar) const
{
tFontDefHashElement *element = NULL;
tFontDefHashElement *element = nullptr;
// unichar is a short, and an int is needed on HASH_FIND_INT
unsigned int key = theChar;
@ -85,28 +85,28 @@ int FontFNT::getAdvanceForChar(unsigned short theChar)
return element->fontDef.xAdvance;
}
int FontFNT::getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar)
int FontFNT::getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar) const
{
int ret = 0;
unsigned int key = (firstChar<<16) | (secondChar & 0xffff);
unsigned int key = (firstChar << 16) | (secondChar & 0xffff);
if( _configuration->_kerningDictionary )
if (_configuration->_kerningDictionary)
{
tKerningHashElement *element = NULL;
tKerningHashElement *element = nullptr;
HASH_FIND_INT(_configuration->_kerningDictionary, &key, element);
if(element)
if (element)
ret = element->amount;
}
return ret;
}
Rect FontFNT::getRectForCharInternal(unsigned short theChar)
Rect FontFNT::getRectForCharInternal(unsigned short theChar) const
{
Rect retRect;
ccBMFontDef fontDef;
tFontDefHashElement *element = NULL;
tFontDefHashElement *element = nullptr;
unsigned int key = theChar;
HASH_FIND_INT(_configuration->_fontDefDictionary, &key, element);
@ -119,7 +119,7 @@ Rect FontFNT::getRectForCharInternal(unsigned short theChar)
return retRect;
}
Rect FontFNT::getRectForChar(unsigned short theChar)
Rect FontFNT::getRectForChar(unsigned short theChar) const
{
return getRectForCharInternal(theChar);
}
@ -146,15 +146,15 @@ FontAtlas * FontFNT::createFontAtlas()
ccBMFontDef fontDef;
tFontDefHashElement *current_element, *tmp;
tFontDefHashElement *currentElement, *tmp;
// Purge uniform hash
HASH_ITER(hh, _configuration->_fontDefDictionary, current_element, tmp)
HASH_ITER(hh, _configuration->_fontDefDictionary, currentElement, tmp)
{
FontLetterDefinition tempDefinition;
fontDef = current_element->fontDef;
fontDef = currentElement->fontDef;
Rect tempRect;
tempRect = fontDef.rect;

View File

@ -37,8 +37,8 @@ public:
static FontFNT * create(const char* fntFilePath);
virtual Size* getAdvancesForTextUTF16(unsigned short *pText, int &outNumLetters) override;
virtual Rect getRectForChar(unsigned short theChar) override;
virtual Size* getAdvancesForTextUTF16(unsigned short *text, int &outNumLetters) const override;
virtual Rect getRectForChar(unsigned short theChar) const override;
virtual FontAtlas *createFontAtlas() override;
protected:
@ -48,9 +48,9 @@ protected:
private:
int getAdvanceForChar(unsigned short theChar);
int getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar);
Rect getRectForCharInternal(unsigned short theChar);
int getAdvanceForChar(unsigned short theChar) const;
int getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar) const;
Rect getRectForCharInternal(unsigned short theChar) const;
CCBMFontConfiguration * _configuration;

View File

@ -51,7 +51,7 @@ FontFreeType * FontFreeType::create(const std::string &fontName, int fontSize, G
tempFont->setCurrentGlyphCollection(glyphs, customGlyphs);
if( !tempFont->createFontObject(fontName, fontSize))
if (!tempFont->createFontObject(fontName, fontSize))
{
delete tempFont;
return nullptr;
@ -94,11 +94,10 @@ FontFreeType::FontFreeType() : _letterPadding(5)
bool FontFreeType::createFontObject(const std::string &fontName, int fontSize)
{
unsigned char* data = NULL;
int dpi = 72;
int dpi = 72;
int len = 0;
data = FileUtils::getInstance()->getFileData(fontName.c_str(), "rb", (unsigned long *)(&len) );
unsigned char* data = FileUtils::getInstance()->getFileData(fontName.c_str(), "rb", (unsigned long *)(&len));
if (!data)
return false;
@ -107,16 +106,16 @@ bool FontFreeType::createFontObject(const std::string &fontName, int fontSize)
FT_Face face;
// create the face from the data
if ( FT_New_Memory_Face(getFTLibrary(), data, len, 0, &face ) )
if (FT_New_Memory_Face(getFTLibrary(), data, len, 0, &face ))
return false;
//we want to use unicode
if( FT_Select_Charmap(face, FT_ENCODING_UNICODE) )
if (FT_Select_Charmap(face, FT_ENCODING_UNICODE))
return false;
// set the requested font size
int fontSizePoints = (int)(64.f * fontSize);
if( FT_Set_Char_Size(face, fontSizePoints, fontSizePoints, dpi, dpi) )
if (FT_Set_Char_Size(face, fontSizePoints, fontSizePoints, dpi, dpi))
return false;
// store the face globally
@ -137,10 +136,9 @@ FontFreeType::~FontFreeType()
FontAtlas * FontFreeType::createFontAtlas()
{
FontDefinitionTTF *def = 0;
def = FontDefinitionTTF::create(this);
FontDefinitionTTF *def = FontDefinitionTTF::create(this);
if(!def)
if (!def)
return nullptr;
FontAtlas *atlas = def->createFontAtlas();
@ -150,7 +148,7 @@ FontAtlas * FontFreeType::createFontAtlas()
return atlas;
}
bool FontFreeType::getBBOXFotChar(unsigned short theChar, Rect &outRect)
bool FontFreeType::getBBOXFotChar(unsigned short theChar, Rect &outRect) const
{
if (!_fontRef)
return false;
@ -174,17 +172,17 @@ bool FontFreeType::getBBOXFotChar(unsigned short theChar, Rect &outRect)
return true;
}
GlyphDef * FontFreeType::getGlyphDefintionsForText(const char *pText, int &outNumGlyphs, bool UTF16text)
GlyphDef * FontFreeType::getGlyphDefintionsForText(const char *text, int &outNumGlyphs, bool UTF16text) const
{
unsigned short* utf16String = 0;
if (UTF16text)
{
utf16String = (unsigned short*) pText;
utf16String = (unsigned short*) text;
}
else
{
utf16String = cc_utf8_to_utf16(pText);
utf16String = cc_utf8_to_utf16(text);
}
//
@ -196,17 +194,17 @@ GlyphDef * FontFreeType::getGlyphDefintionsForText(const char *pText, int &outNu
return 0;
// allocate the needed Glyphs
GlyphDef *pGlyphs = new GlyphDef[numChar];
assert( pGlyphs != NULL );
if (!pGlyphs)
GlyphDef *glyphs = new GlyphDef[numChar];
assert(glyphs != nullptr);
if (!glyphs)
return 0;
// sore result as CCRect
for (int c=0; c<numChar; ++c)
for (int c = 0; c < numChar; ++c)
{
Rect tempRect;
if( !getBBOXFotChar(utf16String[c], tempRect) )
if (!getBBOXFotChar(utf16String[c], tempRect))
{
log("Warning: Cannot find definition for glyph: %c in font:%s", utf16String[c], _fontName.c_str());
@ -215,20 +213,17 @@ GlyphDef * FontFreeType::getGlyphDefintionsForText(const char *pText, int &outNu
tempRect.size.width = 0;
tempRect.size.height = 0;
pGlyphs[c].setRect(tempRect);
pGlyphs[c].setUTF16Letter(utf16String[c]);
pGlyphs[c].setValid(false);
pGlyphs[c].setPadding(_letterPadding);
glyphs[c].setRect(tempRect);
glyphs[c].setUTF16Letter(utf16String[c]);
glyphs[c].setValid(false);
glyphs[c].setPadding(_letterPadding);
}
else
{
pGlyphs[c].setRect(tempRect);
pGlyphs[c].setUTF16Letter(utf16String[c]);
pGlyphs[c].setPadding(_letterPadding);
pGlyphs[c].setValid(true);
glyphs[c].setRect(tempRect);
glyphs[c].setUTF16Letter(utf16String[c]);
glyphs[c].setPadding(_letterPadding);
glyphs[c].setValid(true);
}
}
@ -239,40 +234,40 @@ GlyphDef * FontFreeType::getGlyphDefintionsForText(const char *pText, int &outNu
delete [] utf16String;
// done
return pGlyphs;
return glyphs;
}
Size * FontFreeType::getAdvancesForTextUTF16(unsigned short *pText, int &outNumLetters)
Size * FontFreeType::getAdvancesForTextUTF16(unsigned short *text, int &outNumLetters) const
{
if (!pText)
if (!text)
return 0;
outNumLetters = cc_wcslen(pText);
outNumLetters = cc_wcslen(text);
if (!outNumLetters)
return 0;
Size *pSizes = new Size[outNumLetters];
if (!pSizes)
Size *sizes = new Size[outNumLetters];
if (!sizes)
return 0;
for (int c = 0; c<outNumLetters; ++c)
for (int c = 0; c < outNumLetters; ++c)
{
int advance = 0;
int kerning = 0;
advance = getAdvanceForChar(pText[c]) - getBearingXForChar(pText[c]);
advance = getAdvanceForChar(text[c]) - getBearingXForChar(text[c]);
if ( c < (outNumLetters-1) )
kerning = getHorizontalKerningForChars(pText[c], pText[c+1]);
if (c < (outNumLetters-1))
kerning = getHorizontalKerningForChars(text[c], text[c+1]);
pSizes[c].width = (advance + kerning);
sizes[c].width = (advance + kerning);
}
return pSizes;
return sizes;
}
int FontFreeType::getAdvanceForChar(unsigned short theChar)
int FontFreeType::getAdvanceForChar(unsigned short theChar) const
{
if (!_fontRef)
return 0;
@ -291,26 +286,26 @@ int FontFreeType::getAdvanceForChar(unsigned short theChar)
return (_fontRef->glyph->advance.x >> 6);
}
int FontFreeType::getBearingXForChar(unsigned short theChar)
int FontFreeType::getBearingXForChar(unsigned short theChar) const
{
if (!_fontRef)
return 0;
// get the ID to the char we need
int glyph_index = FT_Get_Char_Index(_fontRef, theChar);
int glyphIndex = FT_Get_Char_Index(_fontRef, theChar);
if (!glyph_index)
if (!glyphIndex)
return 0;
// load glyph infos
if (FT_Load_Glyph(_fontRef, glyph_index, FT_LOAD_DEFAULT))
if (FT_Load_Glyph(_fontRef, glyphIndex, FT_LOAD_DEFAULT))
return 0;
return (_fontRef->glyph->metrics.horiBearingX >>6);
}
int FontFreeType::getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar)
int FontFreeType::getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar) const
{
if (!_fontRef)
return 0;
@ -321,43 +316,43 @@ int FontFreeType::getHorizontalKerningForChars(unsigned short firstChar, unsign
return 0;
// get the ID to the char we need
int glyph_index1 = FT_Get_Char_Index(_fontRef, firstChar);
int glyphIndex1 = FT_Get_Char_Index(_fontRef, firstChar);
if (!glyph_index1)
if (!glyphIndex1)
return 0;
// get the ID to the char we need
int glyph_index2 = FT_Get_Char_Index(_fontRef, secondChar);
int glyphIndex2 = FT_Get_Char_Index(_fontRef, secondChar);
if (!glyph_index2)
if (!glyphIndex2)
return 0;
FT_Vector kerning;
if (FT_Get_Kerning( _fontRef, glyph_index1, glyph_index2, FT_KERNING_DEFAULT, &kerning ))
if (FT_Get_Kerning( _fontRef, glyphIndex1, glyphIndex2, FT_KERNING_DEFAULT, &kerning))
return 0;
return ( kerning.x >> 6 );
return (kerning.x >> 6);
}
int FontFreeType::getFontMaxHeight()
int FontFreeType::getFontMaxHeight() const
{
return (_fontRef->size->metrics.height >> 6);
}
unsigned char * FontFreeType::getGlyphBitmap(unsigned short theChar, int &outWidth, int &outHeight)
unsigned char * FontFreeType::getGlyphBitmap(unsigned short theChar, int &outWidth, int &outHeight) const
{
if (!_fontRef)
return 0;
// get the ID to the char we need
int glyph_index = FT_Get_Char_Index(_fontRef, theChar);
int glyphIndex = FT_Get_Char_Index(_fontRef, theChar);
if (!glyph_index)
if (!glyphIndex)
return 0;
// load glyph infos
if (FT_Load_Glyph(_fontRef, glyph_index, FT_LOAD_DEFAULT))
if (FT_Load_Glyph(_fontRef, glyphIndex, FT_LOAD_DEFAULT))
return 0;
if (FT_Render_Glyph( _fontRef->glyph, FT_RENDER_MODE_NORMAL ))
@ -370,7 +365,7 @@ unsigned char * FontFreeType::getGlyphBitmap(unsigned short theChar, int &outW
return _fontRef->glyph->bitmap.buffer;
}
int FontFreeType::getLetterPadding()
int FontFreeType::getLetterPadding() const
{
return _letterPadding;
}

View File

@ -41,11 +41,11 @@ public:
static FontFreeType * create(const std::string &fontName, int fontSize, GlyphCollection glyphs, const char *customGlyphs);
virtual FontAtlas * createFontAtlas() override;
virtual Size * getAdvancesForTextUTF16(unsigned short *pText, int &outNumLetters) override;
virtual GlyphDef * getGlyphDefintionsForText(const char *pText, int &outNumGlyphs, bool UTF16text = false) override;
unsigned char * getGlyphBitmap(unsigned short theChar, int &outWidth, int &outHeight) override;
virtual int getFontMaxHeight() override;
virtual int getLetterPadding() override;
virtual Size * getAdvancesForTextUTF16(unsigned short *text, int &outNumLetters) const override;
virtual GlyphDef * getGlyphDefintionsForText(const char *text, int &outNumGlyphs, bool UTF16text = false) const override;
unsigned char * getGlyphBitmap(unsigned short theChar, int &outWidth, int &outHeight) const override;
virtual int getFontMaxHeight() const override;
virtual int getLetterPadding() const override;
protected:
@ -60,10 +60,10 @@ private:
void shutdownFreeType();
FT_Library getFTLibrary();
bool getBBOXFotChar(unsigned short theChar, Rect &outRect);
int getAdvanceForChar(unsigned short theChar);
int getBearingXForChar(unsigned short theChar);
int getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar);
bool getBBOXFotChar(unsigned short theChar, Rect &outRect) const;
int getAdvanceForChar(unsigned short theChar) const;
int getBearingXForChar(unsigned short theChar) const;
int getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar) const;
static FT_Library _FTlibrary;
static bool _FTInitialized;

View File

@ -343,7 +343,7 @@ void Label::resetCurrentString()
}
Sprite * Label::createNewSpriteFromLetterDefinition(FontLetterDefinition &theDefinition, Texture2D *theTexture)
Sprite * Label::createNewSpriteFromLetterDefinition(const FontLetterDefinition &theDefinition, Texture2D *theTexture)
{
Rect uvRect;
uvRect.size.height = theDefinition.height;
@ -372,7 +372,7 @@ Sprite * Label::createNewSpriteFromLetterDefinition(FontLetterDefinition &theDef
return tempSprite;
}
Sprite * Label::updateSpriteWithLetterDefinition(Sprite *spriteToUpdate, FontLetterDefinition &theDefinition, Texture2D *theTexture)
Sprite * Label::updateSpriteWithLetterDefinition(Sprite *spriteToUpdate, const FontLetterDefinition &theDefinition, Texture2D *theTexture)
{
if (!spriteToUpdate)
{
@ -454,7 +454,7 @@ Sprite * Label::getSprite()
{
if (_spriteArrayCache->count())
{
Sprite *retSprite = static_cast<Sprite *>( _spriteArrayCache->getLastObject() );
Sprite *retSprite = static_cast<Sprite *>(_spriteArrayCache->getLastObject());
_spriteArrayCache->removeLastObject();
return retSprite;
}
@ -467,7 +467,7 @@ Sprite * Label::getSprite()
///// PROTOCOL STUFF
Sprite * Label::getSpriteChild(int ID)
Sprite * Label::getSpriteChild(int ID) const
{
Object* pObj = NULL;
CCARRAY_FOREACH(_spriteArray, pObj)
@ -481,7 +481,7 @@ Sprite * Label::getSpriteChild(int ID)
return 0;
}
Array* Label::getChildrenLetters()
Array* Label::getChildrenLetters() const
{
return _spriteArray;
}
@ -516,29 +516,29 @@ Sprite * Label::getSpriteForChar(unsigned short int theChar, int spriteIndexHint
return retSprite;
}
float Label::getLetterPosXLeft( Sprite* sp )
float Label::getLetterPosXLeft( Sprite* sp ) const
{
float scaleX = _scaleX;
return sp->getPosition().x * scaleX - (sp->getContentSize().width * scaleX * sp->getAnchorPoint().x);
}
float Label::getLetterPosXRight( Sprite* sp )
float Label::getLetterPosXRight( Sprite* sp ) const
{
float scaleX = _scaleX;
return sp->getPosition().x * scaleX + (sp->getContentSize().width * scaleX * sp->getAnchorPoint().x);
}
int Label::getCommonLineHeight()
int Label::getCommonLineHeight() const
{
return _commonLineHeight;
}
int Label::getKerningForCharsPair(unsigned short first, unsigned short second)
int Label::getKerningForCharsPair(unsigned short first, unsigned short second) const
{
return 0;
}
int Label::getXOffsetForChar(unsigned short c)
int Label::getXOffsetForChar(unsigned short c) const
{
FontLetterDefinition tempDefinition;
bool validDefinition = _fontAtlas->getLetterDefinitionForChar(c, tempDefinition);
@ -548,7 +548,7 @@ int Label::getXOffsetForChar(unsigned short c)
return (tempDefinition.offsetX);
}
int Label::getYOffsetForChar(unsigned short c)
int Label::getYOffsetForChar(unsigned short c) const
{
FontLetterDefinition tempDefinition;
bool validDefinition = _fontAtlas->getLetterDefinitionForChar(c, tempDefinition);
@ -558,7 +558,7 @@ int Label::getYOffsetForChar(unsigned short c)
return (tempDefinition.offsetY);
}
int Label::getAdvanceForChar(unsigned short c, int hintPositionInString)
int Label::getAdvanceForChar(unsigned short c, int hintPositionInString) const
{
if (_advances)
{
@ -576,13 +576,13 @@ int Label::getAdvanceForChar(unsigned short c, int hintPositionInString)
}
}
Rect Label::getRectForChar(unsigned short c)
Rect Label::getRectForChar(unsigned short c) const
{
return _fontAtlas->getFont().getRectForChar(c);
}
// string related stuff
int Label::getStringNumLines()
int Label::getStringNumLines() const
{
int quantityOfLines = 1;
@ -603,17 +603,17 @@ int Label::getStringNumLines()
return quantityOfLines;
}
int Label::getStringLenght()
int Label::getStringLenght() const
{
return _currentUTF8String ? cc_wcslen(_currentUTF8String) : 0;
}
unsigned short Label::getCharAtStringPosition(int position)
unsigned short Label::getCharAtStringPosition(int position) const
{
return _currentUTF8String[position];
}
unsigned short * Label::getUTF8String()
unsigned short * Label::getUTF8String() const
{
return _currentUTF8String;
}
@ -623,23 +623,23 @@ void Label::assignNewUTF8String(unsigned short *newString)
setCurrentString(newString);
}
TextHAlignment Label::getTextAlignment()
TextHAlignment Label::getTextAlignment() const
{
return _alignment;
}
// label related stuff
float Label::getMaxLineWidth()
float Label::getMaxLineWidth() const
{
return _width;
}
bool Label::breakLineWithoutSpace()
bool Label::breakLineWithoutSpace() const
{
return _lineBreakWithoutSpaces;
}
Size Label::getLabelContentSize()
Size Label::getLabelContentSize() const
{
return getContentSize();
}

View File

@ -38,7 +38,6 @@ enum class GlyphCollection {
NEHE,
ASCII,
CUSTOM
};
//fwd
@ -52,75 +51,75 @@ class CC_DLL Label : public SpriteBatchNode, public LabelProtocol, public RGBAPr
public:
// static create
static Label* createWithTTF( const char* label, const char* fontFilePath, int fontSize, int lineSize = 0, TextHAlignment alignment = TextHAlignment::CENTER, GlyphCollection glyphs = GlyphCollection::NEHE, const char *customGlyphs = 0 );
static Label* createWithTTF(const char* label, const char* fontFilePath, int fontSize, int lineSize = 0, TextHAlignment alignment = TextHAlignment::CENTER, GlyphCollection glyphs = GlyphCollection::NEHE, const char *customGlyphs = 0);
static Label* createWithBMFont( const char* label, const char* bmfontFilePath, TextHAlignment alignment = TextHAlignment::CENTER, int lineSize = 0 );
static Label* createWithBMFont(const char* label, const char* bmfontFilePath, TextHAlignment alignment = TextHAlignment::CENTER, int lineSize = 0);
bool setText(const char *stringToRender, float lineWidth, TextHAlignment alignment = TextHAlignment::LEFT, bool lineBreakWithoutSpaces = false);
virtual void setString(const char *stringToRender);
virtual void setString(const char *stringToRender) override;
virtual void setAlignment(TextHAlignment alignment);
virtual void setWidth(float width);
virtual void setLineBreakWithoutSpace(bool breakWithoutSpace);
virtual void setScale(float scale);
virtual void setScaleX(float scaleX);
virtual void setScaleY(float scaleY);
virtual void setScale(float scale) override;
virtual void setScaleX(float scaleX) override;
virtual void setScaleY(float scaleY) override;
// RGBAProtocol
virtual bool isOpacityModifyRGB() const;
virtual void setOpacityModifyRGB(bool isOpacityModifyRGB);
virtual void setOpacity(GLubyte opacity);
virtual void updateDisplayedOpacity(GLubyte parentOpacity);
virtual bool isCascadeOpacityEnabled() const;
virtual void setCascadeOpacityEnabled(bool cascadeOpacityEnabled);
virtual void setColor(const Color3B& color);
virtual void updateDisplayedColor(const Color3B& parentColor);
virtual bool isCascadeColorEnabled() const;
virtual void setCascadeColorEnabled(bool cascadeColorEnabled);
virtual const Color3B& getColor(void) const;
virtual const Color3B& getDisplayedColor() const;
virtual unsigned char getOpacity() const;
virtual unsigned char getDisplayedOpacity() const;
virtual bool isOpacityModifyRGB() const override;
virtual void setOpacityModifyRGB(bool isOpacityModifyRGB) override;
virtual void setOpacity(GLubyte opacity) override;
virtual void updateDisplayedOpacity(GLubyte parentOpacity) override;
virtual bool isCascadeOpacityEnabled() const override;
virtual void setCascadeOpacityEnabled(bool cascadeOpacityEnabled) override;
virtual void setColor(const Color3B& color) override;
virtual void updateDisplayedColor(const Color3B& parentColor) override;
virtual bool isCascadeColorEnabled() const override;
virtual void setCascadeColorEnabled(bool cascadeColorEnabled) override;
virtual const Color3B& getColor(void) const override;
virtual const Color3B& getDisplayedColor() const override;
virtual unsigned char getOpacity() const override;
virtual unsigned char getDisplayedOpacity() const override;
// CCLabelTextFormat protocol implementation
virtual Sprite * getSpriteChild(int ID);
virtual Array * getChildrenLetters();
virtual Sprite * getSpriteForChar(unsigned short int theChar, int spriteIndexHint);
virtual float getLetterPosXLeft( Sprite* sp );
virtual float getLetterPosXRight( Sprite* sp );
virtual Sprite * getSpriteChild(int ID) const override;
virtual Array * getChildrenLetters() const override;
virtual Sprite * getSpriteForChar(unsigned short int theChar, int spriteIndexHint) override;
virtual float getLetterPosXLeft( Sprite* sp ) const override;
virtual float getLetterPosXRight( Sprite* sp ) const override;
// font related stuff
virtual int getCommonLineHeight();
virtual int getKerningForCharsPair(unsigned short first, unsigned short second);
virtual int getXOffsetForChar(unsigned short c);
virtual int getYOffsetForChar(unsigned short c);
virtual int getAdvanceForChar(unsigned short c, int hintPositionInString);
virtual Rect getRectForChar(unsigned short c) ;
virtual int getCommonLineHeight() const override;
virtual int getKerningForCharsPair(unsigned short first, unsigned short second) const override;
virtual int getXOffsetForChar(unsigned short c) const override;
virtual int getYOffsetForChar(unsigned short c) const override;
virtual int getAdvanceForChar(unsigned short c, int hintPositionInString) const override;
virtual Rect getRectForChar(unsigned short c) const override;
// string related stuff
virtual int getStringNumLines();
virtual int getStringLenght();
virtual unsigned short getCharAtStringPosition(int position);
virtual unsigned short * getUTF8String();
virtual void assignNewUTF8String(unsigned short *newString);
virtual TextHAlignment getTextAlignment();
virtual int getStringNumLines() const override;
virtual int getStringLenght() const override;
virtual unsigned short getCharAtStringPosition(int position) const override;
virtual unsigned short * getUTF8String() const override;
virtual void assignNewUTF8String(unsigned short *newString) override;
virtual TextHAlignment getTextAlignment() const override;
// label related stuff
virtual float getMaxLineWidth() ;
virtual bool breakLineWithoutSpace();
virtual Size getLabelContentSize();
virtual void setLabelContentSize(const Size &newSize);
virtual float getMaxLineWidth() const override;
virtual bool breakLineWithoutSpace() const override;
virtual Size getLabelContentSize() const override;
virtual void setLabelContentSize(const Size &newSize) override;
// carloX
const char * getString() const { return "not implemented"; }
private:
Label(FontAtlas *pAtlas, TextHAlignment alignment);
Label(FontAtlas *atlas, TextHAlignment alignment);
~Label();
static Label* createWithAtlas(FontAtlas *pAtlas, TextHAlignment alignment = TextHAlignment::LEFT, int lineSize = 0);
static Label* createWithAtlas(FontAtlas *atlas, TextHAlignment alignment = TextHAlignment::LEFT, int lineSize = 0);
bool init();
@ -134,8 +133,8 @@ private:
void resetCurrentString();
Sprite * getSprite();
Sprite * createNewSpriteFromLetterDefinition(FontLetterDefinition &theDefinition, Texture2D *theTexture);
Sprite * updateSpriteWithLetterDefinition(Sprite *spriteToUpdate, FontLetterDefinition &theDefinition, Texture2D *theTexture);
Sprite * createNewSpriteFromLetterDefinition(const FontLetterDefinition &theDefinition, Texture2D *theTexture);
Sprite * updateSpriteWithLetterDefinition(Sprite *spriteToUpdate, const FontLetterDefinition &theDefinition, Texture2D *theTexture);
Sprite * getSpriteForLetter(unsigned short int newLetter);
Sprite * updateSpriteForLetter(Sprite *spriteToUpdate, unsigned short int newLetter);

View File

@ -32,33 +32,33 @@ class CC_DLL LabelTextFormatProtocol
public:
// sprite related stuff
virtual cocos2d::Sprite *getSpriteChild(int ID) = 0;
virtual cocos2d::Array *getChildrenLetters() = 0;
virtual cocos2d::Sprite *getSpriteForChar(unsigned short int theChar, int spriteIndexHint) = 0;
virtual float getLetterPosXLeft( cocos2d::Sprite* sp ) = 0;
virtual float getLetterPosXRight( cocos2d::Sprite* sp ) = 0;
virtual cocos2d::Sprite *getSpriteChild(int ID) const = 0;
virtual cocos2d::Array *getChildrenLetters() const = 0;
virtual cocos2d::Sprite *getSpriteForChar(unsigned short int theChar, int spriteIndexHint) = 0;
virtual float getLetterPosXLeft(cocos2d::Sprite* sp) const = 0;
virtual float getLetterPosXRight(cocos2d::Sprite* sp) const = 0;
// font related stuff
virtual int getCommonLineHeight() = 0;
virtual int getKerningForCharsPair(unsigned short first, unsigned short second) = 0;
virtual int getXOffsetForChar(unsigned short c) = 0;
virtual int getYOffsetForChar(unsigned short c) = 0;
virtual int getAdvanceForChar(unsigned short c, int hintPositionInString) = 0;
virtual cocos2d::Rect getRectForChar(unsigned short c) = 0;
virtual int getCommonLineHeight() const = 0;
virtual int getKerningForCharsPair(unsigned short first, unsigned short second) const = 0;
virtual int getXOffsetForChar(unsigned short c) const = 0;
virtual int getYOffsetForChar(unsigned short c) const = 0;
virtual int getAdvanceForChar(unsigned short c, int hintPositionInString) const = 0;
virtual cocos2d::Rect getRectForChar(unsigned short c) const = 0;
// string related stuff
virtual int getStringNumLines() = 0;
virtual int getStringLenght() = 0;
virtual unsigned short getCharAtStringPosition(int position) = 0;
virtual unsigned short * getUTF8String() = 0;
virtual void assignNewUTF8String(unsigned short *newString) = 0;
virtual TextHAlignment getTextAlignment() = 0;
virtual int getStringNumLines() const = 0;
virtual int getStringLenght() const = 0;
virtual unsigned short getCharAtStringPosition(int position) const = 0;
virtual unsigned short * getUTF8String() const = 0;
virtual void assignNewUTF8String(unsigned short *newString) = 0;
virtual TextHAlignment getTextAlignment() const = 0;
// label related stuff
virtual float getMaxLineWidth() = 0;
virtual bool breakLineWithoutSpace() = 0;
virtual cocos2d::Size getLabelContentSize() = 0;
virtual void setLabelContentSize(const Size &newSize) = 0;
virtual float getMaxLineWidth() const = 0;
virtual bool breakLineWithoutSpace() const = 0;
virtual cocos2d::Size getLabelContentSize() const = 0;
virtual void setLabelContentSize(const Size &newSize) = 0;
};

View File

@ -49,7 +49,7 @@ bool LabelTextFormatter::multilineText(LabelTextFormatProtocol *theLabel)
unsigned int line = 1, i = 0;
bool start_line = false, start_word = false;
bool isStartOfLine = false, isStartOfWord = false;
float startOfLine = -1, startOfWord = -1;
int skip = 0;
@ -76,16 +76,16 @@ bool LabelTextFormatter::multilineText(LabelTextFormatProtocol *theLabel)
unsigned short character = strWhole[i];
if (!start_word)
if (!isStartOfWord)
{
startOfWord = theLabel->getLetterPosXLeft( characterSprite );
start_word = true;
isStartOfWord = true;
}
if (!start_line)
if (!isStartOfLine)
{
startOfLine = startOfWord;
start_line = true;
isStartOfLine = true;
}
// Newline.
@ -96,12 +96,12 @@ bool LabelTextFormatter::multilineText(LabelTextFormatProtocol *theLabel)
last_word.push_back('\n');
multiline_string.insert(multiline_string.end(), last_word.begin(), last_word.end());
last_word.clear();
start_word = false;
start_line = false;
isStartOfWord = false;
isStartOfLine = false;
startOfWord = -1;
startOfLine = -1;
i+=justSkipped;
line++;
i += justSkipped;
++line;
if (i >= stringLength)
break;
@ -111,12 +111,12 @@ bool LabelTextFormatter::multilineText(LabelTextFormatProtocol *theLabel)
if (!startOfWord)
{
startOfWord = theLabel->getLetterPosXLeft( characterSprite );
start_word = true;
isStartOfWord = true;
}
if (!startOfLine)
{
startOfLine = startOfWord;
start_line = true;
isStartOfLine = true;
}
}
@ -126,14 +126,14 @@ bool LabelTextFormatter::multilineText(LabelTextFormatProtocol *theLabel)
last_word.push_back(character);
multiline_string.insert(multiline_string.end(), last_word.begin(), last_word.end());
last_word.clear();
start_word = false;
isStartOfWord = false;
startOfWord = -1;
i++;
++i;
continue;
}
// Out of bounds.
if ( theLabel->getLetterPosXRight( characterSprite ) - startOfLine > theLabel->getMaxLineWidth() )
if (theLabel->getLetterPosXRight( characterSprite ) - startOfLine > theLabel->getMaxLineWidth())
{
if (!theLabel->breakLineWithoutSpace())
{
@ -148,10 +148,10 @@ bool LabelTextFormatter::multilineText(LabelTextFormatProtocol *theLabel)
if (multiline_string.size() > 0)
multiline_string.push_back('\n');
line++;
start_line = false;
++line;
isStartOfLine = false;
startOfLine = -1;
i++;
++i;
}
else
{
@ -160,11 +160,11 @@ bool LabelTextFormatter::multilineText(LabelTextFormatProtocol *theLabel)
last_word.push_back('\n');
multiline_string.insert(multiline_string.end(), last_word.begin(), last_word.end());
last_word.clear();
start_word = false;
start_line = false;
isStartOfWord = false;
isStartOfLine = false;
startOfWord = -1;
startOfLine = -1;
line++;
++line;
if (i >= stringLength)
break;
@ -172,15 +172,15 @@ bool LabelTextFormatter::multilineText(LabelTextFormatProtocol *theLabel)
if (!startOfWord)
{
startOfWord = theLabel->getLetterPosXLeft( characterSprite );
start_word = true;
isStartOfWord = true;
}
if (!startOfLine)
{
startOfLine = startOfWord;
start_line = true;
isStartOfLine = true;
}
j--;
--j;
}
continue;
@ -189,7 +189,7 @@ bool LabelTextFormatter::multilineText(LabelTextFormatProtocol *theLabel)
{
// Character is normal.
last_word.push_back(character);
i++;
++i;
continue;
}
}
@ -197,15 +197,15 @@ bool LabelTextFormatter::multilineText(LabelTextFormatProtocol *theLabel)
multiline_string.insert(multiline_string.end(), last_word.begin(), last_word.end());
int size = multiline_string.size();
unsigned short* str_new = new unsigned short[size + 1];
unsigned short* strNew = new unsigned short[size + 1];
for (int i = 0; i < size; ++i)
{
str_new[i] = multiline_string[i];
strNew[i] = multiline_string[i];
}
str_new[size] = 0;
theLabel->assignNewUTF8String(str_new);
strNew[size] = 0;
theLabel->assignNewUTF8String(strNew);
return true;
}
@ -220,28 +220,28 @@ bool LabelTextFormatter::alignText(LabelTextFormatProtocol *theLabel)
int i = 0;
int lineNumber = 0;
int str_len = cc_wcslen(theLabel->getUTF8String());
vector<unsigned short> last_line;
for (int ctr = 0; ctr <= str_len; ++ctr)
int strLen = cc_wcslen(theLabel->getUTF8String());
vector<unsigned short> lastLine;
for (int ctr = 0; ctr <= strLen; ++ctr)
{
unsigned short int currentChar = theLabel->getCharAtStringPosition(ctr);
if (currentChar == '\n' || currentChar == 0)
{
float lineWidth = 0.0f;
unsigned int line_length = last_line.size();
unsigned int lineLength = lastLine.size();
// if last line is empty we must just increase lineNumber and work with next line
if (line_length == 0)
if (lineLength == 0)
{
lineNumber++;
continue;
}
int index = i + line_length - 1 + lineNumber;
int index = i + lineLength - 1 + lineNumber;
if (index < 0) continue;
Sprite* lastChar = theLabel->getSpriteChild(index);
if ( lastChar == NULL )
if (lastChar == nullptr)
continue;
lineWidth = lastChar->getPosition().x + lastChar->getContentSize().width / 2.0f;
@ -261,7 +261,7 @@ bool LabelTextFormatter::alignText(LabelTextFormatProtocol *theLabel)
if (shift != 0)
{
for (unsigned j = 0; j < line_length; j++)
for (unsigned j = 0; j < lineLength; ++j)
{
index = i + j + lineNumber;
if (index < 0) continue;
@ -273,14 +273,14 @@ bool LabelTextFormatter::alignText(LabelTextFormatProtocol *theLabel)
}
}
i += line_length;
lineNumber++;
i += lineLength;
++lineNumber;
last_line.clear();
lastLine.clear();
continue;
}
last_line.push_back(currentChar);
lastLine.push_back(currentChar);
}
return true;
@ -311,7 +311,7 @@ bool LabelTextFormatter::createStringSprites(LabelTextFormatProtocol *theLabel)
int commonLineHeight = theLabel->getCommonLineHeight();
totalHeight = commonLineHeight * quantityOfLines;
nextFontPositionY = 0 - ( commonLineHeight - totalHeight );
nextFontPositionY = 0 - (commonLineHeight - totalHeight);
Rect rect;
@ -340,9 +340,9 @@ bool LabelTextFormatter::createStringSprites(LabelTextFormatProtocol *theLabel)
}
// get the sprite to this letter
Sprite *pLetterSprite = theLabel->getSpriteForChar(c, i);
Sprite *letterSprite = theLabel->getSpriteForChar(c, i);
if (!pLetterSprite)
if (!letterSprite)
{
log("WARNING: can't find letter definition in font file for letter: %c", c);
continue;
@ -352,11 +352,11 @@ bool LabelTextFormatter::createStringSprites(LabelTextFormatProtocol *theLabel)
int yOffset = commonLineHeight - charYOffset;
Point fontPos = Point( (float)nextFontPositionX + charXOffset + charRect.size.width * 0.5f + kerningAmount,
(float)nextFontPositionY + yOffset - charRect.size.height * 0.5f );
Point fontPos = Point((float)nextFontPositionX + charXOffset + charRect.size.width * 0.5f + kerningAmount,
(float)nextFontPositionY + yOffset - charRect.size.height * 0.5f);
// set the sprite position
pLetterSprite->setPosition(CC_POINT_PIXELS_TO_POINTS(fontPos));
letterSprite->setPosition(CC_POINT_PIXELS_TO_POINTS(fontPos));
// update kerning
nextFontPositionX += charAdvance + kerningAmount;

View File

@ -81,7 +81,7 @@ bool TextPageDef::generatePageTexture(bool releasePageData)
}
Size imageSize = Size((float)(_width), (float)(_height));
if( (imageSize.width <=0) || (imageSize.height<=0) )
if((imageSize.width <= 0) || (imageSize.height <= 0))
return false;
_pageTexture = new Texture2D();
@ -92,7 +92,7 @@ bool TextPageDef::generatePageTexture(bool releasePageData)
bool textureCreated = _pageTexture->initWithData(_pageData, dataLenght, Texture2D::PixelFormat::RGBA8888, _width, _height, imageSize);
// release the page data if requested
if ( releasePageData && textureCreated )
if (releasePageData && textureCreated)
{
delete [] _pageData;
_pageData = 0;
@ -123,7 +123,7 @@ TextFontPagesDef::TextFontPagesDef()
TextFontPagesDef::~TextFontPagesDef()
{
int numPages = _pages.size();
for( int c = 0; c<numPages; ++c )
for( int c = 0; c < numPages; ++c )
{
if (_pages[c])
delete _pages[c];
@ -143,7 +143,7 @@ TextImage::~TextImage()
_font->release();
}
bool TextImage::initWithString(const char *text, int nWidth, int nHeight, cocos2d::Font* font, bool releaseRAWData)
bool TextImage::initWithString(const char *text, int width, int height, cocos2d::Font* font, bool releaseRAWData)
{
bool textIsUTF16 = false;
@ -157,14 +157,14 @@ bool TextImage::initWithString(const char *text, int nWidth, int nHeight, cocos2
_font = font;
// generate the glyphs for the requested text (glyphs are latter's bounding boxes)
if ( !generateTextGlyphs(text) )
if (!generateTextGlyphs(text))
return false;
Size constrainSize;
unsigned short int *strUTF16 = 0;
int stringNumChars;
if ( textIsUTF16 )
if (textIsUTF16)
{
strUTF16 = (unsigned short int *)text;
stringNumChars = cc_wcslen(strUTF16);
@ -179,11 +179,11 @@ bool TextImage::initWithString(const char *text, int nWidth, int nHeight, cocos2
return false;
// create all the needed pages
if (!createPageDefinitions(strUTF16, nWidth, nHeight, _font->getFontMaxHeight()))
if (!createPageDefinitions(strUTF16, width, height, _font->getFontMaxHeight()))
return false;
// release the original string if needed
if ( !textIsUTF16 )
if (!textIsUTF16)
delete [] strUTF16;
// actually create the needed images
@ -212,7 +212,7 @@ bool TextImage::createPageDefinitions(unsigned short int *inText, int imageWidth
// create the first page (ther is going to be at least one page)
TextPageDef *currentPageDef = new TextPageDef(currentPage, imageWidth, imageHeight);
if ( !currentPageDef )
if (!currentPageDef)
return false;
// add the current page
@ -223,14 +223,14 @@ bool TextImage::createPageDefinitions(unsigned short int *inText, int imageWidth
do {
// choose texture page
if ( ( currentY + lineHeight ) > imageHeight )
if ((currentY + lineHeight) > imageHeight)
{
currentY = 0;
currentPage += 1;
// create a new page and add
currentPageDef = new TextPageDef(currentPage, imageWidth, imageHeight);
if ( !currentPageDef )
if (!currentPageDef)
return false;
_fontPages->addPage(currentPageDef);
@ -251,7 +251,7 @@ bool TextImage::createPageDefinitions(unsigned short int *inText, int imageWidth
// create the new line and add to the current page
TextLineDef *newLine = new TextLineDef(0.0, currentY, newLineSize, lineHeight);
if ( !newLine )
if (!newLine)
return false;
// add all the glyphs to this line
@ -268,7 +268,7 @@ bool TextImage::createPageDefinitions(unsigned short int *inText, int imageWidth
delta = (stringLenght - numFittingChar);
// there is still some leftover, need to work on it
if ( delta )
if (delta)
{
// create the new string
unsigned short int *tempS = _font->trimUTF16Text(strUTF16, numFittingChar, (stringLenght - 1));
@ -286,7 +286,7 @@ bool TextImage::createPageDefinitions(unsigned short int *inText, int imageWidth
// go to next line
currentY += lineHeight;
} while( delta );
} while(delta);
if (needToReleaseText)
delete [] strUTF16;
@ -305,7 +305,7 @@ int TextImage::getNumGlyphsFittingInSize(std::map<unsigned short int, GlyphDef>
// get the string to UTF8
int numChar = cc_wcslen(strUTF8);
for (int c = 0; c<numChar; ++c)
for (int c = 0; c < numChar; ++c)
{
widthWithBBX += (glyphDefs[strUTF8[c]].getRect().size.width + glyphDefs[strUTF8[c]].getPadding());
@ -332,7 +332,7 @@ bool TextImage::addGlyphsToLine(TextLineDef *line, const char *lineText, bool te
if (textIsUTF16)
{
UTF16string = (unsigned short int *) lineText;
UTF16string = (unsigned short int *)lineText;
numLetters = cc_wcslen(UTF16string);
}
else
@ -340,7 +340,7 @@ bool TextImage::addGlyphsToLine(TextLineDef *line, const char *lineText, bool te
UTF16string = _font->getUTF16Text(lineText, numLetters);
}
for (int c=0; c<numLetters; ++c)
for (int c = 0; c < numLetters; ++c)
{
_textGlyphs[UTF16string[c]].setCommonHeight(line->getHeight());
line->addGlyph(_textGlyphs[UTF16string[c]] );
@ -352,24 +352,24 @@ bool TextImage::addGlyphsToLine(TextLineDef *line, const char *lineText, bool te
return true;
}
bool TextImage::generateTextGlyphs(const char * pText)
bool TextImage::generateTextGlyphs(const char * text)
{
if (!_font)
return false;
int numGlyphs = 0;
GlyphDef *pNewGlyphs = _font->getGlyphDefintionsForText(pText, numGlyphs);
GlyphDef *newGlyphs = _font->getGlyphDefintionsForText(text, numGlyphs);
if (!pNewGlyphs)
if (!newGlyphs)
return false;
if (!_textGlyphs.empty())
_textGlyphs.clear();
for (int c=0; c < numGlyphs; ++c)
_textGlyphs[pNewGlyphs[c].getUTF8Letter()] = pNewGlyphs[c];
for (int c = 0; c < numGlyphs; ++c)
_textGlyphs[newGlyphs[c].getUTF8Letter()] = newGlyphs[c];
delete [] pNewGlyphs;
delete [] newGlyphs;
return true;
}
@ -381,13 +381,13 @@ bool TextImage::createImageDataFromPages(TextFontPagesDef *thePages, bool releas
for (int c = 0; c < numPages; ++c)
{
unsigned char *pPageData = 0;
pPageData = preparePageGlyphData(thePages->getPageAt(c));
unsigned char *pageData = 0;
pageData = preparePageGlyphData(thePages->getPageAt(c));
if (pPageData)
if (pageData)
{
// set the page data
thePages->getPageAt(c)->setPageData(pPageData);
thePages->getPageAt(c)->setPageData(pageData);
// crete page texture and relase RAW data
thePages->getPageAt(c)->preparePageTexture(releaseRAWData);
@ -396,7 +396,6 @@ bool TextImage::createImageDataFromPages(TextFontPagesDef *thePages, bool releas
{
return false;
}
}
return true;
@ -432,18 +431,18 @@ unsigned char * TextImage::renderGlyphData(TextPageDef *thePage)
int numLines = thePage->getNumLines();
for (int c = 0; c<numLines; ++c)
for (int c = 0; c < numLines; ++c)
{
TextLineDef *pCurrentLine = thePage->getLineAt(c);
TextLineDef *currentLine = thePage->getLineAt(c);
float origX = _font->getLetterPadding();
float origY = pCurrentLine->getY();
float origY = currentLine->getY();
int numGlyphToRender = pCurrentLine->getNumGlyph();
int numGlyphToRender = currentLine->getNumGlyph();
for (int cglyph = 0; cglyph < numGlyphToRender; ++cglyph)
{
GlyphDef currGlyph = pCurrentLine->getGlyphAt(cglyph);
GlyphDef currGlyph = currentLine->getGlyphAt(cglyph);
renderCharAt(currGlyph.getUTF8Letter(), origX, origY, data, pageWidth);
origX += (currGlyph.getRect().size.width + _font->getLetterPadding());
}
@ -454,9 +453,9 @@ unsigned char * TextImage::renderGlyphData(TextPageDef *thePage)
char outFilename[512];
sprintf(outFilename,"testIMG%d", counter);
++counter;
Image *pImage = new Image;
pImage->initWithRawData(data, (pageWidth * pageWidth * 4), 1024, 1024, 8, false);
pImage->saveToFile(outFilename);
Image *image = new Image;
image->initWithRawData(data, (pageWidth * pageWidth * 4), 1024, 1024, 8, false);
image->saveToFile(outFilename);
#endif
// we are done here
@ -475,7 +474,7 @@ bool TextImage::renderCharAt(unsigned short int charToRender, int posX, int posY
// get the glyph's bitmap
sourceBitmap = _font->getGlyphBitmap(charToRender, sourceWidth, sourceHeight);
if(!sourceBitmap)
if (!sourceBitmap)
return false;
int iX = posX;

View File

@ -43,12 +43,12 @@ class CC_DLL GlyphDef
public:
GlyphDef() : _validGlyph(false) { /*do nothing*/ }
GlyphDef(unsigned short int letterUTF8, Rect &rect) { _gliphRect = rect; _uTF16Letter = letterUTF8; }
GlyphDef(unsigned short int letterUTF8, const Rect &rect) { _gliphRect = rect; _uTF16Letter = letterUTF8; }
void setUTF16Letter(unsigned short int letterUTF8) { _uTF16Letter = letterUTF8; }
void setRect(Rect & theRect) { _gliphRect = theRect; }
void setRect(const Rect & theRect) { _gliphRect = theRect; }
unsigned short int getUTF8Letter() { return _uTF16Letter; }
Rect & getRect() { return _gliphRect; }
const Rect & getRect() const { return _gliphRect; }
void setPadding(float padding) { _padding = padding; }
float getPadding() { return _padding; }
void setCommonHeight(float commonHeight) { _commonHeight = commonHeight; }
@ -77,14 +77,14 @@ public:
TextLineDef(float x, float y, float width, float height);
float getX() { return _x; }
float getY() { return _y; }
float getWidth() { return _width; }
float getHeight() { return _height; }
float getX() const { return _x; }
float getY() const { return _y; }
float getWidth() const { return _width; }
float getHeight() const { return _height; }
void addGlyph(GlyphDef theGlyph) { _glyphs.push_back(theGlyph); }
int getNumGlyph() { return _glyphs.size(); }
GlyphDef & getGlyphAt(int index) { return _glyphs[index]; }
void addGlyph(GlyphDef theGlyph) { _glyphs.push_back(theGlyph); }
int getNumGlyph() const { return _glyphs.size(); }
const GlyphDef & getGlyphAt(int index) const { return _glyphs[index]; }
private:
@ -109,13 +109,13 @@ public:
~TextPageDef();
void addLine(TextLineDef *theLine) { _lines.push_back(theLine); }
int getNumLines() { return _lines.size(); }
TextLineDef * getLineAt(int index) { return _lines[index]; }
int getWidth() { return _width; }
int getHeight() { return _height; }
int getPageNumber() { return _pageNum; }
void setPageData(unsigned char *pData) { _pageData = pData; }
unsigned char * getPageData() { return _pageData; }
int getNumLines() const { return _lines.size(); }
TextLineDef * getLineAt(int index) const { return _lines[index]; }
int getWidth() const { return _width; }
int getHeight() const { return _height; }
int getPageNumber() const { return _pageNum; }
void setPageData(unsigned char *data) { _pageData = data; }
const unsigned char * getPageData() const { return _pageData; }
Texture2D *getPageTexture();
void preparePageTexture(bool releaseRAWData = true);
@ -143,9 +143,9 @@ public:
TextFontPagesDef();
~TextFontPagesDef();
void addPage(TextPageDef *newPage) { _pages.push_back(newPage); }
int getNumPages() { return _pages.size(); }
TextPageDef* getPageAt(int index) { return _pages[index]; }
void addPage(TextPageDef *newPage) { _pages.push_back(newPage); }
int getNumPages() const { return _pages.size(); }
TextPageDef* getPageAt(int index) const { return _pages[index]; }
private:
@ -163,16 +163,16 @@ public:
TextImage();
~TextImage();
bool initWithString(const char *text, int nWidth, int nHeight, Font* font, bool releaseRAWData = true);
bool initWithString(const char *text, int width, int height, Font* font, bool releaseRAWData = true);
TextFontPagesDef * getPages() { return _fontPages; }
Font * getFont() { return _font; }
TextFontPagesDef * getPages() const { return _fontPages; }
Font * getFont() const { return _font; }
private:
bool createImageDataFromPages(TextFontPagesDef *thePages, bool releaseRAWData = true);
bool addGlyphsToLine(TextLineDef *line, const char *lineText, bool textIsUTF16 = false);
bool generateTextGlyphs(const char * pText);
bool generateTextGlyphs(const char * text);
int getNumGlyphsFittingInSize(std::map<unsigned short int, GlyphDef> &glyphDefs, unsigned short int *strUTF8, Font *pFont, Size *constrainSize, int &outNewSize);
bool createPageDefinitions(unsigned short int *inText, int imageWidth, int imageHeight, int lineHeight);
unsigned char * preparePageGlyphData(TextPageDef *thePage);

View File

@ -17,7 +17,7 @@ Rect Paddle::getRect()
Paddle* Paddle::createWithTexture(Texture2D* aTexture)
{
Paddle* pPaddle = new Paddle();
pPaddle->initWithTexture( aTexture );
pPaddle->initWithTexture(aTexture);
pPaddle->autorelease();
return pPaddle;
@ -25,7 +25,7 @@ Paddle* Paddle::createWithTexture(Texture2D* aTexture)
bool Paddle::initWithTexture(Texture2D* aTexture)
{
if( Sprite::initWithTexture(aTexture) )
if( Sprite::initWithTexture(aTexture) )
{
_state = kPaddleStateUngrabbed;
}