Skip to content

Commit 8d2646b

Browse files
rh101minggo
authored andcommitted
V4 bmfont improved api (#20406)
* Merged improved BM font API from Cocos2d-x V3. Code based on #20309 and #20300 * Added missing test resources.
1 parent 1959c79 commit 8d2646b

18 files changed

+1288
-555
lines changed

cocos/2d/CCFontAtlas.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ bool FontAtlas::prepareLetterDefinitions(const std::u32string& utf32Text)
445445
tempDef.height = tempDef.height / scaleFactor;
446446
tempDef.U = tempDef.U / scaleFactor;
447447
tempDef.V = tempDef.V / scaleFactor;
448+
tempDef.rotated = false;
448449
}
449450
else{
450451
if(bitmap)
@@ -461,6 +462,7 @@ bool FontAtlas::prepareLetterDefinitions(const std::u32string& utf32Text)
461462
tempDef.offsetX = 0;
462463
tempDef.offsetY = 0;
463464
tempDef.textureID = 0;
465+
tempDef.rotated = false;
464466
_currentPageOrigX += 1;
465467
}
466468

cocos/2d/CCFontAtlas.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ struct FontLetterDefinition
5555
int textureID;
5656
bool validDefinition;
5757
int xAdvance;
58+
bool rotated;
5859
};
5960

6061
class CC_DLL FontAtlas : public Ref

cocos/2d/CCFontAtlasCache.cpp

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,53 @@ FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config)
8888
return nullptr;
8989
}
9090

91-
FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset /* = Vec2::ZERO */)
91+
FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName)
9292
{
93-
auto realFontFilename = FileUtils::getInstance()->getNewFilename(fontFileName); // resolves real file path, to prevent storing multiple atlases for the same file.
93+
return getFontAtlasFNT(fontFileName, Rect::ZERO, false);
94+
}
95+
96+
FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const std::string& subTextureKey)
97+
{
98+
const auto realFontFilename = FileUtils::getInstance()->getNewFilename(fontFileName); // resolves real file path, to prevent storing multiple atlases for the same file.
99+
std::string atlasName = subTextureKey + " " + realFontFilename;
100+
101+
const auto it = _atlasMap.find(atlasName);
102+
if (it == _atlasMap.end())
103+
{
104+
const auto font = FontFNT::create(realFontFilename, subTextureKey);
105+
106+
if (font)
107+
{
108+
const auto tempAtlas = font->createFontAtlas();
109+
if (tempAtlas)
110+
{
111+
_atlasMap[atlasName] = tempAtlas;
112+
return _atlasMap[atlasName];
113+
}
114+
}
115+
}
116+
else
117+
return it->second;
118+
119+
return nullptr;
120+
}
121+
122+
FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const Rect& imageRect, bool imageRotated)
123+
{
124+
const auto realFontFilename = FileUtils::getInstance()->getNewFilename(fontFileName); // resolves real file path, to prevent storing multiple atlases for the same file.
94125
char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
95-
snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageOffset.x, imageOffset.y);
126+
snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageRect.origin.x, imageRect.origin.y);
96127
std::string atlasName(keyPrefix);
97128
atlasName += realFontFilename;
98129

99-
auto it = _atlasMap.find(atlasName);
130+
const auto it = _atlasMap.find(atlasName);
100131
if ( it == _atlasMap.end() )
101132
{
102-
auto font = FontFNT::create(realFontFilename, imageOffset);
133+
const auto font = FontFNT::create(realFontFilename, imageRect, imageRotated);
103134

104135
if(font)
105136
{
106-
auto tempAtlas = font->createFontAtlas();
137+
const auto tempAtlas = font->createFontAtlas();
107138
if (tempAtlas)
108139
{
109140
_atlasMap[atlasName] = tempAtlas;
@@ -117,9 +148,14 @@ FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, cons
117148
return nullptr;
118149
}
119150

151+
FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset)
152+
{
153+
return getFontAtlasFNT(fontFileName, Rect(imageOffset.x, imageOffset.y, 0, 0), false);
154+
}
155+
120156
FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile)
121157
{
122-
std::string atlasName = plistFile;
158+
const std::string& atlasName = plistFile;
123159

124160
auto it = _atlasMap.find(atlasName);
125161
if ( it == _atlasMap.end() )
@@ -220,10 +256,10 @@ bool FontAtlasCache::releaseFontAtlas(FontAtlas *atlas)
220256
return false;
221257
}
222258

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

@@ -234,7 +270,7 @@ void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const V
234270
_atlasMap.erase(it);
235271
}
236272
FontFNT::reloadBMFontResource(fontFileName);
237-
auto font = FontFNT::create(fontFileName, imageOffset);
273+
auto font = FontFNT::create(fontFileName, imageRect, imageRotated);
238274
if (font)
239275
{
240276
auto tempAtlas = font->createFontAtlas();
@@ -243,7 +279,11 @@ void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const V
243279
_atlasMap[atlasName] = tempAtlas;
244280
}
245281
}
282+
}
246283

284+
void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset)
285+
{
286+
reloadFontAtlasFNT(fontFileName, Rect(imageOffset.x, imageOffset.y, 0, 0), false);
247287
}
248288

249289
void FontAtlasCache::unloadFontAtlasTTF(const std::string& fontFileName)

cocos/2d/CCFontAtlasCache.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ class CC_DLL FontAtlasCache
4242
{
4343
public:
4444
static FontAtlas* getFontAtlasTTF(const _ttfConfig* config);
45-
static FontAtlas* getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset = Vec2::ZERO);
45+
46+
static FontAtlas* getFontAtlasFNT(const std::string& fontFileName);
47+
static FontAtlas* getFontAtlasFNT(const std::string& fontFileName, const std::string& subTextureKey);
48+
static FontAtlas* getFontAtlasFNT(const std::string& fontFileName, const Rect& imageRect, bool imageRotated);
49+
CC_DEPRECATED_ATTRIBUTE static FontAtlas* getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset);
4650

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

6470
/** Unload all texture atlas texture create by special file name.
6571
CAUTION : All component use this font texture should be reset font name, though the file name is same!

cocos/2d/CCFontCharMap.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ FontAtlas * FontCharMap::createFontAtlas()
126126
tempDefinition.width = _itemWidth / contentScaleFactor;
127127
tempDefinition.height = _itemHeight / contentScaleFactor;
128128
tempDefinition.xAdvance = _itemWidth;
129+
tempDefinition.rotated = false;
129130

130131
int charId = _mapStartChar;
131132
for (int row = 0; row < itemsPerColumn; ++row)

0 commit comments

Comments
 (0)