2013-07-11 02:59:05 +08:00
|
|
|
/****************************************************************************
|
|
|
|
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-19 04:13:41 +08:00
|
|
|
#ifndef _TextImage_h_
|
|
|
|
#define _TextImage_h_
|
2013-07-11 02:59:05 +08:00
|
|
|
|
2013-08-07 05:37:19 +08:00
|
|
|
//#include "CCFont.h"
|
|
|
|
#include <vector>
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-08-07 05:37:19 +08:00
|
|
|
class Font;
|
|
|
|
|
2013-07-11 02:59:05 +08:00
|
|
|
/** @brief GlyphDef defines one single glyph (character) in a text image
|
|
|
|
*
|
|
|
|
* it defines the bounding box for the glyph in the texture page, the character the padding (spacing) between characters
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
class CC_DLL GlyphDef
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2013-07-30 04:43:23 +08:00
|
|
|
GlyphDef() : _validGlyph(false) { /*do nothing*/ }
|
2013-09-13 11:46:46 +08:00
|
|
|
GlyphDef(unsigned short int letterUTF8, const Rect &rect) { _gliphRect = rect; _uTF16Letter = letterUTF8; }
|
2013-07-11 02:59:05 +08:00
|
|
|
|
2013-07-20 01:33:26 +08:00
|
|
|
void setUTF16Letter(unsigned short int letterUTF8) { _uTF16Letter = letterUTF8; }
|
2013-09-13 11:46:46 +08:00
|
|
|
void setRect(const Rect & theRect) { _gliphRect = theRect; }
|
2013-07-20 01:33:26 +08:00
|
|
|
unsigned short int getUTF8Letter() { return _uTF16Letter; }
|
2013-09-13 11:46:46 +08:00
|
|
|
const Rect & getRect() const { return _gliphRect; }
|
2013-07-20 01:33:26 +08:00
|
|
|
void setPadding(float padding) { _padding = padding; }
|
|
|
|
float getPadding() { return _padding; }
|
|
|
|
void setCommonHeight(float commonHeight) { _commonHeight = commonHeight; }
|
|
|
|
float getCommonHeight() { return _commonHeight; }
|
2013-07-30 04:43:23 +08:00
|
|
|
void setValid(bool isValid) { _validGlyph = isValid; }
|
|
|
|
bool isValid() { return _validGlyph; }
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
Rect _gliphRect;
|
2013-07-20 01:33:26 +08:00
|
|
|
unsigned short int _uTF16Letter;
|
2013-07-11 02:59:05 +08:00
|
|
|
float _padding;
|
|
|
|
float _commonHeight;
|
2013-07-30 04:43:23 +08:00
|
|
|
bool _validGlyph;
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief TextLineDef define a line of text in a text image texture page
|
|
|
|
*
|
|
|
|
* conllects all the GlyphDef for a text line plus line size and line position in text image space
|
|
|
|
*
|
|
|
|
*/
|
2013-07-25 01:22:46 +08:00
|
|
|
class CC_DLL TextLineDef
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
TextLineDef(float x, float y, float width, float height);
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
float getX() const { return _x; }
|
|
|
|
float getY() const { return _y; }
|
|
|
|
float getWidth() const { return _width; }
|
|
|
|
float getHeight() const { return _height; }
|
2013-07-11 02:59:05 +08:00
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
void addGlyph(GlyphDef theGlyph) { _glyphs.push_back(theGlyph); }
|
2013-12-06 16:32:06 +08:00
|
|
|
int getNumGlyph() const { return static_cast<int>(_glyphs.size()); }
|
2013-09-13 11:46:46 +08:00
|
|
|
const GlyphDef & getGlyphAt(int index) const { return _glyphs[index]; }
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2013-07-19 04:13:41 +08:00
|
|
|
float _x;
|
|
|
|
float _y;
|
|
|
|
float _width;
|
|
|
|
float _height;
|
|
|
|
std::vector<GlyphDef> _glyphs;
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief TextPageDef defines one text image page (a TextImage can have/use more than one page)
|
|
|
|
*
|
|
|
|
* collects all the TextLineDef for one page, the witdh and height of the page and the graphics (texture) for the page
|
|
|
|
*
|
|
|
|
*/
|
2013-07-25 01:22:46 +08:00
|
|
|
class CC_DLL TextPageDef
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-07-11 02:59:05 +08:00
|
|
|
TextPageDef(int pageNum, int width, int height);
|
2013-09-13 13:52:42 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-11 02:59:05 +08:00
|
|
|
~TextPageDef();
|
|
|
|
|
|
|
|
void addLine(TextLineDef *theLine) { _lines.push_back(theLine); }
|
2013-12-06 16:32:06 +08:00
|
|
|
int getNumLines() const { return static_cast<int>(_lines.size()); }
|
2013-09-13 11:46:46 +08:00
|
|
|
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; }
|
2013-07-11 02:59:05 +08:00
|
|
|
Texture2D *getPageTexture();
|
|
|
|
void preparePageTexture(bool releaseRAWData = true);
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
bool generatePageTexture(bool releasePageData = false);
|
|
|
|
|
2013-07-19 04:13:41 +08:00
|
|
|
int _pageNum;
|
|
|
|
int _width;
|
|
|
|
int _height;
|
|
|
|
unsigned char * _pageData;
|
|
|
|
Texture2D * _pageTexture;
|
|
|
|
std::vector<TextLineDef *> _lines;
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief CCTextFontPages collection of pages (TextPageDef)
|
|
|
|
*
|
|
|
|
* A TextImage is composed by one or more text pages. This calss collects all of those pages
|
|
|
|
*/
|
2013-07-25 01:22:46 +08:00
|
|
|
class CC_DLL TextFontPagesDef
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-07-20 01:33:26 +08:00
|
|
|
TextFontPagesDef();
|
2013-09-13 13:52:42 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-11 02:59:05 +08:00
|
|
|
~TextFontPagesDef();
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
void addPage(TextPageDef *newPage) { _pages.push_back(newPage); }
|
2013-12-06 16:32:06 +08:00
|
|
|
int getNumPages() const { return static_cast<int>(_pages.size()); }
|
2013-09-13 11:46:46 +08:00
|
|
|
TextPageDef* getPageAt(int index) const { return _pages[index]; }
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
std::vector<TextPageDef *> _pages;
|
2013-07-19 05:11:12 +08:00
|
|
|
|
2013-07-11 02:59:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief TextImage
|
|
|
|
*
|
|
|
|
*/
|
2013-07-25 01:22:46 +08:00
|
|
|
class CC_DLL TextImage
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-07-11 02:59:05 +08:00
|
|
|
TextImage();
|
2013-09-13 13:52:42 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-11 02:59:05 +08:00
|
|
|
~TextImage();
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
bool initWithString(const char *text, int width, int height, Font* font, bool releaseRAWData = true);
|
2013-08-06 06:11:07 +08:00
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
TextFontPagesDef * getPages() const { return _fontPages; }
|
|
|
|
Font * getFont() const { return _font; }
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
bool createImageDataFromPages(TextFontPagesDef *thePages, bool releaseRAWData = true);
|
2013-07-18 08:31:28 +08:00
|
|
|
bool addGlyphsToLine(TextLineDef *line, const char *lineText, bool textIsUTF16 = false);
|
2013-09-13 11:46:46 +08:00
|
|
|
bool generateTextGlyphs(const char * text);
|
2013-12-18 17:47:20 +08:00
|
|
|
int 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
|
|
|
bool createPageDefinitions(unsigned short int *inText, int imageWidth, int imageHeight, int lineHeight);
|
2013-08-06 06:56:18 +08:00
|
|
|
unsigned char * preparePageGlyphData(TextPageDef *thePage);
|
2013-07-11 02:59:05 +08:00
|
|
|
|
2013-08-07 04:43:29 +08:00
|
|
|
// glyph rendering
|
|
|
|
unsigned char * renderGlyphData(TextPageDef *thePage);
|
|
|
|
|
2013-07-11 02:59:05 +08:00
|
|
|
std::map<unsigned short int, GlyphDef> _textGlyphs;
|
|
|
|
TextFontPagesDef * _fontPages;
|
2013-08-06 06:56:18 +08:00
|
|
|
Font * _font;
|
2013-07-11 02:59:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|