2013-07-25 01:22:46 +08:00
|
|
|
//
|
|
|
|
// CCFontAtlasFactory.cpp
|
|
|
|
// cocos2d_libs
|
|
|
|
//
|
|
|
|
// Created by Carlo Morgantini on 7/23/13.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "CCFontAtlasFactory.h"
|
2013-07-26 08:58:13 +08:00
|
|
|
#include "CCFontFNT.h"
|
2013-07-25 01:22:46 +08:00
|
|
|
|
|
|
|
// carloX this NEEDS to be changed
|
|
|
|
#include "CCLabelBMFont.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-07-26 08:58:13 +08:00
|
|
|
|
2013-08-02 08:54:08 +08:00
|
|
|
const char *FontAtlasFactory::glyphASCII = "\"!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ ";
|
|
|
|
|
|
|
|
const char *FontAtlasFactory::glyphNEHE = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ";
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
|
|
|
|
|
2013-08-03 07:11:20 +08:00
|
|
|
FontAtlas * FontAtlasFactory::createAtlasFromTTF(const char* fntFilePath, int fontSize, GlyphCollection glyphs, const char *customGlyphs)
|
2013-07-25 01:22:46 +08:00
|
|
|
{
|
2013-07-31 07:41:26 +08:00
|
|
|
FontDefinitionTTF *def = 0;
|
|
|
|
if ( (glyphs == GlyphCollection::NEHE) || (glyphs == GlyphCollection::ASCII) )
|
|
|
|
{
|
2013-08-03 07:11:20 +08:00
|
|
|
def = FontDefinitionTTF::create(fntFilePath, fontSize, getGlyphCollection(glyphs));
|
2013-07-31 07:41:26 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( glyphs == GlyphCollection::DYNAMIC )
|
|
|
|
{
|
|
|
|
log("ERROR: GlyphCollection::DYNAMIC is not supported yet!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( !customGlyphs )
|
|
|
|
{
|
|
|
|
log("ERROR: GlyphCollection::CUSTOM used but no input glyphs provided!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-08-03 07:11:20 +08:00
|
|
|
def = FontDefinitionTTF::create(fntFilePath, fontSize, customGlyphs);
|
2013-07-31 07:41:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
if(!def)
|
2013-07-25 08:21:51 +08:00
|
|
|
return nullptr;
|
2013-07-25 01:22:46 +08:00
|
|
|
|
|
|
|
// create the font atlas from the font definition
|
|
|
|
FontAtlas *tempAtlas = def->createFontAtlas();
|
|
|
|
|
|
|
|
// release the font definition, we don't need it anymore
|
|
|
|
def->release();
|
|
|
|
|
|
|
|
// return the atlas
|
|
|
|
return tempAtlas;
|
|
|
|
}
|
|
|
|
|
|
|
|
FontAtlas * FontAtlasFactory::createAtlasFromFNT(const char* fntFilePath)
|
|
|
|
{
|
|
|
|
CCBMFontConfiguration *newConf = FNTConfigLoadFile(fntFilePath);
|
2013-07-26 08:58:13 +08:00
|
|
|
|
|
|
|
if (newConf)
|
|
|
|
return createFontAtlasFromFNTConfig(newConf);
|
|
|
|
else
|
|
|
|
return 0;
|
2013-07-25 01:22:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char * FontAtlasFactory::getGlyphCollection(GlyphCollection glyphs)
|
|
|
|
{
|
|
|
|
switch (glyphs)
|
|
|
|
{
|
|
|
|
case GlyphCollection::NEHE:
|
2013-07-27 02:58:18 +08:00
|
|
|
return glyphNEHE;
|
2013-07-25 01:22:46 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GlyphCollection::ASCII:
|
2013-07-27 02:58:18 +08:00
|
|
|
return glyphASCII;
|
2013-07-25 01:22:46 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 08:58:13 +08:00
|
|
|
FontAtlas * FontAtlasFactory::createFontAtlasFromFNTConfig(CCBMFontConfiguration *theConfig)
|
|
|
|
{
|
|
|
|
if (!theConfig)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
FontFNT *tempFont = new FontFNT(theConfig);
|
|
|
|
if (!tempFont)
|
|
|
|
return 0;
|
|
|
|
|
2013-08-02 08:08:57 +08:00
|
|
|
FontAtlas *tempAtlas = new FontAtlas(*tempFont);
|
2013-07-26 08:58:13 +08:00
|
|
|
if (!tempAtlas)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// check that everything is fine with the BMFontCofniguration
|
|
|
|
if (!theConfig->_fontDefDictionary)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
int numGlyphs = theConfig->_characterSet->size();
|
|
|
|
if (!numGlyphs)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (theConfig->_commonHeight == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// commone height
|
|
|
|
tempAtlas->setCommonLineHeight(theConfig->_commonHeight);
|
|
|
|
|
|
|
|
|
|
|
|
ccBMFontDef fontDef;
|
|
|
|
tFontDefHashElement *current_element, *tmp;
|
|
|
|
|
|
|
|
// Purge uniform hash
|
|
|
|
HASH_ITER(hh, theConfig->_fontDefDictionary, current_element, tmp)
|
|
|
|
{
|
|
|
|
|
|
|
|
FontLetterDefinition tempDefinition;
|
|
|
|
|
|
|
|
fontDef = current_element->fontDef;
|
|
|
|
Rect tempRect;
|
|
|
|
|
|
|
|
tempRect = fontDef.rect;
|
|
|
|
tempRect = CC_RECT_PIXELS_TO_POINTS(tempRect);
|
|
|
|
|
|
|
|
tempDefinition.letteCharUTF16 = fontDef.charID;
|
|
|
|
|
|
|
|
tempDefinition.offsetX = fontDef.xOffset;
|
|
|
|
tempDefinition.offsetY = fontDef.yOffset;
|
|
|
|
|
|
|
|
tempDefinition.U = tempRect.origin.x;
|
|
|
|
tempDefinition.V = tempRect.origin.y;
|
|
|
|
|
|
|
|
tempDefinition.width = tempRect.size.width;
|
|
|
|
tempDefinition.height = tempRect.size.height;
|
|
|
|
|
|
|
|
//carloX: only one texture supported FOR NOW
|
|
|
|
tempDefinition.textureID = 0;
|
|
|
|
|
|
|
|
tempDefinition.anchorX = 0.5f;
|
|
|
|
tempDefinition.anchorY = 0.5f;
|
|
|
|
|
|
|
|
// add the new definition
|
|
|
|
tempAtlas->addLetterDefinition(tempDefinition);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the texture (only one texture for now)
|
|
|
|
|
|
|
|
Texture2D *tempTexture = TextureCache::getInstance()->addImage(theConfig->getAtlasName());
|
|
|
|
if (!tempTexture)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// add the texture
|
2013-08-02 08:49:51 +08:00
|
|
|
tempAtlas->addTexture(*tempTexture, 0);
|
2013-07-26 08:58:13 +08:00
|
|
|
return tempAtlas;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
NS_CC_END
|