axmol/cocos/2d/CCTextImage.cpp

478 lines
12 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2013 Zynga Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2013-07-11 02:59:05 +08:00
#include <stdio.h>
#include <string.h>
#include <vector>
#include <string>
2013-07-12 05:41:03 +08:00
#include "cocos2d.h"
2013-07-11 02:59:05 +08:00
#include "CCTextImage.h"
#include "CCFontFreeType.h"
2013-08-07 05:37:19 +08:00
#include "CCFont.h"
2013-07-11 02:59:05 +08:00
NS_CC_BEGIN
TextLineDef::TextLineDef(float x, float y, float width, float height) :_x(x), _y(y), _width(width), _height(height)
{
}
TextPageDef::TextPageDef(int pageNum, int width, int height): _pageNum(pageNum),
_width(width),
_height(height),
_pageData(0),
_pageTexture(0)
2013-07-11 02:59:05 +08:00
{
}
TextPageDef::~TextPageDef()
{
2013-12-06 16:32:06 +08:00
size_t numLines = _lines.size();
2013-07-11 02:59:05 +08:00
for( int c = 0; c<numLines; ++c )
{
delete _lines[c];
}
_lines.clear();
if (_pageData)
{
delete [] _pageData;
}
if (_pageTexture)
{
_pageTexture->release();
2013-07-11 02:59:05 +08:00
}
}
bool TextPageDef::generatePageTexture(bool releasePageData)
{
if (!_pageData)
return false;
if (_pageTexture)
{
_pageTexture->release();
2013-07-11 02:59:05 +08:00
_pageTexture = 0;
}
Size imageSize = Size((float)(_width), (float)(_height));
2013-09-13 11:46:46 +08:00
if((imageSize.width <= 0) || (imageSize.height <= 0))
2013-07-11 02:59:05 +08:00
return false;
_pageTexture = new Texture2D();
if (!_pageTexture)
return false;
int dataLenght = (_width * _height * 1);
bool textureCreated = _pageTexture->initWithData(_pageData, dataLenght, Texture2D::PixelFormat::A8, _width, _height, imageSize);
2013-07-11 02:59:05 +08:00
// release the page data if requested
2013-09-13 11:46:46 +08:00
if (releasePageData && textureCreated)
2013-07-11 02:59:05 +08:00
{
delete [] _pageData;
_pageData = 0;
}
return textureCreated;
}
void TextPageDef::preparePageTexture(bool releaseRAWData)
{
generatePageTexture(releaseRAWData);
}
Texture2D *TextPageDef::getPageTexture()
{
if (!_pageTexture)
{
generatePageTexture();
}
return _pageTexture;
}
2013-07-20 01:33:26 +08:00
TextFontPagesDef::TextFontPagesDef()
2013-07-11 02:59:05 +08:00
{
}
TextFontPagesDef::~TextFontPagesDef()
{
2013-12-06 16:32:06 +08:00
size_t numPages = _pages.size();
for( size_t c = 0; c < numPages; ++c )
2013-07-11 02:59:05 +08:00
{
if (_pages[c])
delete _pages[c];
}
}
2013-11-13 11:22:34 +08:00
TextImage::TextImage(): _fontPages(0), _font(0)
2013-07-11 02:59:05 +08:00
{
}
TextImage::~TextImage()
{
if (_fontPages)
delete _fontPages;
if (_font)
_font->release();
2013-07-11 02:59:05 +08:00
}
2013-09-13 11:46:46 +08:00
bool TextImage::initWithString(const char *text, int width, int height, cocos2d::Font* font, bool releaseRAWData)
{
bool textIsUTF16 = false;
if (_font)
{
_font->release();
_font = 0;
}
// carloX
_font = font;
// generate the glyphs for the requested text (glyphs are latter's bounding boxes)
2013-09-13 11:46:46 +08:00
if (!generateTextGlyphs(text))
return false;
Size constrainSize;
unsigned short int *strUTF16 = 0;
int stringNumChars;
2013-09-13 11:46:46 +08:00
if (textIsUTF16)
{
strUTF16 = (unsigned short int *)text;
stringNumChars = cc_wcslen(strUTF16);
}
else
{
// string needs to go to unicode
strUTF16 = _font->getUTF16Text(text, stringNumChars);
}
if (!strUTF16 || !stringNumChars)
return false;
// create all the needed pages
2013-09-13 11:46:46 +08:00
if (!createPageDefinitions(strUTF16, width, height, _font->getFontMaxHeight()))
return false;
// release the original string if needed
2013-09-13 11:46:46 +08:00
if (!textIsUTF16)
delete [] strUTF16;
// actually create the needed images
return createImageDataFromPages(_fontPages, releaseRAWData);
return true;
}
2013-07-20 01:33:26 +08:00
bool TextImage::createPageDefinitions(unsigned short int *inText, int imageWidth, int imageHeight, int lineHeight)
{
bool needToReleaseText = false;
int delta = 0;
int currentPage = 0;
float currentY = 0.0;
2013-07-20 01:33:26 +08:00
//
unsigned short int *strUTF16 = inText;
if (_fontPages)
delete _fontPages;
// create pages for the font
2013-07-20 01:33:26 +08:00
_fontPages = new TextFontPagesDef();
if (!_fontPages)
return false;
// create the first page (ther is going to be at least one page)
2013-07-20 01:33:26 +08:00
TextPageDef *currentPageDef = new TextPageDef(currentPage, imageWidth, imageHeight);
2013-09-13 11:46:46 +08:00
if (!currentPageDef)
return false;
// add the current page
_fontPages->addPage(currentPageDef);
2013-07-20 01:33:26 +08:00
// work out creating pages
do {
// choose texture page
2013-09-13 11:46:46 +08:00
if ((currentY + lineHeight) > imageHeight)
{
currentY = 0;
currentPage += 1;
// create a new page and add
2013-07-20 01:33:26 +08:00
currentPageDef = new TextPageDef(currentPage, imageWidth, imageHeight);
2013-09-13 11:46:46 +08:00
if (!currentPageDef)
return false;
_fontPages->addPage(currentPageDef);
}
// get the new fitting string
Size tempSize;
2013-07-20 01:33:26 +08:00
tempSize.width = imageWidth;
tempSize.height = imageHeight;
// figure out how many glyphs fit in this line
int newLineSize = 0;
2013-07-20 01:33:26 +08:00
int numFittingChar = getNumGlyphsFittingInSize(_textGlyphs, strUTF16, _font, &tempSize, newLineSize);
// crete the temporary new string
unsigned short int *pTempString = 0;
pTempString = _font->trimUTF16Text(strUTF16, 0, (numFittingChar - 1));
// create the new line and add to the current page
TextLineDef *newLine = new TextLineDef(0.0, currentY, newLineSize, lineHeight);
2013-09-13 11:46:46 +08:00
if (!newLine)
return false;
// add all the glyphs to this line
addGlyphsToLine(newLine, (const char *)pTempString, true);
// add the line the to current page
currentPageDef->addLine(newLine);
// can now release the string
delete [] pTempString;
// create the new string
int stringLenght = _font->getUTF16TextLenght(strUTF16);
delta = (stringLenght - numFittingChar);
// there is still some leftover, need to work on it
2013-09-13 11:46:46 +08:00
if (delta)
{
// create the new string
unsigned short int *tempS = _font->trimUTF16Text(strUTF16, numFittingChar, (stringLenght - 1));
2013-07-20 01:33:26 +08:00
if (needToReleaseText)
delete [] strUTF16;
2013-07-20 01:33:26 +08:00
// a copy of the string has been created, so next time I'll need to release it
needToReleaseText = true;
// assign pointer
strUTF16 = tempS;
}
// go to next line
currentY += lineHeight;
2013-09-13 11:46:46 +08:00
} while(delta);
2013-07-20 01:33:26 +08:00
if (needToReleaseText)
delete [] strUTF16;
2013-07-20 01:33:26 +08:00
return true;
}
int TextImage::getNumGlyphsFittingInSize(std::map<unsigned short int, GlyphDef> &glyphDefs, unsigned short int *strUTF8, Font *font, Size *constrainSize, int &outNewSize)
2013-07-20 01:33:26 +08:00
{
if (!strUTF8)
return 0;
2013-07-20 01:33:26 +08:00
float widthWithBBX = 0.0f;
float lastWidth = 0.0f;
// get the string to UTF8
int numChar = cc_wcslen(strUTF8);
2013-09-13 11:46:46 +08:00
for (int c = 0; c < numChar; ++c)
2013-07-20 01:33:26 +08:00
{
widthWithBBX += (glyphDefs[strUTF8[c]].getRect().size.width + glyphDefs[strUTF8[c]].getPadding());
if (widthWithBBX >= constrainSize->width)
{
outNewSize = lastWidth;
return c;
}
lastWidth = widthWithBBX;
}
outNewSize = constrainSize->width;
return numChar;
2013-07-11 02:59:05 +08:00
}
bool TextImage::addGlyphsToLine(TextLineDef *line, const char *lineText, bool textIsUTF16)
2013-07-11 02:59:05 +08:00
{
if (!_font)
return false;
int numLetters = 0;
unsigned short int *UTF16string = 0;
if (textIsUTF16)
{
2013-09-13 11:46:46 +08:00
UTF16string = (unsigned short int *)lineText;
numLetters = cc_wcslen(UTF16string);
}
else
{
UTF16string = _font->getUTF16Text(lineText, numLetters);
}
2013-09-13 11:46:46 +08:00
for (int c = 0; c < numLetters; ++c)
{
_textGlyphs[UTF16string[c]].setCommonHeight(line->getHeight());
line->addGlyph(_textGlyphs[UTF16string[c]] );
}
if(!textIsUTF16)
delete [] UTF16string;
return true;
}
2013-09-13 11:46:46 +08:00
bool TextImage::generateTextGlyphs(const char * text)
{
2013-07-20 01:33:26 +08:00
if (!_font)
return false;
int numGlyphs = 0;
2013-09-13 11:46:46 +08:00
GlyphDef *newGlyphs = _font->getGlyphDefintionsForText(text, numGlyphs);
2013-09-13 11:46:46 +08:00
if (!newGlyphs)
return false;
2013-07-20 01:33:26 +08:00
if (!_textGlyphs.empty())
_textGlyphs.clear();
2013-09-13 11:46:46 +08:00
for (int c = 0; c < numGlyphs; ++c)
_textGlyphs[newGlyphs[c].getUTF8Letter()] = newGlyphs[c];
2013-09-13 11:46:46 +08:00
delete [] newGlyphs;
return true;
}
2013-07-11 02:59:05 +08:00
bool TextImage::createImageDataFromPages(TextFontPagesDef *thePages, bool releaseRAWData)
{
int numPages = thePages->getNumPages();
if (!numPages)
return false;
2013-07-11 02:59:05 +08:00
for (int c = 0; c < numPages; ++c)
{
2013-09-13 11:46:46 +08:00
unsigned char *pageData = 0;
pageData = preparePageGlyphData(thePages->getPageAt(c));
2013-07-11 02:59:05 +08:00
2013-09-13 11:46:46 +08:00
if (pageData)
2013-07-11 02:59:05 +08:00
{
// set the page data
2013-09-13 11:46:46 +08:00
thePages->getPageAt(c)->setPageData(pageData);
2013-07-11 02:59:05 +08:00
// crete page texture and relase RAW data
thePages->getPageAt(c)->preparePageTexture(releaseRAWData);
}
else
{
return false;
}
}
return true;
}
2013-07-19 05:11:12 +08:00
unsigned char * TextImage::preparePageGlyphData(TextPageDef *thePage)
2013-07-11 02:59:05 +08:00
{
2013-08-07 04:43:29 +08:00
return renderGlyphData(thePage);
}
unsigned char * TextImage::renderGlyphData(TextPageDef *thePage)
{
if (!thePage)
return 0;
if (!_font)
return 0;
if (thePage->getNumLines() == 0)
return nullptr;
2013-08-07 04:43:29 +08:00
int pageWidth = thePage->getWidth();
int pageHeight = thePage->getHeight();
// prepare memory and clean to 0
int sizeInBytes = (pageWidth * pageHeight * 1);
2013-08-07 04:43:29 +08:00
unsigned char* data = new unsigned char[sizeInBytes];
if (!data)
return 0;
2013-07-11 02:59:05 +08:00
2013-08-07 04:43:29 +08:00
memset(data, 0, sizeInBytes);
int numLines = thePage->getNumLines();
2013-09-13 11:46:46 +08:00
for (int c = 0; c < numLines; ++c)
2013-07-11 02:59:05 +08:00
{
2013-09-13 11:46:46 +08:00
TextLineDef *currentLine = thePage->getLineAt(c);
2013-08-07 04:43:29 +08:00
float origX = _font->getLetterPadding();
2013-09-13 11:46:46 +08:00
float origY = currentLine->getY();
2013-08-07 04:43:29 +08:00
2013-09-13 11:46:46 +08:00
int numGlyphToRender = currentLine->getNumGlyph();
2013-08-07 04:43:29 +08:00
for (int cglyph = 0; cglyph < numGlyphToRender; ++cglyph)
{
2013-09-13 11:46:46 +08:00
GlyphDef currGlyph = currentLine->getGlyphAt(cglyph);
_font->renderCharAt(currGlyph.getUTF8Letter(), origX, origY, data, pageWidth);
2013-08-07 04:43:29 +08:00
origX += (currGlyph.getRect().size.width + _font->getLetterPadding());
}
2013-07-11 02:59:05 +08:00
}
2013-08-07 04:43:29 +08:00
#ifdef _DEBUG_FONTS_
static int counter = 0;
char outFilename[512];
sprintf(outFilename,"testIMG%d", counter);
++counter;
2013-09-13 11:46:46 +08:00
Image *image = new Image;
image->initWithRawData(data, (pageWidth * pageWidth * 4), 1024, 1024, 8, false);
image->saveToFile(outFilename);
2013-08-07 04:43:29 +08:00
#endif
// we are done here
return data;
}
2013-07-11 02:59:05 +08:00
NS_CC_END