mirror of https://github.com/axmolengine/axmol.git
close #3765:Simplify label.Remove FontAtlasFactory,FontDefinition and TextImage.
This commit is contained in:
parent
0a6316a2f3
commit
a0b86abcf4
|
@ -50,8 +50,6 @@ CCFont.cpp \
|
|||
CCFontCharMap.cpp \
|
||||
CCFontAtlas.cpp \
|
||||
CCFontAtlasCache.cpp \
|
||||
CCFontAtlasFactory.cpp \
|
||||
CCFontDefinition.cpp \
|
||||
CCFontFNT.cpp \
|
||||
CCFontFreeType.cpp \
|
||||
ccFPSImages.c \
|
||||
|
@ -91,7 +89,6 @@ CCSpriteBatchNode.cpp \
|
|||
CCSpriteFrame.cpp \
|
||||
CCSpriteFrameCache.cpp \
|
||||
CCTextFieldTTF.cpp \
|
||||
CCTextImage.cpp \
|
||||
CCTexture2D.cpp \
|
||||
CCTextureAtlas.cpp \
|
||||
CCTextureCache.cpp \
|
||||
|
|
|
@ -25,10 +25,6 @@
|
|||
|
||||
#include "CCFont.h"
|
||||
#include "ccUTF8.h"
|
||||
|
||||
#include "CCFontFNT.h"
|
||||
#include "CCFontFreeType.h"
|
||||
#include "CCFontCharMap.h"
|
||||
#include "edtaa3func.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
@ -107,31 +103,6 @@ const char * Font::getCurrentGlyphCollection() const
|
|||
}
|
||||
}
|
||||
|
||||
Font* Font::createWithTTF(const std::string& fntName, int fontSize, GlyphCollection glyphs, const char *customGlyphs)
|
||||
{
|
||||
return FontFreeType::create(fntName, fontSize, glyphs, customGlyphs);
|
||||
}
|
||||
|
||||
Font* Font::createWithFNT(const std::string& fntFilePath)
|
||||
{
|
||||
return FontFNT::create(fntFilePath);
|
||||
}
|
||||
|
||||
Font* Font::createWithCharMap(const std::string& plistFile)
|
||||
{
|
||||
return FontCharMap::create(plistFile);
|
||||
}
|
||||
|
||||
Font* Font::createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
|
||||
{
|
||||
return FontCharMap::create(texture,itemWidth,itemHeight,startCharMap);
|
||||
}
|
||||
|
||||
Font* Font::createWithCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
|
||||
{
|
||||
return FontCharMap::create(charMapFile,itemWidth,itemHeight,startCharMap);
|
||||
}
|
||||
|
||||
unsigned char * Font::makeDistanceMap( unsigned char *img, unsigned int width, unsigned int height)
|
||||
{
|
||||
unsigned int pixelAmount = (width + 2 * DistanceMapSpread) * (height + 2 * DistanceMapSpread);
|
||||
|
|
|
@ -33,7 +33,6 @@
|
|||
NS_CC_BEGIN
|
||||
|
||||
// fwd
|
||||
class GlyphDef;
|
||||
class FontAtlas;
|
||||
|
||||
|
||||
|
@ -41,13 +40,6 @@ class CC_DLL Font : public Object
|
|||
{
|
||||
public:
|
||||
static const int DistanceMapSpread;
|
||||
// create the font
|
||||
static Font* createWithTTF(const std::string& fntName, int fontSize, GlyphCollection glyphs, const char *customGlyphs);
|
||||
static Font* createWithFNT(const std::string& fntFilePath);
|
||||
|
||||
static Font * createWithCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
|
||||
static Font * createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
|
||||
static Font * createWithCharMap(const std::string& plistFile);
|
||||
|
||||
static unsigned char * makeDistanceMap(unsigned char *img, unsigned int width, unsigned int height);
|
||||
void setDistanceFieldEnabled(bool distanceFieldEnabled);
|
||||
|
@ -61,7 +53,7 @@ public:
|
|||
|
||||
virtual int getLetterPadding() const { return 0; }
|
||||
virtual unsigned char * getGlyphBitmap(unsigned short theChar, int &outWidth, int &outHeight) const { return 0; }
|
||||
virtual GlyphDef* getGlyphDefintionsForText(const char *text, int &outNumGlyphs, bool UTF16text = false) const { return 0; }
|
||||
|
||||
virtual int getFontMaxHeight() const { return 0; }
|
||||
virtual Rect getRectForChar(unsigned short theChar) const;
|
||||
|
||||
|
|
|
@ -24,7 +24,6 @@
|
|||
****************************************************************************/
|
||||
|
||||
#include "CCFontAtlas.h"
|
||||
#include "CCFont.h"
|
||||
#include "CCFontFreeType.h"
|
||||
#include "ccUTF8.h"
|
||||
#include "CCDirector.h"
|
||||
|
@ -42,7 +41,7 @@ _currentPageData(nullptr)
|
|||
_makeDistanceMap = _font->isDistanceFieldEnabled();
|
||||
|
||||
FontFreeType* fontTTf = dynamic_cast<FontFreeType*>(_font);
|
||||
if (fontTTf && fontTTf->isDynamicGlyphCollection())
|
||||
if (fontTTf)
|
||||
{
|
||||
_currentPageLineHeight = _font->getFontMaxHeight();
|
||||
_commonLineHeight = _currentPageLineHeight * 0.8f;
|
||||
|
@ -56,12 +55,8 @@ _currentPageData(nullptr)
|
|||
{
|
||||
_commonLineHeight += 2 * Font::DistanceMapSpread;
|
||||
_letterPadding += 2 * Font::DistanceMapSpread;
|
||||
_currentPageDataSize = (PAGE_WIDTH * PAGE_HEIGHT * 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentPageDataSize = (PAGE_WIDTH * PAGE_HEIGHT * 1);
|
||||
}
|
||||
|
||||
_currentPageData = new unsigned char[_currentPageDataSize];
|
||||
memset(_currentPageData, 0, _currentPageDataSize);
|
||||
|
@ -165,7 +160,8 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String)
|
|||
Size _pageContentSize = Size(PAGE_WIDTH,PAGE_HEIGHT);
|
||||
float scaleFactor = CC_CONTENT_SCALE_FACTOR();
|
||||
float glyphWidth;
|
||||
Texture2D::PixelFormat pixelFormat = _makeDistanceMap ? Texture2D::PixelFormat::A8 : Texture2D::PixelFormat::A8;
|
||||
Texture2D::PixelFormat pixelFormat = Texture2D::PixelFormat::A8;
|
||||
|
||||
|
||||
for(auto it = fontDefs.begin(); it != fontDefs.end(); it++)
|
||||
{
|
||||
|
|
|
@ -26,8 +26,10 @@
|
|||
#include <sstream>
|
||||
|
||||
#include "CCFontAtlasCache.h"
|
||||
#include "CCFontAtlasFactory.h"
|
||||
|
||||
#include "CCFontFNT.h"
|
||||
#include "CCFontFreeType.h"
|
||||
#include "CCFontCharMap.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
|
@ -40,11 +42,20 @@ FontAtlas * FontAtlasCache::getFontAtlasTTF(const std::string& fontFileName, int
|
|||
|
||||
if ( !tempAtlas )
|
||||
{
|
||||
tempAtlas = FontAtlasFactory::createAtlasFromTTF(fontFileName, size, glyphs, customGlyphs, useDistanceField);
|
||||
Font *font = FontFreeType::create(fontFileName, size, glyphs, customGlyphs);
|
||||
if (font)
|
||||
{
|
||||
font->setDistanceFieldEnabled(useDistanceField);
|
||||
tempAtlas = font->createFontAtlas();
|
||||
if (tempAtlas)
|
||||
_atlasMap[atlasName] = tempAtlas;
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
tempAtlas->retain();
|
||||
}
|
||||
|
@ -59,11 +70,20 @@ FontAtlas * FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName)
|
|||
|
||||
if ( !tempAtlas )
|
||||
{
|
||||
tempAtlas = FontAtlasFactory::createAtlasFromFNT(fontFileName);
|
||||
Font *font = FontFNT::create(fontFileName);
|
||||
|
||||
if(font)
|
||||
{
|
||||
tempAtlas = font->createFontAtlas();
|
||||
if (tempAtlas)
|
||||
_atlasMap[atlasName] = tempAtlas;
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
tempAtlas->retain();
|
||||
}
|
||||
|
@ -78,11 +98,20 @@ FontAtlas * FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile)
|
|||
|
||||
if ( !tempAtlas )
|
||||
{
|
||||
tempAtlas = FontAtlasFactory::createAtlasFromCharMap(plistFile);
|
||||
Font *font = FontCharMap::create(plistFile);
|
||||
|
||||
if(font)
|
||||
{
|
||||
tempAtlas = font->createFontAtlas();
|
||||
if (tempAtlas)
|
||||
_atlasMap[atlasName] = tempAtlas;
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
tempAtlas->retain();
|
||||
}
|
||||
|
@ -99,11 +128,20 @@ FontAtlas * FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidt
|
|||
|
||||
if ( !tempAtlas )
|
||||
{
|
||||
tempAtlas = FontAtlasFactory::createAtlasFromCharMap(texture,itemWidth,itemHeight,startCharMap);
|
||||
Font *font = FontCharMap::create(texture,itemWidth,itemHeight,startCharMap);
|
||||
|
||||
if(font)
|
||||
{
|
||||
tempAtlas = font->createFontAtlas();
|
||||
if (tempAtlas)
|
||||
_atlasMap[atlasName] = tempAtlas;
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
tempAtlas->retain();
|
||||
}
|
||||
|
@ -118,11 +156,20 @@ FontAtlas * FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile,
|
|||
|
||||
if ( !tempAtlas )
|
||||
{
|
||||
tempAtlas = FontAtlasFactory::createAtlasFromCharMap(charMapFile,itemWidth,itemHeight,startCharMap);
|
||||
Font *font = FontCharMap::create(charMapFile,itemWidth,itemHeight,startCharMap);
|
||||
|
||||
if(font)
|
||||
{
|
||||
tempAtlas = font->createFontAtlas();
|
||||
if (tempAtlas)
|
||||
_atlasMap[atlasName] = tempAtlas;
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
tempAtlas->retain();
|
||||
}
|
||||
|
|
|
@ -1,105 +0,0 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2013 Zynga Inc.
|
||||
Copyright (c) 2013-2014 Chukong Technologies 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.
|
||||
****************************************************************************/
|
||||
|
||||
#include "CCFontAtlasFactory.h"
|
||||
#include "CCFontFNT.h"
|
||||
|
||||
// carloX this NEEDS to be changed
|
||||
#include "CCLabelBMFont.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
FontAtlas * FontAtlasFactory::createAtlasFromTTF(const std::string& fntFilePath, int fontSize, GlyphCollection glyphs, const char *customGlyphs, bool useDistanceField)
|
||||
{
|
||||
|
||||
Font *font = Font::createWithTTF(fntFilePath, fontSize, glyphs, customGlyphs);
|
||||
if (font)
|
||||
{
|
||||
font->setDistanceFieldEnabled(useDistanceField);
|
||||
return font->createFontAtlas();
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
FontAtlas * FontAtlasFactory::createAtlasFromFNT(const std::string& fntFilePath)
|
||||
{
|
||||
Font *font = Font::createWithFNT(fntFilePath);
|
||||
|
||||
if(font)
|
||||
{
|
||||
return font->createFontAtlas();
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
FontAtlas * FontAtlasFactory::createAtlasFromCharMap(const std::string& plistFile)
|
||||
{
|
||||
Font *font = Font::createWithCharMap(plistFile);
|
||||
|
||||
if(font)
|
||||
{
|
||||
return font->createFontAtlas();
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
FontAtlas * FontAtlasFactory::createAtlasFromCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
|
||||
{
|
||||
Font *font = Font::createWithCharMap(texture,itemWidth,itemHeight,startCharMap);
|
||||
|
||||
if(font)
|
||||
{
|
||||
return font->createFontAtlas();
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
FontAtlas * FontAtlasFactory::createAtlasFromCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
|
||||
{
|
||||
Font *font = Font::createWithCharMap(charMapFile,itemWidth,itemHeight,startCharMap);
|
||||
|
||||
if(font)
|
||||
{
|
||||
return font->createFontAtlas();
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
NS_CC_END
|
|
@ -1,52 +0,0 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2013 Zynga Inc.
|
||||
Copyright (c) 2013-2014 Chukong Technologies 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 _CCFontAtlasFactory_h_
|
||||
#define _CCFontAtlasFactory_h_
|
||||
|
||||
#include "CCFontAtlas.h"
|
||||
#include "CCLabel.h"
|
||||
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
class CC_DLL FontAtlasFactory
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
static FontAtlas * createAtlasFromTTF(const std::string& fntFilePath, int fontSize, GlyphCollection glyphs, const char *customGlyphs = 0, bool useDistanceField = false);
|
||||
static FontAtlas * createAtlasFromFNT(const std::string& fntFilePath);
|
||||
|
||||
static FontAtlas * createAtlasFromCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
|
||||
static FontAtlas * createAtlasFromCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
|
||||
static FontAtlas * createAtlasFromCharMap(const std::string& plistFile);
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
||||
#endif /* defined(_CCFontAtlasFactory_h_) */
|
|
@ -1,235 +0,0 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2013 Zynga Inc.
|
||||
Copyright (c) 2013-2014 Chukong Technologies 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.
|
||||
****************************************************************************/
|
||||
|
||||
|
||||
#include "CCFontDefinition.h"
|
||||
#include "CCDirector.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
const int FontDefinitionTTF::DEFAUL_ATLAS_TEXTURE_SIZE = 1024;
|
||||
|
||||
FontDefinitionTTF::FontDefinitionTTF():_textImages(0), _commonLineHeight(0)
|
||||
{
|
||||
}
|
||||
|
||||
FontDefinitionTTF* FontDefinitionTTF::create(Font *font, int textureSize)
|
||||
{
|
||||
if (textureSize == 0)
|
||||
textureSize = DEFAUL_ATLAS_TEXTURE_SIZE;
|
||||
|
||||
FontDefinitionTTF *ret = new FontDefinitionTTF;
|
||||
|
||||
if (!ret)
|
||||
return 0;
|
||||
|
||||
const char *glyph = font->getCurrentGlyphCollection();
|
||||
if (!glyph)
|
||||
return nullptr;
|
||||
|
||||
if (ret->initDefinition(font, glyph, textureSize))
|
||||
{
|
||||
ret->autorelease();
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
delete ret;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
FontDefinitionTTF::~FontDefinitionTTF()
|
||||
{
|
||||
if (_textImages)
|
||||
{
|
||||
delete _textImages;
|
||||
}
|
||||
}
|
||||
|
||||
bool FontDefinitionTTF::prepareLetterDefinitions(TextFontPagesDef *pageDefs)
|
||||
{
|
||||
// get all the pages
|
||||
TextFontPagesDef *pages = pageDefs;
|
||||
if (!pages)
|
||||
return (false);
|
||||
|
||||
float maxLineHeight = -1;
|
||||
|
||||
// loops all the pages
|
||||
for (int cPages = 0; cPages < pages->getNumPages(); ++cPages)
|
||||
{
|
||||
// loops all the lines in this page
|
||||
for (int cLines = 0; cLines<pages->getPageAt(cPages)->getNumLines(); ++cLines)
|
||||
{
|
||||
float posXUV = 0.0;
|
||||
float posYUV = pages->getPageAt(cPages)->getLineAt(cLines)->getY();
|
||||
|
||||
int charsCounter = 0;
|
||||
|
||||
for (int c = 0; c < pages->getPageAt(cPages)->getLineAt(cLines)->getNumGlyph(); ++c)
|
||||
{
|
||||
// the current glyph
|
||||
GlyphDef currentGlyph = pages->getPageAt(cPages)->getLineAt(cLines)->getGlyphAt(c);
|
||||
|
||||
// letter width
|
||||
float letterWidth = currentGlyph.getRect().size.width;
|
||||
|
||||
// letter height
|
||||
float letterHeight = pages->getPageAt(cPages)->getLineAt(cLines)->getHeight();
|
||||
|
||||
// add this letter definition
|
||||
FontLetterDefinition tempDef;
|
||||
|
||||
|
||||
// carloX little hack (this should be done outside the loop)
|
||||
if (posXUV == 0.0)
|
||||
posXUV = currentGlyph.getPadding();
|
||||
|
||||
tempDef.validDefinition = currentGlyph.isValid();
|
||||
|
||||
if (tempDef.validDefinition)
|
||||
{
|
||||
tempDef.letteCharUTF16 = currentGlyph.getUTF8Letter();
|
||||
tempDef.width = letterWidth + currentGlyph.getPadding();
|
||||
tempDef.height = (letterHeight - 1);
|
||||
tempDef.U = (posXUV - 1);
|
||||
tempDef.V = posYUV;
|
||||
tempDef.offsetX = currentGlyph.getRect().origin.x;
|
||||
tempDef.offsetY = currentGlyph.getRect().origin.y;
|
||||
tempDef.textureID = cPages;
|
||||
tempDef.commonLineHeight = currentGlyph.getCommonHeight();
|
||||
|
||||
// take from pixels to points
|
||||
tempDef.width = tempDef.width / CC_CONTENT_SCALE_FACTOR();
|
||||
tempDef.height = tempDef.height / CC_CONTENT_SCALE_FACTOR();
|
||||
tempDef.U = tempDef.U / CC_CONTENT_SCALE_FACTOR();
|
||||
tempDef.V = tempDef.V / CC_CONTENT_SCALE_FACTOR();
|
||||
|
||||
if (tempDef.commonLineHeight>maxLineHeight)
|
||||
maxLineHeight = tempDef.commonLineHeight;
|
||||
}
|
||||
else
|
||||
{
|
||||
tempDef.letteCharUTF16 = currentGlyph.getUTF8Letter();
|
||||
tempDef.commonLineHeight = 0;
|
||||
tempDef.width = 0;
|
||||
tempDef.height = 0;
|
||||
tempDef.U = 0;
|
||||
tempDef.V = 0;
|
||||
tempDef.offsetX = 0;
|
||||
tempDef.offsetY = 0;
|
||||
tempDef.textureID = 0;
|
||||
}
|
||||
|
||||
|
||||
// add this definition
|
||||
addLetterDefinition(tempDef);
|
||||
|
||||
// move bounding box to the next letter
|
||||
posXUV += letterWidth + currentGlyph.getPadding();
|
||||
|
||||
// next char in the string
|
||||
++charsCounter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// store the common line height
|
||||
_commonLineHeight = maxLineHeight;
|
||||
|
||||
//
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FontDefinitionTTF::initDefinition(cocos2d::Font *font, const char *letters, int textureSize)
|
||||
{
|
||||
// preare texture/image stuff
|
||||
_textImages = new TextImage();
|
||||
if (!_textImages)
|
||||
return false;
|
||||
|
||||
if (!_textImages->initWithString(letters, textureSize, textureSize, font, true))
|
||||
{
|
||||
delete _textImages;
|
||||
_textImages = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
// prepare the new letter definition
|
||||
return prepareLetterDefinitions(_textImages->getPages());
|
||||
}
|
||||
|
||||
void FontDefinitionTTF::addLetterDefinition(const FontLetterDefinition &defToAdd)
|
||||
{
|
||||
if (_fontLettersDefinitionUTF16.find(defToAdd.letteCharUTF16) == _fontLettersDefinitionUTF16.end())
|
||||
{
|
||||
_fontLettersDefinitionUTF16[defToAdd.letteCharUTF16] = defToAdd;
|
||||
}
|
||||
}
|
||||
|
||||
FontAtlas * FontDefinitionTTF::createFontAtlas()
|
||||
{
|
||||
int numTextures = 0;
|
||||
TextFontPagesDef *pages = _textImages->getPages();
|
||||
|
||||
if (pages)
|
||||
numTextures = pages->getNumPages();
|
||||
else
|
||||
return nullptr;
|
||||
|
||||
if (!numTextures)
|
||||
return nullptr;
|
||||
|
||||
FontAtlas *retAtlas = new FontAtlas(*_textImages->getFont());
|
||||
|
||||
if (!retAtlas)
|
||||
return 0;
|
||||
|
||||
for (int c = 0; c < numTextures; ++c)
|
||||
{
|
||||
TextFontPagesDef *pPages = _textImages->getPages();
|
||||
retAtlas->addTexture(*(pPages->getPageAt(c)->getPageTexture()), c);
|
||||
}
|
||||
|
||||
// set the common line height
|
||||
retAtlas->setCommonLineHeight(_commonLineHeight * 0.8);
|
||||
|
||||
for( auto &item: _fontLettersDefinitionUTF16 )
|
||||
{
|
||||
if ( item.second.validDefinition )
|
||||
{
|
||||
FontLetterDefinition tempDefinition = item.second;
|
||||
tempDefinition.anchorX = 0.0f;
|
||||
tempDefinition.anchorY = 1.0f;
|
||||
retAtlas->addLetterDefinition(tempDefinition);
|
||||
}
|
||||
}
|
||||
|
||||
// done here
|
||||
return retAtlas;
|
||||
}
|
||||
|
||||
NS_CC_END
|
|
@ -1,70 +0,0 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2013 Zynga Inc.
|
||||
Copyright (c) 2013-2014 Chukong Technologies 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 _FontDefinition_h_
|
||||
#define _FontDefinition_h_
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include "CCTextImage.h"
|
||||
#include "CCFont.h"
|
||||
#include "CCFontAtlas.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
/**
|
||||
*/
|
||||
class CC_DLL FontDefinitionTTF : public Object
|
||||
{
|
||||
public:
|
||||
|
||||
static FontDefinitionTTF* create(Font *font, int textureSize = 0);
|
||||
FontAtlas * createFontAtlas();
|
||||
|
||||
private:
|
||||
/**
|
||||
* @js ctor
|
||||
*/
|
||||
FontDefinitionTTF();
|
||||
/**
|
||||
* @js NA
|
||||
* @lua NA
|
||||
*/
|
||||
~FontDefinitionTTF();
|
||||
|
||||
bool initDefinition(Font *font, const char *letters, int textureSize);
|
||||
bool prepareLetterDefinitions(TextFontPagesDef *pageDefs);
|
||||
void addLetterDefinition(const FontLetterDefinition &defToAdd);
|
||||
|
||||
TextImage * _textImages;
|
||||
std::unordered_map<unsigned short, FontLetterDefinition> _fontLettersDefinitionUTF16;
|
||||
float _commonLineHeight;
|
||||
|
||||
static const int DEFAUL_ATLAS_TEXTURE_SIZE;
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
||||
#endif
|
|
@ -28,9 +28,6 @@ THE SOFTWARE.
|
|||
|
||||
#include "ccUTF8.h"
|
||||
#include "CCFontFreeType.h"
|
||||
#include "CCTextImage.h"
|
||||
#include "CCFont.h"
|
||||
#include "CCFontDefinition.h"
|
||||
#include "platform/CCFileUtils.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
@ -41,11 +38,7 @@ bool FontFreeType::_FTInitialized = false;
|
|||
|
||||
FontFreeType * FontFreeType::create(const std::string &fontName, int fontSize, GlyphCollection glyphs, const char *customGlyphs)
|
||||
{
|
||||
bool dynamicGlyphCollection = false;
|
||||
if(glyphs == GlyphCollection::DYNAMIC)
|
||||
dynamicGlyphCollection = true;
|
||||
|
||||
FontFreeType *tempFont = new FontFreeType(dynamicGlyphCollection);
|
||||
FontFreeType *tempFont = new FontFreeType();
|
||||
|
||||
if (!tempFont)
|
||||
return nullptr;
|
||||
|
@ -89,10 +82,9 @@ FT_Library FontFreeType::getFTLibrary()
|
|||
return _FTlibrary;
|
||||
}
|
||||
|
||||
FontFreeType::FontFreeType(bool dynamicGlyphCollection)
|
||||
FontFreeType::FontFreeType()
|
||||
: _fontRef(nullptr),
|
||||
_letterPadding(5),
|
||||
_dynamicGlyphCollection(dynamicGlyphCollection)
|
||||
_letterPadding(5)
|
||||
{
|
||||
if(_distanceFieldEnabled)
|
||||
_letterPadding += 2 * DistanceMapSpread;
|
||||
|
@ -140,25 +132,15 @@ FontFreeType::~FontFreeType()
|
|||
}
|
||||
|
||||
FontAtlas * FontFreeType::createFontAtlas()
|
||||
{
|
||||
if (_dynamicGlyphCollection)
|
||||
{
|
||||
FontAtlas *atlas = new FontAtlas(*this);
|
||||
if (_usedGlyphs != GlyphCollection::DYNAMIC)
|
||||
{
|
||||
atlas->prepareLetterDefinitions(cc_utf8_to_utf16(getCurrentGlyphCollection()));
|
||||
}
|
||||
this->release();
|
||||
return atlas;
|
||||
}
|
||||
else
|
||||
{
|
||||
FontDefinitionTTF *def = FontDefinitionTTF::create(this);
|
||||
|
||||
if (!def)
|
||||
return nullptr;
|
||||
|
||||
FontAtlas *atlas = def->createFontAtlas();
|
||||
|
||||
return atlas;
|
||||
}
|
||||
}
|
||||
|
||||
bool FontFreeType::getBBOXFotChar(unsigned short theChar, Rect &outRect) const
|
||||
{
|
||||
|
@ -184,71 +166,6 @@ bool FontFreeType::getBBOXFotChar(unsigned short theChar, Rect &outRect) const
|
|||
return true;
|
||||
}
|
||||
|
||||
GlyphDef * FontFreeType::getGlyphDefintionsForText(const char *text, int &outNumGlyphs, bool UTF16text) const
|
||||
{
|
||||
unsigned short* utf16String = 0;
|
||||
|
||||
if (UTF16text)
|
||||
{
|
||||
utf16String = (unsigned short*) text;
|
||||
}
|
||||
else
|
||||
{
|
||||
utf16String = cc_utf8_to_utf16(text);
|
||||
}
|
||||
|
||||
//
|
||||
if (!utf16String)
|
||||
return 0;
|
||||
|
||||
int numChar = cc_wcslen(utf16String);
|
||||
if (!numChar)
|
||||
return 0;
|
||||
|
||||
// allocate the needed Glyphs
|
||||
GlyphDef *glyphs = new GlyphDef[numChar];
|
||||
assert(glyphs != nullptr);
|
||||
if (!glyphs)
|
||||
return 0;
|
||||
|
||||
// sore result as CCRect
|
||||
for (int c = 0; c < numChar; ++c)
|
||||
{
|
||||
Rect tempRect;
|
||||
|
||||
if (!getBBOXFotChar(utf16String[c], tempRect))
|
||||
{
|
||||
log("Warning: Cannot find definition for glyph: %c in font:%s", utf16String[c], _fontName.c_str());
|
||||
|
||||
tempRect.origin.x = 0;
|
||||
tempRect.origin.y = 0;
|
||||
tempRect.size.width = 0;
|
||||
tempRect.size.height = 0;
|
||||
|
||||
glyphs[c].setRect(tempRect);
|
||||
glyphs[c].setUTF16Letter(utf16String[c]);
|
||||
glyphs[c].setValid(false);
|
||||
glyphs[c].setPadding(_letterPadding);
|
||||
}
|
||||
else
|
||||
{
|
||||
glyphs[c].setRect(tempRect);
|
||||
glyphs[c].setUTF16Letter(utf16String[c]);
|
||||
glyphs[c].setPadding(_letterPadding);
|
||||
glyphs[c].setValid(true);
|
||||
}
|
||||
}
|
||||
|
||||
outNumGlyphs = numChar;
|
||||
|
||||
// free memory
|
||||
if (!UTF16text)
|
||||
delete [] utf16String;
|
||||
|
||||
// done
|
||||
return glyphs;
|
||||
}
|
||||
|
||||
Size * FontFreeType::getAdvancesForTextUTF16(unsigned short *text, int &outNumLetters) const
|
||||
{
|
||||
if (!text)
|
||||
|
|
|
@ -46,18 +46,16 @@ public:
|
|||
|
||||
virtual FontAtlas * createFontAtlas() override;
|
||||
virtual Size * getAdvancesForTextUTF16(unsigned short *text, int &outNumLetters) const override;
|
||||
virtual GlyphDef * getGlyphDefintionsForText(const char *text, int &outNumGlyphs, bool UTF16text = false) const override;
|
||||
|
||||
unsigned char * getGlyphBitmap(unsigned short theChar, int &outWidth, int &outHeight) const override;
|
||||
virtual int getFontMaxHeight() const override;
|
||||
virtual int getLetterPadding() const override;
|
||||
|
||||
bool getBBOXFotChar(unsigned short theChar, Rect &outRect) const;
|
||||
|
||||
inline bool isDynamicGlyphCollection() { return _dynamicGlyphCollection;}
|
||||
|
||||
protected:
|
||||
|
||||
FontFreeType(bool dynamicGlyphCollection = false);
|
||||
FontFreeType();
|
||||
virtual ~FontFreeType();
|
||||
bool createFontObject(const std::string &fontName, int fontSize);
|
||||
|
||||
|
@ -76,7 +74,6 @@ private:
|
|||
int _letterPadding;
|
||||
std::string _fontName;
|
||||
Data _ttfData;
|
||||
bool _dynamicGlyphCollection;
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -1,478 +0,0 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2013 Zynga Inc.
|
||||
Copyright (c) 2013-2014 Chukong Technologies 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.
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "CCTextImage.h"
|
||||
#include "CCFontFreeType.h"
|
||||
#include "CCFont.h"
|
||||
#include "ccUTF8.h"
|
||||
|
||||
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)
|
||||
{
|
||||
}
|
||||
|
||||
TextPageDef::~TextPageDef()
|
||||
{
|
||||
size_t numLines = _lines.size();
|
||||
|
||||
for( size_t c = 0; c<numLines; ++c )
|
||||
{
|
||||
delete _lines[c];
|
||||
}
|
||||
|
||||
_lines.clear();
|
||||
|
||||
if (_pageData)
|
||||
{
|
||||
delete [] _pageData;
|
||||
}
|
||||
|
||||
if (_pageTexture)
|
||||
{
|
||||
_pageTexture->release();
|
||||
}
|
||||
}
|
||||
|
||||
bool TextPageDef::generatePageTexture(bool releasePageData)
|
||||
{
|
||||
if (!_pageData)
|
||||
return false;
|
||||
|
||||
if (_pageTexture)
|
||||
{
|
||||
_pageTexture->release();
|
||||
_pageTexture = 0;
|
||||
}
|
||||
|
||||
Size imageSize = Size((float)(_width), (float)(_height));
|
||||
if((imageSize.width <= 0) || (imageSize.height <= 0))
|
||||
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);
|
||||
|
||||
// release the page data if requested
|
||||
if (releasePageData && textureCreated)
|
||||
{
|
||||
delete [] _pageData;
|
||||
_pageData = 0;
|
||||
}
|
||||
|
||||
return textureCreated;
|
||||
}
|
||||
|
||||
void TextPageDef::preparePageTexture(bool releaseRAWData)
|
||||
{
|
||||
generatePageTexture(releaseRAWData);
|
||||
}
|
||||
|
||||
Texture2D *TextPageDef::getPageTexture()
|
||||
{
|
||||
if (!_pageTexture)
|
||||
{
|
||||
generatePageTexture();
|
||||
}
|
||||
|
||||
return _pageTexture;
|
||||
}
|
||||
|
||||
TextFontPagesDef::TextFontPagesDef()
|
||||
{
|
||||
}
|
||||
|
||||
TextFontPagesDef::~TextFontPagesDef()
|
||||
{
|
||||
size_t numPages = _pages.size();
|
||||
for( size_t c = 0; c < numPages; ++c )
|
||||
{
|
||||
if (_pages[c])
|
||||
delete _pages[c];
|
||||
}
|
||||
}
|
||||
|
||||
TextImage::TextImage(): _fontPages(0), _font(0)
|
||||
{
|
||||
}
|
||||
|
||||
TextImage::~TextImage()
|
||||
{
|
||||
if (_fontPages)
|
||||
delete _fontPages;
|
||||
|
||||
if (_font)
|
||||
_font->release();
|
||||
}
|
||||
|
||||
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)
|
||||
if (!generateTextGlyphs(text))
|
||||
return false;
|
||||
|
||||
Size constrainSize;
|
||||
unsigned short int *strUTF16 = 0;
|
||||
|
||||
int stringNumChars;
|
||||
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
|
||||
if (!createPageDefinitions(strUTF16, width, height, _font->getFontMaxHeight()))
|
||||
return false;
|
||||
|
||||
// release the original string if needed
|
||||
if (!textIsUTF16)
|
||||
delete [] strUTF16;
|
||||
|
||||
// actually create the needed images
|
||||
return createImageDataFromPages(_fontPages, releaseRAWData);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
//
|
||||
unsigned short int *strUTF16 = inText;
|
||||
|
||||
if (_fontPages)
|
||||
delete _fontPages;
|
||||
|
||||
// create pages for the font
|
||||
_fontPages = new TextFontPagesDef();
|
||||
if (!_fontPages)
|
||||
return false;
|
||||
|
||||
// create the first page (ther is going to be at least one page)
|
||||
TextPageDef *currentPageDef = new TextPageDef(currentPage, imageWidth, imageHeight);
|
||||
if (!currentPageDef)
|
||||
return false;
|
||||
|
||||
// add the current page
|
||||
_fontPages->addPage(currentPageDef);
|
||||
|
||||
// work out creating pages
|
||||
|
||||
do {
|
||||
|
||||
// choose texture page
|
||||
if ((currentY + lineHeight) > imageHeight)
|
||||
{
|
||||
currentY = 0;
|
||||
currentPage += 1;
|
||||
|
||||
// create a new page and add
|
||||
currentPageDef = new TextPageDef(currentPage, imageWidth, imageHeight);
|
||||
if (!currentPageDef)
|
||||
return false;
|
||||
|
||||
_fontPages->addPage(currentPageDef);
|
||||
}
|
||||
|
||||
// get the new fitting string
|
||||
Size tempSize;
|
||||
tempSize.width = imageWidth;
|
||||
tempSize.height = imageHeight;
|
||||
|
||||
// figure out how many glyphs fit in this line
|
||||
int newLineSize = 0;
|
||||
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);
|
||||
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
|
||||
if (delta)
|
||||
{
|
||||
// create the new string
|
||||
unsigned short int *tempS = _font->trimUTF16Text(strUTF16, numFittingChar, (stringLenght - 1));
|
||||
|
||||
if (needToReleaseText)
|
||||
delete [] strUTF16;
|
||||
|
||||
// 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;
|
||||
|
||||
} while(delta);
|
||||
|
||||
if (needToReleaseText)
|
||||
delete [] strUTF16;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int TextImage::getNumGlyphsFittingInSize(std::unordered_map<unsigned short int, GlyphDef> &glyphDefs, unsigned short int *strUTF8, Font *font, Size *constrainSize, int &outNewSize)
|
||||
{
|
||||
if (!strUTF8)
|
||||
return 0;
|
||||
|
||||
float widthWithBBX = 0.0f;
|
||||
float lastWidth = 0.0f;
|
||||
|
||||
// get the string to UTF8
|
||||
int numChar = cc_wcslen(strUTF8);
|
||||
|
||||
for (int c = 0; c < numChar; ++c)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
bool TextImage::addGlyphsToLine(TextLineDef *line, const char *lineText, bool textIsUTF16)
|
||||
{
|
||||
if (!_font)
|
||||
return false;
|
||||
|
||||
int numLetters = 0;
|
||||
unsigned short int *UTF16string = 0;
|
||||
|
||||
if (textIsUTF16)
|
||||
{
|
||||
UTF16string = (unsigned short int *)lineText;
|
||||
numLetters = cc_wcslen(UTF16string);
|
||||
}
|
||||
else
|
||||
{
|
||||
UTF16string = _font->getUTF16Text(lineText, numLetters);
|
||||
}
|
||||
|
||||
for (int c = 0; c < numLetters; ++c)
|
||||
{
|
||||
_textGlyphs[UTF16string[c]].setCommonHeight(line->getHeight());
|
||||
line->addGlyph(_textGlyphs[UTF16string[c]] );
|
||||
}
|
||||
|
||||
if(!textIsUTF16)
|
||||
delete [] UTF16string;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TextImage::generateTextGlyphs(const char * text)
|
||||
{
|
||||
if (!_font)
|
||||
return false;
|
||||
|
||||
int numGlyphs = 0;
|
||||
GlyphDef *newGlyphs = _font->getGlyphDefintionsForText(text, numGlyphs);
|
||||
|
||||
if (!newGlyphs)
|
||||
return false;
|
||||
|
||||
if (!_textGlyphs.empty())
|
||||
_textGlyphs.clear();
|
||||
|
||||
for (int c = 0; c < numGlyphs; ++c)
|
||||
_textGlyphs[newGlyphs[c].getUTF8Letter()] = newGlyphs[c];
|
||||
|
||||
delete [] newGlyphs;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TextImage::createImageDataFromPages(TextFontPagesDef *thePages, bool releaseRAWData)
|
||||
{
|
||||
int numPages = thePages->getNumPages();
|
||||
if (!numPages)
|
||||
return false;
|
||||
|
||||
for (int c = 0; c < numPages; ++c)
|
||||
{
|
||||
unsigned char *pageData = 0;
|
||||
pageData = preparePageGlyphData(thePages->getPageAt(c));
|
||||
|
||||
if (pageData)
|
||||
{
|
||||
// set the page data
|
||||
thePages->getPageAt(c)->setPageData(pageData);
|
||||
|
||||
// crete page texture and relase RAW data
|
||||
thePages->getPageAt(c)->preparePageTexture(releaseRAWData);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
unsigned char * TextImage::preparePageGlyphData(TextPageDef *thePage)
|
||||
{
|
||||
return renderGlyphData(thePage);
|
||||
}
|
||||
|
||||
unsigned char * TextImage::renderGlyphData(TextPageDef *thePage)
|
||||
{
|
||||
if (!thePage)
|
||||
return 0;
|
||||
|
||||
if (!_font)
|
||||
return 0;
|
||||
|
||||
if (thePage->getNumLines() == 0)
|
||||
return nullptr;
|
||||
|
||||
int pageWidth = thePage->getWidth();
|
||||
int pageHeight = thePage->getHeight();
|
||||
|
||||
// prepare memory and clean to 0
|
||||
int sizeInBytes = (pageWidth * pageHeight * 1);
|
||||
unsigned char* data = new unsigned char[sizeInBytes];
|
||||
|
||||
if (!data)
|
||||
return 0;
|
||||
|
||||
memset(data, 0, sizeInBytes);
|
||||
|
||||
int numLines = thePage->getNumLines();
|
||||
|
||||
for (int c = 0; c < numLines; ++c)
|
||||
{
|
||||
TextLineDef *currentLine = thePage->getLineAt(c);
|
||||
|
||||
float origX = _font->getLetterPadding();
|
||||
float origY = currentLine->getY();
|
||||
|
||||
int numGlyphToRender = currentLine->getNumGlyph();
|
||||
|
||||
for (int cglyph = 0; cglyph < numGlyphToRender; ++cglyph)
|
||||
{
|
||||
GlyphDef currGlyph = currentLine->getGlyphAt(cglyph);
|
||||
_font->renderCharAt(currGlyph.getUTF8Letter(), origX, origY, data, pageWidth);
|
||||
origX += (currGlyph.getRect().size.width + _font->getLetterPadding());
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _DEBUG_FONTS_
|
||||
static int counter = 0;
|
||||
char outFilename[512];
|
||||
sprintf(outFilename,"testIMG%d", counter);
|
||||
++counter;
|
||||
Image *image = new Image;
|
||||
image->initWithRawData(data, (pageWidth * pageWidth * 4), 1024, 1024, 8, false);
|
||||
image->saveToFile(outFilename);
|
||||
#endif
|
||||
|
||||
// we are done here
|
||||
return data;
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,215 +0,0 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2013 Zynga Inc.
|
||||
Copyright (c) 2013-2014 Chukong Technologies 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 _CCTextImage_h_
|
||||
#define _CCTextImage_h_
|
||||
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "CCPlatformMacros.h"
|
||||
#include "CCGeometry.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
class Font;
|
||||
class Texture2D;
|
||||
|
||||
/** @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
|
||||
{
|
||||
public:
|
||||
|
||||
GlyphDef() : _validGlyph(false) {}
|
||||
GlyphDef(unsigned short int letterUTF8, const Rect &rect) {
|
||||
_gliphRect = rect;
|
||||
_uTF16Letter = letterUTF8;
|
||||
}
|
||||
|
||||
void setUTF16Letter(unsigned short int letterUTF8) { _uTF16Letter = letterUTF8; }
|
||||
void setRect(const Rect & theRect) { _gliphRect = theRect; }
|
||||
|
||||
unsigned short int getUTF8Letter() const { return _uTF16Letter; }
|
||||
const Rect& getRect() const { return _gliphRect; }
|
||||
|
||||
void setPadding(float padding) { _padding = padding; }
|
||||
float getPadding() const { return _padding; }
|
||||
|
||||
void setCommonHeight(float commonHeight) { _commonHeight = commonHeight; }
|
||||
float getCommonHeight() const { return _commonHeight; }
|
||||
|
||||
void setValid(bool isValid) { _validGlyph = isValid; }
|
||||
bool isValid() const { return _validGlyph; }
|
||||
|
||||
protected:
|
||||
Rect _gliphRect;
|
||||
unsigned short int _uTF16Letter;
|
||||
float _padding;
|
||||
float _commonHeight;
|
||||
bool _validGlyph;
|
||||
|
||||
};
|
||||
|
||||
/** @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
|
||||
{
|
||||
public:
|
||||
|
||||
TextLineDef(float x, float y, float width, float height);
|
||||
|
||||
float getX() const { return _x; }
|
||||
float getY() const { return _y; }
|
||||
float getWidth() const { return _width; }
|
||||
float getHeight() const { return _height; }
|
||||
|
||||
void addGlyph(GlyphDef theGlyph) { _glyphs.push_back(theGlyph); }
|
||||
int getNumGlyph() const { return static_cast<int>(_glyphs.size()); }
|
||||
const GlyphDef & getGlyphAt(int index) const { return _glyphs[index]; }
|
||||
|
||||
protected:
|
||||
float _x;
|
||||
float _y;
|
||||
float _width;
|
||||
float _height;
|
||||
std::vector<GlyphDef> _glyphs;
|
||||
};
|
||||
|
||||
/** @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
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @js NA
|
||||
*/
|
||||
TextPageDef(int pageNum, int width, int height);
|
||||
/**
|
||||
* @js NA
|
||||
* @lua NA
|
||||
*/
|
||||
~TextPageDef();
|
||||
|
||||
void addLine(TextLineDef *theLine) { _lines.push_back(theLine); }
|
||||
int getNumLines() const { return static_cast<int>(_lines.size()); }
|
||||
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; }
|
||||
Texture2D *getPageTexture();
|
||||
void preparePageTexture(bool releaseRAWData = true);
|
||||
|
||||
protected:
|
||||
bool generatePageTexture(bool releasePageData = false);
|
||||
|
||||
int _pageNum;
|
||||
int _width;
|
||||
int _height;
|
||||
unsigned char * _pageData;
|
||||
Texture2D* _pageTexture;
|
||||
std::vector<TextLineDef*> _lines;
|
||||
};
|
||||
|
||||
/** @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
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @js ctor
|
||||
*/
|
||||
TextFontPagesDef();
|
||||
/**
|
||||
* @js NA
|
||||
* @lua NA
|
||||
*/
|
||||
~TextFontPagesDef();
|
||||
|
||||
void addPage(TextPageDef *newPage) { _pages.push_back(newPage); }
|
||||
int getNumPages() const { return static_cast<int>(_pages.size()); }
|
||||
TextPageDef* getPageAt(int index) const { return _pages[index]; }
|
||||
|
||||
protected:
|
||||
std::vector<TextPageDef*> _pages;
|
||||
|
||||
};
|
||||
|
||||
/** @brief TextImage
|
||||
*
|
||||
*/
|
||||
class CC_DLL TextImage
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @js ctor
|
||||
*/
|
||||
TextImage();
|
||||
/**
|
||||
* @js NA
|
||||
* @lua NA
|
||||
*/
|
||||
~TextImage();
|
||||
|
||||
bool initWithString(const char *text, int width, int height, Font* font, bool releaseRAWData = true);
|
||||
|
||||
TextFontPagesDef* getPages() const { return _fontPages; }
|
||||
Font* getFont() const { return _font; }
|
||||
|
||||
protected:
|
||||
bool createImageDataFromPages(TextFontPagesDef *thePages, bool releaseRAWData = true);
|
||||
bool addGlyphsToLine(TextLineDef *line, const char *lineText, bool textIsUTF16 = false);
|
||||
bool generateTextGlyphs(const char * text);
|
||||
int getNumGlyphsFittingInSize(std::unordered_map<unsigned short int, GlyphDef> &glyphDefs, unsigned short int *strUTF8, Font *font, Size *constrainSize, int &outNewSize);
|
||||
bool createPageDefinitions(unsigned short int *inText, int imageWidth, int imageHeight, int lineHeight);
|
||||
unsigned char * preparePageGlyphData(TextPageDef *thePage);
|
||||
|
||||
// glyph rendering
|
||||
unsigned char * renderGlyphData(TextPageDef *thePage);
|
||||
|
||||
std::unordered_map<unsigned short int, GlyphDef> _textGlyphs;
|
||||
TextFontPagesDef* _fontPages;
|
||||
Font* _font;
|
||||
};
|
||||
|
||||
|
||||
NS_CC_END
|
||||
|
||||
|
||||
#endif // _CCTextImage_h_
|
|
@ -67,8 +67,6 @@ set(COCOS2D_SRC
|
|||
CCFont.cpp
|
||||
CCFontAtlas.cpp
|
||||
CCFontAtlasCache.cpp
|
||||
CCFontAtlasFactory.cpp
|
||||
CCFontDefinition.cpp
|
||||
CCFontFNT.cpp
|
||||
CCFontFreeType.cpp
|
||||
CCFontCharMap.cpp
|
||||
|
@ -77,7 +75,6 @@ set(COCOS2D_SRC
|
|||
CCLabelBMFont.cpp
|
||||
CCLabelTTF.cpp
|
||||
CCLabelTextFormatter.cpp
|
||||
CCTextImage.cpp
|
||||
CCLayer.cpp
|
||||
CCScene.cpp
|
||||
CCTransition.cpp
|
||||
|
|
|
@ -247,9 +247,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou
|
|||
<ClCompile Include="CCFont.cpp" />
|
||||
<ClCompile Include="CCFontAtlas.cpp" />
|
||||
<ClCompile Include="CCFontAtlasCache.cpp" />
|
||||
<ClCompile Include="CCFontAtlasFactory.cpp" />
|
||||
<ClCompile Include="CCFontCharMap.cpp" />
|
||||
<ClCompile Include="CCFontDefinition.cpp" />
|
||||
<ClCompile Include="CCFontFNT.cpp" />
|
||||
<ClCompile Include="CCFontFreeType.cpp" />
|
||||
<ClCompile Include="ccFPSImages.c" />
|
||||
|
@ -288,7 +286,6 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou
|
|||
<ClCompile Include="CCSpriteFrame.cpp" />
|
||||
<ClCompile Include="CCSpriteFrameCache.cpp" />
|
||||
<ClCompile Include="CCTextFieldTTF.cpp" />
|
||||
<ClCompile Include="CCTextImage.cpp" />
|
||||
<ClCompile Include="CCTexture2D.cpp" />
|
||||
<ClCompile Include="CCTextureAtlas.cpp" />
|
||||
<ClCompile Include="CCTextureCache.cpp" />
|
||||
|
@ -429,9 +426,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou
|
|||
<ClInclude Include="CCFont.h" />
|
||||
<ClInclude Include="CCFontAtlas.h" />
|
||||
<ClInclude Include="CCFontAtlasCache.h" />
|
||||
<ClInclude Include="CCFontAtlasFactory.h" />
|
||||
<ClInclude Include="CCFontCharMap.h" />
|
||||
<ClInclude Include="CCFontDefinition.h" />
|
||||
<ClInclude Include="CCFontFNT.h" />
|
||||
<ClInclude Include="CCFontFreeType.h" />
|
||||
<ClInclude Include="ccFPSImages.h" />
|
||||
|
@ -490,7 +485,6 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou
|
|||
<ClInclude Include="CCSpriteFrame.h" />
|
||||
<ClInclude Include="CCSpriteFrameCache.h" />
|
||||
<ClInclude Include="CCTextFieldTTF.h" />
|
||||
<ClInclude Include="CCTextImage.h" />
|
||||
<ClInclude Include="CCTexture2D.h" />
|
||||
<ClInclude Include="CCTextureAtlas.h" />
|
||||
<ClInclude Include="CCTextureCache.h" />
|
||||
|
|
|
@ -318,12 +318,6 @@
|
|||
<ClCompile Include="CCFontAtlasCache.cpp">
|
||||
<Filter>label_nodes</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CCFontAtlasFactory.cpp">
|
||||
<Filter>label_nodes</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CCFontDefinition.cpp">
|
||||
<Filter>label_nodes</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CCFontFNT.cpp">
|
||||
<Filter>label_nodes</Filter>
|
||||
</ClCompile>
|
||||
|
@ -345,9 +339,6 @@
|
|||
<ClCompile Include="CCLabelTTF.cpp">
|
||||
<Filter>label_nodes</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CCTextImage.cpp">
|
||||
<Filter>label_nodes</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\base\etc1.cpp">
|
||||
<Filter>platform\etc</Filter>
|
||||
</ClCompile>
|
||||
|
@ -834,12 +825,6 @@
|
|||
<ClInclude Include="CCFontAtlasCache.h">
|
||||
<Filter>label_nodes</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CCFontAtlasFactory.h">
|
||||
<Filter>label_nodes</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CCFontDefinition.h">
|
||||
<Filter>label_nodes</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CCFontFNT.h">
|
||||
<Filter>label_nodes</Filter>
|
||||
</ClInclude>
|
||||
|
@ -864,9 +849,6 @@
|
|||
<ClInclude Include="CCLabelTTF.h">
|
||||
<Filter>label_nodes</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CCTextImage.h">
|
||||
<Filter>label_nodes</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\base\etc1.h">
|
||||
<Filter>platform\etc</Filter>
|
||||
</ClInclude>
|
||||
|
|
Loading…
Reference in New Issue