Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
30fb96e
Merge pull request #4 from cocos2d/v3
rh101 May 29, 2019
67d2f79
Added RenderTexture::saveToFileAsNonPMA() to save images without PMA.
rh101 May 30, 2019
b5bdada
[CCImage-ios.mm] Fixed indentation.
rh101 Jun 4, 2019
7065335
Merge pull request #5 from cocos2d/v3
rh101 Jun 16, 2019
94ec877
Merge pull request #7 from cocos2d/v3
rh101 Jun 22, 2019
92dd2e5
Merge pull request #8 from cocos2d/v3
rh101 Jun 30, 2019
628c758
Merge pull request #9 from cocos2d/v3
rh101 Jul 20, 2019
df5ef30
Merge pull request #10 from cocos2d/v3
rh101 Jul 26, 2019
03c7479
Merge pull request #11 from cocos2d/v3
rh101 Sep 20, 2019
ffc7cc7
Merge pull request #13 from cocos2d/v3
rh101 Sep 23, 2019
5eac99f
Merge pull request #15 from cocos2d/v3
rh101 Nov 6, 2019
d6bdf28
Merge pull request #16 from cocos2d/v3
rh101 Nov 8, 2019
aed03c8
Added support for adding bitmap font atlas image sub-textures in a la…
rh101 Nov 8, 2019
29a8914
[LabelTest.cpp] Updated BMFontOneAtlas test to support new BMFont lab…
rh101 Nov 8, 2019
db96260
[CCFontFNT.cpp] Added missing include statement.
rh101 Nov 8, 2019
b720301
Updated LUA and JS bindings for the new BMFont code.
rh101 Nov 8, 2019
8e417f8
[CCLabelBMFont] Reverted the API to the original interface since this…
rh101 Nov 8, 2019
28b317e
[LabelTest/LabelTest.cpp] Reverted usage of deprecated API.
rh101 Nov 8, 2019
a430f40
Re-added old API for creating bitmap font labels, but prefixed them a…
rh101 Nov 11, 2019
b9dc850
Fix for incorrect usage of variable that may be null.
rh101 Nov 12, 2019
5266ffb
Added two new label tests for BM Font labels. One tests a shared atl…
rh101 Nov 12, 2019
63da6fa
Moved variable definitions outside of loop.
rh101 Nov 13, 2019
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 49 additions & 9 deletions cocos/2d/CCFontAtlasCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,53 @@ FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config)
return nullptr;
}

FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset /* = Vec2::ZERO */)
FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName)
{
auto realFontFilename = FileUtils::getInstance()->getNewFilename(fontFileName); // resolves real file path, to prevent storing multiple atlases for the same file.
return getFontAtlasFNT(fontFileName, Rect::ZERO, false);
}

FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const std::string& subTextureKey)
{
const auto realFontFilename = FileUtils::getInstance()->getNewFilename(fontFileName); // resolves real file path, to prevent storing multiple atlases for the same file.
std::string atlasName = subTextureKey + " " + realFontFilename;

const auto it = _atlasMap.find(atlasName);
if (it == _atlasMap.end())
{
const auto font = FontFNT::create(realFontFilename, subTextureKey);

if (font)
{
const auto tempAtlas = font->createFontAtlas();
if (tempAtlas)
{
_atlasMap[atlasName] = tempAtlas;
return _atlasMap[atlasName];
}
}
}
else
return it->second;

return nullptr;
}

FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const Rect& imageRect, bool imageRotated)
{
const auto realFontFilename = FileUtils::getInstance()->getNewFilename(fontFileName); // resolves real file path, to prevent storing multiple atlases for the same file.
char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageOffset.x, imageOffset.y);
snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageRect.origin.x, imageRect.origin.y);
std::string atlasName(keyPrefix);
atlasName += realFontFilename;

auto it = _atlasMap.find(atlasName);
const auto it = _atlasMap.find(atlasName);
if ( it == _atlasMap.end() )
{
auto font = FontFNT::create(realFontFilename, imageOffset);
const auto font = FontFNT::create(realFontFilename, imageRect, imageRotated);

if(font)
{
auto tempAtlas = font->createFontAtlas();
const auto tempAtlas = font->createFontAtlas();
if (tempAtlas)
{
_atlasMap[atlasName] = tempAtlas;
Expand All @@ -117,6 +148,11 @@ FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, cons
return nullptr;
}

FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset)
{
return getFontAtlasFNT(fontFileName, Rect(imageOffset.x, imageOffset.y, 0, 0), false);
}

FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile)
{
const std::string& atlasName = plistFile;
Expand Down Expand Up @@ -220,10 +256,10 @@ bool FontAtlasCache::releaseFontAtlas(FontAtlas *atlas)
return false;
}

void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset/* = Vec2::ZERO*/)
void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const Rect& imageRect, bool imageRotated)
{
char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageOffset.x, imageOffset.y);
snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageRect.origin.x, imageRect.origin.y);
std::string atlasName(keyPrefix);
atlasName += fontFileName;

Expand All @@ -234,7 +270,7 @@ void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const V
_atlasMap.erase(it);
}
FontFNT::reloadBMFontResource(fontFileName);
auto font = FontFNT::create(fontFileName, imageOffset);
auto font = FontFNT::create(fontFileName, imageRect, imageRotated);
if (font)
{
auto tempAtlas = font->createFontAtlas();
Expand All @@ -243,7 +279,11 @@ void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const V
_atlasMap[atlasName] = tempAtlas;
}
}
}

void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset)
{
reloadFontAtlasFNT(fontFileName, Rect(imageOffset.x, imageOffset.y, 0, 0), false);
}

void FontAtlasCache::unloadFontAtlasTTF(const std::string& fontFileName)
Expand Down
10 changes: 8 additions & 2 deletions cocos/2d/CCFontAtlasCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ class CC_DLL FontAtlasCache
{
public:
static FontAtlas* getFontAtlasTTF(const _ttfConfig* config);
static FontAtlas* getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset = Vec2::ZERO);

static FontAtlas* getFontAtlasFNT(const std::string& fontFileName);
static FontAtlas* getFontAtlasFNT(const std::string& fontFileName, const std::string& subTextureKey);
static FontAtlas* getFontAtlasFNT(const std::string& fontFileName, const Rect& imageRect, bool imageRotated);
CC_DEPRECATED_ATTRIBUTE static FontAtlas* getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset);

static FontAtlas* getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
static FontAtlas* getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
Expand All @@ -59,7 +63,9 @@ class CC_DLL FontAtlasCache
CAUTION : All component use this font texture should be reset font name, though the file name is same!
otherwise, it will cause program crash!
*/
static void reloadFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset = Vec2::ZERO);
static void reloadFontAtlasFNT(const std::string& fontFileName, const Rect& imageRect, bool imageRotated);

CC_DEPRECATED_ATTRIBUTE static void reloadFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset = Vec2::ZERO);

/** Unload all texture atlas texture create by special file name.
CAUTION : All component use this font texture should be reset font name, though the file name is same!
Expand Down
111 changes: 90 additions & 21 deletions cocos/2d/CCFontFNT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "2d/CCFontFNT.h"
#include "2d/CCFontAtlas.h"
#include "2d/CCSpriteFrameCache.h"
#include "platform/CCFileUtils.h"
#include "base/CCConfiguration.h"
#include "base/CCDirector.h"
Expand Down Expand Up @@ -393,9 +394,10 @@ void BMFontConfiguration::parseImageFileName(const char* line, const std::string
int pageId;
sscanf(line, "page id=%d", &pageId);
CCASSERT(pageId == 0, "LabelBMFont file could not be found");

// file
char fileName[255];
sscanf(strchr(line,'"') + 1, "%[^\"]", fileName);
sscanf(strstr(line, "file=\"") + 6, "%[^\"]", fileName);
_atlasName = FileUtils::getInstance()->fullPathFromRelativeFile(fileName, fntFile);
}

Expand Down Expand Up @@ -508,20 +510,59 @@ void BMFontConfiguration::parseKerningEntry(const char* line)
_kerningDictionary[key] = amount;
}

FontFNT * FontFNT::create(const std::string& fntFilePath, const Vec2& imageOffset /* = Vec2::ZERO */)
FontFNT * FontFNT::create(const std::string& fntFilePath, const Rect& imageRect, bool imageRotated)
{
BMFontConfiguration *newConf = FNTConfigLoadFile(fntFilePath);
const auto newConf = FNTConfigLoadFile(fntFilePath);
if (!newConf)
return nullptr;

const auto tempFont = new FontFNT(newConf, imageRect, imageRotated);

if (!tempFont)
{
return nullptr;
}
tempFont->setFontSize(newConf->_fontSize);
tempFont->autorelease();
return tempFont;
}

FontFNT* FontFNT::create(const std::string& fntFilePath, const std::string& subTextureKey)
{
const auto newConf = FNTConfigLoadFile(fntFilePath);
if (!newConf)
return nullptr;

const auto frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(subTextureKey);
if (!frame)
{
return nullptr;
}
auto tempFont = new FontFNT(newConf, frame->getRectInPixels(), frame->isRotated());

if (!tempFont)
{
return nullptr;
}
tempFont->setFontSize(newConf->_fontSize);
tempFont->autorelease();
return tempFont;
}

FontFNT* FontFNT::create(const std::string& fntFilePath)
{
const auto newConf = FNTConfigLoadFile(fntFilePath);
if (!newConf)
return nullptr;

// add the texture
Texture2D *tempTexture = Director::getInstance()->getTextureCache()->addImage(newConf->getAtlasName());
const auto tempTexture = Director::getInstance()->getTextureCache()->addImage(newConf->getAtlasName());
if (!tempTexture)
{
return nullptr;
}

FontFNT *tempFont = new FontFNT(newConf,imageOffset);
FontFNT* tempFont = new FontFNT(newConf);

tempFont->setFontSize(newConf->_fontSize);
if (!tempFont)
{
Expand All @@ -531,13 +572,26 @@ FontFNT * FontFNT::create(const std::string& fntFilePath, const Vec2& imageOffse
return tempFont;
}

FontFNT::FontFNT(BMFontConfiguration *theContfig, const Vec2& imageOffset /* = Vec2::ZERO */)
:_configuration(theContfig)
,_imageOffset(CC_POINT_PIXELS_TO_POINTS(imageOffset))
FontFNT* FontFNT::create(const std::string& fntFilePath, const Vec2& imageOffset)
{
return create(fntFilePath, Rect(imageOffset.x, imageOffset.y, 0, 0), false);
}

FontFNT::FontFNT(BMFontConfiguration *theContfig, const Rect& imageRect, bool imageRotated)
: _configuration(theContfig)
, _imageRectInPoints(CC_RECT_PIXELS_TO_POINTS(imageRect))
, _imageRotated(imageRotated)
{
_configuration->retain();
}

FontFNT::FontFNT(BMFontConfiguration* theContfig)
: _configuration(theContfig)
, _imageRectInPoints(Rect::ZERO)
, _imageRotated(false)
{
}

FontFNT::~FontFNT()
{
_configuration->release();
Expand Down Expand Up @@ -627,24 +681,39 @@ FontAtlas * FontFNT::createFontAtlas()
}

tempAtlas->setLineHeight(originalLineHeight * factor);


auto rw = _imageRectInPoints.size.width;
auto rh = _imageRectInPoints.size.height;

if (_imageRotated)
std::swap(rw, rh);

const auto left = _imageRectInPoints.origin.x;
const auto right = _imageRectInPoints.origin.x + rw;
const auto top = _imageRectInPoints.origin.y;

for (auto&& e : _configuration->_fontDefDictionary)
{
BMFontDef& fontDef = e.second;

FontLetterDefinition tempDefinition;

Rect tempRect;

tempRect = fontDef.rect;
tempRect = CC_RECT_PIXELS_TO_POINTS(tempRect);
const auto tempRect = CC_RECT_PIXELS_TO_POINTS(fontDef.rect);

tempDefinition.offsetX = fontDef.xOffset;
tempDefinition.offsetY = fontDef.yOffset;

tempDefinition.U = tempRect.origin.x + _imageOffset.x;
tempDefinition.V = tempRect.origin.y + _imageOffset.y;


if (_imageRotated)
{
tempDefinition.U = right - tempRect.origin.y - tempRect.size.height;
tempDefinition.V = tempRect.origin.x + top;
}
else
{
tempDefinition.U = tempRect.origin.x + left;
tempDefinition.V = tempRect.origin.y + top;
}

tempDefinition.width = tempRect.size.width;
tempDefinition.height = tempRect.size.height;

Expand All @@ -653,7 +722,7 @@ FontAtlas * FontFNT::createFontAtlas()

tempDefinition.validDefinition = true;
tempDefinition.xAdvance = fontDef.xAdvance;
tempDefinition.rotated = false;
tempDefinition.rotated = _imageRotated;

// add the new definition
if (65535 < fontDef.charID) {
Expand Down Expand Up @@ -690,12 +759,12 @@ void FontFNT::reloadBMFontResource(const std::string& fntFilePath)
{
s_configurations->erase(fntFilePath);
}

ret = BMFontConfiguration::create(fntFilePath);
if (ret)
{
s_configurations->insert(fntFilePath, ret);
Director::getInstance()->getTextureCache()->reloadTexture(ret->getAtlasName());

}
}

Expand Down
22 changes: 18 additions & 4 deletions cocos/2d/CCFontFNT.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,19 @@ class CC_DLL BMFontConfiguration : public Ref

//! FNTConfig: Common Height Should be signed (issue #1343)
int _commonHeight;

//! Padding
BMFontPadding _padding;

//! atlas name
std::string _atlasName;

//! values for kerning
std::unordered_map<uint64_t /* key */, int /* amount */> _kerningDictionary;

// Character Set defines the letters that actually exist in the font
std::set<unsigned int>* _characterSet;

//! Font Size
int _fontSize;
public:
Expand Down Expand Up @@ -136,7 +140,12 @@ class CC_DLL FontFNT : public Font

public:

static FontFNT * create(const std::string& fntFilePath, const Vec2& imageOffset = Vec2::ZERO);
static FontFNT* create(const std::string& fntFilePath, const Rect& imageRect, bool imageRotated);
static FontFNT* create(const std::string& fntFilePath, const std::string& subTextureKey);
static FontFNT* create(const std::string& fntFilePath);

CC_DEPRECATED_ATTRIBUTE static FontFNT* create(const std::string& fntFilePath, const Vec2& imageOffset = Vec2::ZERO);

/** Purges the cached data.
Removes from memory the cached configurations and the atlas name dictionary.
*/
Expand All @@ -153,17 +162,22 @@ class CC_DLL FontFNT : public Font

protected:

FontFNT(BMFontConfiguration *theContfig, const Vec2& imageOffset = Vec2::ZERO);
FontFNT(BMFontConfiguration* theContfig, const Rect& imageRect, bool imageRotated);
FontFNT(BMFontConfiguration* theContfig);

/**
* @js NA
* @lua NA
*/
virtual ~FontFNT();

BMFontConfiguration* _configuration;

int getHorizontalKerningForChars(char32_t firstChar, char32_t secondChar) const;

Vec2 _imageOffset;

Rect _imageRectInPoints;
bool _imageRotated;

//User defined font size
float _fontSize;
};
Expand Down
Loading