2011-03-19 10:59:01 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-03-19 10:59:01 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2011-07-04 10:59:35 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2011-03-19 10:59:01 +08:00
|
|
|
|
|
|
|
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.
|
2011-07-08 14:32:09 +08:00
|
|
|
|
2012-03-22 09:46:07 +08:00
|
|
|
Use any of these editors to generate BMFonts:
|
|
|
|
http://glyphdesigner.71squared.com/ (Commercial, Mac OS X)
|
|
|
|
http://www.n4te.com/hiero/hiero.jnlp (Free, Java)
|
|
|
|
http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java)
|
2011-07-08 14:32:09 +08:00
|
|
|
http://www.angelcode.com/products/bmfont/ (Free, Windows only)
|
|
|
|
|
2011-03-19 10:59:01 +08:00
|
|
|
****************************************************************************/
|
|
|
|
#ifndef __CCBITMAP_FONT_ATLAS_H__
|
|
|
|
#define __CCBITMAP_FONT_ATLAS_H__
|
2012-03-22 09:46:07 +08:00
|
|
|
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "sprite_nodes/CCSpriteBatchNode.h"
|
2012-11-15 17:16:51 +08:00
|
|
|
#include "support/data_support/uthash.h"
|
2012-01-18 18:02:39 +08:00
|
|
|
#include <map>
|
2012-03-22 09:46:07 +08:00
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup GUI
|
|
|
|
* @{
|
|
|
|
* @addtogroup label
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
enum {
|
2013-06-20 14:13:12 +08:00
|
|
|
kLabelAutomaticWidth = -1,
|
2012-03-22 09:46:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _FontDefHashElement;
|
|
|
|
|
|
|
|
/**
|
|
|
|
@struct ccBMFontDef
|
|
|
|
BMFont definition
|
|
|
|
*/
|
|
|
|
typedef struct _BMFontDef {
|
2012-04-19 14:35:52 +08:00
|
|
|
//! ID of the character
|
|
|
|
unsigned int charID;
|
|
|
|
//! origin and size of the font
|
2013-06-20 14:13:12 +08:00
|
|
|
Rect rect;
|
2012-04-19 14:35:52 +08:00
|
|
|
//! The X amount the image should be offset when drawing the image (in pixels)
|
|
|
|
short xOffset;
|
|
|
|
//! The Y amount the image should be offset when drawing the image (in pixels)
|
|
|
|
short yOffset;
|
|
|
|
//! The amount to move the current position after drawing the character (in pixels)
|
|
|
|
short xAdvance;
|
2012-03-22 09:46:07 +08:00
|
|
|
} ccBMFontDef;
|
|
|
|
|
|
|
|
/** @struct ccBMFontPadding
|
|
|
|
BMFont padding
|
|
|
|
@since v0.8.2
|
|
|
|
*/
|
|
|
|
typedef struct _BMFontPadding {
|
2012-04-19 14:35:52 +08:00
|
|
|
/// padding left
|
|
|
|
int left;
|
|
|
|
/// padding top
|
|
|
|
int top;
|
|
|
|
/// padding right
|
|
|
|
int right;
|
|
|
|
/// padding bottom
|
|
|
|
int bottom;
|
2012-03-22 09:46:07 +08:00
|
|
|
} ccBMFontPadding;
|
|
|
|
|
2012-11-15 17:16:51 +08:00
|
|
|
typedef struct _FontDefHashElement
|
|
|
|
{
|
|
|
|
unsigned int key; // key. Font Unicode value
|
|
|
|
ccBMFontDef fontDef; // font definition
|
|
|
|
UT_hash_handle hh;
|
2013-06-20 14:13:12 +08:00
|
|
|
} tFontDefHashElement;
|
2012-11-15 17:16:51 +08:00
|
|
|
|
|
|
|
// Equal function for targetSet.
|
|
|
|
typedef struct _KerningHashElement
|
|
|
|
{
|
|
|
|
int key; // key for the hash. 16-bit for 1st element, 16-bit for 2nd element
|
|
|
|
int amount;
|
|
|
|
UT_hash_handle hh;
|
2013-06-20 14:13:12 +08:00
|
|
|
} tKerningHashElement;
|
2012-03-22 09:46:07 +08:00
|
|
|
|
|
|
|
/** @brief CCBMFontConfiguration has parsed configuration of the the .fnt file
|
|
|
|
@since v0.8
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL CCBMFontConfiguration : public Object
|
2012-03-22 09:46:07 +08:00
|
|
|
{
|
2012-09-17 15:02:24 +08:00
|
|
|
// XXX: Creating a public interface so that the bitmapFontArray[] is accessible
|
2012-03-22 09:46:07 +08:00
|
|
|
public://@public
|
2012-04-19 14:35:52 +08:00
|
|
|
// BMFont definitions
|
2013-06-20 14:13:12 +08:00
|
|
|
tFontDefHashElement *_fontDefDictionary;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-08 15:29:47 +08:00
|
|
|
//! FNTConfig: Common Height Should be signed (issue #1343)
|
2013-06-15 14:03:30 +08:00
|
|
|
int _commonHeight;
|
2012-04-19 14:35:52 +08:00
|
|
|
//! Padding
|
2013-06-15 14:03:30 +08:00
|
|
|
ccBMFontPadding _padding;
|
2012-04-19 14:35:52 +08:00
|
|
|
//! atlas name
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string _atlasName;
|
2012-04-19 14:35:52 +08:00
|
|
|
//! values for kerning
|
2013-06-20 14:13:12 +08:00
|
|
|
tKerningHashElement *_kerningDictionary;
|
2012-11-15 17:16:51 +08:00
|
|
|
|
|
|
|
// Character Set defines the letters that actually exist in the font
|
2013-06-15 14:03:30 +08:00
|
|
|
std::set<unsigned int> *_characterSet;
|
2012-03-22 09:46:07 +08:00
|
|
|
public:
|
2012-04-19 14:35:52 +08:00
|
|
|
CCBMFontConfiguration();
|
|
|
|
virtual ~CCBMFontConfiguration();
|
|
|
|
const char * description();
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** allocates a CCBMFontConfiguration with a FNT file */
|
2012-06-14 15:13:16 +08:00
|
|
|
static CCBMFontConfiguration * create(const char *FNTfile);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** initializes a BitmapFontConfiguration with a FNT file */
|
|
|
|
bool initWithFNTfile(const char *FNTfile);
|
2012-06-08 15:29:47 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
inline const char* getAtlasName(){ return _atlasName.c_str(); }
|
|
|
|
inline void setAtlasName(const char* atlasName) { _atlasName = atlasName; }
|
2012-11-15 17:16:51 +08:00
|
|
|
|
|
|
|
std::set<unsigned int>* getCharacterSet() const;
|
2012-03-22 09:46:07 +08:00
|
|
|
private:
|
2012-11-15 17:16:51 +08:00
|
|
|
std::set<unsigned int>* parseConfigFile(const char *controlFile);
|
2012-04-19 14:35:52 +08:00
|
|
|
void parseCharacterDefinition(std::string line, ccBMFontDef *characterDefinition);
|
|
|
|
void parseInfoArguments(std::string line);
|
|
|
|
void parseCommonArguments(std::string line);
|
|
|
|
void parseImageFileName(std::string line, const char *fntFile);
|
|
|
|
void parseKerningEntry(std::string line);
|
|
|
|
void purgeKerningDictionary();
|
|
|
|
void purgeFontDefDictionary();
|
2012-03-22 09:46:07 +08:00
|
|
|
};
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
/** @brief LabelBMFont is a subclass of SpriteBatchNode.
|
2012-03-22 09:46:07 +08:00
|
|
|
|
|
|
|
Features:
|
2013-06-20 14:13:12 +08:00
|
|
|
- Treats each character like a Sprite. This means that each individual character can be:
|
2012-03-22 09:46:07 +08:00
|
|
|
- rotated
|
|
|
|
- scaled
|
|
|
|
- translated
|
|
|
|
- tinted
|
2012-09-17 15:02:24 +08:00
|
|
|
- change the opacity
|
2012-03-22 09:46:07 +08:00
|
|
|
- It can be used as part of a menu item.
|
|
|
|
- anchorPoint can be used to align the "label"
|
|
|
|
- Supports AngelCode text format
|
|
|
|
|
|
|
|
Limitations:
|
|
|
|
- All inner characters are using an anchorPoint of (0.5f, 0.5f) and it is not recommend to change it
|
|
|
|
because it might affect the rendering
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelBMFont implements the protocol LabelProtocol, like Label and LabelAtlas.
|
|
|
|
LabelBMFont has the flexibility of Label, the speed of LabelAtlas and all the features of Sprite.
|
|
|
|
If in doubt, use LabelBMFont instead of LabelAtlas / Label.
|
2012-03-22 09:46:07 +08:00
|
|
|
|
|
|
|
Supported editors:
|
|
|
|
http://glyphdesigner.71squared.com/ (Commercial, Mac OS X)
|
|
|
|
http://www.n4te.com/hiero/hiero.jnlp (Free, Java)
|
|
|
|
http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java)
|
|
|
|
http://www.angelcode.com/products/bmfont/ (Free, Windows only)
|
|
|
|
|
|
|
|
@since v0.8
|
|
|
|
*/
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL LabelBMFont : public SpriteBatchNode, public LabelProtocol, public RGBAProtocol
|
2012-03-22 09:46:07 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelBMFont();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual ~LabelBMFont();
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Purges the cached data.
|
|
|
|
Removes from memory the cached configurations and the atlas name dictionary.
|
|
|
|
@since v0.99.3
|
|
|
|
*/
|
|
|
|
static void purgeCachedData();
|
2012-12-06 18:51:33 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
/** creates a bitmap font atlas with an initial string and the FNT file */
|
2013-06-20 14:13:12 +08:00
|
|
|
static LabelBMFont * create(const char *str, const char *fntFile, float width, TextAlignment alignment, Point imageOffset);
|
2012-08-30 23:10:09 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
static LabelBMFont * create(const char *str, const char *fntFile, float width, TextAlignment alignment);
|
2012-08-30 23:10:09 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
static LabelBMFont * create(const char *str, const char *fntFile, float width);
|
2012-08-30 23:10:09 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
static LabelBMFont * create(const char *str, const char *fntFile);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-14 18:32:44 +08:00
|
|
|
/** Creates an label.
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
static LabelBMFont * create();
|
2012-06-14 18:32:44 +08:00
|
|
|
|
2012-06-12 14:33:53 +08:00
|
|
|
bool init();
|
2012-09-17 15:02:24 +08:00
|
|
|
/** init a bitmap font atlas with an initial string and the FNT file */
|
2013-06-20 14:13:12 +08:00
|
|
|
bool initWithString(const char *str, const char *fntFile, float width = kLabelAutomaticWidth, TextAlignment alignment = kTextAlignmentLeft, Point imageOffset = PointZero);
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** updates the font chars based on the string to render */
|
|
|
|
void createFontChars();
|
|
|
|
// super method
|
2013-03-20 13:55:43 +08:00
|
|
|
virtual void setString(const char *newString);
|
|
|
|
virtual void setString(const char *newString, bool needUpdateLabel);
|
|
|
|
|
2013-07-04 08:22:15 +08:00
|
|
|
virtual const char* getString(void) const;
|
2012-03-22 09:46:07 +08:00
|
|
|
virtual void setCString(const char *label);
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual void setAnchorPoint(const Point& var);
|
2012-04-19 14:35:52 +08:00
|
|
|
virtual void updateLabel();
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual void setAlignment(TextAlignment alignment);
|
2012-04-19 14:35:52 +08:00
|
|
|
virtual void setWidth(float width);
|
|
|
|
virtual void setLineBreakWithoutSpace(bool breakWithoutSpace);
|
2012-04-25 18:17:04 +08:00
|
|
|
virtual void setScale(float scale);
|
|
|
|
virtual void setScaleX(float scaleX);
|
|
|
|
virtual void setScaleY(float scaleY);
|
2012-11-15 17:16:51 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// RGBAProtocol
|
2013-07-04 08:22:15 +08:00
|
|
|
virtual bool isOpacityModifyRGB() const;
|
|
|
|
virtual void setOpacityModifyRGB(bool isOpacityModifyRGB);
|
|
|
|
virtual GLubyte getOpacity() const;
|
|
|
|
virtual GLubyte getDisplayedOpacity() const;
|
2013-02-28 10:37:47 +08:00
|
|
|
virtual void setOpacity(GLubyte opacity);
|
|
|
|
virtual void updateDisplayedOpacity(GLubyte parentOpacity);
|
2013-07-04 08:22:15 +08:00
|
|
|
virtual bool isCascadeOpacityEnabled() const;
|
2013-02-28 10:37:47 +08:00
|
|
|
virtual void setCascadeOpacityEnabled(bool cascadeOpacityEnabled);
|
2013-07-04 08:22:15 +08:00
|
|
|
virtual const ccColor3B& getColor(void) const;
|
|
|
|
virtual const ccColor3B& getDisplayedColor() const;
|
2013-02-28 10:37:47 +08:00
|
|
|
virtual void setColor(const ccColor3B& color);
|
|
|
|
virtual void updateDisplayedColor(const ccColor3B& parentColor);
|
2013-07-04 08:22:15 +08:00
|
|
|
virtual bool isCascadeColorEnabled() const;
|
2013-02-28 10:37:47 +08:00
|
|
|
virtual void setCascadeColorEnabled(bool cascadeColorEnabled);
|
2011-12-28 14:46:49 +08:00
|
|
|
|
2012-06-12 14:33:53 +08:00
|
|
|
void setFntFile(const char* fntFile);
|
|
|
|
const char* getFntFile();
|
2011-07-04 10:59:35 +08:00
|
|
|
#if CC_LABELBMFONT_DEBUG_DRAW
|
2012-04-19 14:35:52 +08:00
|
|
|
virtual void draw();
|
2011-07-04 10:59:35 +08:00
|
|
|
#endif // CC_LABELBMFONT_DEBUG_DRAW
|
2012-03-22 09:46:07 +08:00
|
|
|
private:
|
2012-04-19 14:35:52 +08:00
|
|
|
char * atlasNameFromFntFile(const char *fntFile);
|
|
|
|
int kerningAmountForFirst(unsigned short first, unsigned short second);
|
2013-06-20 14:13:12 +08:00
|
|
|
float getLetterPosXLeft( Sprite* characterSprite );
|
|
|
|
float getLetterPosXRight( Sprite* characterSprite );
|
2013-02-28 10:37:47 +08:00
|
|
|
|
|
|
|
protected:
|
2013-03-20 13:55:43 +08:00
|
|
|
virtual void setString(unsigned short *newString, bool needUpdateLabel);
|
2013-02-28 10:37:47 +08:00
|
|
|
// string to render
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned short* _string;
|
2013-02-28 10:37:47 +08:00
|
|
|
|
|
|
|
// name of fntFile
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string _fntFile;
|
2013-02-28 10:37:47 +08:00
|
|
|
|
|
|
|
// initial string without line breaks
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned short* _initialString;
|
|
|
|
std::string _initialStringUTF8;
|
2013-03-20 13:55:43 +08:00
|
|
|
|
2013-02-28 10:37:47 +08:00
|
|
|
// alignment of all lines
|
2013-06-20 14:13:12 +08:00
|
|
|
TextAlignment _alignment;
|
2013-02-28 10:37:47 +08:00
|
|
|
// max width until a line break is added
|
2013-06-15 14:03:30 +08:00
|
|
|
float _width;
|
2013-02-28 10:37:47 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CCBMFontConfiguration *_configuration;
|
2013-02-28 10:37:47 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
bool _lineBreakWithoutSpaces;
|
2013-02-28 10:37:47 +08:00
|
|
|
// offset of the texture atlas
|
2013-06-20 14:13:12 +08:00
|
|
|
Point _imageOffset;
|
2013-02-28 10:37:47 +08:00
|
|
|
|
|
|
|
// reused char
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite *_reusedChar;
|
2013-02-28 10:37:47 +08:00
|
|
|
|
|
|
|
// texture RGBA
|
2013-06-15 14:03:30 +08:00
|
|
|
GLubyte _displayedOpacity;
|
|
|
|
GLubyte _realOpacity;
|
|
|
|
ccColor3B _displayedColor;
|
|
|
|
ccColor3B _realColor;
|
|
|
|
bool _cascadeColorEnabled;
|
|
|
|
bool _cascadeOpacityEnabled;
|
2013-06-20 14:13:12 +08:00
|
|
|
/** conforms to RGBAProtocol protocol */
|
2013-06-15 14:03:30 +08:00
|
|
|
bool _isOpacityModifyRGB;
|
2010-08-12 17:39:25 +08:00
|
|
|
|
2012-03-22 09:46:07 +08:00
|
|
|
};
|
2010-08-12 17:39:25 +08:00
|
|
|
|
2012-03-22 09:46:07 +08:00
|
|
|
/** Free function that parses a FNT file a place it on the cache
|
|
|
|
*/
|
|
|
|
CC_DLL CCBMFontConfiguration * FNTConfigLoadFile( const char *file );
|
|
|
|
/** Purges the FNT config cache
|
|
|
|
*/
|
|
|
|
CC_DLL void FNTConfigRemoveCache( void );
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of GUI group
|
|
|
|
/// @}
|
|
|
|
/// @}
|
|
|
|
|
2012-03-22 09:46:07 +08:00
|
|
|
NS_CC_END
|
2011-03-19 10:59:01 +08:00
|
|
|
|
|
|
|
#endif //__CCBITMAP_FONT_ATLAS_H__
|