Skip to content

Commit

Permalink
V4 bmfont improved api (cocos2d#20406)
Browse files Browse the repository at this point in the history
* Merged improved BM font API from Cocos2d-x V3.  Code based on cocos2d#20309 and cocos2d#20300

* Added missing test resources.
  • Loading branch information
rh101 authored and minggo committed Jan 6, 2020
1 parent 1959c79 commit 8d2646b
Show file tree
Hide file tree
Showing 18 changed files with 1,288 additions and 555 deletions.
2 changes: 2 additions & 0 deletions cocos/2d/CCFontAtlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ bool FontAtlas::prepareLetterDefinitions(const std::u32string& utf32Text)
tempDef.height = tempDef.height / scaleFactor;
tempDef.U = tempDef.U / scaleFactor;
tempDef.V = tempDef.V / scaleFactor;
tempDef.rotated = false;
}
else{
if(bitmap)
Expand All @@ -461,6 +462,7 @@ bool FontAtlas::prepareLetterDefinitions(const std::u32string& utf32Text)
tempDef.offsetX = 0;
tempDef.offsetY = 0;
tempDef.textureID = 0;
tempDef.rotated = false;
_currentPageOrigX += 1;
}

Expand Down
1 change: 1 addition & 0 deletions cocos/2d/CCFontAtlas.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ struct FontLetterDefinition
int textureID;
bool validDefinition;
int xAdvance;
bool rotated;
};

class CC_DLL FontAtlas : public Ref
Expand Down
60 changes: 50 additions & 10 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,9 +148,14 @@ 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)
{
std::string atlasName = plistFile;
const std::string& atlasName = plistFile;

auto it = _atlasMap.find(atlasName);
if ( it == _atlasMap.end() )
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
1 change: 1 addition & 0 deletions cocos/2d/CCFontCharMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ FontAtlas * FontCharMap::createFontAtlas()
tempDefinition.width = _itemWidth / contentScaleFactor;
tempDefinition.height = _itemHeight / contentScaleFactor;
tempDefinition.xAdvance = _itemWidth;
tempDefinition.rotated = false;

int charId = _mapStartChar;
for (int row = 0; row < itemsPerColumn; ++row)
Expand Down
Loading

0 comments on commit 8d2646b

Please sign in to comment.