axmol/cocos2dx/label_nodes/CCTextImage.h

192 lines
7.0 KiB
C
Raw Normal View History

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.
****************************************************************************/
#ifndef _TextImage_h_
#define _TextImage_h_
2013-07-11 02:59:05 +08:00
#include "CCFontRender.h"
#include "CCFont.h"
NS_CC_BEGIN
/** @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
*
*/
class CC_DLL GlyphDef
2013-07-11 02:59:05 +08:00
{
public:
GlyphDef() : _validGlyph(false) { /*do nothing*/ }
2013-07-20 01:33:26 +08:00
GlyphDef(unsigned short int letterUTF8, 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-07-11 02:59:05 +08:00
void setRect(Rect & theRect) { _gliphRect = theRect; }
2013-07-20 01:33:26 +08:00
unsigned short int getUTF8Letter() { return _uTF16Letter; }
2013-07-11 02:59:05 +08:00
Rect & getRect() { 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; }
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;
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
*
*/
class CC_DLL TextLineDef
2013-07-11 02:59:05 +08:00
{
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; }
void addGlyph(GlyphDef theGlyph) { _glyphs.push_back(theGlyph); }
int getNumGlyph() { return _glyphs.size(); }
GlyphDef & getGlyphAt(int index) { return _glyphs[index]; }
private:
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
*
*/
class CC_DLL TextPageDef
2013-07-11 02:59:05 +08:00
{
public:
TextPageDef(int pageNum, int width, int height);
~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; }
Texture2D *getPageTexture();
void preparePageTexture(bool releaseRAWData = true);
private:
bool generatePageTexture(bool releasePageData = false);
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
*/
class CC_DLL TextFontPagesDef
2013-07-11 02:59:05 +08:00
{
public:
2013-07-20 01:33:26 +08:00
TextFontPagesDef();
2013-07-11 02:59:05 +08:00
~TextFontPagesDef();
void addPage(TextPageDef *newPage) { _pages.push_back(newPage); }
int getNumPages() { return _pages.size(); }
2013-07-20 01:33:26 +08:00
TextPageDef* getPageAt(int index) { 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
*
*/
class CC_DLL TextImage
2013-07-11 02:59:05 +08:00
{
public:
2013-07-11 02:59:05 +08:00
TextImage();
~TextImage();
bool initWithString(const char *text, int nWidth, int nHeight, Font* font, bool releaseRAWData = true);
2013-07-11 02:59:05 +08:00
TextFontPagesDef * getPages() { return _fontPages; }
Font * getFont() { return _font; }
private:
bool createImageDataFromPages(TextFontPagesDef *thePages, bool releaseRAWData = true);
bool createFontRender();
bool addGlyphsToLine(TextLineDef *line, const char *lineText, bool textIsUTF16 = false);
bool generateTextGlyphs(const char * pText);
2013-07-20 01:33:26 +08:00
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);
2013-08-06 06:56:18 +08:00
unsigned char * preparePageGlyphData(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;
FontRender * _fontRender;
2013-07-11 02:59:05 +08:00
2013-07-17 06:18:39 +08:00
2013-07-11 02:59:05 +08:00
};
NS_CC_END
#endif